From a0a42fb7cf8ccbd84ba4ab6457c2725f877fcc2a Mon Sep 17 00:00:00 2001 From: Vincenzo Chianese Date: Sat, 5 Jan 2019 18:49:30 +0100 Subject: [PATCH] give love to the current user tests --- lib/services/consumers/user.service.js | 2 +- test/services/users.test.js | 81 +++++++++----------------- 2 files changed, 29 insertions(+), 54 deletions(-) diff --git a/lib/services/consumers/user.service.js b/lib/services/consumers/user.service.js index c46403842..c3fde6edf 100644 --- a/lib/services/consumers/user.service.js +++ b/lib/services/consumers/user.service.js @@ -119,7 +119,7 @@ s.remove = function (userId) { return this.get(userId) // validate user exists .then(user => Promise.all([user, !user ? false : userDao.remove(userId)])) .then(([user, userDeleted]) => { - if (!userDeleted) { + if (user && !userDeleted) { throw new Error('user delete failed'); } else { return Promise.all([applicationService.removeAll(userId), // Cascade delete all apps associated with user diff --git a/test/services/users.test.js b/test/services/users.test.js index 06420411e..5595d0e7c 100644 --- a/test/services/users.test.js +++ b/test/services/users.test.js @@ -76,20 +76,13 @@ describe('User service tests', function () { describe('Get and Find User tests', function () { let user; - before(function (done) { - db.flushdb() - .then(function () { - user = createRandomUserObject(); - userService - .insert(user) - .then(function (newUser) { - should.exist(newUser); - user.id = newUser.id; - done(); - }); - }) - .catch(done); - }); + before(() => db.flushdb().then(() => { + user = createRandomUserObject(); + return userService.insert(user); + }).then(newUser => { + should.exist(newUser); + user.id = newUser.id; + })); it('should get user by userId', function (done) { userService.get(user.id) @@ -110,6 +103,7 @@ describe('User service tests', function () { }) .catch(done); }); + it('should get user all users', function () { return userService.findAll() .then(function (data) { @@ -264,21 +258,13 @@ describe('User service tests', function () { describe('Activate and deactivate user tests', function () { let user; - before(function (done) { - db.flushdb() - .then(function () { - user = createRandomUserObject(); - userService - .insert(user) - .then(function (newUser) { - should.exist(newUser); - user = Object.assign(user, newUser); - user.createdAt = newUser.createdAt; - done(); - }); - }) - .catch(done); - }); + before(() => db.flushdb().then(() => { + user = createRandomUserObject(); + return userService.insert(user); + }).then(newUser => { + should.exist(newUser); + user.id = newUser.id; + })); it('should deactivate user', function (done) { userService.deactivate(user.id) @@ -328,39 +314,28 @@ describe('User service tests', function () { describe('Delete user tests', function () { let user; - before(function (done) { - db.flushdb() - .then(function () { - user = createRandomUserObject(); - userService - .insert(user) - .then(function (newUser) { - should.exist(newUser); - user.id = newUser.id; - done(); - }); - }) - .catch(done); - }); + before(() => db.flushdb().then(() => { + user = createRandomUserObject(); + return userService.insert(user); + }).then(newUser => { + should.exist(newUser); + user.id = newUser.id; + })); - it('should delete user', function (done) { - userService.remove(user.id) + it('should delete user', function () { + return userService.remove(user.id) .then(function (deleted) { should.exist(deleted); deleted.should.eql(true); - done(); - }) - .catch(done); + }); }); - it('should not delete user with invalid id', function (done) { - userService.remove('invalid_id') + it('should not delete user with invalid id', function () { + return userService.remove('invalid_id') .then(function (deleted) { should.exist(deleted); deleted.should.eql(false); - done(); - }) - .catch(done); + }); }); }); });