From 42a0924137dd6f48aad77f8457e0780d46158a16 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 21 Feb 2024 10:26:26 -0500 Subject: [PATCH] test: refactor AP tests --- src/activitypub/index.js | 1 + src/activitypub/notes.js | 4 +- test/activitypub.js | 79 +++++++++++++++++++++++----------------- 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/activitypub/index.js b/src/activitypub/index.js index f9ff9c3507..6f787b7b4f 100644 --- a/src/activitypub/index.js +++ b/src/activitypub/index.js @@ -16,6 +16,7 @@ const ActivityPub = module.exports; ActivityPub._constants = Object.freeze({ publicAddress: 'https://www.w3.org/ns/activitystreams#Public', }); +ActivityPub._cache = requestCache; ActivityPub.helpers = require('./helpers'); ActivityPub.inbox = require('./inbox'); diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js index 7dbac80d7f..068fd8cb51 100644 --- a/src/activitypub/notes.js +++ b/src/activitypub/notes.js @@ -27,7 +27,9 @@ Notes.assert = async (uid, input, options = {}) => { await Promise.all(input.map(async (item) => { 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 exists = await db.exists(key); winston.verbose(`[activitypub/notes.assert] Asserting note id ${id}`); diff --git a/test/activitypub.js b/test/activitypub.js index d065563cc9..d002bca19d 100644 --- a/test/activitypub.js +++ b/test/activitypub.js @@ -97,15 +97,15 @@ describe('ActivityPub integration', () => { uid = await user.create({ username: slug }); }); - it('should throw when an invalid input is passed in', async () => { - await assert.rejects( - activitypub.helpers.resolveLocalId('ncl28h3qwhoiclwnevoinw3u'), - { message: '[[error:activitypub.invalid-id]]' } - ); + it('should return null when an invalid input is passed in', async () => { + const { type, id } = await activitypub.helpers.resolveLocalId('ncl28h3qwhoiclwnevoinw3u'); + assert.strictEqual(type, null); + assert.strictEqual(id, null); }); 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); }); @@ -400,51 +400,62 @@ describe('ActivityPub integration', () => { describe('Receipt of ActivityPub events to inboxes (federating IN)', () => { describe('Create', () => { describe('Note', () => { - let category; - let uid; - let note; + const slug = utils.generateUUID(); + const id = `https://example.org/status/${slug}`; + 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: 'Baz quux', + published: new Date().toISOString(), + source: { + content: '**Baz quux**', + mediaType: 'text/markdown', + }, + }; + let topic; before(async () => { - category = await categories.create({ name: utils.generateUUID().slice(0, 8) }); - const slug = slugify(utils.generateUUID().slice(0, 8)); - uid = await user.create({ username: slug }); + const controllers = require('../src/controllers'); - const { postData, topicData } = await topics.post({ - uid, - cid: category.cid, - title: 'Lipsum title', - content: 'Lorem ipsum dolor sit amet', - }); + activitypub._cache.set(`0;${id}`, remoteNote); + await controllers.activitypub.postInbox({ + body: { + type: 'Create', + actor: 'https://example.org/user/foobar', + object: remoteNote, + }, + }, { sendStatus: () => {} }); - const post = (await posts.getPostSummaryByPids([postData.pid], uid, { stripTags: false })).pop(); - note = await activitypub.mocks.note(post); - - 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); + const tid = await posts.getPostField(id, 'tid'); + const 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 () => { - const saved = await db.getObject(`post:${note.id}`); + const saved = await db.getObject(`post:${id}`); 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); + + topic = await topics.getTopicData(saved.tid); + assert(topic); + assert.strictEqual(saved.uid, 'https://example.org/user/foobar'); + assert.strictEqual(saved.content, 'Baz quux'); }); 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 () => { - assert.strictEqual(topic.mainPid, note.id); + assert.strictEqual(topic.mainPid, id); }); // todo: test topic replies, too