From 1658ebbe208568cf6cf89d261b0563f119456dd4 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 23 Nov 2016 13:38:20 +0300 Subject: [PATCH] categories update test --- src/categories/update.js | 159 +++++++++++++++++++++------------------ test/categories.js | 79 +++++++++++++++++-- 2 files changed, 157 insertions(+), 81 deletions(-) diff --git a/src/categories/update.js b/src/categories/update.js index 4f4229b5fc..485d3e3834 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -21,24 +21,30 @@ module.exports = function (Categories) { }; function updateCategory(cid, modifiedFields, callback) { - Categories.exists(cid, function (err, exists) { - if (err || !exists) { - return callback(err); - } - - - if (modifiedFields.hasOwnProperty('name')) { - translator.translate(modifiedFields.name, function (translated) { - modifiedFields.slug = cid + '/' + utils.slugify(translated); - }); - } - - plugins.fireHook('filter:category.update', {category: modifiedFields}, function (err, categoryData) { - if (err) { - return callback(err); + var category; + async.waterfall([ + function (next) { + Categories.exists(cid, next); + }, + function (exists, next) { + if (!exists) { + return callback(); } - var category = categoryData.category; + if (modifiedFields.hasOwnProperty('name')) { + translator.translate(modifiedFields.name, function (translated) { + modifiedFields.slug = cid + '/' + utils.slugify(translated); + next(); + }); + } else { + next(); + } + }, + function (next) { + plugins.fireHook('filter:category.update', {category: modifiedFields}, next); + }, + function (categoryData, next) { + category = categoryData.category; var fields = Object.keys(category); // move parent to front, so its updated first var parentCidIndex = fields.indexOf('parentCid'); @@ -48,15 +54,13 @@ module.exports = function (Categories) { async.eachSeries(fields, function (key, next) { updateCategoryField(cid, key, category[key], next); - }, function (err) { - if (err) { - return callback(err); - } - plugins.fireHook('action:category.update', {cid: cid, modified: category}); - callback(); - }); - }); - }); + }, next); + }, + function (next) { + plugins.fireHook('action:category.update', {cid: cid, modified: category}); + next(); + } + ], callback); } function updateCategoryField(cid, key, value, callback) { @@ -64,73 +68,80 @@ module.exports = function (Categories) { return updateParent(cid, value, callback); } - db.setObjectField('category:' + cid, key, value, function (err) { - if (err) { - return callback(err); - } - - if (key === 'order') { - updateOrder(cid, value, callback); - } else if (key === 'description') { - Categories.parseDescription(cid, value, callback); - } else { - callback(); + async.waterfall([ + function (next) { + db.setObjectField('category:' + cid, key, value, next); + }, + function (next) { + if (key === 'order') { + updateOrder(cid, value, next); + } else if (key === 'description') { + Categories.parseDescription(cid, value, next); + } else { + next(); + } } - }); + ], callback); } function updateParent(cid, newParent, callback) { if (parseInt(cid, 10) === parseInt(newParent, 10)) { return callback(new Error('[[error:cant-set-self-as-parent]]')); } - Categories.getCategoryField(cid, 'parentCid', function (err, oldParent) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + Categories.getCategoryField(cid, 'parentCid', next); + }, + function (oldParent, next) { + async.series([ + function (next) { + oldParent = parseInt(oldParent, 10) || 0; + db.sortedSetRemove('cid:' + oldParent + ':children', cid, next); + }, + function (next) { + newParent = parseInt(newParent, 10) || 0; + db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next); + }, + function (next) { + db.setObjectField('category:' + cid, 'parentCid', newParent, next); + } + ], next); } - - async.series([ - function (next) { - oldParent = parseInt(oldParent, 10) || 0; - db.sortedSetRemove('cid:' + oldParent + ':children', cid, next); - }, - function (next) { - newParent = parseInt(newParent, 10) || 0; - db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next); - }, - function (next) { - db.setObjectField('category:' + cid, 'parentCid', newParent, next); - } - ], function (err) { - callback(err); - }); + ], function (err) { + callback(err); }); } function updateOrder(cid, order, callback) { - Categories.getCategoryField(cid, 'parentCid', function (err, parentCid) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + Categories.getCategoryField(cid, 'parentCid', next); + }, + function (parentCid, next) { + async.parallel([ + function (next) { + db.sortedSetAdd('categories:cid', order, cid, next); + }, + function (next) { + parentCid = parseInt(parentCid, 10) || 0; + db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next); + } + ], next); } - - async.parallel([ - function (next) { - db.sortedSetAdd('categories:cid', order, cid, next); - }, - function (next) { - parentCid = parseInt(parentCid, 10) || 0; - db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next); - } - ], callback); + ], function (err) { + callback(err); }); } Categories.parseDescription = function (cid, description, callback) { - plugins.fireHook('filter:parse.raw', description, function (err, parsedDescription) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + plugins.fireHook('filter:parse.raw', description, next); + }, + function (parsedDescription, next) { + Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, next); } - Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, callback); - }); + ], callback); }; }; diff --git a/test/categories.js b/test/categories.js index 876b4cb2bf..a7e37f244e 100644 --- a/test/categories.js +++ b/test/categories.js @@ -11,20 +11,27 @@ 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'); describe('Categories', function () { var categoryObj; var posterUid; + var adminUid; before(function (done) { - User.create({username: 'poster'}, function (err, _posterUid) { - if (err) { - return done(err); + groups.resetCache(); + async.parallel({ + posterUid: function (next) { + User.create({username: 'poster'}, next); + }, + adminUid: function (next) { + User.create({username: 'admin'}, next); } - - posterUid = _posterUid; - - done(); + }, function (err, results) { + assert.ifError(err); + posterUid = results.posterUid; + adminUid = results.adminUid; + groups.join('administrators', adminUid, done); }); }); @@ -299,8 +306,66 @@ describe('Categories', function () { done(); }); }); + }); + describe('admin socket methods', function () { + var socketCategories = require('../src/socket.io/admin/categories'); + var cid; + before(function (done) { + Categories.create({ + name: 'update name', + description: 'update description', + parentCid: categoryObj.cid, + icon: 'fa-check', + order: '5' + }, function (err, category) { + assert.ifError(err); + cid = category.cid; + done(); + }); + }); + + it('should return error with invalid data', function (done) { + socketCategories.update({uid: adminUid}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should error if you try to set parent as self', function (done) { + var updateData = {}; + updateData[cid] = { + parentCid: cid + }; + socketCategories.update({uid: adminUid}, updateData, function (err) { + assert.equal(err.message, '[[error:cant-set-self-as-parent]]'); + done(); + }); + }); + + it('should update category data', function (done) { + var updateData = {}; + updateData[cid] = { + name: 'new name', + description: 'new description', + parentCid: 0, + order: 3, + icon: 'fa-hammer' + }; + socketCategories.update({uid: adminUid}, updateData, function (err) { + assert.ifError(err); + Categories.getCategoryData(cid, function (err, data) { + assert.ifError(err); + assert.equal(data.name, updateData[cid].name); + assert.equal(data.description, updateData[cid].description); + assert.equal(data.parentCid, updateData[cid].parentCid); + assert.equal(data.order, updateData[cid].order); + assert.equal(data.icon, updateData[cid].icon); + done(); + }); + }); + }); });