From 205a859989649b474e4d1373ef2381fae38aa6f7 Mon Sep 17 00:00:00 2001 From: Moritz Raho Date: Thu, 1 Oct 2020 17:36:41 +0200 Subject: [PATCH 1/2] delete the file - no tests yet --- src/commands/app/init.js | 8 ++++++++ src/commands/app/use.js | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/commands/app/init.js b/src/commands/app/init.js index 43e9fa90..4247338f 100644 --- a/src/commands/app/init.js +++ b/src/commands/app/init.js @@ -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() @@ -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)) } @@ -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({ diff --git a/src/commands/app/use.js b/src/commands/app/use.js index 292a46c0..a5fa4dc8 100644 --- a/src/commands/app/use.js +++ b/src/commands/app/use.js @@ -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') const SERVICE_API_KEY_ENV = 'SERVICE_API_KEY' @@ -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(flags.import) + return config } } } From 65f83ec40143e284483b0bd8cdd901548dc35195 Mon Sep 17 00:00:00 2001 From: Moritz Raho Date: Thu, 1 Oct 2020 17:58:06 +0200 Subject: [PATCH 2/2] added tests --- src/commands/app/use.js | 4 ++-- test/commands/app/init.test.js | 42 ++++++++++++++++++++++++++++++++++ test/commands/app/use.test.js | 12 +++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/commands/app/use.js b/src/commands/app/use.js index a5fa4dc8..2a921035 100644 --- a/src/commands/app/use.js +++ b/src/commands/app/use.js @@ -17,7 +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') +const fs = require('fs-extra') const SERVICE_API_KEY_ENV = 'SERVICE_API_KEY' @@ -94,7 +94,7 @@ class Use extends BaseCommand { if (file) { const config = this.importConfigFile(file, flags) // delete file only if it was downloaded - fs.unlinkSync(flags.import) + fs.unlinkSync(file) return config } } diff --git a/test/commands/app/init.test.js b/test/commands/app/init.test.js index 2f5efeba..32c9a8c1 100644 --- a/test/commands/app/init.test.js +++ b/test/commands/app/init.test.js @@ -48,6 +48,7 @@ beforeEach(() => { mockRun.mockReset() yeoman.createEnv.mockClear() fs.ensureDirSync.mockClear() + fs.unlinkSync.mockClear() importLib.importConfigJson.mockReset() importLib.writeAio.mockReset() }) @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -292,6 +299,7 @@ describe('run', () => { }) expect(fs.ensureDirSync).not.toHaveBeenCalled() expect(spyChdir).not.toHaveBeenCalled() + expect(fs.unlinkSync).not.toHaveBeenCalled() }) test('no-path', async () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -520,5 +561,6 @@ describe('run', () => { 'destination-file': 'console.json', 'ims-env': 'prod' }) + expect(fs.unlinkSync).toHaveBeenCalledWith('console.json') }) }) diff --git a/test/commands/app/use.test.js b/test/commands/app/use.test.js index f7f1ba43..4f9a7075 100644 --- a/test/commands/app/use.test.js +++ b/test/commands/app/use.test.js @@ -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') @@ -71,6 +73,7 @@ beforeEach(() => { mockGetCli.mockReturnValue({}) importLib.loadConfigFile.mockReset() importLib.validateConfig.mockReset() + fs.unlinkSync.mockClear() }) test('exports', async () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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() })