2025-04-16 08:07:51 +00:00
// Copyright 2023, 2024, 2025 The Forgejo Authors. All rights reserved.
2024-05-07 17:58:13 +02:00
// SPDX-License-Identifier: MIT
package forgefed
import (
"testing"
2025-06-02 22:29:10 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2024-05-07 17:58:13 +02:00
)
2025-06-02 22:29:10 +02:00
func TestActorNew ( t * testing . T ) {
sut , err := NewActorID ( "https://an.other.forgejo.host/api/v1/activitypub/user-id/5" )
require . NoError ( t , err )
assert . Equal ( t , ActorID {
ID : "5" ,
HostSchema : "https" ,
Path : "api/v1/activitypub/user-id" ,
Host : "an.other.forgejo.host" ,
HostPort : 443 ,
UnvalidatedInput : "https://an.other.forgejo.host/api/v1/activitypub/user-id/5" ,
IsPortSupplemented : true ,
} , sut )
sut , err = NewActorID ( "https://an.other.forgejo.host/api/v1/activitypub/actor" )
require . NoError ( t , err )
assert . Equal ( t , ActorID {
ID : "actor" ,
HostSchema : "https" ,
Path : "api/v1/activitypub" ,
Host : "an.other.forgejo.host" ,
HostPort : 443 ,
UnvalidatedInput : "https://an.other.forgejo.host/api/v1/activitypub/actor" ,
IsPortSupplemented : true ,
} , sut )
sut , err = NewActorID ( "https://an.other.gts.host/users/me" )
require . NoError ( t , err )
assert . Equal ( t , ActorID {
ID : "me" ,
HostSchema : "https" ,
Path : "users" ,
Host : "an.other.gts.host" ,
HostPort : 443 ,
UnvalidatedInput : "https://an.other.gts.host/users/me" ,
IsPortSupplemented : true ,
} , sut )
2024-05-07 17:58:13 +02:00
}
func TestActorIdValidation ( t * testing . T ) {
sut := ActorID { }
2025-04-16 08:07:51 +00:00
sut . HostSchema = "https"
2024-05-07 17:58:13 +02:00
sut . Path = "api/v1/activitypub/user-id"
sut . Host = "an.other.host"
2025-04-16 08:07:51 +00:00
sut . HostPort = 443
sut . IsPortSupplemented = true
2024-05-07 17:58:13 +02:00
sut . UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/"
2025-06-02 22:29:10 +02:00
result := sut . Validate ( )
assert . Len ( t , result , 1 )
assert . Equal ( t , "ID should not be empty" , result [ 0 ] )
2024-05-07 17:58:13 +02:00
sut = ActorID { }
sut . ID = "1"
2025-04-16 08:07:51 +00:00
sut . HostSchema = "https"
2024-05-07 17:58:13 +02:00
sut . Path = "api/v1/activitypub/user-id"
sut . Host = "an.other.host"
2025-04-16 08:07:51 +00:00
sut . HostPort = 443
sut . IsPortSupplemented = true
2024-05-07 17:58:13 +02:00
sut . UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action"
2025-06-02 22:29:10 +02:00
result = sut . Validate ( )
assert . Len ( t , result , 1 )
assert . Equal ( t , "not all input was parsed, \nUnvalidated Input:\"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" \nParsed URI: \"https://an.other.host/api/v1/activitypub/user-id/1\"" , result [ 0 ] )
2024-05-07 17:58:13 +02:00
}