2023-11-15 09:07:23 -05:00
|
|
|
// Copyright 2023 The forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-11-24 07:23:03 -05:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/forgefed"
|
|
|
|
ap "github.com/go-ap/activitypub"
|
2023-11-15 09:07:23 -05:00
|
|
|
)
|
|
|
|
|
2023-11-24 07:23:03 -05:00
|
|
|
var emptyMockStar *forgefed.Star = &forgefed.Star{
|
|
|
|
Source: "",
|
|
|
|
Activity: ap.Activity{
|
|
|
|
Actor: ap.IRI(""),
|
|
|
|
Type: "Star",
|
|
|
|
Object: ap.IRI(""),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 06:03:42 -05:00
|
|
|
var invalidMockStar *forgefed.Star = &forgefed.Star{
|
|
|
|
Source: "",
|
|
|
|
Activity: ap.Activity{
|
|
|
|
Actor: ap.IRI(""),
|
|
|
|
Type: "Star",
|
|
|
|
Object: ap.IRI("https://example.com/"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-24 07:23:03 -05:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-07 06:03:42 -05:00
|
|
|
func TestValidateAndParseIRIEmpty(t *testing.T) {
|
|
|
|
item := emptyMockStar.Object.GetLink().String()
|
2023-11-22 10:40:28 -05:00
|
|
|
|
2023-12-07 06:03:42 -05:00
|
|
|
_, err := ValidateAndParseIRI(item)
|
2023-11-22 10:40:28 -05:00
|
|
|
|
2023-12-07 06:03:42 -05:00
|
|
|
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.")
|
2023-11-22 10:40:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActorParserValid(t *testing.T) {
|
2023-12-07 06:03:42 -05:00
|
|
|
item, _ := ValidateAndParseIRI(mockStar.Actor.GetID().String())
|
2023-12-08 12:33:26 -05:00
|
|
|
want := ActorId{
|
2023-11-22 10:40:28 -05:00
|
|
|
userId: "1",
|
2023-11-24 07:23:03 -05:00
|
|
|
source: "forgejo",
|
|
|
|
schema: "https",
|
2023-12-06 10:14:50 -05:00
|
|
|
path: "api/v1/activitypub/user-id",
|
2023-11-22 10:40:28 -05:00
|
|
|
host: "repo.prod.meissa.de",
|
|
|
|
port: "",
|
|
|
|
}
|
|
|
|
|
2023-12-07 06:03:42 -05:00
|
|
|
got := ParseActorID(item, "forgejo")
|
2023-11-22 10:40:28 -05:00
|
|
|
|
|
|
|
if got != want {
|
2023-11-24 07:23:03 -05:00
|
|
|
t.Errorf("\nParseActorID did not return want: %v\n but %v", want, got)
|
2023-11-22 10:40:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateValid(t *testing.T) {
|
2023-12-08 12:33:26 -05:00
|
|
|
item := ActorId{
|
2023-11-22 10:40:28 -05:00
|
|
|
userId: "1",
|
2023-11-24 07:23:03 -05:00
|
|
|
source: "forgejo",
|
|
|
|
schema: "https",
|
2023-11-22 10:40:28 -05:00
|
|
|
path: "/api/v1/activitypub/user-id/1",
|
|
|
|
host: "repo.prod.meissa.de",
|
|
|
|
port: "",
|
2023-11-15 09:07:23 -05:00
|
|
|
}
|
|
|
|
|
2023-11-24 05:38:01 -05:00
|
|
|
if valid, _ := item.IsValid(); !valid {
|
|
|
|
t.Errorf("Actor was invalid with valid input.")
|
2023-11-15 09:07:23 -05:00
|
|
|
}
|
2023-11-22 10:40:28 -05:00
|
|
|
}
|
2023-11-15 09:07:23 -05:00
|
|
|
|
2023-11-22 10:40:28 -05:00
|
|
|
func TestValidateInvalid(t *testing.T) {
|
2023-12-07 06:03:42 -05:00
|
|
|
item, _ := ValidateAndParseIRI("https://example.org/some-path/to/nowhere/")
|
2023-11-15 09:07:23 -05:00
|
|
|
|
2023-12-07 06:03:42 -05:00
|
|
|
actor := ParseActorID(item, "forgejo")
|
2023-11-16 10:03:05 -05:00
|
|
|
|
2023-11-24 05:38:01 -05:00
|
|
|
if valid, _ := actor.IsValid(); valid {
|
|
|
|
t.Errorf("Actor was valid with invalid input.")
|
2023-11-15 09:07:23 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-23 11:02:54 -05:00
|
|
|
|
|
|
|
func TestGetHostAndPort(t *testing.T) {
|
2023-12-08 12:33:26 -05:00
|
|
|
item := ActorId{
|
2023-11-23 11:02:54 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-12-08 12:08:54 -05:00
|
|
|
|
|
|
|
func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
|
|
|
|
_, err := NewActorId("", "forgejo")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("empty input should be invalid.")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewActorId("http://localhost:3000/api/v1/something", "forgejo")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("localhost uris are not external")
|
|
|
|
}
|
2023-12-08 12:33:26 -05:00
|
|
|
_, err = NewActorId("./api/v1/something", "forgejo")
|
|
|
|
if err == nil {
|
2023-12-08 13:41:22 -05:00
|
|
|
t.Errorf("relative uris are not alowed")
|
|
|
|
}
|
|
|
|
_, err = NewActorId("http://1.2.3.4/api/v1/something", "forgejo")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uri may not be ip-4 based")
|
|
|
|
}
|
|
|
|
_, err = NewActorId("http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something", "forgejo")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uri may not be ip-6 based")
|
|
|
|
}
|
|
|
|
_, err = NewActorId("https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345", "forgejo")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uri may not contain relative path elements")
|
2023-12-08 12:33:26 -05:00
|
|
|
}
|
2023-12-08 12:08:54 -05:00
|
|
|
|
2023-12-08 13:41:22 -05:00
|
|
|
_, err = NewActorId("https://an.other.host/api/v1/activitypub/user-id/1", "forgejo")
|
2023-12-08 12:08:54 -05:00
|
|
|
if err != nil {
|
2023-12-08 13:41:22 -05:00
|
|
|
t.Errorf("this uri should be valid but was: %v", err)
|
2023-12-08 12:08:54 -05:00
|
|
|
}
|
|
|
|
}
|