forgejo/models/forgefed/actor_test.go

155 lines
4.9 KiB
Go
Raw Normal View History

2023-11-15 17:07:23 +03:00
// Copyright 2023 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2023-12-09 16:53:40 +03:00
package forgefed
2023-11-15 17:07:23 +03:00
import (
"testing"
2023-12-09 20:30:47 +03:00
"code.gitea.io/gitea/modules/setting"
2023-11-15 17:07:23 +03:00
)
2023-12-08 22:37:26 +03:00
func TestNewPersonId(t *testing.T) {
2023-12-09 20:30:47 +03:00
expected := PersonId{}
expected.Id = "1"
expected.Source = "forgejo"
expected.Schema = "https"
expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host"
expected.Port = ""
expected.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1"
2023-12-08 22:37:26 +03:00
sut, _ := NewPersonId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo")
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
}
2023-12-09 20:30:47 +03:00
expected = PersonId{}
expected.Id = "1"
expected.Source = "forgejo"
expected.Schema = "https"
expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host"
expected.Port = "443"
expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1"
2023-12-08 22:37:26 +03:00
sut, _ = NewPersonId("https://an.other.host:443/api/v1/activitypub/user-id/1", "forgejo")
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
2023-11-15 17:07:23 +03:00
}
}
2023-11-15 17:07:23 +03:00
2023-12-09 20:30:47 +03:00
func TestNewRepositoryId(t *testing.T) {
setting.AppURL = "http://localhost:3000/"
expected := RepositoryId{}
expected.Id = "1"
expected.Source = "forgejo"
expected.Schema = "http"
expected.Path = "api/activitypub/repository-id"
expected.Host = "localhost"
expected.Port = "3000"
expected.UnvalidatedInput = "http://localhost:3000/api/activitypub/repository-id/1"
sut, _ := NewRepositoryId("http://localhost:3000/api/activitypub/repository-id/1", "forgejo")
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
2023-12-08 22:51:54 +03:00
}
2023-12-09 20:30:47 +03:00
}
2023-12-21 17:01:49 +03:00
func TestActorIdValidation(t *testing.T) {
sut := ActorId{}
2023-12-09 20:30:47 +03:00
sut.Source = "forgejo"
sut.Schema = "https"
sut.Path = "api/v1/activitypub/user-id"
sut.Host = "an.other.host"
sut.Port = ""
sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/"
2023-12-08 22:51:54 +03:00
if sut.Validate()[0] != "Field userId may not be empty" {
t.Errorf("validation error expected but was: %v\n", sut.Validate())
}
2023-12-21 17:01:49 +03:00
sut = ActorId{}
2023-12-09 20:30:47 +03:00
sut.Id = "1"
sut.Source = "forgejox"
sut.Schema = "https"
sut.Path = "api/v1/activitypub/user-id"
sut.Host = "an.other.host"
sut.Port = ""
sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1"
2023-12-08 22:51:54 +03:00
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())
}
2023-12-21 17:01:49 +03:00
sut = ActorId{}
2023-12-09 20:30:47 +03:00
sut.Id = "1"
sut.Source = "forgejo"
sut.Schema = "https"
2023-12-21 17:01:49 +03:00
sut.Path = "api/v1/activitypub/user-id"
2023-12-09 20:30:47 +03:00
sut.Host = "an.other.host"
sut.Port = ""
2023-12-21 17:01:49 +03:00
sut.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\"" {
2023-12-08 22:51:54 +03:00
t.Errorf("validation error expected but was: %v\n", sut.Validate())
}
2023-12-21 17:01:49 +03:00
}
2023-12-08 22:51:54 +03:00
2023-12-21 17:01:49 +03:00
func TestPersonIdValidation(t *testing.T) {
sut := PersonId{}
2023-12-09 20:30:47 +03:00
sut.Id = "1"
sut.Source = "forgejo"
sut.Schema = "https"
2023-12-21 17:01:49 +03:00
sut.Path = "path"
2023-12-09 20:30:47 +03:00
sut.Host = "an.other.host"
sut.Port = ""
2023-12-21 17:01:49 +03:00
sut.UnvalidatedInput = "https://an.other.host/path/1"
if _, err := sut.IsValid(); err.Error() != "path: \"path\" has to be an api path" {
t.Errorf("validation error expected but was: %v\n", err)
2023-12-08 22:51:54 +03:00
}
}
2023-12-08 22:37:26 +03:00
func TestWebfingerId(t *testing.T) {
sut, _ := NewPersonId("https://codeberg.org/api/v1/activitypub/user-id/12345", "forgejo")
if sut.AsWebfinger() != "@12345@codeberg.org" {
t.Errorf("wrong webfinger: %v", sut.AsWebfinger())
2023-11-15 17:07:23 +03:00
}
2023-11-23 19:02:54 +03:00
2023-12-08 22:37:26 +03:00
sut, _ = NewPersonId("https://Codeberg.org/api/v1/activitypub/user-id/12345", "forgejo")
if sut.AsWebfinger() != "@12345@codeberg.org" {
t.Errorf("wrong webfinger: %v", sut.AsWebfinger())
2023-11-23 19:02:54 +03:00
}
}
2023-12-08 20:08:54 +03:00
func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
2023-12-08 21:43:49 +03:00
_, err := NewPersonId("", "forgejo")
2023-12-08 20:08:54 +03:00
if err == nil {
t.Errorf("empty input should be invalid.")
}
2023-12-08 21:43:49 +03:00
_, err = NewPersonId("http://localhost:3000/api/v1/something", "forgejo")
2023-12-08 20:08:54 +03:00
if err == nil {
t.Errorf("localhost uris are not external")
}
2023-12-08 21:43:49 +03:00
_, err = NewPersonId("./api/v1/something", "forgejo")
2023-12-08 20:33:26 +03:00
if err == nil {
2023-12-08 21:41:22 +03:00
t.Errorf("relative uris are not alowed")
}
2023-12-08 21:43:49 +03:00
_, err = NewPersonId("http://1.2.3.4/api/v1/something", "forgejo")
2023-12-08 21:41:22 +03:00
if err == nil {
t.Errorf("uri may not be ip-4 based")
}
2023-12-08 21:43:49 +03:00
_, err = NewPersonId("http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something", "forgejo")
2023-12-08 21:41:22 +03:00
if err == nil {
t.Errorf("uri may not be ip-6 based")
}
2023-12-08 21:43:49 +03:00
_, err = NewPersonId("https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345", "forgejo")
2023-12-08 21:41:22 +03:00
if err == nil {
t.Errorf("uri may not contain relative path elements")
2023-12-08 20:33:26 +03:00
}
2023-12-08 22:37:26 +03:00
_, err = NewPersonId("https://myuser@an.other.host/api/v1/activitypub/user-id/1", "forgejo")
if err == nil {
t.Errorf("uri may not contain unparsed elements")
}
2023-12-08 20:08:54 +03:00
2023-12-08 21:43:49 +03:00
_, err = NewPersonId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo")
2023-12-08 20:08:54 +03:00
if err != nil {
2023-12-08 21:41:22 +03:00
t.Errorf("this uri should be valid but was: %v", err)
2023-12-08 20:08:54 +03:00
}
}