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

delete console.json after generation in init and use #302

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class InitCommand extends BaseCommand {
// client id of the console's workspace jwt credentials
let serviceClientId = ''

// delete console credentials only if it was generated
let deleteConsoleCredentials = false

if (!flags.import && !flags.yes && flags.login) {
try {
const { accessToken, env: imsEnv } = await getCliInfo()
Expand All @@ -60,6 +63,8 @@ class InitCommand extends BaseCommand {
})
// trigger import
flags.import = generatedFile
// delete console credentials
deleteConsoleCredentials = true
} catch (e) {
this.log(chalk.red(e.message))
}
Expand Down Expand Up @@ -92,6 +97,9 @@ class InitCommand extends BaseCommand {
const merge = true
if (flags.import) {
await importConfigJson(flags.import, process.cwd(), { interactive, merge }, { [SERVICE_API_KEY_ENV]: serviceClientId })
if (deleteConsoleCredentials) {
fs.unlinkSync(flags.import)
}
} else {
// write default services value to .aio
await writeAio({
Expand Down
6 changes: 5 additions & 1 deletion src/commands/app/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const config = require('@adobe/aio-lib-core-config')
const { EOL } = require('os')
const yeoman = require('yeoman-environment')
const { getCliInfo } = require('../../lib/app-helper')
const fs = require('fs-extra')

const SERVICE_API_KEY_ENV = 'SERVICE_API_KEY'

Expand Down Expand Up @@ -91,7 +92,10 @@ class Use extends BaseCommand {
}
const file = await this.useConsoleConfig(flags)
if (file) {
return this.importConfigFile(file, flags)
const config = this.importConfigFile(file, flags)
// delete file only if it was downloaded
fs.unlinkSync(file)
return config
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/commands/app/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ beforeEach(() => {
mockRun.mockReset()
yeoman.createEnv.mockClear()
fs.ensureDirSync.mockClear()
fs.unlinkSync.mockClear()
importLib.importConfigJson.mockReset()
importLib.writeAio.mockReset()
})
Expand Down Expand Up @@ -176,6 +177,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).toHaveBeenCalledWith(expect.stringContaining('some-path'))
expect(spyChdir).toHaveBeenCalledWith(expect.stringContaining('some-path'))
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('some-path, --yes --skip-install', async () => {
Expand All @@ -195,6 +197,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).toHaveBeenCalledWith(expect.stringContaining('some-path'))
expect(spyChdir).toHaveBeenCalledWith(expect.stringContaining('some-path'))
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path, --yes', async () => {
Expand All @@ -213,6 +216,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).not.toHaveBeenCalled()
expect(spyChdir).not.toHaveBeenCalled()
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path, --no-login', async () => {
Expand All @@ -231,6 +235,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).not.toHaveBeenCalled()
expect(spyChdir).not.toHaveBeenCalled()
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path, --yes --skip-install', async () => {
Expand All @@ -249,6 +254,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).not.toHaveBeenCalled()
expect(spyChdir).not.toHaveBeenCalled()
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path, --skip-install', async () => {
Expand All @@ -272,6 +278,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).not.toHaveBeenCalled()
expect(spyChdir).not.toHaveBeenCalled()
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})

test('getCliInfo error', async () => {
Expand All @@ -292,6 +299,7 @@ describe('run', () => {
})
expect(fs.ensureDirSync).not.toHaveBeenCalled()
expect(spyChdir).not.toHaveBeenCalled()
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path', async () => {
Expand All @@ -316,6 +324,34 @@ describe('run', () => {
'project-name': project.name,
'adobe-services': getFullServicesList()
})
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})

test('some-path', async () => {
const project = mockValidConfig()
await TheCommand.run(['some-path'])

expect(fs.ensureDirSync).toHaveBeenCalledWith(expect.stringContaining('some-path'))
expect(spyChdir).toHaveBeenCalledWith(expect.stringContaining('some-path'))

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(2)
const genConsole = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenNthCalledWith(1, genConsole, {
'access-token': mockAccessToken,
'destination-file': 'console.json',
'ims-env': 'prod'
})
const genApp = mockRegister.mock.calls[1][1]
expect(mockRun).toHaveBeenNthCalledWith(2, genApp, {
'skip-prompt': false,
'skip-install': false,
'project-name': project.name,
'adobe-services': getFullServicesList()
})

// we changed dir, console.json is in cwd
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})

