added more tests

This commit is contained in:
Michael Jerger 2023-12-08 20:51:54 +01:00
parent b5a467e94d
commit 184388015d
3 changed files with 55 additions and 2 deletions

View file

@ -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"))
}
}

View file

@ -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" {

View file

@ -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{}
}