Skip to content

Commit

Permalink
test: added test cases for .addReport and .rescindReport()
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Oct 12, 2023
1 parent bc0f362 commit da23905
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/flags.js
Expand Up @@ -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],
Expand Down
77 changes: 77 additions & 0 deletions test/flags.js
Expand Up @@ -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');

Expand All @@ -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' });

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit da23905

Please sign in to comment.