From da6d503b1a272610f916c03cf474bdb390a3f660 Mon Sep 17 00:00:00 2001 From: Arjun Raj Date: Tue, 19 May 2020 16:10:33 +0530 Subject: [PATCH 1/5] fix: clear tokens on logout - show confirmation messages on logout - show confirmation message if already logged out --- src/commands/auth/logout.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/commands/auth/logout.js b/src/commands/auth/logout.js index 2b2780a..d498c50 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) - try { - await invalidateToken(flags.ctx, flags.force) + const accessToken = await context.get(`${flags.ctx}.access_token`) + if (!accessToken.data) { + this.log('You are already logged out.') + return + } + await invalidateToken(flags.ctx, true) + 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 }) From 97560aabb538065325a8f15df54cb67f6ad35479 Mon Sep 17 00:00:00 2001 From: Arjun Raj Date: Mon, 1 Jun 2020 13:08:52 +0530 Subject: [PATCH 2/5] fix: check token Check if the user is already logged out --- src/commands/auth/logout.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/commands/auth/logout.js b/src/commands/auth/logout.js index d498c50..8c73cb8 100644 --- a/src/commands/auth/logout.js +++ b/src/commands/auth/logout.js @@ -21,14 +21,14 @@ class LogoutCommand extends ImsBaseCommand { 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 { - const accessToken = await context.get(`${flags.ctx}.access_token`) - if (!accessToken.data) { - this.log('You are already logged out.') - return - } await invalidateToken(flags.ctx, true) - this.log('You have successfully logged out') + 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 }) From 9c6a208aea3fdd6dfa0e4ace19db56199b115cc3 Mon Sep 17 00:00:00 2001 From: Arjun Raj Date: Mon, 1 Jun 2020 13:09:03 +0530 Subject: [PATCH 3/5] add: unit tests --- test/commands/auth/logout.test.js | 45 ++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/test/commands/auth/logout.test.js b/test/commands/auth/logout.test.js index e5ce04c..b52d349 100644 --- a/test/commands/auth/logout.test.js +++ b/test/commands/auth/logout.test.js @@ -41,7 +41,39 @@ 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 - already 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 +82,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 +96,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 = [] From d54c7975339571766090b6d25edd0879abda4ba8 Mon Sep 17 00:00:00 2001 From: Arjun Raj Date: Mon, 1 Jun 2020 13:10:27 +0530 Subject: [PATCH 4/5] fix: linting --- test/commands/auth/logout.test.js | 36 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/test/commands/auth/logout.test.js b/test/commands/auth/logout.test.js index b52d349..99cdfa5 100644 --- a/test/commands/auth/logout.test.js +++ b/test/commands/auth/logout.test.js @@ -53,25 +53,23 @@ test('run - already logged out', async () => { await expect(data).toEqual('You are already logged out.') }) -test('run - already 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 - 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' @@ -97,9 +95,9 @@ test('run - error logging out', async () => { }) ims.context.get.mockImplementation(async data => { - return { - data: 'TOKEN' - } + return { + data: 'TOKEN' + } }) // coverage From 5062c76c6129a18657961251386c1d9883269c8f Mon Sep 17 00:00:00 2001 From: Arjun Raj Date: Mon, 1 Jun 2020 13:12:12 +0530 Subject: [PATCH 5/5] fix: make force optional --- src/commands/auth/logout.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/auth/logout.js b/src/commands/auth/logout.js index 8c73cb8..46c8c0c 100644 --- a/src/commands/auth/logout.js +++ b/src/commands/auth/logout.js @@ -27,7 +27,7 @@ class LogoutCommand extends ImsBaseCommand { return } try { - await invalidateToken(flags.ctx, true) + await invalidateToken(flags.ctx, flags.force) this.log('You have successfully logged out.') } catch (err) { this.debugError('Logout failure', err)