From 1c6cee207fd3e5bf15201d19088e97865299df65 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 10 Dec 2016 00:51:50 +0300 Subject: [PATCH] meta/settings test --- src/meta/settings.js | 62 ++++++++++++++------------ test/messaging.js | 1 - test/meta.js | 104 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 30 deletions(-) create mode 100644 test/meta.js diff --git a/src/meta/settings.js b/src/meta/settings.js index 299286abdd..785d22961f 100644 --- a/src/meta/settings.js +++ b/src/meta/settings.js @@ -1,5 +1,7 @@ 'use strict'; +var async = require('async'); + var db = require('../database'); var plugins = require('../plugins'); @@ -18,20 +20,20 @@ module.exports = function (Meta) { }; Meta.settings.set = function (hash, values, callback) { - var key = 'settings:' + hash; - db.setObject(key, values, function (err) { - if (err) { - return callback(err); - } - - plugins.fireHook('action:settings.set', { - plugin: hash, - settings: values - }); + async.waterfall([ + function (next) { + db.setObject('settings:' + hash, values, next); + }, + function (next) { + plugins.fireHook('action:settings.set', { + plugin: hash, + settings: values + }); - Meta.reloadRequired = true; - callback(); - }); + Meta.reloadRequired = true; + next(); + } + ], callback); }; Meta.settings.setOne = function (hash, field, value, callback) { @@ -39,23 +41,25 @@ module.exports = function (Meta) { }; Meta.settings.setOnEmpty = function (hash, values, callback) { - db.getObject('settings:' + hash, function (err, settings) { - if (err) { - return callback(err); - } - settings = settings || {}; - var empty = {}; - Object.keys(values).forEach(function (key) { - if (!settings.hasOwnProperty(key)) { - empty[key] = values[key]; - } - }); + async.waterfall([ + function (next) { + db.getObject('settings:' + hash, next); + }, + function (settings, next) { + settings = settings || {}; + var empty = {}; + Object.keys(values).forEach(function (key) { + if (!settings.hasOwnProperty(key)) { + empty[key] = values[key]; + } + }); - if (Object.keys(empty).length) { - db.setObject('settings:' + hash, empty, callback); - } else { - callback(); + if (Object.keys(empty).length) { + db.setObject('settings:' + hash, empty, next); + } else { + next(); + } } - }); + ], callback); }; }; \ No newline at end of file diff --git a/test/messaging.js b/test/messaging.js index 06991b2088..a60504544b 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -14,7 +14,6 @@ var helpers = require('./helpers'); describe('Messaging Library', function () { - //var testUids; var fooUid; var bazUid; var herpUid; diff --git a/test/meta.js b/test/meta.js new file mode 100644 index 0000000000..b0e076090e --- /dev/null +++ b/test/meta.js @@ -0,0 +1,104 @@ +'use strict'; + +var assert = require('assert'); +var async = require('async'); + +var db = require('./mocks/databasemock'); +var meta = require('../src/meta'); +var User = require('../src/user'); +var Groups = require('../src/groups'); + +describe('Messaging Library', function () { + var fooUid; + var bazUid; + var herpUid; + + before(function (done) { + Groups.resetCache(); + // Create 3 users: 1 admin, 2 regular + async.series([ + async.apply(User.create, { username: 'foo', password: 'barbar' }), // admin + async.apply(User.create, { username: 'baz', password: 'quuxquux' }), // restricted user + async.apply(User.create, { username: 'herp', password: 'derpderp' }) // regular user + ], function (err, uids) { + if (err) { + return done(err); + } + + fooUid = uids[0]; + bazUid = uids[1]; + herpUid = uids[2]; + + Groups.join('administrators', fooUid, done); + }); + }); + + describe('settings', function () { + var socketAdmin = require('../src/socket.io/admin'); + it('it should set setting', function (done) { + socketAdmin.settings.set({uid: fooUid}, {hash: 'some:hash', values: {foo: '1', derp: 'value'}}, function (err) { + assert.ifError(err); + db.getObject('settings:some:hash', function (err, data) { + assert.ifError(err); + assert.equal(data.foo, '1'); + assert.equal(data.derp, 'value'); + done(); + }); + }); + }); + + it('it should get setting', function (done) { + socketAdmin.settings.get({uid: fooUid}, {hash: 'some:hash'}, function (err, data) { + assert.ifError(err); + assert.equal(data.foo, '1'); + assert.equal(data.derp, 'value'); + done(); + }); + }); + + it('should not set setting if not empty', function (done) { + meta.settings.setOnEmpty('some:hash', {foo: 2}, function (err) { + assert.ifError(err); + db.getObject('settings:some:hash', function (err, data) { + assert.ifError(err); + assert.equal(data.foo, '1'); + assert.equal(data.derp, 'value'); + done(); + }); + }); + }); + + it('should set setting if empty', function (done) { + meta.settings.setOnEmpty('some:hash', {empty: '2'}, function (err) { + assert.ifError(err); + db.getObject('settings:some:hash', function (err, data) { + assert.ifError(err); + assert.equal(data.foo, '1'); + assert.equal(data.derp, 'value'); + assert.equal(data.empty, '2'); + done(); + }); + }); + }); + + it('should set one and get one', function (done) { + meta.settings.setOne('some:hash', 'myField', 'myValue', function (err) { + assert.ifError(err); + meta.settings.getOne('some:hash', 'myField', function (err, myValue) { + assert.ifError(err); + assert.equal(myValue, 'myValue'); + done(); + }); + }); + }); + + }); + + + + + + after(function (done) { + db.emptydb(done); + }); +});