Node.js based forum software built for the modern web
test: add tests for Announce(Delete) handling in inbox
Julian Lamb8b25936f960
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.jsindex 35efdfaa77..d827140e1a 100644--- a/src/activitypub/inbox.js+++ b/src/activitypub/inbox.js@@ -448,23 +448,10 @@ inbox.announce = async (req) => {break;}- case object.type === 'Create': {- object = object.object;- // falls through- }-case object.type === 'Delete': {- const id = object.id || object.object;- let type = object.type || undefined;-- // Deletes don't have their objects resolved automatically- try {- if (!type) {- ({ type } = await activitypub.get('uid', 0, id));- }- } catch (e) {- // probably 410/404- }+ let id = object.object.id || object.object; // expecting object reference+ const { id: localId } = await activitypub.helpers.resolveLocalId(id);+ id = localId || id;const exists = await posts.exists(id);if (!exists) {@@ -473,7 +460,7 @@ inbox.announce = async (req) => {}const _cid = await posts.getCidByPid(id);- if (_cid !== cid) {+ if (!utils.isNumber(cid) && _cid !== cid) { // matching & remote categories onlythrow new Error('[[error:invalid-cid]]');}@@ -482,6 +469,11 @@ inbox.announce = async (req) => {break;}+ case object.type === 'Create': {+ object = object.object;+ // falls through+ }+// Announce(Object)case activitypub._constants.acceptedPostTypes.includes(object.type): {if (String(object.id).startsWith(nconf.get('url'))) { // Local objectdiff --git a/test/activitypub/helpers.js b/test/activitypub/helpers.jsindex eb3056d2af..beb5d18c14 100644--- a/test/activitypub/helpers.js+++ b/test/activitypub/helpers.js@@ -252,3 +252,26 @@ Helpers.mocks.delete = (override = {}) => {return { activity };};+Helpers.mocks.delete = (override = {}) => {+ let actor = override.actor;+ let object = override.object;+ if (!actor) {+ ({ id: actor } = Helpers.mocks.person());+ }+ if (!object) {+ ({ id: object } = Helpers.mocks.note());+ }++ const activity = {+ '@context': 'https://www.w3.org/ns/activitystreams',+ id: `${Helpers.mocks._baseUrl}/delete/${encodeURIComponent(object.id || object)}`,+ type: 'Delete',+ to: [activitypub._constants.publicAddress],+ cc: [`${actor}/followers`],+ actor,+ object,+ };++ return { activity };+};+diff --git a/test/activitypub/inbox.js b/test/activitypub/inbox.jsindex 0a4c46691d..c855219653 100644--- a/test/activitypub/inbox.js+++ b/test/activitypub/inbox.js@@ -426,6 +426,77 @@ describe('Inbox', () => {assert.strictEqual(content, note.content);});});++ describe('(Delete)', () => {+ it('should delete a local post when announced', async () => {+ const uid = await user.create({ username: utils.generateUUID().slice(0, 10) });+ const { id: cid } = helpers.mocks.group();+ await activitypub.actors.assertGroup(cid);+ const { postData } = await topics.post({+ uid,+ cid,+ title: utils.generateUUID(),+ content: utils.generateUUID(),+ });++ // Create a delete activity for the local post+ const object = await activitypub.mocks.notes.public(postData);+ const { activity: deleteActivity } = helpers.mocks.delete({+ actor: `${nconf.get('url')}/uid/${postData.uid}`,+ object,+ });++ // Wrap it in an announce+ const { activity } = helpers.mocks.announce({+ actor: cid,+ object: deleteActivity,+ });++ // Verify post exists before deletion+ assert(await posts.exists(postData.pid));++ // Process the announce+ await activitypub.inbox.announce({ body: activity });++ // Verify post is deleted+ const isDeleted = await posts.getPostField(postData.pid, 'deleted');+ const exists = await posts.exists(postData.pid);+ assert.strictEqual(isDeleted, 1);+ });++ it('should delete a remote post when announced', async () => {+ const { id: cid } = helpers.mocks.group();++ // Create a remote note first+ const { id } = helpers.mocks.note({+ audience: [cid],+ });+ await activitypub.notes.assert(0, id, { skipChecks: true });++ // Verify it exists before deletion+ assert(await posts.exists(id));++ // Create a delete activity for the remote post+ const { activity: deleteActivity } = helpers.mocks.delete({+ actor: cid,+ object: id,+ });++ // Wrap it in an announce+ const { activity } = helpers.mocks.announce({+ actor: cid,+ object: deleteActivity,+ });++ // Process the announce+ await activitypub.inbox.announce({ body: activity });++ // Verify post is deleted+ const exists = await posts.exists(id);+ const isDeleted = await posts.getPostField(id, 'deleted');+ assert.strictEqual(isDeleted, 1);+ });+ });});describe('Like', () => {