test: added test cases for .addReport and .rescindReport()

This commit is contained in:
Julian Lam
2023-10-11 11:23:55 -04:00
parent bc0f362cd7
commit da2390594a
2 changed files with 78 additions and 0 deletions

View File

@@ -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],

View File

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