Skip to content

Commit

Permalink
fix: AIO_CLI_ENV=stage aio console:org:list always uses prod env (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron authored Jun 23, 2021
1 parent c7ca60c commit 4916e19
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions src/commands/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 })
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -27,6 +26,5 @@ const CONFIG_KEYS = {
module.exports = {
ORG_TYPE_ENTERPRISE,
CONFIG_KEYS,
API_KEYS,
DEFAULT_ENV
API_KEYS
}
57 changes: 53 additions & 4 deletions test/commands/console/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 4916e19

Please sign in to comment.