2023-11-15 15:07:23 +01:00
|
|
|
// Copyright 2023 The forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-12-08 20:37:26 +01:00
|
|
|
func TestNewPersonId(t *testing.T) {
|
|
|
|
expected := 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",
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = PersonId{
|
|
|
|
userId: "1",
|
|
|
|
source: "forgejo",
|
|
|
|
schema: "https",
|
|
|
|
path: "api/v1/activitypub/user-id",
|
|
|
|
host: "an.other.host",
|
|
|
|
port: "443",
|
|
|
|
unvalidatedInput: "https://an.other.host:443/api/v1/activitypub/user-id/1",
|
|
|
|
}
|
|
|
|
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 15:07:23 +01:00
|
|
|
}
|
2023-11-22 16:40:28 +01:00
|
|
|
}
|
2023-11-15 15:07:23 +01:00
|
|
|
|
2023-12-08 20:37:26 +01: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 15:07:23 +01:00
|
|
|
}
|
2023-11-23 17:02:54 +01:00
|
|
|
|
2023-12-08 20:37:26 +01: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 17:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-08 18:08:54 +01:00
|
|
|
|
|
|
|
func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err := NewPersonId("", "forgejo")
|
2023-12-08 18:08:54 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("empty input should be invalid.")
|
|
|
|
}
|
|
|
|
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err = NewPersonId("http://localhost:3000/api/v1/something", "forgejo")
|
2023-12-08 18:08:54 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("localhost uris are not external")
|
|
|
|
}
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err = NewPersonId("./api/v1/something", "forgejo")
|
2023-12-08 18:33:26 +01:00
|
|
|
if err == nil {
|
2023-12-08 19:41:22 +01:00
|
|
|
t.Errorf("relative uris are not alowed")
|
|
|
|
}
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err = NewPersonId("http://1.2.3.4/api/v1/something", "forgejo")
|
2023-12-08 19:41:22 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uri may not be ip-4 based")
|
|
|
|
}
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err = NewPersonId("http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something", "forgejo")
|
2023-12-08 19:41:22 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uri may not be ip-6 based")
|
|
|
|
}
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err = NewPersonId("https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345", "forgejo")
|
2023-12-08 19:41:22 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uri may not contain relative path elements")
|
2023-12-08 18:33:26 +01:00
|
|
|
}
|
2023-12-08 20:37:26 +01: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 18:08:54 +01:00
|
|
|
|
2023-12-08 19:43:49 +01:00
|
|
|
_, err = NewPersonId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo")
|
2023-12-08 18:08:54 +01:00
|
|
|
if err != nil {
|
2023-12-08 19:41:22 +01:00
|
|
|
t.Errorf("this uri should be valid but was: %v", err)
|
2023-12-08 18:08:54 +01:00
|
|
|
}
|
|
|
|
}
|