From 4916e19474446685440b6ff425b3a76ba91e076b Mon Sep 17 00:00:00 2001 From: Shazron Abdullah Date: Wed, 23 Jun 2021 17:30:20 +0800 Subject: [PATCH] fix: AIO_CLI_ENV=stage aio console:org:list always uses prod env (#162) --- package.json | 1 + src/commands/console/index.js | 14 +++---- src/config.js | 4 +- test/commands/console/index.test.js | 57 +++++++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 2959d17..e90313f 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dependencies": { "@adobe/aio-lib-core-config": "^2.0.0", "@adobe/aio-lib-core-logging": "^1.1.0", + "@adobe/aio-lib-env": "^1.1.0", "@adobe/aio-lib-ims": "^4.0.0", "@adobe/generator-aio-console": "^2.0.6", "@oclif/command": "^1", diff --git a/src/commands/console/index.js b/src/commands/console/index.js index 5992f9e..174b023 100644 --- a/src/commands/console/index.js +++ b/src/commands/console/index.js @@ -13,12 +13,13 @@ governing permissions and limitations under the License. const aioConsoleLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-console', { provider: 'debug' }) const config = require('@adobe/aio-lib-core-config') const { Command, flags } = require('@oclif/command') -const { getToken, context } = require('@adobe/aio-lib-ims') +const { getToken } = require('@adobe/aio-lib-ims') const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli') const { CLI } = require('@adobe/aio-lib-ims/src/context') const Help = require('@oclif/plugin-help').default const yaml = require('js-yaml') -const { CONFIG_KEYS, DEFAULT_ENV, API_KEYS } = require('../../config') +const { CONFIG_KEYS, API_KEYS } = require('../../config') +const { getCliEnv } = require('@adobe/aio-lib-env') class ConsoleCommand extends Command { async run () { @@ -27,14 +28,13 @@ class ConsoleCommand extends Command { } async initSdk () { - const currConfig = await context.getCli() - this.imsEnv = (currConfig && currConfig.env) || DEFAULT_ENV - this.apiKey = API_KEYS[this.imsEnv] + this.cliEnv = getCliEnv() + this.apiKey = API_KEYS[this.cliEnv] - await context.setCli({ 'cli.bare-output': true }, false) // set this globally + // await context.setCli({ 'cli.bare-output': true }, false) // set this globally aioConsoleLogger.debug('Retrieving Auth Token') this.accessToken = await getToken(CLI) - this.consoleCLI = await LibConsoleCLI.init({ accessToken: this.accessToken, apiKey: this.apiKey, env: this.imsEnv }) + this.consoleCLI = await LibConsoleCLI.init({ accessToken: this.accessToken, apiKey: this.apiKey, env: this.cliEnv }) } /** diff --git a/src/config.js b/src/config.js index 6ede878..65924aa 100644 --- a/src/config.js +++ b/src/config.js @@ -9,7 +9,6 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -const DEFAULT_ENV = 'prod' const ORG_TYPE_ENTERPRISE = 'entp' const API_KEYS = { @@ -27,6 +26,5 @@ const CONFIG_KEYS = { module.exports = { ORG_TYPE_ENTERPRISE, CONFIG_KEYS, - API_KEYS, - DEFAULT_ENV + API_KEYS } diff --git a/test/commands/console/index.test.js b/test/commands/console/index.test.js index 4f45188..176576d 100644 --- a/test/commands/console/index.test.js +++ b/test/commands/console/index.test.js @@ -14,15 +14,25 @@ const mockLogger = require('@adobe/aio-lib-core-logging') const config = require('@adobe/aio-lib-core-config') const Help = require('@oclif/plugin-help').default const yaml = require('js-yaml') -const { CONFIG_KEYS } = require('../../../src/config') +const { CONFIG_KEYS, API_KEYS } = require('../../../src/config') +const { PROD_ENV, STAGE_ENV, DEFAULT_ENV } = require('@adobe/aio-lib-env') + +jest.mock('@adobe/aio-lib-ims') describe('ConsoleCommand', () => { - beforeEach(() => { - mockLogger.mockReset() + let command + let ORIGINAL_AIO_CLI_ENV + + beforeAll(() => { + ORIGINAL_AIO_CLI_ENV = process.env.AIO_CLI_ENV + }) + afterAll(() => { + process.env.AIO_CLI_ENV = ORIGINAL_AIO_CLI_ENV }) - let command beforeEach(() => { + delete process.env.AIO_CLI_ENV + mockLogger.mockReset() jest.clearAllMocks() command = new ConsoleCommand([]) command.log = jest.fn() @@ -40,6 +50,45 @@ describe('ConsoleCommand', () => { expect(ConsoleCommand.flags.help.type).toBe('boolean') }) + describe('initSDK', () => { + test('exists', async () => { + expect(command.initSdk).toBeInstanceOf(Function) + }) + + test('env STAGE_ENV', async () => { + process.env.AIO_CLI_ENV = STAGE_ENV + + await command.initSdk() + expect(command.cliEnv).toEqual(STAGE_ENV) + expect(command.apiKey).toEqual(API_KEYS[STAGE_ENV]) + expect(command.consoleCLI.sdkClient.env).toEqual(STAGE_ENV) + }) + test('env PROD_ENV', async () => { + process.env.AIO_CLI_ENV = PROD_ENV + + await command.initSdk() + expect(command.cliEnv).toEqual(PROD_ENV) + expect(command.apiKey).toEqual(API_KEYS[PROD_ENV]) + expect(command.consoleCLI.sdkClient.env).toEqual(PROD_ENV) + }) + test('env unknown', async () => { + process.env.AIO_CLI_ENV = 'unknown-env' + + await command.initSdk() + expect(command.cliEnv).toEqual(DEFAULT_ENV) + expect(command.apiKey).toEqual(API_KEYS[DEFAULT_ENV]) + expect(command.consoleCLI.sdkClient.env).toEqual(DEFAULT_ENV) + }) + test('env not set', async () => { + delete process.env.AIO_CLI_ENV + + await command.initSdk() + expect(command.cliEnv).toEqual(DEFAULT_ENV) + expect(command.apiKey).toEqual(API_KEYS[DEFAULT_ENV]) + expect(command.consoleCLI.sdkClient.env).toEqual(DEFAULT_ENV) + }) + }) + describe('run', () => { test('exists', async () => { expect(command.run).toBeInstanceOf(Function)