meta/settings test

v1.18.x
barisusakli 8 years ago
parent 9627e53922
commit 1c6cee207f

@ -1,5 +1,7 @@
'use strict'; 'use strict';
var async = require('async');
var db = require('../database'); var db = require('../database');
var plugins = require('../plugins'); var plugins = require('../plugins');
@ -18,20 +20,20 @@ module.exports = function (Meta) {
}; };
Meta.settings.set = function (hash, values, callback) { Meta.settings.set = function (hash, values, callback) {
var key = 'settings:' + hash; async.waterfall([
db.setObject(key, values, function (err) { function (next) {
if (err) { db.setObject('settings:' + hash, values, next);
return callback(err); },
} function (next) {
plugins.fireHook('action:settings.set', {
plugins.fireHook('action:settings.set', { plugin: hash,
plugin: hash, settings: values
settings: values });
});
Meta.reloadRequired = true; Meta.reloadRequired = true;
callback(); next();
}); }
], callback);
}; };
Meta.settings.setOne = function (hash, field, value, callback) { Meta.settings.setOne = function (hash, field, value, callback) {
@ -39,23 +41,25 @@ module.exports = function (Meta) {
}; };
Meta.settings.setOnEmpty = function (hash, values, callback) { Meta.settings.setOnEmpty = function (hash, values, callback) {
db.getObject('settings:' + hash, function (err, settings) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getObject('settings:' + hash, next);
} },
settings = settings || {}; function (settings, next) {
var empty = {}; settings = settings || {};
Object.keys(values).forEach(function (key) { var empty = {};
if (!settings.hasOwnProperty(key)) { Object.keys(values).forEach(function (key) {
empty[key] = values[key]; if (!settings.hasOwnProperty(key)) {
} empty[key] = values[key];
}); }
});
if (Object.keys(empty).length) { if (Object.keys(empty).length) {
db.setObject('settings:' + hash, empty, callback); db.setObject('settings:' + hash, empty, next);
} else { } else {
callback(); next();
}
} }
}); ], callback);
}; };
}; };

@ -14,7 +14,6 @@ var helpers = require('./helpers');
describe('Messaging Library', function () { describe('Messaging Library', function () {
//var testUids;
var fooUid; var fooUid;
var bazUid; var bazUid;
var herpUid; var herpUid;

@ -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);
});
});
Loading…
Cancel
Save