From 7a8de6169b6ed2c715068c2c1e93cfaac0078c74 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 31 Oct 2016 13:52:26 +0300 Subject: [PATCH] admin controller tests --- src/controllers/admin/groups.js | 12 +-- src/controllers/admin/settings.js | 66 ++++++++-------- test/controllers-admin.js | 121 ++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 38 deletions(-) create mode 100644 test/controllers-admin.js diff --git a/src/controllers/admin/groups.js b/src/controllers/admin/groups.js index a424a5c070..38563d96a5 100644 --- a/src/controllers/admin/groups.js +++ b/src/controllers/admin/groups.js @@ -1,12 +1,12 @@ "use strict"; -var async = require('async'), +var async = require('async'); - db = require('../../database'), - groups = require('../../groups'), - meta = require('../../meta'), - pagination = require('../../pagination'), - helpers = require('../helpers'); +var db = require('../../database'); +var groups = require('../../groups'); +var meta = require('../../meta'); +var pagination = require('../../pagination'); +var helpers = require('../helpers'); var groupsController = {}; diff --git a/src/controllers/admin/settings.js b/src/controllers/admin/settings.js index a6afb80cfc..639267f5f5 100644 --- a/src/controllers/admin/settings.js +++ b/src/controllers/admin/settings.js @@ -1,8 +1,10 @@ 'use strict'; -var settingsController = {}; -var async = require('async'), - meta = require('../../meta'); + +var async = require('async'); +var meta = require('../../meta'); + +var settingsController = module.exports; settingsController.get = function (req, res, next) { var term = req.params.term ? req.params.term : 'general'; @@ -24,41 +26,41 @@ function renderEmail(req, res, next) { var utils = require('../../../public/src/utils'); var emailsPath = path.join(__dirname, '../../../public/templates/emails'); - utils.walk(emailsPath, function (err, emails) { - if (err) { - return next(err); - } - async.map(emails, function (email, next) { - var path = email.replace(emailsPath, '').substr(1).replace('.tpl', ''); + async.waterfall([ + function (next) { + utils.walk(emailsPath, next); + }, + function (emails, next) { + async.map(emails, function (email, next) { + var path = email.replace(emailsPath, '').substr(1).replace('.tpl', ''); - fs.readFile(email, function (err, original) { - if (err) { - return next(err); - } + fs.readFile(email, function (err, original) { + if (err) { + return next(err); + } - var text = meta.config['email:custom:' + path] ? meta.config['email:custom:' + path] : original.toString(); + var text = meta.config['email:custom:' + path] ? meta.config['email:custom:' + path] : original.toString(); - next(null, { - path: path, - fullpath: email, - text: text, - original: original.toString() + next(null, { + path: path, + fullpath: email, + text: text, + original: original.toString() + }); }); - }); - }, function (err, emails) { - if (err) { - return next(err); - } + }, next); + } + ], function (err, emails) { + if (err) { + return next(err); + } - res.render('admin/settings/email', { - emails: emails, - sendable: emails.filter(function (email) { - return email.path.indexOf('_plaintext') === -1 && email.path.indexOf('partials') === -1; - }) - }); + res.render('admin/settings/email', { + emails: emails, + sendable: emails.filter(function (email) { + return email.path.indexOf('_plaintext') === -1 && email.path.indexOf('partials') === -1; + }) }); }); } - -module.exports = settingsController; \ No newline at end of file diff --git a/test/controllers-admin.js b/test/controllers-admin.js new file mode 100644 index 0000000000..9571a1c917 --- /dev/null +++ b/test/controllers-admin.js @@ -0,0 +1,121 @@ +'use strict'; + +var async = require('async'); +var assert = require('assert'); +var nconf = require('nconf'); +var request = require('request'); + +var db = require('./mocks/databasemock'); +var categories = require('../src/categories'); +var topics = require('../src/topics'); +var user = require('../src/user'); +var groups = require('../src/groups'); +var helpers = require('./helpers'); + +describe('Admin Controllers', function () { + + var tid; + var cid; + var pid; + var adminUid; + var jar; + + before(function (done) { + async.series({ + category: function (next) { + categories.create({ + name: 'Test Category', + 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); + } + }, function (err, results) { + if (err) { + return done(err); + } + cid = results.category.cid; + + topics.post({uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid}, function (err, result) { + tid = result.topicData.tid; + pid = result.postData.pid; + done(err); + }); + }); + }); + + it('should 403 if user is not admin', function (done) { + helpers.loginUser('admin', 'barbar', function (err, _jar) { + assert.ifError(err); + jar = _jar; + request(nconf.get('url') + '/admin', {jar: jar}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 403); + assert(body); + done(); + }); + }); + }); + + it('should load admin dashboard', function (done) { + groups.join('administrators', adminUid, function (err) { + assert.ifError(err); + request(nconf.get('url') + '/admin', {jar: jar}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + }); + + it('should load groups page', function (done) { + request(nconf.get('url') + '/admin/manage/groups', {jar: jar}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should load groups detail page', function (done) { + request(nconf.get('url') + '/admin/manage/groups/administrators', {jar: jar}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + + it('should load general settings page', function (done) { + request(nconf.get('url') + '/admin/settings', {jar: jar}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should load email settings page', function (done) { + request(nconf.get('url') + '/admin/settings/email', {jar: jar}, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + after(function (done) { + db.emptydb(done); + }); +});