2021-07-10 17:14:57 +03:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import * as assert from 'assert';
|
2022-05-21 16:21:41 +03:00
|
|
|
import rndstr from 'rndstr';
|
2023-02-19 08:27:14 +02:00
|
|
|
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';
|
2021-07-10 17:14:57 +03:00
|
|
|
|
|
|
|
describe('ActivityPub', () => {
|
2023-02-19 08:27:14 +02:00
|
|
|
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(() => {}));
|
|
|
|
});
|
|
|
|
|
2021-07-10 17:14:57 +03:00
|
|
|
describe('Parse minimum object', () => {
|
|
|
|
const host = 'https://host1.test';
|
|
|
|
const preferredUsername = `${rndstr('A-Z', 4)}${rndstr('a-z', 4)}`;
|
|
|
|
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
|
|
|
|
|
|
|
|
const actor = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
id: actorId,
|
|
|
|
type: 'Person',
|
|
|
|
preferredUsername,
|
|
|
|
inbox: `${actorId}/inbox`,
|
|
|
|
outbox: `${actorId}/outbox`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const post = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
id: `${host}/users/${rndstr('0-9a-z', 8)}`,
|
|
|
|
type: 'Note',
|
|
|
|
attributedTo: actor.id,
|
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
content: 'あ',
|
|
|
|
};
|
|
|
|
|
2023-02-02 11:18:25 +02:00
|
|
|
test('Minimum Actor', async () => {
|
2021-07-10 17:14:57 +03:00
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
|
2023-02-19 08:27:14 +02:00
|
|
|
const user = await personService.createPerson(actor.id, resolver);
|
2021-07-10 17:14:57 +03:00
|
|
|
|
|
|
|
assert.deepStrictEqual(user.uri, actor.id);
|
|
|
|
assert.deepStrictEqual(user.username, actor.preferredUsername);
|
|
|
|
assert.deepStrictEqual(user.inbox, actor.inbox);
|
|
|
|
});
|
|
|
|
|
2023-02-02 11:18:25 +02:00
|
|
|
test('Minimum Note', async () => {
|
2021-07-10 17:14:57 +03:00
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
resolver._register(post.id, post);
|
|
|
|
|
2023-02-19 08:27:14 +02:00
|
|
|
const note = await noteService.createNote(post.id, resolver, true);
|
2021-07-10 17:14:57 +03:00
|
|
|
|
|
|
|
assert.deepStrictEqual(note?.uri, post.id);
|
2022-05-21 16:21:41 +03:00
|
|
|
assert.deepStrictEqual(note.visibility, 'public');
|
|
|
|
assert.deepStrictEqual(note.text, post.content);
|
2021-07-10 17:14:57 +03:00
|
|
|
});
|
|
|
|
});
|
2021-08-17 11:25:19 +03:00
|
|
|
|
|
|
|
describe('Truncate long name', () => {
|
|
|
|
const host = 'https://host1.test';
|
|
|
|
const preferredUsername = `${rndstr('A-Z', 4)}${rndstr('a-z', 4)}`;
|
|
|
|
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
|
|
|
|
|
|
|
|
const name = rndstr('0-9a-z', 129);
|
|
|
|
|
|
|
|
const actor = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
id: actorId,
|
|
|
|
type: 'Person',
|
|
|
|
preferredUsername,
|
|
|
|
name,
|
|
|
|
inbox: `${actorId}/inbox`,
|
|
|
|
outbox: `${actorId}/outbox`,
|
|
|
|
};
|
|
|
|
|
2023-02-02 11:18:25 +02:00
|
|
|
test('Actor', async () => {
|
2021-08-17 11:25:19 +03:00
|
|
|
resolver._register(actor.id, actor);
|
|
|
|
|
2023-02-19 08:27:14 +02:00
|
|
|
const user = await personService.createPerson(actor.id, resolver);
|
2021-08-17 11:25:19 +03:00
|
|
|
|
|
|
|
assert.deepStrictEqual(user.name, actor.name.substr(0, 128));
|
|
|
|
});
|
|
|
|
});
|
2021-07-10 17:14:57 +03:00
|
|
|
});
|