1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-26 09:09:36 -05:00
forgejo/models/forgefed/nodeinfo_test.go

90 lines
3.1 KiB
Go
Raw Normal View History

2023-12-29 06:10:07 -05:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
"fmt"
"reflect"
"testing"
"code.gitea.io/gitea/modules/validation"
)
func Test_NodeInfoWellKnownUnmarshalJSON(t *testing.T) {
type testPair struct {
item []byte
want NodeInfoWellKnown
wantErr error
}
tests := map[string]testPair{
"with href": {
item: []byte(`{"links":[{"href":"https://federated-repo.prod.meissa.de/api/v1/nodeinfo","rel":"http://nodeinfo.diaspora.software/ns/schema/2.1"}]}`),
want: NodeInfoWellKnown{
Href: "https://federated-repo.prod.meissa.de/api/v1/nodeinfo",
},
},
"empty": {
item: []byte(``),
wantErr: fmt.Errorf("cannot parse JSON: cannot parse empty string; unparsed tail: \"\""),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := NodeInfoWellKnownUnmarshalJSON(tt.item)
if (err != nil || tt.wantErr != nil) && tt.wantErr.Error() != err.Error() {
t.Errorf("UnmarshalJSON() error = \"%v\", wantErr \"%v\"", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UnmarshalJSON() got = %q, want %q", got, tt.want)
}
})
}
}
func Test_NodeInfoWellKnownValidate(t *testing.T) {
sut := NodeInfoWellKnown{Href: "https://federated-repo.prod.meissa.de/api/v1/nodeinfo"}
if b, err := validation.IsValid(sut); !b {
t.Errorf("sut should be valid, %v, %v", sut, err)
}
sut = NodeInfoWellKnown{Href: "./federated-repo.prod.meissa.de/api/v1/nodeinfo"}
if _, err := validation.IsValid(sut); err.Error() != "Href has to be absolute\nValue is not contained in allowed values [[http https]]" {
t.Errorf("validation error expected but was: %v\n", err)
}
sut = NodeInfoWellKnown{Href: "https://federated-repo.prod.meissa.de/api/v1/nodeinfo?alert=1"}
if _, err := validation.IsValid(sut); err.Error() != "Href may not contain query" {
t.Errorf("sut should be valid, %v, %v", sut, err)
}
}
2023-12-29 09:48:45 -05:00
func Test_NewNodeInfoWellKnown(t *testing.T) {
2024-01-14 07:03:51 -05:00
sut, _ := NewNodeInfoWellKnown([]byte(`{"links":[{"href":"https://federated-repo.prod.meissa.de/api/v1/nodeinfo","rel":"http://nodeinfo.diaspora.software/ns/schema/2.1"}]}`))
2023-12-29 09:48:45 -05:00
expected := NodeInfoWellKnown{Href: "https://federated-repo.prod.meissa.de/api/v1/nodeinfo"}
if sut != expected {
t.Errorf("expected was: %v but was: %v", expected, sut)
}
2024-01-14 07:03:51 -05:00
_, err := NewNodeInfoWellKnown([]byte(`invalid`))
2023-12-29 09:48:45 -05:00
if err == nil {
t.Errorf("error was expected here")
}
}
func Test_NewNodeInfo(t *testing.T) {
2024-01-14 07:03:51 -05:00
sut, _ := NewNodeInfo([]byte(`{"version":"2.1","software":{"name":"gitea","version":"1.20.0+dev-2539-g5840cc6d3","repository":"https://github.com/go-gitea/gitea.git","homepage":"https://gitea.io/"},"protocols":["activitypub"],"services":{"inbound":[],"outbound":["rss2.0"]},"openRegistrations":true,"usage":{"users":{"total":13,"activeHalfyear":1,"activeMonth":1}},"metadata":{}}`))
2023-12-29 09:48:45 -05:00
expected := NodeInfo{Source: "gitea"}
if sut != expected {
t.Errorf("expected was: %v but was: %v", expected, sut)
}
2024-01-14 07:03:51 -05:00
_, err := NewNodeInfo([]byte(`invalid`))
2023-12-29 09:48:45 -05:00
if err == nil {
t.Errorf("error was expected here")
}
}