2023-11-15 14:07:23 +00:00
|
|
|
// Copyright 2023 The forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-11-24 12:23:03 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/forgefed"
|
|
|
|
ap "github.com/go-ap/activitypub"
|
2023-11-15 14:07:23 +00:00
|
|
|
)
|
|
|
|
|
2023-11-24 12:23:03 +00:00
|
|
|
var emptyMockStar *forgefed.Star = &forgefed.Star{
|
|
|
|
Source: "",
|
|
|
|
Activity: ap.Activity{
|
|
|
|
Actor: ap.IRI(""),
|
|
|
|
Type: "Star",
|
|
|
|
Object: ap.IRI(""),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-22 15:40:28 +00:00
|
|
|
func TestActorParserEmpty(t *testing.T) {
|
2023-12-06 15:14:50 +00:00
|
|
|
item := ""
|
2023-11-22 15:40:28 +00:00
|
|
|
want := ActorID{}
|
|
|
|
|
2023-12-06 15:14:50 +00:00
|
|
|
got, _ := ParseActorID(item, "forgejo")
|
2023-11-22 15:40:28 +00:00
|
|
|
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("ParseActorID returned non empty actor id for empty input.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActorParserValid(t *testing.T) {
|
2023-12-06 15:14:50 +00:00
|
|
|
item := mockStar.Actor.GetID().String()
|
2023-11-22 15:40:28 +00:00
|
|
|
want := ActorID{
|
|
|
|
userId: "1",
|
2023-11-24 12:23:03 +00:00
|
|
|
source: "forgejo",
|
|
|
|
schema: "https",
|
2023-12-06 15:14:50 +00:00
|
|
|
path: "api/v1/activitypub/user-id",
|
2023-11-22 15:40:28 +00:00
|
|
|
host: "repo.prod.meissa.de",
|
|
|
|
port: "",
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:14:50 +00:00
|
|
|
got, _ := ParseActorID(item, "forgejo")
|
2023-11-22 15:40:28 +00:00
|
|
|
|
|
|
|
if got != want {
|
2023-11-24 12:23:03 +00:00
|
|
|
t.Errorf("\nParseActorID did not return want: %v\n but %v", want, got)
|
2023-11-22 15:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateValid(t *testing.T) {
|
|
|
|
item := ActorID{
|
|
|
|
userId: "1",
|
2023-11-24 12:23:03 +00:00
|
|
|
source: "forgejo",
|
|
|
|
schema: "https",
|
2023-11-22 15:40:28 +00:00
|
|
|
path: "/api/v1/activitypub/user-id/1",
|
|
|
|
host: "repo.prod.meissa.de",
|
|
|
|
port: "",
|
2023-11-15 14:07:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 10:38:01 +00:00
|
|
|
if valid, _ := item.IsValid(); !valid {
|
|
|
|
t.Errorf("Actor was invalid with valid input.")
|
2023-11-15 14:07:23 +00:00
|
|
|
}
|
2023-11-22 15:40:28 +00:00
|
|
|
}
|
2023-11-15 14:07:23 +00:00
|
|
|
|
2023-11-22 15:40:28 +00:00
|
|
|
func TestValidateInvalid(t *testing.T) {
|
2023-12-06 15:14:50 +00:00
|
|
|
item := emptyMockStar.Actor.GetID().String()
|
2023-11-15 14:07:23 +00:00
|
|
|
|
2023-12-06 15:14:50 +00:00
|
|
|
actor, _ := ParseActorID(item, "forgejo")
|
2023-11-16 15:03:05 +00:00
|
|
|
|
2023-11-24 10:38:01 +00:00
|
|
|
if valid, _ := actor.IsValid(); valid {
|
|
|
|
t.Errorf("Actor was valid with invalid input.")
|
2023-11-15 14:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-23 16:02:54 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|