ad validation for like activity

This commit is contained in:
Michael Jerger 2024-01-03 18:52:41 +01:00
parent 3ab2d9a449
commit 0505baab2b
3 changed files with 45 additions and 1 deletions

View file

@ -26,9 +26,13 @@ func (s *ForgeLike) UnmarshalJSON(data []byte) error {
func (s ForgeLike) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(s.Type), []any{"Like"})...)
result = append(result, validation.ValidateNotEmpty(s.Actor.GetID().String(), "actor")...)
result = append(result, validation.ValidateNotEmpty(s.Object.GetID().String(), "object")...)
result = append(result, validation.ValidateNotEmpty(s.StartTime.String(), "startTime")...)
if s.StartTime.IsZero() {
result = append(result, "StartTime was invalid.")
}
return result
}

View file

@ -7,6 +7,7 @@ import (
"reflect"
"testing"
"code.gitea.io/gitea/modules/validation"
ap "github.com/go-ap/activitypub"
)
@ -82,3 +83,37 @@ func Test_StarUnmarshalJSON(t *testing.T) {
})
}
}
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())
}
}