diff --git a/src/commands/auth/logout.js b/src/commands/auth/logout.js index 2b2780a..46c8c0c 100644 --- a/src/commands/auth/logout.js +++ b/src/commands/auth/logout.js @@ -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 }) diff --git a/test/commands/auth/logout.test.js b/test/commands/auth/logout.test.js index e5ce04c..99cdfa5 100644 --- a/test/commands/auth/logout.test.js +++ b/test/commands/auth/logout.test.js @@ -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 @@ -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]: { @@ -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 = []