From 835669805a48c53760029c0f0fdc96ddbf54c1a2 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 15 Dec 2016 17:26:54 +0300 Subject: [PATCH] tag tests --- src/socket.io/admin/tags.js | 10 +-- src/topics/tags.js | 8 +++ test/topics.js | 117 ++++++++++++++++++++++++++++++++++-- 3 files changed, 126 insertions(+), 9 deletions(-) diff --git a/src/socket.io/admin/tags.js b/src/socket.io/admin/tags.js index c00740d9d5..4362159502 100644 --- a/src/socket.io/admin/tags.js +++ b/src/socket.io/admin/tags.js @@ -1,7 +1,8 @@ "use strict"; var topics = require('../../topics'); -var Tags = {}; + +var Tags = module.exports; Tags.create = function (socket, data, callback) { if (!data) { @@ -20,8 +21,9 @@ Tags.update = function (socket, data, callback) { }; Tags.deleteTags = function (socket, data, callback) { + if (!data) { + return callback(new Error('[[error:invalid-data]]')); + } + topics.deleteTags(data.tags, callback); }; - - -module.exports = Tags; \ No newline at end of file diff --git a/src/topics/tags.js b/src/topics/tags.js index 3f6bfbab36..352635cd04 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -96,6 +96,9 @@ module.exports = function (Topics) { }; Topics.updateTag = function (tag, data, callback) { + if (!tag) { + return setImmediate(callback, new Error('[[error:invalid-tag]]')); + } db.setObject('tag:' + tag, data, callback); }; @@ -136,6 +139,11 @@ module.exports = function (Topics) { }, function (next) { db.sortedSetRemove('tags:topic:count', tags, next); + }, + function (next) { + db.deleteAll(tags.map(function (tag) { + return 'tag:' + tag; + }), next); } ], callback); }; diff --git a/test/topics.js b/test/topics.js index 4b85261ef9..a48a2d17a5 100644 --- a/test/topics.js +++ b/test/topics.js @@ -602,7 +602,7 @@ describe('Topic\'s', function () { }); it('should error with invalid data', function (done) { - socketTopics.loadMoreUnreadTopics({uid: adminUid}, {after: 'invalid'}, function (err, data) { + socketTopics.loadMoreUnreadTopics({uid: adminUid}, {after: 'invalid'}, function (err) { assert.equal(err.message, '[[error:invalid-data]]'); done(); }); @@ -621,7 +621,7 @@ describe('Topic\'s', function () { }); it('should error with invalid data', function (done) { - socketTopics.loadMoreRecentTopics({uid: adminUid}, {after: 'invalid'}, function (err, data) { + socketTopics.loadMoreRecentTopics({uid: adminUid}, {after: 'invalid'}, function (err) { assert.equal(err.message, '[[error:invalid-data]]'); done(); }); @@ -638,7 +638,7 @@ describe('Topic\'s', function () { }); it('should error with invalid data', function (done) { - socketTopics.loadMoreFromSet({uid: adminUid}, {after: 'invalid'}, function (err, data) { + socketTopics.loadMoreFromSet({uid: adminUid}, {after: 'invalid'}, function (err) { assert.equal(err.message, '[[error:invalid-data]]'); done(); }); @@ -876,6 +876,8 @@ describe('Topic\'s', function () { describe('tags', function () { var socketTopics = require('../src/socket.io/topics'); + var socketAdmin = require('../src/socket.io/admin'); + before(function (done) { async.parallel({ topic1: function (next) { @@ -884,7 +886,7 @@ describe('Topic\'s', function () { topic2: function (next) { topics.post({uid: adminUid, tags: ['javascript', 'mysql', 'python', 'nodejs'], title: 'topic title 2', content: 'topic 2 content', cid: topic.categoryId}, next); } - }, function (err, results) { + }, function (err) { assert.ifError(err); done(); }); @@ -952,7 +954,7 @@ describe('Topic\'s', function () { }); }); - it('shold return error if data is invalid', function (done) { + it('should return error if data is invalid', function (done) { socketTopics.loadMoreTags({uid: adminUid}, {after: 'asd'}, function (err) { assert.equal(err.message, '[[error:invalid-data]]'); done(); @@ -967,6 +969,111 @@ describe('Topic\'s', function () { done(); }); }); + + it('should error if data is invalid', function (done) { + socketAdmin.tags.create({uid: adminUid}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should error if tag is invalid', function (done) { + socketAdmin.tags.create({uid: adminUid}, {tag: ''}, function (err) { + assert.equal(err.message, '[[error:invalid-tag]]'); + done(); + }); + }); + + it('should error if tag is too short', function (done) { + socketAdmin.tags.create({uid: adminUid}, {tag: 'as'}, function (err) { + assert.equal(err.message, '[[error:tag-too-short]]'); + done(); + }); + }); + + it('should create empty tag', function (done) { + socketAdmin.tags.create({uid: adminUid}, {tag: 'emptytag'}, function (err) { + assert.ifError(err); + db.sortedSetScore('tags:topic:count', 'emptytag', function (err, score) { + assert.ifError(err); + assert.equal(score, 0); + done(); + }); + }); + }); + + it('should do nothing if tag exists', function (done) { + socketAdmin.tags.create({uid: adminUid}, {tag: 'emptytag'}, function (err) { + assert.ifError(err); + db.sortedSetScore('tags:topic:count', 'emptytag', function (err, score) { + assert.ifError(err); + assert.equal(score, 0); + done(); + }); + }); + }); + + it('should error if data is invalid', function (done) { + socketAdmin.tags.update({uid: adminUid}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should error if data.tag is invalid', function (done) { + socketAdmin.tags.update({uid: adminUid}, { + bgColor: '#ff0000', + color: '#00ff00' + }, function (err) { + assert.equal(err.message, '[[error:invalid-tag]]'); + done(); + }); + }); + + it('should update tag', function (done) { + socketAdmin.tags.update({uid: adminUid}, { + tag: 'emptytag', + bgColor: '#ff0000', + color: '#00ff00' + }, function (err) { + assert.ifError(err); + db.getObject('tag:emptytag', function (err, data) { + assert.ifError(err); + assert.equal(data.bgColor, '#ff0000'); + assert.equal(data.color, '#00ff00'); + done(); + }); + }); + }); + + it('should return error with invalid data', function (done) { + socketAdmin.tags.deleteTags({uid: adminUid}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should do nothing if arrays is empty', function (done) { + socketAdmin.tags.deleteTags({uid: adminUid}, {tags: []}, function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should delete tags', function (done) { + socketAdmin.tags.create({uid: adminUid}, {tag: 'emptytag2'}, function (err) { + assert.ifError(err); + socketAdmin.tags.deleteTags({uid: adminUid}, {tags: ['emptytag', 'emptytag2']}, function (err) { + assert.ifError(err); + db.getObjects(['tag:emptytag', 'tag:emptytag2'], function (err, data) { + assert.ifError(err); + assert(!data[0]); + assert(!data[1]); + done(); + }); + }); + }); + }); });