From 7c36daf49663a6f4012cd9e3b9f9e77ca3410b34 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Mon, 29 Jan 2018 21:20:04 +0100 Subject: [PATCH] backend unit test for routes logout --- services/auth/server/boot/routes.js | 5 +++-- services/auth/server/boot/specs/routes.spec.js | 17 ++++++++++++++++- services/db/localDB09Inserts.sql | 1 - 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/services/auth/server/boot/routes.js b/services/auth/server/boot/routes.js index 16c4603d4..e84749bc8 100644 --- a/services/auth/server/boot/routes.js +++ b/services/auth/server/boot/routes.js @@ -85,7 +85,8 @@ module.exports = function(app) { }); app.get('/logout', function(req, res) { - User.logout(req.accessToken.id, - () => res.redirect('/')); + User.logout(req.accessToken.id, () => { + res.redirect('/'); + }); }); }; diff --git a/services/auth/server/boot/specs/routes.spec.js b/services/auth/server/boot/specs/routes.spec.js index afe562a4c..7dd5a49d4 100644 --- a/services/auth/server/boot/specs/routes.spec.js +++ b/services/auth/server/boot/specs/routes.spec.js @@ -2,14 +2,18 @@ import app from '../../../server/server'; import routes from '../routes'; describe('Auth routes', () => { + let User = app.models.User; let loginFunction; + let logoutFunction; let res; let req; beforeEach(() => { spyOn(app, 'post'); + spyOn(app, 'get').and.callThrough(); routes(app); loginFunction = app.post.calls.mostRecent().args[1]; + logoutFunction = app.get.calls.argsFor(2)[1]; res = {}; req = {body: {}}; }); @@ -46,11 +50,22 @@ describe('Auth routes', () => { }; loginFunction(req, res); }); + + it('should logout after login', done => { + spyOn(User, 'logout').and.callThrough(); + req.accessToken = {id: 'testingTokenId'}; + logoutFunction(req, res); + res.redirect = url => { + expect(User.logout).toHaveBeenCalledWith('testingTokenId', jasmine.any(Function)); + expect(url).toBe('/'); + done(); + }; + }); }); describe('when the user is incorrect', () => { it('should return a 401 unauthorized', done => { - req.body.user = 'IDontExists'; + req.body.user = 'IDontExist'; req.body.password = 'TotallyWrongPassword'; res.status = status => { expect(status).toBe(401); diff --git a/services/db/localDB09Inserts.sql b/services/db/localDB09Inserts.sql index dd03e6d27..366202f92 100644 --- a/services/db/localDB09Inserts.sql +++ b/services/db/localDB09Inserts.sql @@ -389,7 +389,6 @@ INSERT INTO `vn`.`client`(`id`,`name`,`fi`,`socialName`,`contact`,`street`,`city (9, 'Bruce Banner', '16104829E', 'Hulk', 'Black widow', 'Somewhere in New York', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'BruceBanner@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 1, NULL, 0, 0, 4, 0, 1), (10, 'Jessica Jones', '58282869H', 'Jessica Jones', 'Luke Cage', 'NYCC 2015 Poster', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'JessicaJones@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 1, NULL, 0, 0, 4, 0, 1); - INSERT INTO `salix`.`Address`(`id`, `consignee`, `street`, `city`, `postcode`, `provinceFk`, `phone`, `mobile`, `isEnabled`, `isDefaultAddress`, `clientFk`, `defaultAgencyFk`, `longitude`, `latitude`, `isEqualizated`) VALUES (1, 'Bruce Wayne', 'The Bat cave', 'Silla', 46460, 1, NULL, NULL, 1, 1, 1, 2, NULL, NULL, 0),