test('no imports should write aio config', async () => {
Expand Down Expand Up @@ -348,6 +384,7 @@ describe('run', () => {
process.cwd(),
{ interactive: false, merge: true }
)
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path --import file=invalid config', async () => {
Expand Down Expand Up @@ -380,6 +417,7 @@ describe('run', () => {
process.cwd(),
{ interactive: false, merge: true },
{ SERVICE_API_KEY: 'fakeId123' })
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path --import file={name: lifeisgood, services:AdobeTargetSDK,CampaignSDK, credentials:fake}', async () => {
Expand Down Expand Up @@ -408,6 +446,7 @@ describe('run', () => {
process.cwd(),
{ interactive: false, merge: true },
{ SERVICE_API_KEY: '' })
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path --import file={name: lifeisgood, services:AdobeTargetSDK,CampaignSDK, credentials:null}', async () => {
Expand Down Expand Up @@ -436,6 +475,7 @@ describe('run', () => {
process.cwd(),
{ interactive: false, merge: true },
{ SERVICE_API_KEY: '' })
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('no-path --yes --import file={name: lifeisgood, services:AdobeTargetSDK,CampaignSDK, credentials:fake,jwt}', async () => {
Expand All @@ -462,6 +502,7 @@ describe('run', () => {
process.cwd(),
{ interactive: false, merge: true },
{ SERVICE_API_KEY: 'fakeId123' })
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('some-path --import file={name: lifeisgood, services:undefined, credentials:fake,jwt}', async () => {
Expand Down Expand Up @@ -520,5 +561,6 @@ describe('run', () => {
'destination-file': 'console.json',
'ims-env': 'prod'
})
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})
})
12 changes: 11 additions & 1 deletion test/commands/app/use.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
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/use')
const BaseCommand = require('../../../src/BaseCommand')
const importLib = require('../../../src/lib/import')
const inquirer = require('inquirer')
const fs = require('fs-extra')

jest.mock('fs-extra')

jest.mock('@adobe/aio-lib-core-config')
const mockConfig = require('@adobe/aio-lib-core-config')
Expand Down Expand Up @@ -71,6 +73,7 @@ beforeEach(() => {
mockGetCli.mockReturnValue({})
importLib.loadConfigFile.mockReset()
importLib.validateConfig.mockReset()
fs.unlinkSync.mockClear()
})

test('exports', async () => {
Expand Down Expand Up @@ -99,11 +102,13 @@ test('runs (config file)', async () => {
await TheCommand.run(['config-file', '--merge'])

expect(importLib.importConfigJson).toHaveBeenCalledTimes(3)
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('runs invalid config', async () => {
mockInvalidConfig()
await expect(TheCommand.run(['config-file'])).rejects.toThrow('fake error')
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('runs (generator, confirmation yes, got global console config)', async () => {
Expand All @@ -119,6 +124,7 @@ test('runs (generator, confirmation yes, got global console config)', async () =
await TheCommand.run([])
expect(importLib.importConfigJson).toHaveBeenCalledTimes(1)
expect(importLib.importConfigJson).toHaveBeenCalledWith('console.json', process.cwd(), { interactive: true, merge: false, overwrite: false }, { SERVICE_API_KEY: '' })
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})

test('runs (generator, confirmation yes, got global console config, no cli context)', async () => {
Expand All @@ -134,6 +140,7 @@ test('runs (generator, confirmation yes, got global console config, no cli conte
await TheCommand.run([])
expect(importLib.importConfigJson).toHaveBeenCalledTimes(1)
expect(importLib.importConfigJson).toHaveBeenCalledWith('console.json', process.cwd(), { interactive: true, merge: false, overwrite: false }, { SERVICE_API_KEY: '' })
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})

test('runs (generator, confirmation yes, got global console config, no cli context, jwt client id is set)', async () => {
Expand All @@ -153,6 +160,7 @@ test('runs (generator, confirmation yes, got global console config, no cli conte
await TheCommand.run([])
expect(importLib.importConfigJson).toHaveBeenCalledTimes(1)
expect(importLib.importConfigJson).toHaveBeenCalledWith('console.json', process.cwd(), { interactive: true, merge: false, overwrite: false }, { SERVICE_API_KEY: 'fakeId123' })
expect(fs.unlinkSync).toHaveBeenCalledWith('console.json')
})

test('runs (generator, confirmation no, got global console config)', async () => {
Expand All @@ -167,6 +175,7 @@ test('runs (generator, confirmation no, got global console config)', async () =>

await TheCommand.run([])
expect(importLib.importConfigJson).toHaveBeenCalledTimes(0)
expect(fs.unlinkSync).not.toHaveBeenCalled()
})

test('runs (generator, error in global console config)', async () => {
Expand All @@ -175,4 +184,5 @@ test('runs (generator, error in global console config)', async () => {

await expect(TheCommand.run([])).rejects.toThrowError()
expect(importLib.importConfigJson).toHaveBeenCalledTimes(0)
expect(fs.unlinkSync).not.toHaveBeenCalled()
})