test(backend): restore AP unit tests (#9987)

This commit is contained in:
Kagami Sascha Rosylight 2023-02-19 07:27:14 +01:00 committed by GitHub
parent cd5615d354
commit 2aa73fdf6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 17 deletions

View file

@ -1,5 +1,16 @@
import Resolver from '../../src/activitypub/resolver.js';
import { IObject } from '../../src/activitypub/type.js';
import type { Config } from '@/config.js';
import type { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js';
import type { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import type { ApRequestService } from '@/core/activitypub/ApRequestService.js';
import { Resolver } from '@/core/activitypub/ApResolverService.js';
import type { IObject } from '@/core/activitypub/type.js';
import type { HttpRequestService } from '@/core/HttpRequestService.js';
import type { InstanceActorService } from '@/core/InstanceActorService.js';
import type { LoggerService } from '@/core/LoggerService.js';
import type { MetaService } from '@/core/MetaService.js';
import type { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
import type { NoteReactionsRepository, NotesRepository, PollsRepository, UsersRepository } from '@/models/index.js';
type MockResponse = {
type: string;
@ -8,6 +19,25 @@ type MockResponse = {
export class MockResolver extends Resolver {
private _rs = new Map<string, MockResponse>();
constructor(loggerService: LoggerService) {
super(
{} as Config,
{} as UsersRepository,
{} as NotesRepository,
{} as PollsRepository,
{} as NoteReactionsRepository,
{} as UtilityService,
{} as InstanceActorService,
{} as MetaService,
{} as ApRequestService,
{} as HttpRequestService,
{} as ApRendererService,
{} as ApDbResolverService,
loggerService,
);
}
public async _register(uri: string, content: string | Record<string, any>, type = 'application/activity+json') {
this._rs.set(uri, {
type,

View file

@ -2,8 +2,39 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import rndstr from 'rndstr';
import { Test } from '@nestjs/testing';
import { jest } from '@jest/globals';
import { ApNoteService } from '@/core/activitypub/models/ApNoteService.js';
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
import { GlobalModule } from '@/GlobalModule.js';
import { CoreModule } from '@/core/CoreModule.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { MockResolver } from '../misc/mock-resolver.js';
describe('ActivityPub', () => {
let noteService: ApNoteService;
let personService: ApPersonService;
let resolver: MockResolver;
beforeEach(async () => {
const app = await Test.createTestingModule({
imports: [GlobalModule, CoreModule],
}).compile();
await app.init();
app.enableShutdownHooks();
noteService = app.get<ApNoteService>(ApNoteService);
personService = app.get<ApPersonService>(ApPersonService);
resolver = new MockResolver(await app.resolve<LoggerService>(LoggerService));
// Prevent ApPersonService from fetching instance, as it causes Jest import-after-test error
const federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService);
jest.spyOn(federatedInstanceService, 'fetch').mockImplementation(() => new Promise(() => {}));
});
describe('Parse minimum object', () => {
const host = 'https://host1.test';
const preferredUsername = `${rndstr('A-Z', 4)}${rndstr('a-z', 4)}`;
@ -28,13 +59,9 @@ describe('ActivityPub', () => {
};
test('Minimum Actor', async () => {
const { MockResolver } = await import('../misc/mock-resolver.js');
const { createPerson } = await import('../../src/activitypub/models/person.js');
const resolver = new MockResolver();
resolver._register(actor.id, actor);
const user = await createPerson(actor.id, resolver);
const user = await personService.createPerson(actor.id, resolver);
assert.deepStrictEqual(user.uri, actor.id);
assert.deepStrictEqual(user.username, actor.preferredUsername);
@ -42,14 +69,10 @@ describe('ActivityPub', () => {
});
test('Minimum Note', async () => {
const { MockResolver } = await import('../misc/mock-resolver.js');
const { createNote } = await import('../../src/activitypub/models/note.js');
const resolver = new MockResolver();
resolver._register(actor.id, actor);
resolver._register(post.id, post);
const note = await createNote(post.id, resolver, true);
const note = await noteService.createNote(post.id, resolver, true);
assert.deepStrictEqual(note?.uri, post.id);
assert.deepStrictEqual(note.visibility, 'public');
@ -75,13 +98,9 @@ describe('ActivityPub', () => {
};
test('Actor', async () => {
const { MockResolver } = await import('../misc/mock-resolver.js');
const { createPerson } = await import('../../src/activitypub/models/person.js');
const resolver = new MockResolver();
resolver._register(actor.id, actor);
const user = await createPerson(actor.id, resolver);
const user = await personService.createPerson(actor.id, resolver);
assert.deepStrictEqual(user.name, actor.name.substr(0, 128));
});