From 3bd1c6c2b550189b8860e1258e1b1d86ae47724e Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 15 Dec 2016 16:08:32 +0300 Subject: [PATCH] notification tests --- src/plugins/install.js | 2 +- src/socket.io/notifications.js | 12 +++-- test/notifications.js | 91 ++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 9 deletions(-) diff --git a/src/plugins/install.js b/src/plugins/install.js index 9638dbc0dd..a914a09ca6 100644 --- a/src/plugins/install.js +++ b/src/plugins/install.js @@ -109,7 +109,7 @@ module.exports = function (Plugins) { winston.verbose('[plugins/' + command + '] ' + stdout); callback(); - }); + }); } Plugins.upgrade = function (id, version, callback) { diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js index ea32cd7394..cc83b0b80b 100644 --- a/src/socket.io/notifications.js +++ b/src/socket.io/notifications.js @@ -24,12 +24,14 @@ SocketNotifs.loadMore = function (socket, data, callback) { } var start = parseInt(data.after, 10); var stop = start + 20; - user.notifications.getAll(socket.uid, start, stop, function (err, notifications) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + user.notifications.getAll(socket.uid, start, stop, next); + }, + function (notifications, next) { + next(null, {notifications: notifications, nextStart: stop}); } - callback(null, {notifications: notifications, nextStart: stop}); - }); + ], callback); }; SocketNotifs.getCount = function (socket, data, callback) { diff --git a/test/notifications.js b/test/notifications.js index 5ccf0b1248..c36c8008e4 100644 --- a/test/notifications.js +++ b/test/notifications.js @@ -8,6 +8,7 @@ var async = require('async'); var db = require('./mocks/databasemock'); var user = require('../src/user'); var notifications = require('../src/notifications'); +var socketNotifications = require('../src/socket.io/notifications'); describe('Notifications', function () { var uid; @@ -27,7 +28,8 @@ describe('Notifications', function () { it('should create a notification', function (done) { notifications.create({ bodyShort: 'bodyShort', - nid: 'notification_id' + nid: 'notification_id', + path: '/notification/path' }, function (err, _notification) { notification = _notification; assert.ifError(err); @@ -94,7 +96,7 @@ describe('Notifications', function () { }); it('should mark a notification read', function (done) { - notifications.markRead(notification.nid, uid, function (err) { + socketNotifications.markRead({uid: uid}, notification.nid, function (err) { assert.ifError(err); db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { assert.ifError(err); @@ -109,7 +111,7 @@ describe('Notifications', function () { }); it('should mark a notification unread', function (done) { - notifications.markUnread(notification.nid, uid, function (err) { + socketNotifications.markUnread({uid: uid}, notification.nid, function (err) { assert.ifError(err); db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { assert.ifError(err); @@ -117,6 +119,25 @@ describe('Notifications', function () { db.isSortedSetMember('uid:' + uid + ':notifications:read', notification.nid, function (err, isMember) { assert.ifError(err); assert.equal(isMember, false); + socketNotifications.getCount({uid: uid}, null, function (err, count) { + assert.ifError(err); + assert.equal(count, 1); + done(); + }); + }); + }); + }); + }); + + it('should mark all notifications read', function (done) { + socketNotifications.markAllRead({uid: uid}, null, function (err) { + assert.ifError(err); + db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { + assert.ifError(err); + assert.equal(isMember, false); + db.isSortedSetMember('uid:' + uid + ':notifications:read', notification.nid, function (err, isMember) { + assert.ifError(err); + assert.equal(isMember, true); done(); }); }); @@ -193,6 +214,70 @@ describe('Notifications', function () { }); }); + it('should get notification by nid', function (done) { + socketNotifications.get({uid: uid}, {nids: [notification.nid]}, function (err, data) { + assert.ifError(err); + assert.equal(data[0].bodyShort, 'bodyShort'); + assert.equal(data[0].nid, 'notification_id'); + assert.equal(data[0].path, '/notification/path'); + done(); + }); + }); + + it('should get user\'s notifications', function (done) { + socketNotifications.get({uid: uid}, {}, function (err, data) { + assert.ifError(err); + assert.equal(data.unread.length, 0); + assert.equal(data.read[0].nid, 'notification_id'); + done(); + }); + }); + + it('should error with invalid data', function (done) { + socketNotifications.loadMore({uid: uid}, {after: 'test'}, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should error if not logged in', function (done) { + socketNotifications.loadMore({uid: 0}, {after: 10}, function (err) { + assert.equal(err.message, '[[error:no-privileges]]'); + done(); + }); + }); + + it('should load more notifications', function (done) { + socketNotifications.loadMore({uid: uid}, {after: 0}, function (err, data) { + assert.ifError(err); + assert.equal(data.notifications[0].bodyShort, 'bodyShort'); + assert.equal(data.notifications[0].nid, 'notification_id'); + assert.equal(data.notifications[0].path, '/notification/path'); + done(); + }); + }); + + + it('should error if not logged in', function (done) { + socketNotifications.deleteAll({uid: 0}, null, function (err) { + assert.equal(err.message, '[[error:no-privileges]]'); + done(); + }); + }); + + it('should delete all user notifications', function (done) { + socketNotifications.deleteAll({uid: uid}, null, function (err) { + assert.ifError(err); + socketNotifications.get({uid: uid}, {}, function (err, data) { + assert.ifError(err); + assert.equal(data.unread.length, 0); + assert.equal(data.read.length, 0); + done(); + }); + }); + }); + + after(function (done) { db.emptydb(done); });