flag controller tests

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 5a9b7b673a
commit bf78786a68

@ -7,18 +7,21 @@ var categories = require('../categories');
var flags = require('../flags'); var flags = require('../flags');
var analytics = require('../analytics'); var analytics = require('../analytics');
var modsController = { var modsController = module.exports;
flags: {}, modsController.flags = {};
};
modsController.flags.list = function (req, res, next) { modsController.flags.list = function (req, res, next) {
var filters;
var hasFilter;
async.waterfall([
function (next) {
async.parallel({ async.parallel({
isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid), isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid),
moderatedCids: async.apply(user.getModeratedCids, req.uid), moderatedCids: async.apply(user.getModeratedCids, req.uid),
}, function (err, results) { }, next);
if (err) { },
return next(err); function (results, next) {
} else if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) { if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) {
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
@ -27,9 +30,9 @@ modsController.flags.list = function (req, res, next) {
} }
// Parse query string params for filters // Parse query string params for filters
var hasFilter = false; hasFilter = false;
var valid = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick']; var valid = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick'];
var filters = valid.reduce(function (memo, cur) { filters = valid.reduce(function (memo, cur) {
if (req.query.hasOwnProperty(cur)) { if (req.query.hasOwnProperty(cur)) {
memo[cur] = req.query[cur]; memo[cur] = req.query[cur];
} }
@ -57,11 +60,9 @@ modsController.flags.list = function (req, res, next) {
flags: async.apply(flags.list, filters, req.uid), flags: async.apply(flags.list, filters, req.uid),
analytics: async.apply(analytics.getDailyStatsForSet, 'analytics:flags', Date.now(), 30), analytics: async.apply(analytics.getDailyStatsForSet, 'analytics:flags', Date.now(), 30),
categories: async.apply(categories.buildForSelect, req.uid), categories: async.apply(categories.buildForSelect, req.uid),
}, function (err, data) { }, next);
if (err) { },
return next(err); function (data) {
}
// If res.locals.cids is populated, then slim down the categories list // If res.locals.cids is populated, then slim down the categories list
if (res.locals.cids) { if (res.locals.cids) {
data.categories = data.categories.filter(function (category) { data.categories = data.categories.filter(function (category) {
@ -92,8 +93,8 @@ modsController.flags.list = function (req, res, next) {
filters: filters, filters: filters,
title: '[[pages:flags]]', title: '[[pages:flags]]',
}); });
}); },
}); ], next);
}; };
modsController.flags.detail = function (req, res, next) { modsController.flags.detail = function (req, res, next) {
@ -124,5 +125,3 @@ modsController.flags.detail = function (req, res, next) {
})); }));
}); });
}; };
module.exports = modsController;

@ -5,7 +5,7 @@ var async = require('async');
var user = require('../user'); var user = require('../user');
var flags = require('../flags'); var flags = require('../flags');
var SocketFlags = {}; var SocketFlags = module.exports;
SocketFlags.create = function (socket, data, callback) { SocketFlags.create = function (socket, data, callback) {
if (!socket.uid) { if (!socket.uid) {
@ -98,5 +98,3 @@ SocketFlags.appendNote = function (socket, data, callback) {
}, },
], callback); ], callback);
}; };
module.exports = SocketFlags;

@ -18,6 +18,7 @@ describe('Admin Controllers', function () {
var pid; var pid;
var adminUid; var adminUid;
var regularUid; var regularUid;
var moderatorUid;
var jar; var jar;
before(function (done) { before(function (done) {
@ -35,12 +36,16 @@ describe('Admin Controllers', function () {
regularUid: function (next) { regularUid: function (next) {
user.create({ username: 'regular' }, next); user.create({ username: 'regular' }, next);
}, },
moderatorUid: function (next) {
user.create({ username: 'moderator', password: 'modmod' }, next);
},
}, function (err, results) { }, function (err, results) {
if (err) { if (err) {
return done(err); return done(err);
} }
adminUid = results.adminUid; adminUid = results.adminUid;
regularUid = results.regularUid; regularUid = results.regularUid;
moderatorUid = results.moderatorUid;
cid = results.category.cid; cid = results.category.cid;
topics.post({ uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, function (err, result) { 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) { 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.ifError(err);
assert.equal(res.statusCode, 404); assert.equal(res.statusCode, 404);
done(); 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) { after(function (done) {
db.emptydb(done); db.emptydb(done);
}); });

@ -16,6 +16,7 @@ var Meta = require('../src/meta');
describe('Flags', function () { describe('Flags', function () {
before(function (done) { before(function (done) {
Groups.resetCache();
// Create some stuff to flag // Create some stuff to flag
async.waterfall([ async.waterfall([
async.apply(User.create, { username: 'testUser', password: 'abcdef', email: 'b@c.com' }), async.apply(User.create, { username: 'testUser', password: 'abcdef', email: 'b@c.com' }),

Loading…
Cancel
Save