mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-15 22:38:31 +01:00
119 lines
2.6 KiB
Go
119 lines
2.6 KiB
Go
// Copyright 2023 The forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
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(""),
|
|
},
|
|
}
|
|
|
|
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.")
|
|
}
|
|
}
|
|
|
|
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 := ActorID{
|
|
userId: "1",
|
|
source: "forgejo",
|
|
schema: "https",
|
|
path: "api/v1/activitypub/user-id",
|
|
host: "repo.prod.meissa.de",
|
|
port: "",
|
|
}
|
|
|
|
got := ParseActorID(item, "forgejo")
|
|
|
|
if got != want {
|
|
t.Errorf("\nParseActorID did not return want: %v\n but %v", want, got)
|
|
}
|
|
}
|
|
|
|
func TestValidateValid(t *testing.T) {
|
|
item := ActorID{
|
|
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 := ActorID{
|
|
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)
|
|
}
|
|
|
|
}
|