Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear tokens on logout #33

Merged
merged 8 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/commands/auth/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ class LogoutCommand extends ImsBaseCommand {
const { invalidateToken, context } = require('@adobe/aio-lib-ims')
const { CLI } = require('@adobe/aio-lib-ims/src/context')
const current = await context.getCurrent()

flags.ctx = flags.ctx || (current || CLI)

const accessToken = await context.get(`${flags.ctx}.access_token`)
if (!accessToken || !accessToken.data) {
this.log('You are already logged out.')
return
}
try {
await invalidateToken(flags.ctx, flags.force)
this.log('You have successfully logged out.')
} catch (err) {
this.debugError('Logout failure', err)
this.error(`Cannot logout context '${flags.ctx}': ${err.message || err}`, { exit: 1 })
Expand Down
43 changes: 37 additions & 6 deletions test/commands/auth/logout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,37 @@ test('run - success', async () => {
await expect(runResult).resolves.not.toThrow()
})

test('run - error', async () => {
test('run - already logged out', async () => {
let data
command.log = jest.fn((logData) => {
data = logData
})

const runResult = command.run([])
await expect(runResult instanceof Promise).toBeTruthy()
await expect(runResult).resolves.not.toThrow()
await expect(data).toEqual('You are already logged out.')
})

test('run - successfully logged out', async () => {
let data
command.log = jest.fn((logData) => {
data = logData
})

ims.context.get.mockImplementation(async data => {
return {
data: 'TOKEN'
}
})

const runResult = command.run([])
await expect(runResult instanceof Promise).toBeTruthy()
await expect(runResult).resolves.not.toThrow()
await expect(data).toEqual('You have successfully logged out.')
})

test('run - error logging out', async () => {
const errorMessage = 'my-error'
const context = 'my-context'
let runResult
Expand All @@ -50,11 +80,6 @@ test('run - error', async () => {
throw new Error(errorMessage)
})

command.argv = ['--ctx', context]
runResult = command.run()
await expect(runResult instanceof Promise).toBeTruthy()
await expect(runResult).rejects.toEqual(new Error(`Cannot logout context '${context}': ${errorMessage}`))

const IMS = '$ims'
const store = {
[IMS]: {
Expand All @@ -69,6 +94,12 @@ test('run - error', async () => {
return store.$ims.$current
})

ims.context.get.mockImplementation(async data => {
return {
data: 'TOKEN'
}
})

// coverage
await ims.context.setCurrent(context)
command.argv = []
Expand Down