From da2390594ae4687d8902ebfc8ba8c883239cced3 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 11 Oct 2023 11:23:55 -0400 Subject: [PATCH] test: added test cases for .addReport and .rescindReport() --- src/flags.js | 1 + test/flags.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/flags.js b/src/flags.js index 8cd845f107..fbbf103e5a 100644 --- a/src/flags.js +++ b/src/flags.js @@ -545,6 +545,7 @@ Flags.getReports = async function (flagId) { return reports; }; +// Not meant to be called directly, call Flags.create() instead. Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) { await db.sortedSetAddBulk([ [`flags:byReporter:${uid}`, timestamp, flagId], diff --git a/test/flags.js b/test/flags.js index 2912346519..cda71a111f 100644 --- a/test/flags.js +++ b/test/flags.js @@ -19,6 +19,7 @@ const User = require('../src/user'); const Groups = require('../src/groups'); const Meta = require('../src/meta'); const Privileges = require('../src/privileges'); +const plugins = require('../src/plugins'); const utils = require('../src/utils'); const api = require('../src/api'); @@ -31,6 +32,13 @@ describe('Flags', () => { let csrfToken; let category; before(async () => { + const dummyEmailerHook = async (data) => {}; + // Attach an emailer hook so related requests do not error + plugins.hooks.register('flags-test', { + hook: 'filter:email.send', + method: dummyEmailerHook, + }); + // Create some stuff to flag uid1 = await User.create({ username: 'testUser', password: 'abcdef', email: 'b@c.com' }); @@ -61,6 +69,10 @@ describe('Flags', () => { csrfToken = login.csrf_token; }); + after(() => { + plugins.hooks.unregister('flags-test', 'filter:email.send'); + }); + describe('.create()', () => { it('should create a flag and return its data', (done) => { Flags.create('post', 1, 1, 'Test flag', (err, flagData) => { @@ -99,6 +111,71 @@ describe('Flags', () => { }); }); + describe('.addReport()', () => { + let flagId; + let postData; + + before(async () => { + // Create a topic and flag it + ({ postData } = await Topics.post({ + cid: category.cid, + uid: uid1, + title: utils.generateUUID(), + content: utils.generateUUID(), + })); + ({ flagId } = await Flags.create('post', postData.pid, adminUid, utils.generateUUID())); + }); + + it('should add a report to an existing flag', async () => { + await Flags.addReport(flagId, 'post', postData.pid, uid3, utils.generateUUID(), Date.now()); + + const reports = await db.getSortedSetMembers(`flag:${flagId}:reports`); + assert.strictEqual(reports.length, 2); + }); + + it('should add an additional report even if same user calls it again', async () => { + // This isn't exposed to the end user, but is possible via direct method call + await Flags.addReport(flagId, 'post', postData.pid, uid3, utils.generateUUID(), Date.now()); + + const reports = await db.getSortedSetMembers(`flag:${flagId}:reports`); + assert.strictEqual(reports.length, 3); + }); + }); + + describe('.rescindReport()', () => { + let flagId; + let postData; + + before(async () => { + // Create a topic and flag it + ({ postData } = await Topics.post({ + cid: category.cid, + uid: uid1, + title: utils.generateUUID(), + content: utils.generateUUID(), + })); + ({ flagId } = await Flags.create('post', postData.pid, adminUid, utils.generateUUID())); + }); + + it('should remove a report from an existing flag', async () => { + await Flags.create('post', postData.pid, uid3, utils.generateUUID()); + await Flags.rescindReport('post', postData.pid, uid3); + const reports = await Flags.getReports(flagId); + + assert.strictEqual(reports.length, 1); + assert(reports.every(({ reporter }) => reporter.uid !== uid3)); + }); + + it('should automatically mark the flag resolved if there are no reports remaining after removal', async () => { + await Flags.rescindReport('post', postData.pid, adminUid); + const reports = await Flags.getReports(flagId); + const { state } = await Flags.get(flagId); + + assert.strictEqual(reports.length, 0); + assert.strictEqual(state, 'resolved'); + }); + }); + describe('.exists()', () => { it('should return Boolean True if a flag matching the flag hash already exists', (done) => { Flags.exists('post', 1, 1, (err, exists) => {