remove unused & implement webfinger

This commit is contained in:
Michael Jerger 2023-12-08 20:37:26 +01:00
parent 73a38ea0d1
commit b5a467e94d
3 changed files with 119 additions and 261 deletions

View file

@ -5,119 +5,50 @@ package activitypub
import (
"testing"
"code.gitea.io/gitea/modules/forgefed"
ap "github.com/go-ap/activitypub"
)
var emptyMockStar *forgefed.Star = &forgefed.Star{
Source: "",
Activity: ap.Activity{
Actor: ap.IRI(""),
Type: "Star",
Object: ap.IRI(""),
},
}
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)
}
var invalidMockStar *forgefed.Star = &forgefed.Star{
Source: "",
Activity: ap.Activity{
Actor: ap.IRI(""),
Type: "Star",
Object: ap.IRI("https://example.com/"),
},
}
var mockStar *forgefed.Star = &forgefed.Star{
Source: "forgejo",
Activity: ap.Activity{
Actor: ap.IRI("https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"),
Type: "Star",
Object: ap.IRI("https://codeberg.org/api/v1/activitypub/repository-id/1"),
},
}
func TestValidateAndParseIRIEmpty(t *testing.T) {
item := emptyMockStar.Object.GetLink().String()
_, err := ValidateAndParseIRI(item)
if err == nil {
t.Errorf("ValidateAndParseIRI returned no error for empty input.")
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)
}
}
func TestValidateAndParseIRINoPath(t *testing.T) {
item := emptyMockStar.Object.GetLink().String()
_, err := ValidateAndParseIRI(item)
if err == nil {
t.Errorf("ValidateAndParseIRI returned no error for empty path.")
}
}
func TestActorParserValid(t *testing.T) {
item, _ := ValidateAndParseIRI(mockStar.Actor.GetID().String())
want := PersonId{
userId: "1",
source: "forgejo",
schema: "https",
path: "api/v1/activitypub/user-id",
host: "repo.prod.meissa.de",
port: "",
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())
}
got := ParseActorID(item, "forgejo")
if got != want {
t.Errorf("\nParseActorID did not return want: %v\n but %v", want, got)
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())
}
}
func TestValidateValid(t *testing.T) {
item := PersonId{
userId: "1",
source: "forgejo",
schema: "https",
path: "/api/v1/activitypub/user-id/1",
host: "repo.prod.meissa.de",
port: "",
}
if valid, _ := item.IsValid(); !valid {
t.Errorf("Actor was invalid with valid input.")
}
}
func TestValidateInvalid(t *testing.T) {
item, _ := ValidateAndParseIRI("https://example.org/some-path/to/nowhere/")
actor := ParseActorID(item, "forgejo")
if valid, _ := actor.IsValid(); valid {
t.Errorf("Actor was valid with invalid input.")
}
}
func TestGetHostAndPort(t *testing.T) {
item := PersonId{
schema: "https",
userId: "1",
path: "/api/v1/activitypub/user-id/1",
host: "repo.prod.meissa.de",
port: "80",
}
want := "repo.prod.meissa.de:80"
hostAndPort := item.GetHostAndPort()
if hostAndPort != want {
t.Errorf("GetHostAndPort did not return correct host and port combination: %v", hostAndPort)
}
}
func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
_, err := NewPersonId("", "forgejo")
if err == nil {
@ -144,6 +75,10 @@ func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
if err == nil {
t.Errorf("uri may not contain relative path elements")
}
_, 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")
}
_, err = NewPersonId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo")
if err != nil {