This commit is contained in:
Michael Jerger 2024-05-01 14:39:09 +02:00
parent e9fed7a488
commit 6c7cff4f16
2 changed files with 27 additions and 3 deletions

View file

@ -46,8 +46,16 @@ func (like ForgeLike) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...) result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"})...) result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"})...)
if like.Actor == nil {
result = append(result, "Actor my not be nil.")
} else {
result = append(result, validation.ValidateNotEmpty(like.Actor.GetID().String(), "actor")...) result = append(result, validation.ValidateNotEmpty(like.Actor.GetID().String(), "actor")...)
}
if like.Object == nil {
result = append(result, "Object my not be nil.")
} else {
result = append(result, validation.ValidateNotEmpty(like.Object.GetID().String(), "object")...) result = append(result, validation.ValidateNotEmpty(like.Object.GetID().String(), "object")...)
}
result = append(result, validation.ValidateNotEmpty(like.StartTime.String(), "startTime")...) result = append(result, validation.ValidateNotEmpty(like.StartTime.String(), "startTime")...)
if like.StartTime.IsZero() { if like.StartTime.IsZero() {
result = append(result, "StartTime was invalid.") result = append(result, "StartTime was invalid.")

View file

@ -128,7 +128,7 @@ func TestActivityValidation(t *testing.T) {
sut.UnmarshalJSON([]byte(`{"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1", sut.UnmarshalJSON([]byte(`{"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1",
"object":"https://codeberg.org/api/activitypub/repository-id/1", "object":"https://codeberg.org/api/activitypub/repository-id/1",
"startTime": "2014-12-31T23:00:00-08:00"}`)) "startTime": "2014-12-31T23:00:00-08:00"}`))
if sut.Validate()[0] != "Field type should not be empty" { if sut.Validate()[0] != "type should not be empty" {
t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) t.Errorf("validation error expected but was: %v\n", sut.Validate()[0])
} }
@ -147,4 +147,20 @@ func TestActivityValidation(t *testing.T) {
if sut.Validate()[0] != "StartTime was invalid." { if sut.Validate()[0] != "StartTime was invalid." {
t.Errorf("validation error expected but was: %v\n", sut.Validate()) t.Errorf("validation error expected but was: %v\n", sut.Validate())
} }
sut.UnmarshalJSON([]byte(`{"type":"Wrong",
"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 Wrong is not contained in allowed values [Like]" {
t.Errorf("validation error expected but was: %v\n", sut.Validate())
}
}
func TestActivityValidation_Attack(t *testing.T) {
sut := new(ForgeLike)
sut.UnmarshalJSON([]byte(`{rubbish}`))
if len(sut.Validate()) != 5 {
t.Errorf("5 validateion errors expected but was: %v\n", len(sut.Validate()))
}
} }