From 69b766bbc80c0bb90ee3ad4907d0d186b5edb7ad Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 2 Dec 2016 17:05:46 +0300 Subject: [PATCH] more tests --- src/socket.io/user.js | 74 ++++++++++++++++----------------- src/user/settings.js | 3 ++ test/controllers.js | 9 +++- test/user.js | 96 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 39 deletions(-) diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 69b229b4b1..c10a510e71 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -72,15 +72,21 @@ SocketUser.emailConfirm = function (socket, data, callback) { } if (parseInt(meta.config.requireEmailConfirmation, 10) !== 1) { - callback(); + return callback(new Error('[[error:email-confirmations-are-disabled]]')); } - user.getUserField(socket.uid, 'email', function (err, email) { - if (err || !email) { - return callback(err); - } - user.email.sendValidationEmail(socket.uid, email, callback); - }); + async.waterfall([ + function (next) { + user.getUserField(socket.uid, 'email', next); + }, + function (email, next) { + if (!email) { + return callback(); + } + + user.email.sendValidationEmail(socket.uid, email, next); + } + ], callback); }; @@ -109,39 +115,37 @@ SocketUser.reset.commit = function (socket, data, callback) { if (!data || !data.code || !data.password) { return callback(new Error('[[error:invalid-data]]')); } + var uid; + async.waterfall([ + function (next) { + async.parallel({ + uid: async.apply(db.getObjectField, 'reset:uid', data.code), + reset: async.apply(user.reset.commit, data.code, data.password) + }, next); + }, + function (results, next) { + uid = results.uid; + events.log({ + type: 'password-reset', + uid: uid, + ip: socket.ip + }); - async.parallel({ - uid: async.apply(db.getObjectField, 'reset:uid', data.code), - reset: async.apply(user.reset.commit, data.code, data.password) - }, function (err, results) { - if (err) { - return callback(err); - } - - var uid = results.uid; - var now = new Date(); - var parsedDate = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate(); - - user.getUserField(uid, 'username', function (err, username) { - if (err) { - return callback(err); - } - + user.getUserField(uid, 'username', next); + }, + function (username, next) { + var now = new Date(); + var parsedDate = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate(); emailer.send('reset_notify', uid, { username: username, date: parsedDate, site_title: meta.config.title || 'NodeBB', subject: '[[email:reset.notify.subject]]' }); - }); - events.log({ - type: 'password-reset', - uid: uid, - ip: socket.ip - }); - callback(); - }); + next(); + } + ], callback); }; SocketUser.isFollowing = function (socket, data, callback) { @@ -224,16 +228,10 @@ SocketUser.saveSettings = function (socket, data, callback) { }; SocketUser.setTopicSort = function (socket, sort, callback) { - if (!socket.uid) { - return callback(); - } user.setSetting(socket.uid, 'topicPostSort', sort, callback); }; SocketUser.setCategorySort = function (socket, sort, callback) { - if (!socket.uid) { - return callback(); - } user.setSetting(socket.uid, 'categoryTopicSort', sort, callback); }; diff --git a/src/user/settings.js b/src/user/settings.js index 3a5b53b9c3..3d93a9724a 100644 --- a/src/user/settings.js +++ b/src/user/settings.js @@ -160,6 +160,9 @@ module.exports = function (User) { }; User.setSetting = function (uid, key, value, callback) { + if (!parseInt(uid, 10)) { + return callback(); + } db.setObjectField('user:' + uid + ':settings', key, value, callback); }; }; diff --git a/test/controllers.js b/test/controllers.js index 89fce43246..1f31e0a037 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -794,7 +794,14 @@ describe('Controllers', function () { user.create({username: 'follower'}, function (err, _uid) { assert.ifError(err); uid = _uid; - socketUser.follow({uid: uid}, {uid: fooUid}, done); + socketUser.follow({uid: uid}, {uid: fooUid}, function (err) { + assert.ifError(err); + socketUser.isFollowing({uid: uid}, {uid: fooUid}, function (err, isFollowing) { + assert.ifError(err); + assert(isFollowing); + done(); + }); + }); }); }); diff --git a/test/user.js b/test/user.js index 4854fbf110..8621575fe2 100644 --- a/test/user.js +++ b/test/user.js @@ -707,6 +707,102 @@ describe('User', function () { done(); }); }); + + it('should error if requireEmailConfirmation is disabled', function (done) { + socketUser.emailConfirm({uid: testUid}, {}, function (err) { + assert.equal(err.message, '[[error:email-confirmations-are-disabled]]'); + done(); + }); + }); + + it('should send email confirm', function (done) { + Meta.config.requireEmailConfirmation = 1; + socketUser.emailConfirm({uid: testUid}, {}, function (err) { + assert.ifError(err); + Meta.config.requireEmailConfirmation = 0; + done(); + }); + }); + + it('should send reset email', function (done) { + socketUser.reset.send({uid: 0}, 'john@example.com', function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should return invalid-data error', function (done) { + socketUser.reset.send({uid: 0}, null, function (err) { + assert.equal(err.message, '[[error:invalid-data]]'); + done(); + }); + }); + + it('should not error', function (done) { + socketUser.reset.send({uid: 0}, 'doestnot@exist.com', function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should commit reset', function (done) { + db.getObject('reset:uid', function (err, data) { + assert.ifError(err); + var code = Object.keys(data)[0]; + socketUser.reset.commit({uid: 0}, {code: code, password: 'swordfish'}, function (err) { + assert.ifError(err); + done(); + }); + }); + }); + + it('should save user settings', function (done) { + var data = { + uid: 1, + settings: { + bootswatchSkin: 'default', + homePageRoute: 'none', + homePageCustom: '', + openOutgoingLinksInNewTab: 0, + scrollToMyPost: 1, + delayImageLoading: 1, + userLang: 'en-GB', + usePagination: 1, + topicsPerPage: '10', + postsPerPage: '5', + showemail: 1, + showfullname: 1, + restrictChat: 0, + followTopicsOnCreate: 1, + followTopicsOnReply: 1, + notificationSound: '', + incomingChatSound: '', + outgoingChatSound: '' + } + }; + socketUser.saveSettings({uid: testUid}, data, function (err) { + assert.ifError(err); + done(); + }); + }); + + it('should set moderation note', function (done) { + User.create({username: 'noteadmin'}, function (err, adminUid) { + assert.ifError(err); + groups.join('administrators', adminUid, function (err) { + assert.ifError(err); + socketUser.setModerationNote({uid: adminUid}, {uid: testUid, note: 'this is a test user'}, function (err) { + assert.ifError(err); + User.getUserField(testUid, 'moderationNote', function (err, note) { + assert.ifError(err); + assert.equal(note, 'this is a test user'); + done(); + }); + }); + }); + }); + + }); }); describe('approval queue', function () {