From 8a0cf6c0ac61cdec0719a136f2f5c951677e13a7 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 28 Feb 2017 15:16:49 +0300 Subject: [PATCH] more admin socket tests --- src/meta/errors.js | 26 +++--- src/socket.io/admin.js | 99 +++++++++++---------- test/meta.js | 11 +++ test/socket.io.js | 194 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 253 insertions(+), 77 deletions(-) diff --git a/src/meta/errors.js b/src/meta/errors.js index 085cb84fea..fb169764ba 100644 --- a/src/meta/errors.js +++ b/src/meta/errors.js @@ -1,5 +1,6 @@ 'use strict'; +var async = require('async'); var validator = require('validator'); var db = require('../database'); @@ -16,18 +17,19 @@ module.exports = function (Meta) { }; Meta.errors.get = function (escape, callback) { - db.getSortedSetRevRangeWithScores('errors:404', 0, -1, function (err, data) { - if (err) { - return callback(err); - } - - data = data.map(function (nfObject) { - nfObject.value = escape ? validator.escape(String(nfObject.value || '')) : nfObject.value; - return nfObject; - }); - - callback(null, data); - }); + async.waterfall([ + function (next) { + db.getSortedSetRevRangeWithScores('errors:404', 0, -1, next); + }, + function (data, next) { + data = data.map(function (nfObject) { + nfObject.value = escape ? validator.escape(String(nfObject.value || '')) : nfObject.value; + return nfObject; + }); + + next(null, data); + }, + ], callback); }; Meta.errors.clear = function (callback) { diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 0e7c277dfd..9eba77f39b 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -93,17 +93,18 @@ SocketAdmin.themes.set = function (socket, data, callback) { return callback(new Error('[[error:invalid-data]]')); } - var wrappedCallback = function (err) { - if (err) { - return callback(err); - } - meta.themes.set(data, callback); - }; - if (data.type === 'bootswatch') { - wrappedCallback(); - } else { - widgets.reset(wrappedCallback); - } + async.waterfall([ + function (next) { + if (data.type === 'bootswatch') { + setImmediate(next); + } else { + widgets.reset(next); + } + }, + function (next) { + meta.themes.set(data, next); + }, + ], callback); }; SocketAdmin.plugins.toggleActive = function (socket, plugin_id, callback) { @@ -125,7 +126,7 @@ SocketAdmin.plugins.orderActivePlugins = function (socket, data, callback) { if (plugin && plugin.name) { db.sortedSetAdd('plugins:active', plugin.order || 0, plugin.name, next); } else { - next(); + setImmediate(next); } }, callback); }; @@ -148,7 +149,7 @@ SocketAdmin.config.set = function (socket, data, callback) { } var _data = {}; _data[data.key] = data.value; - SocketAdmin.config.setMultiple(socket, data, callback); + SocketAdmin.config.setMultiple(socket, _data, callback); }; SocketAdmin.config.setMultiple = function (socket, data, callback) { @@ -204,6 +205,10 @@ SocketAdmin.email.test = function (socket, data, callback) { }; SocketAdmin.analytics.get = function (socket, data, callback) { + if (!data || !data.graph || !data.units) { + return callback(new Error('[[error:invalid-data]]')); + } + // Default returns views from past 24 hours, by hour if (data.units === 'days') { data.amount = 30; @@ -211,34 +216,30 @@ SocketAdmin.analytics.get = function (socket, data, callback) { data.amount = 24; } - if (data && data.graph && data.units && data.amount) { - if (data.graph === 'traffic') { - async.parallel({ - uniqueVisitors: function (next) { - if (data.units === 'days') { - analytics.getDailyStatsForSet('analytics:uniquevisitors', data.until || Date.now(), data.amount, next); - } else { - analytics.getHourlyStatsForSet('analytics:uniquevisitors', data.until || Date.now(), data.amount, next); - } - }, - pageviews: function (next) { - if (data.units === 'days') { - analytics.getDailyStatsForSet('analytics:pageviews', data.until || Date.now(), data.amount, next); - } else { - analytics.getHourlyStatsForSet('analytics:pageviews', data.until || Date.now(), data.amount, next); - } - }, - monthlyPageViews: function (next) { - analytics.getMonthlyPageViews(next); - }, - }, function (err, data) { - data.pastDay = data.pageviews.reduce(function (a, b) { return parseInt(a, 10) + parseInt(b, 10); }); - data.pageviews[data.pageviews.length - 1] = parseInt(data.pageviews[data.pageviews.length - 1], 10) + analytics.getUnwrittenPageviews(); - callback(err, data); - }); - } - } else { - callback(new Error('Invalid analytics call')); + if (data.graph === 'traffic') { + async.parallel({ + uniqueVisitors: function (next) { + if (data.units === 'days') { + analytics.getDailyStatsForSet('analytics:uniquevisitors', data.until || Date.now(), data.amount, next); + } else { + analytics.getHourlyStatsForSet('analytics:uniquevisitors', data.until || Date.now(), data.amount, next); + } + }, + pageviews: function (next) { + if (data.units === 'days') { + analytics.getDailyStatsForSet('analytics:pageviews', data.until || Date.now(), data.amount, next); + } else { + analytics.getHourlyStatsForSet('analytics:pageviews', data.until || Date.now(), data.amount, next); + } + }, + monthlyPageViews: function (next) { + analytics.getMonthlyPageViews(next); + }, + }, function (err, data) { + data.pastDay = data.pageviews.reduce(function (a, b) { return parseInt(a, 10) + parseInt(b, 10); }); + data.pageviews[data.pageviews.length - 1] = parseInt(data.pageviews[data.pageviews.length - 1], 10) + analytics.getUnwrittenPageviews(); + callback(err, data); + }); } }; @@ -259,13 +260,15 @@ SocketAdmin.deleteAllEvents = function (socket, data, callback) { }; SocketAdmin.getSearchDict = function (socket, data, callback) { - user.getSettings(socket.uid, function (err, settings) { - if (err) { - return callback(err); - } - var lang = settings.userLang || meta.config.defaultLang || 'en-GB'; - getAdminSearchDict(lang, callback); - }); + async.waterfall([ + function (next) { + user.getSettings(socket.uid, next); + }, + function (settings, next) { + var lang = settings.userLang || meta.config.defaultLang || 'en-GB'; + getAdminSearchDict(lang, next); + }, + ], callback); }; SocketAdmin.deleteAllSessions = function (socket, data, callback) { diff --git a/test/meta.js b/test/meta.js index 9d1a69b00c..4544570b42 100644 --- a/test/meta.js +++ b/test/meta.js @@ -123,6 +123,17 @@ describe('meta', function () { }); }); + it('should set single config value', function (done) { + socketAdmin.config.set({ uid: fooUid }, { key: 'someKey', value: 'someValue' }, function (err) { + assert.ifError(err); + meta.configs.getFields(['someKey'], function (err, data) { + assert.ifError(err); + assert.equal(data.someKey, 'someValue'); + done(); + }); + }); + }); + it('should set config value', function (done) { meta.configs.set('someField', 'someValue', function (err) { assert.ifError(err); diff --git a/test/socket.io.js b/test/socket.io.js index 780fdb49aa..095c3b5440 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -16,6 +16,9 @@ var user = require('../src/user'); var groups = require('../src/groups'); var categories = require('../src/categories'); var helpers = require('./helpers'); +var meta = require('../src/meta'); + +var socketAdmin = require('../src/socket.io/admin'); describe('socket.io', function () { var io; @@ -156,7 +159,6 @@ describe('socket.io', function () { }); it('should make user admin', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.makeAdmins({ uid: adminUid }, [regularUid], function (err) { assert.ifError(err); groups.isMember(regularUid, 'administrators', function (err, isMember) { @@ -168,7 +170,6 @@ describe('socket.io', function () { }); it('should make user non-admin', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.removeAdmins({ uid: adminUid }, [regularUid], function (err) { assert.ifError(err); groups.isMember(regularUid, 'administrators', function (err, isMember) { @@ -180,7 +181,6 @@ describe('socket.io', function () { }); describe('create/delete', function () { - var socketAdmin = require('../src/socket.io/admin'); var uid; it('should create a user', function (done) { socketAdmin.user.createUser({ uid: adminUid }, { username: 'foo1' }, function (err, _uid) { @@ -214,7 +214,6 @@ describe('socket.io', function () { }); it('should error with invalid data', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.createUser({ uid: adminUid }, null, function (err) { assert.equal(err.message, '[[error:invalid-data]]'); done(); @@ -222,7 +221,6 @@ describe('socket.io', function () { }); it('should reset lockouts', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.resetLockouts({ uid: adminUid }, [regularUid], function (err) { assert.ifError(err); done(); @@ -230,7 +228,6 @@ describe('socket.io', function () { }); it('should reset flags', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.resetFlags({ uid: adminUid }, [regularUid], function (err) { assert.ifError(err); done(); @@ -239,7 +236,6 @@ describe('socket.io', function () { describe('validation emails', function () { - var socketAdmin = require('../src/socket.io/admin'); var meta = require('../src/meta'); it('should validate emails', function (done) { @@ -254,7 +250,6 @@ describe('socket.io', function () { }); it('should error with invalid uids', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.sendValidationEmail({ uid: adminUid }, null, function (err) { assert.equal(err.message, '[[error:invalid-data]]'); done(); @@ -262,7 +257,6 @@ describe('socket.io', function () { }); it('should error if email validation is not required', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.sendValidationEmail({ uid: adminUid }, [regularUid], function (err) { assert.equal(err.message, '[[error:email-confirmations-are-disabled]]'); done(); @@ -270,7 +264,6 @@ describe('socket.io', function () { }); it('should send validation email', function (done) { - var socketAdmin = require('../src/socket.io/admin'); meta.config.requireEmailConfirmation = 1; socketAdmin.user.sendValidationEmail({ uid: adminUid }, [regularUid], function (err) { assert.ifError(err); @@ -281,7 +274,6 @@ describe('socket.io', function () { }); it('should search users', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.user.search({ uid: adminUid }, { query: 'reg', searchBy: 'username' }, function (err, data) { assert.ifError(err); assert.equal(data.matchCount, 1); @@ -337,6 +329,13 @@ describe('socket.io', function () { }); }); + it('should error to get daily analytics with invalid data', function (done) { + io.emit('admin.analytics.get', null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + it('should get daily analytics', function (done) { io.emit('admin.analytics.get', { graph: 'traffic', units: 'days' }, function (err, data) { assert.ifError(err); @@ -356,7 +355,6 @@ describe('socket.io', function () { }); it('should return error', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.before({ uid: 10 }, 'someMethod', {}, function (err) { assert.equal(err.message, '[[error:no-privileges]]'); done(); @@ -364,8 +362,6 @@ describe('socket.io', function () { }); it('should get room stats', function (done) { - var socketAdmin = require('../src/socket.io/admin'); - io.emit('meta.rooms.enter', { enter: 'topic_1' }, function (err) { assert.ifError(err); socketAdmin.rooms.getAll({ uid: 10 }, {}, function (err) { @@ -387,8 +383,6 @@ describe('socket.io', function () { }); it('should get room stats', function (done) { - var socketAdmin = require('../src/socket.io/admin'); - io.emit('meta.rooms.enter', { enter: 'category_1' }, function (err) { assert.ifError(err); socketAdmin.rooms.getAll({ uid: 10 }, {}, function (err) { @@ -405,7 +399,6 @@ describe('socket.io', function () { }); it('should get admin search dictionary', function (done) { - var socketAdmin = require('../src/socket.io/admin'); socketAdmin.getSearchDict({ uid: adminUid }, {}, function (err, data) { assert.ifError(err); assert(Array.isArray(data)); @@ -416,6 +409,173 @@ describe('socket.io', function () { }); }); + it('should fire event', function (done) { + io.on('testEvent', function (data) { + assert.equal(data.foo, 1); + done(); + }); + socketAdmin.fireEvent({ uid: adminUid }, { name: 'testEvent', payload: { foo: 1 } }, function (err) { + assert.ifError(err); + }); + }); + + it('should error with invalid data', function (done) { + socketAdmin.themes.set({ uid: adminUid }, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should set theme to bootswatch', function (done) { + socketAdmin.themes.set({ uid: adminUid }, { type: 'bootswatch', src: 'darkly' }, function (err) { + assert.ifError(err); + meta.configs.get('theme:src', function (err, id) { + assert.ifError(err); + assert.equal(id, 'darkly'); + done(); + }); + }); + }); + + it('should set theme to local persona', function (done) { + socketAdmin.themes.set({ uid: adminUid }, { type: 'local', id: 'nodebb-theme-persona' }, function (err) { + assert.ifError(err); + meta.configs.get('theme:id', function (err, id) { + assert.ifError(err); + assert.equal(id, 'nodebb-theme-persona'); + done(); + }); + }); + }); + + it('should toggle plugin active', function (done) { + socketAdmin.plugins.toggleActive({ uid: adminUid }, 'nodebb-plugin-location-to-map', function (err, data) { + assert.ifError(err); + assert.deepEqual(data, { id: 'nodebb-plugin-location-to-map', active: true }); + done(); + }); + }); + + it('should toggle plugin install', function (done) { + socketAdmin.plugins.toggleInstall({ uid: adminUid }, { id: 'nodebb-plugin-location-to-map', version: 'latest' }, function (err, data) { + assert.ifError(err); + assert.equal(data.name, 'nodebb-plugin-location-to-map'); + done(); + }); + }); + + it('should get list of active plugins', function (done) { + socketAdmin.plugins.getActive({ uid: adminUid }, {}, function (err, data) { + assert.ifError(err); + assert(Array.isArray(data)); + done(); + }); + }); + + it('should order active plugins', function (done) { + var data = [ + { name: 'nodebb-theme-persona', order: 0 }, + { name: 'nodebb-plugin-dbsearch', order: 1 }, + { ignoreme: 'wrong data' }, + ]; + socketAdmin.plugins.orderActivePlugins({ uid: adminUid }, data, function (err) { + assert.ifError(err); + db.sortedSetRank('plugins:active', 'nodebb-plugin-dbsearch', function (err, rank) { + assert.ifError(err); + assert.equal(rank, 1); + done(); + }); + }); + }); + + it('should upgrade plugin', function (done) { + socketAdmin.plugins.upgrade({ uid: adminUid }, { id: 'nodebb-plugin-location-to-map', version: 'latest' }, function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should error with invalid data', function (done) { + socketAdmin.widgets.set({ uid: adminUid }, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should error with invalid data', function (done) { + var data = { template: 'global', location: 'sidebar', widgets: [{ widget: 'html', data: { html: 'test', title: 'test', container: '' } }] }; + socketAdmin.widgets.set({ uid: adminUid }, data, function (err) { + assert.ifError(err); + db.getObjectField('widgets:global', 'sidebar', function (err, widgetData) { + assert.ifError(err); + + assert.equal(JSON.parse(widgetData)[0].data.html, 'test'); + done(); + }); + }); + }); + + it('should clear sitemap cache', function (done) { + socketAdmin.settings.clearSitemapCache({ uid: adminUid }, {}, function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should send test email', function (done) { + socketAdmin.email.test({ uid: adminUid }, { template: 'digest.tpl' }, function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should get logs', function (done) { + var fs = require('fs'); + var path = require('path'); + meta.logs.path = path.join(nconf.get('base_dir'), 'test/files', 'output.log'); + fs.appendFile(meta.logs.path, 'some logs', function (err) { + assert.ifError(err); + + socketAdmin.logs.get({ uid: adminUid }, {}, function (err, data) { + assert.ifError(err); + assert(data); + done(); + }); + }); + }); + + it('should clear logs', function (done) { + socketAdmin.logs.clear({ uid: adminUid }, {}, function (err) { + assert.ifError(err); + socketAdmin.logs.get({ uid: adminUid }, {}, function (err, data) { + assert.ifError(err); + assert.equal(data.length, 0); + done(); + }); + }); + }); + + it('should clear errors', function (done) { + socketAdmin.errors.clear({ uid: adminUid }, {}, function (err) { + assert.ifError(err); + db.exists('error:404', function (err, exists) { + assert.ifError(err); + assert(!exists); + done(); + }); + }); + }); + + it('shoudl delete all events', function (done) { + socketAdmin.deleteAllEvents({ uid: adminUid }, {}, function (err) { + assert.ifError(err); + db.sortedSetCard('events:time', function (err, count) { + assert.ifError(err); + assert.equal(count, 0); + done(); + }); + }); + }); after(function (done) { db.emptydb(done);