Skip to content

Commit

Permalink
fix(auth): logout with actual token
Browse files Browse the repository at this point in the history
  • Loading branch information
eljefedelrodeodeljefe committed Oct 23, 2019
1 parent 3714540 commit c567573
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
6 changes: 0 additions & 6 deletions src/errors/index.ts
Expand Up @@ -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'
Expand Down
31 changes: 27 additions & 4 deletions src/v0/auth.ts
Expand Up @@ -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
Expand Down Expand Up @@ -232,16 +234,37 @@ export class Auth {
Client.getInstance(clientOptions).setDefaults(clientOptions)
}

async logout(): Promise<LogoutResponse> {
public async logout(): Promise<LogoutResponse> {
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)
}
}
64 changes: 64 additions & 0 deletions test/auth/logout.test.ts
Expand Up @@ -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: {
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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')
Expand Down

0 comments on commit c567573

Please sign in to comment.