From a4dfe608e959b525a6973d5720a36b315623fba0 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 1 Nov 2016 13:02:12 +0300 Subject: [PATCH] ban tests, user/info test --- src/user/bans.js | 2 +- test/controllers-admin.js | 31 +++++++++++++++++++++---------- test/socket.io.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/user/bans.js b/src/user/bans.js index 574f9d587b..a0b81587ee 100644 --- a/src/user/bans.js +++ b/src/user/bans.js @@ -97,7 +97,7 @@ module.exports = function (User) { User.getBannedReason = function (uid, callback) { // Grabs the latest ban reason - db.getSortedSetRevRange('banned:' + uid + ':reasons', 0, 1, function (err, reasons) { + db.getSortedSetRevRange('banned:' + uid + ':reasons', 0, 0, function (err, reasons) { if (err) { return callback(err); } diff --git a/test/controllers-admin.js b/test/controllers-admin.js index 9571a1c917..3a8d151282 100644 --- a/test/controllers-admin.js +++ b/test/controllers-admin.js @@ -18,6 +18,7 @@ describe('Admin Controllers', function () { var cid; var pid; var adminUid; + var regularUid; var jar; before(function (done) { @@ -28,21 +29,18 @@ describe('Admin Controllers', function () { description: 'Test category created by testing script' }, next); }, - user: function (next) { - async.waterfall([ - function (next) { - user.create({username: 'admin', password: 'barbar'}, next); - }, - function (_uid, next) { - adminUid = _uid; - next(); - } - ], next); + adminUid: function (next) { + user.create({username: 'admin', password: 'barbar'}, next); + }, + regularUid: function (next) { + user.create({username: 'regular'}, next); } }, function (err, results) { if (err) { return done(err); } + adminUid = results.adminUid; + regularUid = results.regularUid; cid = results.category.cid; topics.post({uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid}, function (err, result) { @@ -115,6 +113,19 @@ describe('Admin Controllers', function () { }); }); + it('should load info page for a user', function (done) { + request(nconf.get('url') + '/api/user/regular/info', {jar: jar, json: true}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body.history); + assert(Array.isArray(body.history.flags)); + assert(Array.isArray(body.history.bans)); + assert(Array.isArray(body.history.reasons)); + assert(Array.isArray(body.sessions)); + done(); + }); + }); + after(function (done) { db.emptydb(done); }); diff --git a/test/socket.io.js b/test/socket.io.js index a5f4c08100..95fc8d911b 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -18,12 +18,14 @@ var user = require('../src/user'); var groups = require('../src/groups'); var categories = require('../src/categories'); + describe('socket.io', function () { var io; var cid; var tid; var adminUid; + var regularUid; before(function (done) { async.series([ @@ -38,6 +40,7 @@ describe('socket.io', function () { return done(err); } adminUid = data[0]; + regularUid = data[1]; cid = data[2].cid; groups.join('administrators', data[0], done); @@ -132,6 +135,42 @@ describe('socket.io', function () { }); }); + it('should ban a user', function (done) { + var socketUser = require('../src/socket.io/user'); + socketUser.banUsers({uid: adminUid}, {uids: [regularUid], reason: 'spammer'}, function (err) { + assert.ifError(err); + user.getLatestBanInfo(regularUid, function (err, data) { + assert.ifError(err); + assert(data.uid); + assert(data.timestamp); + assert(data.hasOwnProperty('expiry')); + assert(data.hasOwnProperty('expiry_readable')); + assert.equal(data.reason, 'spammer'); + done(); + }); + }); + }); + + it('should return ban reason', function (done) { + user.getBannedReason(regularUid, function (err, reason) { + assert.ifError(err); + assert.equal(reason, 'spammer'); + done(); + }); + }); + + it('should unban a user', function (done) { + var socketUser = require('../src/socket.io/user'); + socketUser.unbanUsers({uid: adminUid}, [regularUid], function (err) { + assert.ifError(err); + user.isBanned(regularUid, function (err, isBanned) { + assert.ifError(err); + assert(!isBanned); + done(); + }); + }); + }); + after(function (done) { db.emptydb(done); });