2023-11-15 14:07:23 +00:00
// Copyright 2023 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2023-12-09 13:53:40 +00:00
package forgefed
2023-11-15 14:07:23 +00:00
import (
2024-01-04 17:04:46 +00:00
"reflect"
"strings"
2023-11-15 14:07:23 +00:00
"testing"
2023-12-09 17:30:47 +00:00
2024-01-04 17:04:46 +00:00
ap "github.com/go-ap/activitypub"
2023-12-09 17:30:47 +00:00
"code.gitea.io/gitea/modules/setting"
2023-12-29 17:09:27 +00:00
"code.gitea.io/gitea/modules/validation"
2023-11-15 14:07:23 +00:00
)
2023-12-08 19:37:26 +00:00
func TestNewPersonId ( t * testing . T ) {
2023-12-22 13:52:10 +00:00
expected := PersonID { }
2023-12-22 14:10:21 +00:00
expected . ID = "1"
2023-12-09 17:30:47 +00:00
expected . Source = "forgejo"
expected . Schema = "https"
expected . Path = "api/v1/activitypub/user-id"
expected . Host = "an.other.host"
expected . Port = ""
expected . UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1"
2023-12-22 13:52:10 +00:00
sut , _ := NewPersonID ( "https://an.other.host/api/v1/activitypub/user-id/1" , "forgejo" )
2023-12-08 19:37:26 +00:00
if sut != expected {
t . Errorf ( "expected: %v\n but was: %v\n" , expected , sut )
}
2023-12-22 13:52:10 +00:00
expected = PersonID { }
2023-12-22 14:10:21 +00:00
expected . ID = "1"
2023-12-09 17:30:47 +00:00
expected . Source = "forgejo"
expected . Schema = "https"
expected . Path = "api/v1/activitypub/user-id"
expected . Host = "an.other.host"
expected . Port = "443"
expected . UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1"
2023-12-22 13:52:10 +00:00
sut , _ = NewPersonID ( "https://an.other.host:443/api/v1/activitypub/user-id/1" , "forgejo" )
2023-12-08 19:37:26 +00:00
if sut != expected {
t . Errorf ( "expected: %v\n but was: %v\n" , expected , sut )
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-12-09 17:30:47 +00:00
func TestNewRepositoryId ( t * testing . T ) {
setting . AppURL = "http://localhost:3000/"
2023-12-22 13:52:10 +00:00
expected := RepositoryID { }
2023-12-22 14:10:21 +00:00
expected . ID = "1"
2023-12-09 17:30:47 +00:00
expected . Source = "forgejo"
expected . Schema = "http"
expected . Path = "api/activitypub/repository-id"
expected . Host = "localhost"
expected . Port = "3000"
expected . UnvalidatedInput = "http://localhost:3000/api/activitypub/repository-id/1"
2023-12-22 14:00:42 +00:00
sut , _ := NewRepositoryID ( "http://localhost:3000/api/activitypub/repository-id/1" , "forgejo" )
2023-12-09 17:30:47 +00:00
if sut != expected {
t . Errorf ( "expected: %v\n but was: %v\n" , expected , sut )
2023-12-08 19:51:54 +00:00
}
2023-12-09 17:30:47 +00:00
}
2023-12-21 14:01:49 +00:00
func TestActorIdValidation ( t * testing . T ) {
2023-12-22 13:52:10 +00:00
sut := ActorID { }
2023-12-09 17:30:47 +00:00
sut . Source = "forgejo"
sut . Schema = "https"
sut . Path = "api/v1/activitypub/user-id"
sut . Host = "an.other.host"
sut . Port = ""
sut . UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/"
2023-12-08 19:51:54 +00:00
if sut . Validate ( ) [ 0 ] != "Field userId may not be empty" {
t . Errorf ( "validation error expected but was: %v\n" , sut . Validate ( ) )
}
2023-12-22 13:52:10 +00:00
sut = ActorID { }
2023-12-22 14:10:21 +00:00
sut . ID = "1"
2023-12-09 17:30:47 +00:00
sut . Source = "forgejo"
sut . Schema = "https"
2023-12-21 14:01:49 +00:00
sut . Path = "api/v1/activitypub/user-id"
2023-12-09 17:30:47 +00:00
sut . Host = "an.other.host"
sut . Port = ""
2023-12-21 14:01:49 +00:00
sut . UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action"
if sut . Validate ( ) [ 0 ] != "not all input: \"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" was parsed: \"https://an.other.host/api/v1/activitypub/user-id/1\"" {
2023-12-08 19:51:54 +00:00
t . Errorf ( "validation error expected but was: %v\n" , sut . Validate ( ) )
}
2023-12-21 14:01:49 +00:00
}
2023-12-08 19:51:54 +00:00
2023-12-21 14:01:49 +00:00
func TestPersonIdValidation ( t * testing . T ) {
2023-12-22 13:52:10 +00:00
sut := PersonID { }
2023-12-22 14:10:21 +00:00
sut . ID = "1"
2023-12-09 17:30:47 +00:00
sut . Source = "forgejo"
sut . Schema = "https"
2023-12-21 14:01:49 +00:00
sut . Path = "path"
2023-12-09 17:30:47 +00:00
sut . Host = "an.other.host"
sut . Port = ""
2023-12-21 14:01:49 +00:00
sut . UnvalidatedInput = "https://an.other.host/path/1"
2023-12-29 17:09:27 +00:00
if _ , err := validation . IsValid ( sut ) ; err . Error ( ) != "path: \"path\" has to be a person specific api path" {
2023-12-21 14:01:49 +00:00
t . Errorf ( "validation error expected but was: %v\n" , err )
2023-12-08 19:51:54 +00:00
}
2023-12-29 14:48:45 +00:00
sut = PersonID { }
sut . ID = "1"
sut . Source = "forgejox"
sut . Schema = "https"
sut . Path = "api/v1/activitypub/user-id"
sut . Host = "an.other.host"
sut . Port = ""
sut . UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1"
if sut . Validate ( ) [ 0 ] != "Value forgejox is not contained in allowed values [[forgejo gitea]]" {
t . Errorf ( "validation error expected but was: %v\n" , sut . Validate ( ) )
}
2023-12-08 19:51:54 +00:00
}
2023-12-08 19:37:26 +00:00
func TestWebfingerId ( t * testing . T ) {
2023-12-22 13:52:10 +00:00
sut , _ := NewPersonID ( "https://codeberg.org/api/v1/activitypub/user-id/12345" , "forgejo" )
2023-12-08 19:37:26 +00:00
if sut . AsWebfinger ( ) != "@12345@codeberg.org" {
t . Errorf ( "wrong webfinger: %v" , sut . AsWebfinger ( ) )
2023-11-15 14:07:23 +00:00
}
2023-11-23 16:02:54 +00:00
2023-12-22 13:52:10 +00:00
sut , _ = NewPersonID ( "https://Codeberg.org/api/v1/activitypub/user-id/12345" , "forgejo" )
2023-12-08 19:37:26 +00:00
if sut . AsWebfinger ( ) != "@12345@codeberg.org" {
t . Errorf ( "wrong webfinger: %v" , sut . AsWebfinger ( ) )
2023-11-23 16:02:54 +00:00
}
}
2023-12-08 17:08:54 +00:00
func TestShouldThrowErrorOnInvalidInput ( t * testing . T ) {
2023-12-29 17:09:27 +00:00
var err any
// TODO: remove after test
//_, err = NewPersonId("", "forgejo")
//if err == nil {
// t.Errorf("empty input should be invalid.")
//}
2023-12-08 17:08:54 +00:00
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "http://localhost:3000/api/v1/something" , "forgejo" )
2023-12-08 17:08:54 +00:00
if err == nil {
t . Errorf ( "localhost uris are not external" )
}
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "./api/v1/something" , "forgejo" )
2023-12-08 17:33:26 +00:00
if err == nil {
2023-12-08 18:41:22 +00:00
t . Errorf ( "relative uris are not alowed" )
}
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "http://1.2.3.4/api/v1/something" , "forgejo" )
2023-12-08 18:41:22 +00:00
if err == nil {
t . Errorf ( "uri may not be ip-4 based" )
}
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "http:///[fe80::1ff:fe23:4567:890a%25eth0]/api/v1/something" , "forgejo" )
2023-12-08 18:41:22 +00:00
if err == nil {
t . Errorf ( "uri may not be ip-6 based" )
}
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "https://codeberg.org/api/v1/activitypub/../activitypub/user-id/12345" , "forgejo" )
2023-12-08 18:41:22 +00:00
if err == nil {
t . Errorf ( "uri may not contain relative path elements" )
2023-12-08 17:33:26 +00:00
}
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "https://myuser@an.other.host/api/v1/activitypub/user-id/1" , "forgejo" )
2023-12-08 19:37:26 +00:00
if err == nil {
t . Errorf ( "uri may not contain unparsed elements" )
}
2023-12-08 17:08:54 +00:00
2023-12-22 13:52:10 +00:00
_ , err = NewPersonID ( "https://an.other.host/api/v1/activitypub/user-id/1" , "forgejo" )
2023-12-08 17:08:54 +00:00
if err != nil {
2023-12-08 18:41:22 +00:00
t . Errorf ( "this uri should be valid but was: %v" , err )
2023-12-08 17:08:54 +00:00
}
}
2024-01-04 17:04:46 +00:00
func Test_PersonMarshalJSON ( t * testing . T ) {
sut := ForgePerson { }
sut . Type = "Person"
sut . PreferredUsername = ap . NaturalLanguageValuesNew ( )
sut . PreferredUsername . Set ( "en" , ap . Content ( "MaxMuster" ) )
result , _ := sut . MarshalJSON ( )
2024-01-14 12:03:51 +00:00
if string ( result ) != "{\"type\":\"Person\",\"preferredUsername\":\"MaxMuster\"}" {
2024-01-04 17:04:46 +00:00
t . Errorf ( "MarshalJSON() was = %q" , result )
}
}
func Test_PersonUnmarshalJSON ( t * testing . T ) {
expected := & ForgePerson {
Actor : ap . Actor {
Type : "Person" ,
PreferredUsername : ap . NaturalLanguageValues {
ap . LangRefValue { Ref : "en" , Value : [ ] byte ( "MaxMuster" ) } ,
} ,
2024-01-13 15:22:49 +00:00
} ,
}
2024-01-04 17:04:46 +00:00
sut := new ( ForgePerson )
err := sut . UnmarshalJSON ( [ ] byte ( ` { "type":"Person","preferredUsername":"MaxMuster"} ` ) )
if err != nil {
t . Errorf ( "UnmarshalJSON() unexpected error: %v" , err )
}
x , _ := expected . MarshalJSON ( )
y , _ := sut . MarshalJSON ( )
if ! reflect . DeepEqual ( x , y ) {
t . Errorf ( "UnmarshalJSON() expected: %q got: %q" , x , y )
}
expectedStr := strings . ReplaceAll ( strings . ReplaceAll ( ` {
"id" : "https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/10" ,
"type" : "Person" ,
"icon" : { "type" : "Image" , "mediaType" : "image/png" , "url" : "https://federated-repo.prod.meissa.de/avatar/fa7f9c4af2a64f41b1bef292bf872614" } ,
"url" : "https://federated-repo.prod.meissa.de/stargoose9" ,
"inbox" : "https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/10/inbox" ,
"outbox" : "https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/10/outbox" ,
"preferredUsername" : "stargoose9" ,
"publicKey" : { "id" : "https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/10#main-key" ,
"owner" : "https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/10" ,
"publicKeyPem" : "-----BEGIN PUBLIC KEY-----\nMIIBoj...XAgMBAAE=\n-----END PUBLIC KEY-----\n" } } ` ,
"\n" , "" ) ,
"\t" , "" )
err = sut . UnmarshalJSON ( [ ] byte ( expectedStr ) )
if err != nil {
t . Errorf ( "UnmarshalJSON() unexpected error: %v" , err )
}
result , _ := sut . MarshalJSON ( )
if expectedStr != string ( result ) {
t . Errorf ( "UnmarshalJSON() expected: %q got: %q" , expectedStr , result )
}
}
func TestForgePersonValidation ( t * testing . T ) {
sut := new ( ForgePerson )
sut . UnmarshalJSON ( [ ] byte ( ` { "type":"Person","preferredUsername":"MaxMuster"} ` ) )
if res , _ := validation . IsValid ( sut ) ; ! res {
t . Errorf ( "sut expected to be valid: %v\n" , sut . Validate ( ) )
}
}