From 184388015da525d50dec8425a2e58af4ea8a5843 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 8 Dec 2023 20:51:54 +0100 Subject: [PATCH] added more tests --- models/activitypub/actor.go | 2 +- models/activitypub/actor_test.go | 53 ++++++++++++++++++++++++++++++++ modules/validation/helpers.go | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/models/activitypub/actor.go b/models/activitypub/actor.go index 3af101b440..13f349faf9 100644 --- a/models/activitypub/actor.go +++ b/models/activitypub/actor.go @@ -83,7 +83,7 @@ func (value PersonId) Validate() []string { result = append(result, validation.ValidateOneOf(value.source, []string{"forgejo", "gitea"})...) switch value.source { case "forgejo", "gitea": - if !strings.Contains(value.path, "api/v1/activitypub/user-id") { + if strings.ToLower(value.path) != "api/v1/activitypub/user-id" { result = append(result, fmt.Sprintf("path has to be a api path")) } } diff --git a/models/activitypub/actor_test.go b/models/activitypub/actor_test.go index 2e8663a7ca..4162c2411b 100644 --- a/models/activitypub/actor_test.go +++ b/models/activitypub/actor_test.go @@ -37,6 +37,59 @@ func TestNewPersonId(t *testing.T) { } } +func TestPersonIdValidation(t *testing.T) { + sut := PersonId{ + source: "forgejo", + schema: "https", + path: "api/v1/activitypub/user-id", + host: "an.other.host", + port: "", + unvalidatedInput: "https://an.other.host/api/v1/activitypub/user-id/", + } + if sut.Validate()[0] != "Field userId may not be empty" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()) + } + + sut = PersonId{ + userId: "1", + source: "forgejox", + schema: "https", + path: "api/v1/activitypub/user-id", + host: "an.other.host", + port: "", + unvalidatedInput: "https://an.other.host/api/v1/activitypub/user-id/1", + } + if sut.Validate()[0] != "Value forgejox is not contained in allowed values [[forgejo gitea]]" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()) + } + + sut = PersonId{ + userId: "1", + source: "forgejo", + schema: "https", + path: "api/v1/activitypub/user-idx", + host: "an.other.host", + port: "", + unvalidatedInput: "https://an.other.host/api/v1/activitypub/user-id/1", + } + if sut.Validate()[0] != "path has to be a api path" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()) + } + + sut = PersonId{ + userId: "1", + source: "forgejo", + schema: "https", + path: "api/v1/activitypub/user-id", + host: "an.other.host", + port: "", + unvalidatedInput: "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action", + } + if sut.Validate()[0] != "not all input: \"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" was parsed: \"https://an.other.host/api/v1/activitypub/user-id/1\"" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()) + } +} + func TestWebfingerId(t *testing.T) { sut, _ := NewPersonId("https://codeberg.org/api/v1/activitypub/user-id/12345", "forgejo") if sut.AsWebfinger() != "@12345@codeberg.org" { diff --git a/modules/validation/helpers.go b/modules/validation/helpers.go index d22520bc33..cc3c40bd15 100644 --- a/modules/validation/helpers.go +++ b/modules/validation/helpers.go @@ -138,7 +138,7 @@ func IsValidUsername(name string) bool { func ValidateNotEmpty(value string, fieldName string) []string { if value == "" { - return []string{fmt.Sprintf("Field %v may not be empty.", fieldName)} + return []string{fmt.Sprintf("Field %v may not be empty", fieldName)} } return []string{} }