Node.js based forum software built for the modern web
test: fix all note tests that were not written well for testing inbox handling of severity 3 blocklist items
Julian Lam0c71c0f28363
diff --git a/src/activitypub/blocklists.js b/src/activitypub/blocklists.jsindex ae4c395a5d..71d921d4d5 100644--- a/src/activitypub/blocklists.js+++ b/src/activitypub/blocklists.js@@ -112,7 +112,7 @@ Blocklists.check = async (domain) => {});return {- allowed: false,+ allowed: bestSeverity > severityScore.silence,severity: bestSeverity,listUrl: bestListUrl,};diff --git a/src/activitypub/notes.js b/src/activitypub/notes.jsindex 0fea5875af..e806443f37 100644--- a/src/activitypub/notes.js+++ b/src/activitypub/notes.js@@ -256,31 +256,29 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {if (!hasTid) {activitypub.helpers.log(`[activitypub/notes.assert] hasTid=${hasTid}, skipChecks=${options.skipChecks}, mainPid=${mainPid}`);- if (!options.skipChecks) {- const { hostname: mainHostname } = new URL(mainPid);- const mainResult = await activitypub.instances.isAllowed(mainHostname);+ const { hostname: mainHostname } = new URL(mainPid);+ const mainResult = await activitypub.instances.isAllowed(mainHostname);- if (!mainResult.allowed) {- activitypub.helpers.log(`[activitypub/notes.assert] Not asserting ${mainPid}, domain is blocked.`);- return null;- }+ if (!mainResult.allowed) {+ activitypub.helpers.log(`[activitypub/notes.assert] Not asserting ${mainPid}, domain is blocked.`);+ return null;+ }- if (mainResult.severity === 3) {- activitypub.helpers.log(`[activitypub/notes.assert] Queuing main post (${mainPid}) due to blocklist severity 3`);- await posts.addToQueue({- uid: authorId,- cid: options.cid || cid,- pid: mainPid,- title,- timestamp,- content: mainPost.content,- sourceContent: mainPost.sourceContent,- generatedTitle,- _activitypub: mainPost._activitypub,- });- // Drop the rest of the chain — replies without OP don't make sense- return { tid: null, queued: 1 };- }+ if (mainResult.severity === 3) {+ activitypub.helpers.log(`[activitypub/notes.assert] Queuing main post (${mainPid}) due to blocklist severity 3`);+ await posts.addToQueue({+ uid: authorId,+ cid: options.cid || cid,+ pid: mainPid,+ title,+ timestamp,+ content: mainPost.content,+ sourceContent: mainPost.sourceContent,+ generatedTitle,+ _activitypub: mainPost._activitypub,+ });+ // Drop the rest of the chain — replies without OP don't make sense+ return { tid: null, queued: 1 };}const { to, cc } = mainPost._activitypub;@@ -335,29 +333,27 @@ Notes.assert = async (uid, input, options = { skipChecks: false }) => {const { to, cc } = post._activitypub;try {- if (!options.skipChecks) {- const { hostname: postHostname } = new URL(post.pid);- const postResult = await activitypub.instances.isAllowed(postHostname);+ const { hostname: postHostname } = new URL(post.pid);+ const postResult = await activitypub.instances.isAllowed(postHostname);- if (!postResult.allowed) {- activitypub.helpers.log(`[activitypub/notes.assert] Not asserting ${post.pid}, domain is blocked.`);- return;- }+ if (!postResult.allowed) {+ activitypub.helpers.log(`[activitypub/notes.assert] Not asserting ${post.pid}, domain is blocked.`);+ return;+ }- if (postResult.severity === 3) {- activitypub.helpers.log(`[activitypub/notes.assert] Queuing reply (${post.pid}) due to blocklist severity 3`);- await posts.addToQueue({- uid: post.uid,- tid,- pid: post.pid,- content: post.content,- sourceContent: post.sourceContent,- timestamp: post.timestamp,- _activitypub: post._activitypub,- });- queued += 1;- return;- }+ if (postResult.severity === 3) {+ activitypub.helpers.log(`[activitypub/notes.assert] Queuing reply (${post.pid}) due to blocklist severity 3`);+ await posts.addToQueue({+ uid: post.uid,+ tid,+ pid: post.pid,+ content: post.content,+ sourceContent: post.sourceContent,+ timestamp: post.timestamp,+ _activitypub: post._activitypub,+ });+ queued += 1;+ return;}const postData = await topics.reply(post);diff --git a/test/activitypub/helpers.js b/test/activitypub/helpers.jsindex bae6e1aac0..25def3ab12 100644--- a/test/activitypub/helpers.js+++ b/test/activitypub/helpers.js@@ -76,9 +76,9 @@ Helpers.mocks.note = (override = {}) => {url: id,type: 'Note',to: ['https://www.w3.org/ns/activitystreams#Public'],- cc: ['https://example.org/user/foobar/followers'],+ cc: [`${Helpers.mocks._baseUrl}/user/foobar/followers`],inReplyTo: null,- attributedTo: 'https://example.org/user/foobar',+ attributedTo: `${Helpers.mocks._baseUrl}/user/foobar`,name: utils.generateUUID(),content: `<p>${utils.generateUUID()}</p>`,published: new Date().toISOString(),diff --git a/test/activitypub/notes.js b/test/activitypub/notes.jsindex f5ac916f35..7cb32ce916 100644--- a/test/activitypub/notes.js+++ b/test/activitypub/notes.js@@ -442,7 +442,7 @@ describe('Notes', () => {activitypub.instances.isAllowed = async (hostname) => {if (hostname === domain) {return {- allowed: false,+ allowed: severity > 2,severity,listUrl: 'https://example.org/blocklist.csv',};@@ -466,12 +466,18 @@ describe('Notes', () => {});describe('!hasTid — new topic', () => {- beforeEach(() => {+ beforeEach(function () {mockBlockedDomain('blocked.example.org', 3);+ this._baseUrl = helpers.mocks._baseUrl;+ helpers.mocks._baseUrl = 'https://blocked.example.org';+ });++ afterEach(function () {+ helpers.mocks._baseUrl = this._baseUrl;});it('should queue the main post instead of creating it', async () => {- const noteId = `https://blocked.example.org/object/${utils.generateUUID()}`;+ const { id: noteId } = helpers.mocks.note();const assertion = await activitypub.notes.assert(0, noteId, {skipChecks: true,});@@ -486,8 +492,10 @@ describe('Notes', () => {});it('should queue parent and also queue replies when hasTid is false', async () => {- const parentId = `https://blocked.example.org/object/${utils.generateUUID()}`;- const replyId = `https://blocked.example.org/object/${utils.generateUUID()}`;+ const { id: parentId } = helpers.mocks.note();+ const { id: replyId } = helpers.mocks.note({+ inReplyTo: parentId,+ });// Assert the parent with severity 3 — should queue and return tid: nullconst parentAssertion = await activitypub.notes.assert(0, parentId, {@@ -536,10 +544,18 @@ describe('Notes', () => {beforeEach(() => {mockBlockedDomain('blocked.example.org', 3);+ this._baseUrl = helpers.mocks._baseUrl;+ helpers.mocks._baseUrl = 'https://blocked.example.org';+ });++ afterEach(function () {+ helpers.mocks._baseUrl = this._baseUrl;});it('should queue replies when severity is 3', async () => {- const replyId = `https://blocked.example.org/object/${utils.generateUUID()}`;+ const { id: replyId } = helpers.mocks.note({+ inReplyTo: mainPid,+ });const assertion = await activitypub.notes.assert(0, replyId, {skipChecks: true,});@@ -553,8 +569,12 @@ describe('Notes', () => {});it('should queue multiple replies', async () => {- const id1 = `https://blocked.example.org/object/${utils.generateUUID()}`;- const id2 = `https://blocked.example.org/object/${utils.generateUUID()}`;+ const { id: id1 } = helpers.mocks.note({+ inReplyTo: mainPid,+ });+ const { id: id2 } = helpers.mocks.note({+ inReplyTo: mainPid,+ });const assertion1 = await activitypub.notes.assert(0, id1, {skipChecks: true,@@ -576,8 +596,17 @@ describe('Notes', () => {});describe('null case', () => {+ beforeEach(() => {+ this._baseUrl = helpers.mocks._baseUrl;+ helpers.mocks._baseUrl = 'https://allowed.example.org';+ });++ afterEach(function () {+ helpers.mocks._baseUrl = this._baseUrl;+ });+it('should NOT queue posts when domain is not blocked', async () => {- const noteId = `https://allowed.example.org/object/${utils.generateUUID()}`;+ const { id: noteId } = helpers.mocks.note();const assertion = await activitypub.notes.assert(0, noteId, {skipChecks: true,});@@ -592,16 +621,23 @@ describe('Notes', () => {});describe('Severity 1 and 2', () => {+ beforeEach(() => {+ this._baseUrl = helpers.mocks._baseUrl;+ helpers.mocks._baseUrl = 'https://blocked.example.org';+ });++ afterEach(function () {+ helpers.mocks._baseUrl = this._baseUrl;+ });+it('should NOT queue posts with severity 1 (suspend)', async () => {mockBlockedDomain('blocked.example.org', 1);- const noteId = `https://blocked.example.org/object/${utils.generateUUID()}`;+ const { id: noteId } = helpers.mocks.note();const assertion = await activitypub.notes.assert(0, noteId, {skipChecks: true,});- assert(assertion);- assert(assertion.tid);- assert.strictEqual(assertion.queued, 0);+ assert(!assertion);const queueCount = await db.sortedSetCard('post:queue');assert.strictEqual(queueCount, 0);@@ -609,14 +645,12 @@ describe('Notes', () => {it('should NOT queue posts with severity 2 (silence)', async () => {mockBlockedDomain('blocked.example.org', 2);- const noteId = `https://blocked.example.org/object/${utils.generateUUID()}`;+ const { id: noteId } = helpers.mocks.note();const assertion = await activitypub.notes.assert(0, noteId, {skipChecks: true,});- assert(assertion);- assert(assertion.tid);- assert.strictEqual(assertion.queued, 0);+ assert(!assertion);const queueCount = await db.sortedSetCard('post:queue');assert.strictEqual(queueCount, 0);