test: refactor AP tests

This commit is contained in:
Julian Lam
2024-02-21 10:26:26 -05:00
parent e138b915b9
commit 42a0924137
3 changed files with 49 additions and 35 deletions

View File

@@ -16,6 +16,7 @@ const ActivityPub = module.exports;
ActivityPub._constants = Object.freeze({ ActivityPub._constants = Object.freeze({
publicAddress: 'https://www.w3.org/ns/activitystreams#Public', publicAddress: 'https://www.w3.org/ns/activitystreams#Public',
}); });
ActivityPub._cache = requestCache;
ActivityPub.helpers = require('./helpers'); ActivityPub.helpers = require('./helpers');
ActivityPub.inbox = require('./inbox'); ActivityPub.inbox = require('./inbox');

View File

@@ -27,7 +27,9 @@ Notes.assert = async (uid, input, options = {}) => {
await Promise.all(input.map(async (item) => { await Promise.all(input.map(async (item) => {
let id = activitypub.helpers.isUri(item) ? item : item.pid; let id = activitypub.helpers.isUri(item) ? item : item.pid;
id = await Notes.resolveId(uid, id); if (activitypub.helpers.isUri(id)) {
id = await Notes.resolveId(uid, id);
}
const key = `post:${id}`; const key = `post:${id}`;
const exists = await db.exists(key); const exists = await db.exists(key);
winston.verbose(`[activitypub/notes.assert] Asserting note id ${id}`); winston.verbose(`[activitypub/notes.assert] Asserting note id ${id}`);

View File

@@ -97,15 +97,15 @@ describe('ActivityPub integration', () => {
uid = await user.create({ username: slug }); uid = await user.create({ username: slug });
}); });
it('should throw when an invalid input is passed in', async () => { it('should return null when an invalid input is passed in', async () => {
await assert.rejects( const { type, id } = await activitypub.helpers.resolveLocalId('ncl28h3qwhoiclwnevoinw3u');
activitypub.helpers.resolveLocalId('ncl28h3qwhoiclwnevoinw3u'), assert.strictEqual(type, null);
{ message: '[[error:activitypub.invalid-id]]' } assert.strictEqual(id, null);
);
}); });
it('should return null when valid input is passed but does not resolve', async () => { it('should return null when valid input is passed but does not resolve', async () => {
const { id } = await activitypub.helpers.resolveLocalId(`acct:foobar@${nconf.get('url_parsed').host}`); const { type, id } = await activitypub.helpers.resolveLocalId(`acct:foobar@${nconf.get('url_parsed').host}`);
assert.strictEqual(type, 'user');
assert.strictEqual(id, null); assert.strictEqual(id, null);
}); });
@@ -400,51 +400,62 @@ describe('ActivityPub integration', () => {
describe('Receipt of ActivityPub events to inboxes (federating IN)', () => { describe('Receipt of ActivityPub events to inboxes (federating IN)', () => {
describe('Create', () => { describe('Create', () => {
describe('Note', () => { describe('Note', () => {
let category; const slug = utils.generateUUID();
let uid; const id = `https://example.org/status/${slug}`;
let note; const remoteNote = {
'@context': 'https://www.w3.org/ns/activitystreams',
id,
url: id,
type: 'Note',
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: ['https://example.org/user/foobar/followers'],
inReplyTo: null,
attributedTo: 'https://example.org/user/foobar',
name: 'Foo Bar',
content: '<b>Baz quux</b>',
published: new Date().toISOString(),
source: {
content: '**Baz quux**',
mediaType: 'text/markdown',
},
};
let topic; let topic;
before(async () => { before(async () => {
category = await categories.create({ name: utils.generateUUID().slice(0, 8) }); const controllers = require('../src/controllers');
const slug = slugify(utils.generateUUID().slice(0, 8));
uid = await user.create({ username: slug });
const { postData, topicData } = await topics.post({ activitypub._cache.set(`0;${id}`, remoteNote);
uid, await controllers.activitypub.postInbox({
cid: category.cid, body: {
title: 'Lipsum title', type: 'Create',
content: 'Lorem ipsum dolor sit amet', actor: 'https://example.org/user/foobar',
}); object: remoteNote,
},
}, { sendStatus: () => {} });
const post = (await posts.getPostSummaryByPids([postData.pid], uid, { stripTags: false })).pop(); const tid = await posts.getPostField(id, 'tid');
note = await activitypub.mocks.note(post); const topic = await topics.getTopicData(tid);
await activitypub.send('uid', uid, [`${nconf.get('url')}/uid/${uid}`], {
type: 'Create',
object: note,
});
const tid = await posts.getPostField(note.id, 'tid');
topic = await topics.getTopicData(tid);
}); });
it('should create a new topic if Note is at root-level or its parent has not been seen before', async () => { it('should create a new topic if Note is at root-level or its parent has not been seen before', async () => {
const saved = await db.getObject(`post:${note.id}`); const saved = await db.getObject(`post:${id}`);
assert(saved); assert(saved);
assert(topic);
assert.strictEqual(saved.uid, `${nconf.get('url')}/uid/${uid}`);
assert.strictEqual(saved.content, 'Lorem ipsum dolor sit amet');
assert(saved.tid); assert(saved.tid);
topic = await topics.getTopicData(saved.tid);
assert(topic);
assert.strictEqual(saved.uid, 'https://example.org/user/foobar');
assert.strictEqual(saved.content, '<b>Baz quux</b>');
}); });
it('should properly save the topic title in the topic hash', async () => { it('should properly save the topic title in the topic hash', async () => {
assert.strictEqual(topic.title, 'Lipsum title'); assert.strictEqual(topic.title, 'Foo Bar');
}); });
it('should properly save the mainPid in the topic hash', async () => { it('should properly save the mainPid in the topic hash', async () => {
assert.strictEqual(topic.mainPid, note.id); assert.strictEqual(topic.mainPid, id);
}); });
// todo: test topic replies, too // todo: test topic replies, too