Skip to content

Node.js based forum software built for the modern web

flag controller tests

Barış Soner Uşaklıbf78786a686c
diff --git a/src/controllers/mods.js b/src/controllers/mods.js
index 6365650323..32e416a20d 100644
--- a/src/controllers/mods.js
+++ b/src/controllers/mods.js
@@ -7,61 +7,62 @@ var categories = require('../categories');
var flags = require('../flags');
var analytics = require('../analytics');
-var modsController = {
- flags: {},
-};
+var modsController = module.exports;
+modsController.flags = {};
modsController.flags.list = function (req, res, next) {
- async.parallel({
- isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid),
- moderatedCids: async.apply(user.getModeratedCids, req.uid),
- }, function (err, results) {
- if (err) {
- return next(err);
- } else if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) {
- return next(new Error('[[error:no-privileges]]'));
- }
-
- if (!results.isAdminOrGlobalMod && results.moderatedCids.length) {
- res.locals.cids = results.moderatedCids;
- }
-
- // Parse query string params for filters
- var hasFilter = false;
- var valid = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick'];
- var filters = valid.reduce(function (memo, cur) {
- if (req.query.hasOwnProperty(cur)) {
- memo[cur] = req.query[cur];
+ var filters;
+ var hasFilter;
+ async.waterfall([
+ function (next) {
+ async.parallel({
+ isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid),
+ moderatedCids: async.apply(user.getModeratedCids, req.uid),
+ }, next);
+ },
+ function (results, next) {
+ if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) {
+ return next(new Error('[[error:no-privileges]]'));
}
- return memo;
- }, {});
- hasFilter = !!Object.keys(filters).length;
-
- if (res.locals.cids) {
- if (!filters.cid) {
- // If mod and no cid filter, add filter for their modded categories
- filters.cid = res.locals.cids;
- } else if (Array.isArray(filters.cid)) {
- // Remove cids they do not moderate
- filters.cid = filters.cid.filter(function (cid) {
- return res.locals.cids.indexOf(String(cid)) !== -1;
- });
- } else if (res.locals.cids.indexOf(String(filters.cid)) === -1) {
- filters.cid = res.locals.cids;
- hasFilter = false;
+ if (!results.isAdminOrGlobalMod && results.moderatedCids.length) {
+ res.locals.cids = results.moderatedCids;
}
- }
- async.parallel({
- flags: async.apply(flags.list, filters, req.uid),
- analytics: async.apply(analytics.getDailyStatsForSet, 'analytics:flags', Date.now(), 30),
- categories: async.apply(categories.buildForSelect, req.uid),
- }, function (err, data) {
- if (err) {
- return next(err);
+ // Parse query string params for filters
+ hasFilter = false;
+ var valid = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick'];
+ filters = valid.reduce(function (memo, cur) {
+ if (req.query.hasOwnProperty(cur)) {
+ memo[cur] = req.query[cur];
+ }
+
+ return memo;
+ }, {});
+ hasFilter = !!Object.keys(filters).length;
+
+ if (res.locals.cids) {
+ if (!filters.cid) {
+ // If mod and no cid filter, add filter for their modded categories
+ filters.cid = res.locals.cids;
+ } else if (Array.isArray(filters.cid)) {
+ // Remove cids they do not moderate
+ filters.cid = filters.cid.filter(function (cid) {
+ return res.locals.cids.indexOf(String(cid)) !== -1;
+ });
+ } else if (res.locals.cids.indexOf(String(filters.cid)) === -1) {
+ filters.cid = res.locals.cids;
+ hasFilter = false;
+ }
}
+ async.parallel({
+ flags: async.apply(flags.list, filters, req.uid),
+ analytics: async.apply(analytics.getDailyStatsForSet, 'analytics:flags', Date.now(), 30),
+ categories: async.apply(categories.buildForSelect, req.uid),
+ }, next);
+ },
+ function (data) {
// If res.locals.cids is populated, then slim down the categories list
if (res.locals.cids) {
data.categories = data.categories.filter(function (category) {
@@ -92,8 +93,8 @@ modsController.flags.list = function (req, res, next) {
filters: filters,
title: '[[pages:flags]]',
});
- });
- });
+ },
+ ], next);
};
modsController.flags.detail = function (req, res, next) {
@@ -124,5 +125,3 @@ modsController.flags.detail = function (req, res, next) {
}));
});
};
-
-module.exports = modsController;
diff --git a/src/socket.io/flags.js b/src/socket.io/flags.js
index 2cc2881832..f2952b1671 100644
--- a/src/socket.io/flags.js
+++ b/src/socket.io/flags.js
@@ -5,7 +5,7 @@ var async = require('async');
var user = require('../user');
var flags = require('../flags');
-var SocketFlags = {};
+var SocketFlags = module.exports;
SocketFlags.create = function (socket, data, callback) {
if (!socket.uid) {
@@ -98,5 +98,3 @@ SocketFlags.appendNote = function (socket, data, callback) {
},
], callback);
};
-
-module.exports = SocketFlags;
diff --git a/test/controllers-admin.js b/test/controllers-admin.js
index 5a17ca96a9..086d54e7a8 100644
--- a/test/controllers-admin.js
+++ b/test/controllers-admin.js
@@ -18,6 +18,7 @@ describe('Admin Controllers', function () {
var pid;
var adminUid;
var regularUid;
+ var moderatorUid;
var jar;
before(function (done) {
@@ -35,12 +36,16 @@ describe('Admin Controllers', function () {
regularUid: function (next) {
user.create({ username: 'regular' }, next);
},
+ moderatorUid: function (next) {
+ user.create({ username: 'moderator', password: 'modmod' }, next);
+ },
}, function (err, results) {
if (err) {
return done(err);
}
adminUid = results.adminUid;
regularUid = results.regularUid;
+ moderatorUid = results.moderatorUid;
cid = results.category.cid;
topics.post({ uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, function (err, result) {
@@ -128,7 +133,7 @@ describe('Admin Controllers', function () {
});
it('should 404 for edit/email page if user does not exist', function (done) {
- request(nconf.get('url') + '/api/user/doesnotexist/edit/email', { jar: jar, json: true }, function (err, res, body) {
+ request(nconf.get('url') + '/api/user/doesnotexist/edit/email', { jar: jar, json: true }, function (err, res) {
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
@@ -446,6 +451,74 @@ describe('Admin Controllers', function () {
});
});
+ describe('mods page', function () {
+ var moderatorJar;
+
+ before(function (done) {
+ helpers.loginUser('moderator', 'modmod', function (err, _jar) {
+ assert.ifError(err);
+ moderatorJar = _jar;
+
+ groups.join('cid:' + cid + ':privileges:mods', moderatorUid, done);
+ });
+ });
+
+ it('should error with no privileges', function (done) {
+ request(nconf.get('url') + '/api/flags', { json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(body.error, '[[error:no-privileges]]');
+ done();
+ });
+ });
+
+ it('should load flags page data', function (done) {
+ request(nconf.get('url') + '/api/flags', { jar: moderatorJar, json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert(body);
+ assert(body.flags);
+ assert(body.categories);
+ assert(body.filters);
+ assert.equal(body.categories[cid], 'Test Category');
+ assert.equal(body.filters.cid.indexOf(cid), -1);
+ done();
+ });
+ });
+
+ it('should return invalid data if flag does not exist', function (done) {
+ request(nconf.get('url') + '/api/flags/123123123', { jar: moderatorJar, json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(body.error, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
+ it('should error with not enough reputation to flag', function (done) {
+ var socketFlags = require('../src/socket.io/flags');
+
+ socketFlags.create({ uid: regularUid }, { id: pid, type: 'post', reason: 'spam' }, function (err) {
+ assert.equal(err.message, '[[error:not-enough-reputation-to-flag]]');
+ done();
+ });
+ });
+
+ it('should return flag details', function (done) {
+ var meta = require('../src/meta');
+ var socketFlags = require('../src/socket.io/flags');
+ var oldValue = meta.config['privileges:flag'];
+ meta.config['privileges:flag'] = 0;
+ socketFlags.create({ uid: regularUid }, { id: pid, type: 'post', reason: 'spam' }, function (err, data) {
+ meta.config['privileges:flag'] = oldValue;
+ assert.ifError(err);
+ request(nconf.get('url') + '/api/flags/' + data.flagId, { jar: moderatorJar, json: true }, function (err, res, body) {
+ assert.ifError(err);
+ assert(body);
+ assert.equal(body.reporter.username, 'regular');
+ done();
+ });
+ });
+ });
+ });
+
after(function (done) {
db.emptydb(done);
});
diff --git a/test/flags.js b/test/flags.js
index 0426541cd8..5d45b8afef 100644
--- a/test/flags.js
+++ b/test/flags.js
@@ -16,6 +16,7 @@ var Meta = require('../src/meta');
describe('Flags', function () {
before(function (done) {
+ Groups.resetCache();
// Create some stuff to flag
async.waterfall([
async.apply(User.create, { username: 'testUser', password: 'abcdef', email: '[email protected]' }),