2023-11-10 17:13:26 +03: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 {
|
2024-01-03 20:29:12 +03:00
|
|
|
item ForgeLike
|
2023-11-10 17:13:26 +03:00
|
|
|
want []byte
|
|
|
|
wantErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testPair{
|
|
|
|
"empty": {
|
2024-01-03 20:29:12 +03:00
|
|
|
item: ForgeLike{},
|
2023-11-10 17:13:26 +03:00
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
"with ID": {
|
2024-01-03 20:29:12 +03:00
|
|
|
item: ForgeLike{
|
2023-11-10 17:13:26 +03:00
|
|
|
Activity: ap.Activity{
|
2023-11-24 15:56:53 +03:00
|
|
|
Actor: ap.IRI("https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"),
|
2024-01-03 20:10:24 +03:00
|
|
|
Type: "Like",
|
2023-11-24 15:56:53 +03:00
|
|
|
Object: ap.IRI("https://codeberg.org/api/v1/activitypub/repository-id/1"),
|
2023-11-10 17:13:26 +03:00
|
|
|
},
|
|
|
|
},
|
2024-01-03 20:10:24 +03:00
|
|
|
want: []byte(`{"type":"Like","actor":"https://repo.prod.meissa.de/api/v1/activitypub/user-id/1","object":"https://codeberg.org/api/v1/activitypub/repository-id/1"}`),
|
2023-11-10 17:13:26 +03: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 18:43:44 +03:00
|
|
|
|
|
|
|
func Test_StarUnmarshalJSON(t *testing.T) {
|
|
|
|
type testPair struct {
|
|
|
|
item []byte
|
2024-01-03 20:29:12 +03:00
|
|
|
want *ForgeLike
|
2023-11-10 18:43:44 +03:00
|
|
|
wantErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testPair{
|
|
|
|
"with ID": {
|
2024-01-03 20:29:12 +03:00
|
|
|
item: []byte(`{"type":"Like","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"}`),
|
|
|
|
want: &ForgeLike{
|
2023-11-10 18:43:44 +03:00
|
|
|
Activity: ap.Activity{
|
|
|
|
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
|
2024-01-03 20:29:12 +03:00
|
|
|
Type: "Like",
|
2023-11-10 18:43:44 +03:00
|
|
|
Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2024-01-03 20:29:12 +03:00
|
|
|
got := new(ForgeLike)
|
2023-11-10 18:43:44 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|