From c5675731c35dd02146e16db10b3dde33813cf460 Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 23 Oct 2019 23:00:47 +0200 Subject: [PATCH] fix(auth): logout with actual token --- src/errors/index.ts | 6 ---- src/v0/auth.ts | 31 ++++++++++++++++--- test/auth/logout.test.ts | 64 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/errors/index.ts b/src/errors/index.ts index 964ef6a14ef..c997dba7209 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -22,12 +22,6 @@ export class PasswordSetRequestFailed extends BaseError { super(message, properties) } } -export class LogoutFailed extends BaseError { - public name = 'LogoutFailed' - constructor(public message: string = 'Could not log out.', properties?: any) { - super(message, properties) - } -} export class UninstantiatedClient extends BaseError { public name = 'UninstantiatedClient' diff --git a/src/v0/auth.ts b/src/v0/auth.ts index b39c4082d86..1f4de6e5edc 100644 --- a/src/v0/auth.ts +++ b/src/v0/auth.ts @@ -164,6 +164,8 @@ export class Auth { }) + console.log('came here') + this.setDefaultHeader( response.data.user.legacy_id || response.data.user.id, response.data.token @@ -232,16 +234,37 @@ export class Auth { Client.getInstance(clientOptions).setDefaults(clientOptions) } - async logout(): Promise { + public async logout(): Promise { + if (!this.token) { + throw new LogoutMissingToken() + } + try { - const { data } = await axios.get(`${this.options.base}/api/v0/users/logout`) + const { data } = await axios.get(`${this.options.base}/api/v0/users/logout`, { + headers: { + Authorization: `Bearer ${this.token}` + } + }) return { msg: data.msg } as LogoutResponse } catch (err) { - - throw new errors.LogoutFailed(undefined, { error: err }) + throw new LogoutFailed(undefined, { error: err }) } } } + +export class LogoutMissingToken extends errors.BaseError { + public name = 'LogoutMissingToken' + constructor(public message: string = 'Could not log out due to missing token.', properties?: any) { + super(message, properties) + } +} + +export class LogoutFailed extends errors.BaseError { + public name = 'LogoutFailed' + constructor(public message: string = 'Could not log out.', properties?: any) { + super(message, properties) + } +} diff --git a/test/auth/logout.test.ts b/test/auth/logout.test.ts index 24d0203a48d..9acc064a1aa 100644 --- a/test/auth/logout.test.ts +++ b/test/auth/logout.test.ts @@ -24,6 +24,30 @@ if (process.env.SYSTEM_TEST) { } describe('Auth: logout', () => { + it('fails on missing token', async () => { + const options = { + credentials: { + username: user.username, + password: user.password + }, + base: process.env.TILLHUB_BASE + } + + if (process.env.SYSTEM_TEST !== 'true') { + mock.onGet('https://api.tillhub.com/api/v0/users/logout').reply(function (config) { + return [500] + }) + } + + const auth = new v0.Auth(options) + + try { + let data = await auth.logout() + } catch (err) { + expect(err.name).toBe('LogoutMissingToken') + } + }) + it('can log out', async () => { const options = { credentials: { @@ -34,6 +58,24 @@ describe('Auth: logout', () => { } if (process.env.SYSTEM_TEST !== 'true') { + mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function (config) { + return [ + 200, + { + token: 'something', + user: { + id: '123', + legacy_id: '4564', + scopes: ['admin'], + role: 'manager' + }, + features: { + inventory: true + } + } + ] + }) + mock.onGet('https://api.tillhub.com/api/v0/users/logout').reply(function (config) { return [ 200, @@ -42,11 +84,13 @@ describe('Auth: logout', () => { } ] }) + } const auth = new v0.Auth(options) try { + await auth.authenticate() let data = await auth.logout() expect(data).toBeTruthy() expect(data.msg === 'Logout successful.').toBe(true) @@ -65,14 +109,34 @@ describe('Auth: logout', () => { } if (process.env.SYSTEM_TEST !== 'true') { + mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function (config) { + return [ + 200, + { + token: 'something', + user: { + id: '123', + legacy_id: '4564', + scopes: ['admin'], + role: 'manager' + }, + features: { + inventory: true + } + } + ] + }) + mock.onGet('https://api.tillhub.com/api/v0/users/logout').reply(function (config) { return [500] }) + } const auth = new v0.Auth(options) try { + await auth.authenticate() let data = await auth.logout() } catch (err) { expect(err.name).toBe('LogoutFailed')