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

added app info command, with tests #399

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
76 changes: 76 additions & 0 deletions src/commands/app/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const BaseCommand = require('../../BaseCommand')
const { flags } = require('@oclif/command')
const yaml = require('js-yaml')

class Info extends BaseCommand {
async run () {
// cli input
const { flags } = this.parse(Info)
const appConfig = this.getAppConfig()
delete appConfig.cli
if (!appConfig.s3.tvmUrl) {
delete appConfig.s3.tvmUrl
}
// todo: mask these
if (!appConfig.s3.creds) {
delete appConfig.s3.creds
}

if (flags.mask) {
appConfig.ow.auth = appConfig.ow.auth ? '<hidden>' : 'undefined'
}

if (flags.json) {
this.log(JSON.stringify(appConfig))
} else if (flags.yml) {
this.log(yaml.safeDump(appConfig))
} else { // flags.hson
this.log(appConfig)
}
}
}

Info.description = `Display settings/configuration in use by an Adobe I/O App

`

Info.flags = {
...BaseCommand.flags,
json: flags.boolean({
description: 'Output json',
char: 'j',
exclusive: ['hson', 'yml']
}),
hson: flags.boolean({
default: true,
description: 'Output human readable json',
char: 'h',
exclusive: ['json', 'yml']
}),
yml: flags.boolean({
description: 'Output yml',
char: 'y',
exclusive: ['hson', 'json']
}),
mask: flags.boolean({
description: 'Hide known private info',
default: true,
allowNo: true
})
}

Info.args = []

module.exports = Info
105 changes: 105 additions & 0 deletions test/commands/app/info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2021 Adobe Inc. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const TheCommand = require('../../../src/commands/app/info.js')
const BaseCommand = require('../../../src/BaseCommand.js')
const HHelp = require('@oclif/plugin-help').default
const owlocal = require('../../../src/lib/owlocal.js')

test('exports', async () => {
expect(typeof TheCommand).toEqual('function')
expect(TheCommand.prototype instanceof BaseCommand).toBeTruthy()
})

test('description', async () => {
expect(TheCommand.description).toBeDefined()
})

test('flags', async () => {
expect(TheCommand.flags).toMatchObject({
json: expect.any(Object),
hson: expect.any(Object),
yml: expect.any(Object),
mask: expect.any(Object)
})
})

test('args', async () => {
expect(TheCommand.args).toBeDefined()
expect(TheCommand.args).toBeInstanceOf(Array)
})

describe('instance methods', () => {
let command

beforeEach(() => {
command = new TheCommand([])
})

describe('run', () => {
test('exists', async () => {
expect(command.run).toBeInstanceOf(Function)
})
})
})

describe('missing props', () => {
test('appConfig.s3.tvmUrl', async () => {
const command = new TheCommand([])
command.error = jest.fn()
command.log = jest.fn()
command.appConfig = { s3: { tvmUrl: 'some url', creds: 'definitely defined' }, ow: { auth: 'super secret' } }
command.run()
expect(command.error).toHaveBeenCalledTimes(0)
expect(command.log).not.toHaveBeenCalledWith(expect.stringContaining('super secret'))
})
})

describe('masking secrets', () => {
test('masks by default', async () => {
const command = new TheCommand([])
command.error = jest.fn()
command.log = jest.fn()
command.appConfig = { s3: {}, ow: { auth: 'super secret' } }
command.run()
expect(command.error).toHaveBeenCalledTimes(0)
expect(command.log).not.toHaveBeenCalledWith(expect.stringContaining('super secret'))
})
// this is really just to get coverage on L32
test('masks without appConfig.ow.auth', async () => {
const command = new TheCommand([])
command.error = jest.fn()
command.log = jest.fn()
command.appConfig = { s3: {}, ow: { auth: '' } }
command.run()
expect(command.error).toHaveBeenCalledTimes(0)
expect(command.log).not.toHaveBeenCalledWith(expect.stringContaining('super secret'))
})
test('no-masking', async () => {
const command = new TheCommand(['--no-mask', '-j'])
command.error = jest.fn()
command.log = jest.fn()
command.appConfig = { s3: {}, ow: { auth: 'super secret' } }
command.run()
expect(command.error).toHaveBeenCalledTimes(0)
expect(command.log).toHaveBeenCalledWith(expect.stringContaining('super secret'))
})
test('no-masking --yaml', async () => {
const command = new TheCommand(['--no-mask', '-y'])
command.error = jest.fn()
command.log = jest.fn()
command.appConfig = { s3: {}, ow: { auth: 'super secret' } }
command.run()
expect(command.error).toHaveBeenCalledTimes(0)
expect(command.log).toHaveBeenCalledWith(expect.stringContaining('super secret'))
})
})