2023-09-22 00:42:59 +02:00
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getConfig } from "@config" ;
import { APActor } from "activitypub-types" ;
import { afterAll , beforeAll , describe , expect , test } from "bun:test" ;
import { AppDataSource } from "~database/datasource" ;
import { RawActivity } from "~database/entities/RawActivity" ;
import { User } from "~database/entities/User" ;
const config = getConfig ( ) ;
beforeAll ( async ( ) = > {
if ( ! AppDataSource . isInitialized ) await AppDataSource . initialize ( ) ;
// Initialize test user
await User . createNew ( {
email : "test@test.com" ,
username : "test" ,
password : "test" ,
display_name : "" ,
} ) ;
} ) ;
2023-09-22 03:09:14 +02:00
describe ( "POST /@test/actor" , ( ) = > {
2023-09-22 00:42:59 +02:00
test ( "should return a valid ActivityPub Actor when querying an existing user" , async ( ) = > {
2023-09-22 04:15:12 +02:00
const response = await fetch ( ` ${ config . http . base_url } /@test/actor ` , {
method : "GET" ,
headers : {
Accept : "application/activity+json" ,
} ,
} ) ;
2023-09-22 00:42:59 +02:00
expect ( response . status ) . toBe ( 200 ) ;
expect ( response . headers . get ( "content-type" ) ) . toBe (
"application/activity+json"
) ;
const actor : APActor = await response . json ( ) ;
expect ( actor . type ) . toBe ( "Person" ) ;
2023-09-22 04:15:12 +02:00
expect ( actor . id ) . toBe ( ` ${ config . http . base_url } /@test ` ) ;
2023-09-22 00:42:59 +02:00
expect ( actor . preferredUsername ) . toBe ( "test" ) ;
2023-09-22 04:15:12 +02:00
expect ( actor . inbox ) . toBe ( ` ${ config . http . base_url } /@test/inbox ` ) ;
expect ( actor . outbox ) . toBe ( ` ${ config . http . base_url } /@test/outbox ` ) ;
expect ( actor . followers ) . toBe ( ` ${ config . http . base_url } /@test/followers ` ) ;
expect ( actor . following ) . toBe ( ` ${ config . http . base_url } /@test/following ` ) ;
2023-09-22 00:42:59 +02:00
expect ( ( actor as any ) . publicKey ) . toBeDefined ( ) ;
expect ( ( actor as any ) . publicKey . id ) . toBeDefined ( ) ;
expect ( ( actor as any ) . publicKey . owner ) . toBe (
2023-09-22 04:15:12 +02:00
` ${ config . http . base_url } /@test `
2023-09-22 00:42:59 +02:00
) ;
expect ( ( actor as any ) . publicKey . publicKeyPem ) . toBeDefined ( ) ;
2023-09-22 03:09:14 +02:00
expect ( ( actor as any ) . publicKey . publicKeyPem ) . toMatch (
/(-----BEGIN PUBLIC KEY-----(\n|\r|\r\n)([0-9a-zA-Z+/=]{64}(\n|\r|\r\n))*([0-9a-zA-Z+/=]{1,63}(\n|\r|\r\n))?-----END PUBLIC KEY-----)|(-----BEGIN PRIVATE KEY-----(\n|\r|\r\n)([0-9a-zA-Z+/=]{64}(\n|\r|\r\n))*([0-9a-zA-Z+/=]{1,63}(\n|\r|\r\n))?-----END PRIVATE KEY-----)/
) ;
2023-09-22 00:42:59 +02:00
} ) ;
} ) ;
afterAll ( async ( ) = > {
// Clean up user
const user = await User . findOneBy ( {
username : "test" ,
} ) ;
const activities = await RawActivity . createQueryBuilder ( "activity" )
. where ( "activity.data->>'actor' = :actor" , {
2023-09-22 04:15:12 +02:00
actor : ` ${ config . http . base_url } /@test ` ,
2023-09-22 00:42:59 +02:00
} )
. leftJoinAndSelect ( "activity.objects" , "objects" )
. getMany ( ) ;
// Delete all created objects and activities as part of testing
await Promise . all (
activities . map ( async activity = > {
await Promise . all (
activity . objects . map ( async object = > await object . remove ( ) )
) ;
await activity . remove ( ) ;
} )
) ;
2023-09-22 03:09:14 +02:00
if ( user ) {
await user . selfDestruct ( ) ;
await user . remove ( ) ;
}
2023-09-22 00:42:59 +02:00
} ) ;