From 4369a6d2908b8418c6bd4123309e9bbae7052d15 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 2 Mar 2017 21:58:33 +0300 Subject: [PATCH] more user tests --- src/controllers/user.js | 15 ++++++++++----- test/controllers.js | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/controllers/user.js b/src/controllers/user.js index c1049adffe..008faa4a06 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -39,12 +39,17 @@ userController.getUserByEmail = function (req, res, next) { }; function byType(type, req, res, next) { - userController.getUserDataByField(req.uid, type, req.params[type], function (err, data) { - if (err || !data) { - return next(err); + async.waterfall([ + function (next) { + userController.getUserDataByField(req.uid, type, req.params[type], next); + }, + function (data, next) { + if (!data) { + return next(); + } + res.json(data); } - res.json(data); - }); + ], next); } userController.getUserDataByField = function (callerUid, field, fieldValue, callback) { diff --git a/test/controllers.js b/test/controllers.js index b745004d59..e86ae0c00e 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -27,7 +27,7 @@ describe('Controllers', function () { }, next); }, user: function (next) { - user.create({ username: 'foo', password: 'barbar' }, next); + user.create({ username: 'foo', password: 'barbar', email: 'foo@test.com' }, next); }, navigation: function (next) { var navigation = require('../src/navigation/admin'); @@ -911,6 +911,42 @@ describe('Controllers', function () { }, ], done); }); + + it('should 404 if user does not exist', function (done) { + request(nconf.get('url') + '/api/user/email/doesnotexist', function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 404); + assert(body); + done(); + }); + }); + + it('should load user by uid', function (done) { + request(nconf.get('url') + '/api/user/uid/' + fooUid, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should load user by username', function (done) { + request(nconf.get('url') + '/api/user/username/foo', function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should load user by email', function (done) { + request(nconf.get('url') + '/api/user/email/foo@test.com', function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); }); describe('account follow page', function () {