2023-11-10 14:13:26 +00:00
|
|
|
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ap "github.com/go-ap/activitypub"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_StarMarshalJSON(t *testing.T) {
|
|
|
|
type testPair struct {
|
|
|
|
item Star
|
|
|
|
want []byte
|
|
|
|
wantErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testPair{
|
|
|
|
"empty": {
|
|
|
|
item: Star{},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
"with ID": {
|
|
|
|
item: Star{
|
|
|
|
Source: "forgejo",
|
|
|
|
Activity: ap.Activity{
|
2023-11-10 15:08:15 +00:00
|
|
|
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
|
|
|
|
Type: "Star",
|
|
|
|
Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"),
|
2023-11-10 14:13:26 +00:00
|
|
|
},
|
|
|
|
},
|
2023-11-10 15:08:15 +00:00
|
|
|
want: []byte(`{"source":"forgejo","type":"Star","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"}`),
|
2023-11-10 14:13:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
got, err := tt.item.MarshalJSON()
|
|
|
|
if (err != nil || tt.wantErr != nil) && tt.wantErr.Error() != err.Error() {
|
|
|
|
t.Errorf("MarshalJSON() error = \"%v\", wantErr \"%v\"", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("MarshalJSON() got = %q, want %q", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 15:43:44 +00:00
|
|
|
|
|
|
|
func Test_StarUnmarshalJSON(t *testing.T) {
|
|
|
|
type testPair struct {
|
|
|
|
item []byte
|
2023-11-10 16:00:08 +00:00
|
|
|
want *Star
|
2023-11-10 15:43:44 +00:00
|
|
|
wantErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testPair{
|
|
|
|
"with ID": {
|
|
|
|
item: []byte(`{"source":"forgejo","type":"Star","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"}`),
|
2023-11-10 16:00:08 +00:00
|
|
|
want: &Star{
|
2023-11-10 15:43:44 +00:00
|
|
|
Source: "forgejo",
|
|
|
|
Activity: ap.Activity{
|
|
|
|
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
|
|
|
|
Type: "Star",
|
|
|
|
Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
got := new(Star)
|
|
|
|
err := got.UnmarshalJSON(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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|