diff --git a/src/controllers/mods.js b/src/controllers/mods.js index f5a6e9b06d..7ebb70d888 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -25,7 +25,11 @@ modsController.flags.list = function (req, res, next) { res.locals.cids = results.moderatedCids; } - flags.list({}, function(err, flags) { + flags.list({}, function (err, flags) { + if (err) { + return next(err); + } + res.render('flags/list', { flags: flags }); diff --git a/src/flags.js b/src/flags.js index 8734f811a8..d9f6aa136f 100644 --- a/src/flags.js +++ b/src/flags.js @@ -64,7 +64,7 @@ Flags.list = function (filters, callback) { 'icon:text': userObj['icon:text'] } })); - }) + }); } ], function (err, flagObj) { if (err) { @@ -110,6 +110,10 @@ Flags.create = function (type, id, uid, reason, callback) { async.apply(Flags.exists, type, id, uid), async.apply(Flags.targetExists, type, id) ], function (err, checks) { + if (err) { + return next(err); + } + if (checks[0]) { return next(new Error('[[error:already-flagged]]')); } else if (!checks[1]) { @@ -132,15 +136,16 @@ Flags.create = function (type, id, uid, reason, callback) { })), async.apply(db.sortedSetAdd.bind(db), 'flags:datetime', Date.now(), flagId), async.apply(db.setObjectField.bind(db), 'flagHash:flagId', [type, id, uid].join(':'), flagId) - ], next); - } - ], function (err) { - if (err) { - return callback(err); - } + ], function (err, data) { + if (err) { + return next(err); + } - callback(); - }); + next(null, flagId); + }); + }, + async.apply(Flags.get) + ], callback); // if (!parseInt(uid, 10) || !reason) { // return callback(); // } diff --git a/test/flags.js b/test/flags.js new file mode 100644 index 0000000000..ed879fba49 --- /dev/null +++ b/test/flags.js @@ -0,0 +1,107 @@ +'use strict'; +/*globals require, before, after, describe, it*/ + +var assert = require('assert'); +var async = require('async'); + +var db = require('./mocks/databasemock'); +var Flags = require('../src/flags'); +var Categories = require('../src/categories'); +var Topics = require('../src/topics'); +var User = require('../src/user'); + +describe('Flags', function () { + before(function (done) { + // Create some stuff to flag + async.waterfall([ + async.apply(User.create, {username: 'testUser', password: 'abcdef', email: 'b@c.com'}), + function (uid, next) { + Categories.create({ + name: 'test category' + }, function (err, category) { + if (err) { + return done(err); + } + + Topics.post({ + cid: category.cid, + uid: uid, + title: 'Topic to flag', + content: 'This is flaggable content' + }, next); + }); + } + ], done); + }); + + describe('.create()', function () { + it('should create a flag and return its data', function (done) { + Flags.create('post', 1, 1, 'Test flag', function (err, flagData) { + assert.ifError(err); + var compare = { + flagId: 1, + uid: 1, + targetId: 1, + type: 'post', + description: 'Test flag', + state: 'open' + }; + + for(var key in compare) { + if (compare.hasOwnProperty(key)) { + assert.ok(flagData[key]); + assert.strictEqual(flagData[key], compare[key]); + } + } + + done(); + }); + }); + }); + + describe('.get()', function () { + it('should retrieve and display a flag\'s data', function (done) { + Flags.get(1, function (err, flagData) { + assert.ifError(err); + var compare = { + flagId: 1, + uid: 1, + targetId: 1, + type: 'post', + description: 'Test flag', + state: 'open' + }; + + for(var key in compare) { + if (compare.hasOwnProperty(key)) { + assert.ok(flagData[key]); + assert.strictEqual(flagData[key], compare[key]); + } + } + + done(); + }); + }); + }); + + describe('.list()', function () { + it('should show a list of flags (with one item)', function (done) { + Flags.list({}, function (err, flags) { + assert.ifError(err); + assert.ok(Array.isArray(flags)); + assert.equal(flags.length, 1); + + Flags.get(flags[0].flagId, function (err, flagData) { + assert.ifError(err); + assert.equal(flags[0].flagId, flagData.flagId); + assert.equal(flags[0].description, flagData.description); + done(); + }); + }); + }); + }); + + after(function (done) { + db.emptydb(done); + }); +});