diff --git a/src/commands/auth/sfdxurl/store.ts b/src/commands/auth/sfdxurl/store.ts index ce2f701d..7b12169c 100644 --- a/src/commands/auth/sfdxurl/store.ts +++ b/src/commands/auth/sfdxurl/store.ts @@ -55,7 +55,7 @@ export default class Store extends SfdxCommand { const authFile = this.flags.sfdxurlfile as string; const sfdxAuthUrl = authFile.endsWith('.json') - ? ((await fs.readJson(authFile)) as Record).sfdxAuthUrl + ? await this.getUrlFromJson(authFile) : await fs.readFile(authFile, 'utf8'); const oauth2Options = AuthInfo.parseSfdxAuthUrl(sfdxAuthUrl); @@ -69,4 +69,15 @@ export default class Store extends SfdxCommand { this.ux.log(successMsg); return result; } + + private async getUrlFromJson(authFile: string): Promise { + const authFileJson = (await fs.readJson(authFile)) as Record; + + if (authFileJson.result) { + // Do a bunch of casting to get TSC to recognize the `sfdxAuthUrl` property as a string + return ((authFileJson as unknown) as Record>).result.sfdxAuthUrl; + } + + return authFileJson.sfdxAuthUrl; + } } diff --git a/test/commands/auth/sfdxurl/store.test.ts b/test/commands/auth/sfdxurl/store.test.ts index e1d4247b..25bc4f87 100644 --- a/test/commands/auth/sfdxurl/store.test.ts +++ b/test/commands/auth/sfdxurl/store.test.ts @@ -74,6 +74,25 @@ describe('auth:sfdxurl:store', async () => { expect(response.result.username).to.equal(testData.username); }); + test + .do(async () => { + await prepareStubs({ fileDoesNotExist: true }); + $$.SANDBOX.stub(fs, 'readFile').callsFake( + async () => '{"result": {"sfdxAuthUrl": "force://PlatformCLI::CoffeeAndBacon@su0503.my.salesforce.com"}}' + ); + }) + .stdout() + .command(['auth:sfdxurl:store', '-f', 'path/to/key.json', '--json']) + .it( + 'should return auth fields when passing in a json result a la `sfdx force:org:display --verbose --json`', + (ctx) => { + const response = parseJson(ctx.stdout); + expect(response.status).to.equal(0); + expect(response.result).to.deep.equal(authFields); + expect(response.result.username).to.equal(testData.username); + } + ); + test .do(async () => prepareStubs()) .stdout()