2024-03-27 14:56:32 -04:00
|
|
|
// Copyright 2023, 2024 The Forgejo Authors. All rights reserved.
|
2023-11-10 09:13:26 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2024-03-27 14:56:32 -04:00
|
|
|
"time"
|
2023-11-10 09:13:26 -05:00
|
|
|
|
2024-01-03 12:52:41 -05:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2024-02-09 11:46:27 -05:00
|
|
|
|
2023-11-10 09:13:26 -05:00
|
|
|
ap "github.com/go-ap/activitypub"
|
|
|
|
)
|
|
|
|
|
2024-03-27 11:20:33 -04:00
|
|
|
func Test_NewForgeLike(t *testing.T) {
|
|
|
|
actorIRI := "https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"
|
|
|
|
objectIRI := "https://codeberg.org/api/v1/activitypub/repository-id/1"
|
2024-03-27 14:56:32 -04:00
|
|
|
want := []byte(`{"type":"Like","startTime":"2024-03-27T00:00:00Z","actor":"https://repo.prod.meissa.de/api/v1/activitypub/user-id/1","object":"https://codeberg.org/api/v1/activitypub/repository-id/1"}`)
|
2024-03-27 11:20:33 -04:00
|
|
|
|
2024-03-27 14:56:32 -04:00
|
|
|
startTime, _ := time.Parse("2006-Jan-02", "2024-Mar-27")
|
|
|
|
sut, err := NewForgeLike(actorIRI, objectIRI, startTime)
|
2024-03-27 11:20:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v\n", err)
|
|
|
|
}
|
|
|
|
if valid, _ := validation.IsValid(sut); !valid {
|
|
|
|
t.Errorf("sut expected to be valid: %v\n", sut.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := sut.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("MarshalJSON() error = \"%v\"", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("MarshalJSON() got = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 09:13:26 -05:00
|
|
|
func Test_StarMarshalJSON(t *testing.T) {
|
|
|
|
type testPair struct {
|
2024-01-03 12:29:12 -05:00
|
|
|
item ForgeLike
|
2023-11-10 09:13:26 -05:00
|
|
|
want []byte
|
|
|
|
wantErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testPair{
|
|
|
|
"empty": {
|
2024-01-03 12:29:12 -05:00
|
|
|
item: ForgeLike{},
|
2023-11-10 09:13:26 -05:00
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
"with ID": {
|
2024-01-03 12:29:12 -05:00
|
|
|
item: ForgeLike{
|
2023-11-10 09:13:26 -05:00
|
|
|
Activity: ap.Activity{
|
2023-11-24 07:56:53 -05:00
|
|
|
Actor: ap.IRI("https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"),
|
2024-01-03 12:10:24 -05:00
|
|
|
Type: "Like",
|
2023-11-24 07:56:53 -05:00
|
|
|
Object: ap.IRI("https://codeberg.org/api/v1/activitypub/repository-id/1"),
|
2023-11-10 09:13:26 -05:00
|
|
|
},
|
|
|
|
},
|
2024-01-03 12:10:24 -05: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 09:13:26 -05: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 10:43:44 -05:00
|
|
|
|
|
|
|
func Test_StarUnmarshalJSON(t *testing.T) {
|
|
|
|
type testPair struct {
|
|
|
|
item []byte
|
2024-01-03 12:29:12 -05:00
|
|
|
want *ForgeLike
|
2023-11-10 10:43:44 -05:00
|
|
|
wantErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testPair{
|
|
|
|
"with ID": {
|
2024-01-03 12:29:12 -05: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 10:43:44 -05:00
|
|
|
Activity: ap.Activity{
|
|
|
|
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
|
2024-01-03 12:29:12 -05:00
|
|
|
Type: "Like",
|
2023-11-10 10:43:44 -05: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 12:29:12 -05:00
|
|
|
got := new(ForgeLike)
|
2023-11-10 10:43:44 -05: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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-03 12:52:41 -05:00
|
|
|
|
|
|
|
func TestAcivityValidation(t *testing.T) {
|
|
|
|
sut := new(ForgeLike)
|
|
|
|
sut.UnmarshalJSON([]byte(`{"type":"Like",
|
|
|
|
"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1",
|
|
|
|
"object":"https://codeberg.org/api/activitypub/repository-id/1",
|
|
|
|
"startTime": "2014-12-31T23:00:00-08:00"}`))
|
|
|
|
if res, _ := validation.IsValid(sut); !res {
|
|
|
|
t.Errorf("sut expected to be valid: %v\n", sut.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
sut.UnmarshalJSON([]byte(`{"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1",
|
|
|
|
"object":"https://codeberg.org/api/activitypub/repository-id/1",
|
|
|
|
"startTime": "2014-12-31T23:00:00-08:00"}`))
|
|
|
|
if sut.Validate()[0] != "Field type may not be empty" {
|
|
|
|
t.Errorf("validation error expected but was: %v\n", sut.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
sut.UnmarshalJSON([]byte(`{"type":"bad-type",
|
|
|
|
"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1",
|
|
|
|
"object":"https://codeberg.org/api/activitypub/repository-id/1",
|
|
|
|
"startTime": "2014-12-31T23:00:00-08:00"}`))
|
|
|
|
if sut.Validate()[0] != "Value bad-type is not contained in allowed values [[Like]]" {
|
|
|
|
t.Errorf("validation error expected but was: %v\n", sut.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
sut.UnmarshalJSON([]byte(`{"type":"Like",
|
|
|
|
"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1",
|
|
|
|
"object":"https://codeberg.org/api/activitypub/repository-id/1",
|
|
|
|
"startTime": "not a date"}`))
|
|
|
|
if sut.Validate()[0] != "StartTime was invalid." {
|
|
|
|
t.Errorf("validation error expected but was: %v\n", sut.Validate())
|
|
|
|
}
|
|
|
|
}
|