Skip to content

Commit

Permalink
feat: add compatibility between force:org:display output and auth:sfd…
Browse files Browse the repository at this point in the history
…xurl:store input
  • Loading branch information
RodEsp committed Mar 13, 2021
1 parent bdf53ac commit 77e3de9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/commands/auth/sfdxurl/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>).sfdxAuthUrl
? await this.getUrlFromJson(authFile)
: await fs.readFile(authFile, 'utf8');

const oauth2Options = AuthInfo.parseSfdxAuthUrl(sfdxAuthUrl);
Expand All @@ -69,4 +69,15 @@ export default class Store extends SfdxCommand {
this.ux.log(successMsg);
return result;
}

private async getUrlFromJson(authFile: string): Promise<string> {
const authFileJson = (await fs.readJson(authFile)) as Record<string, string>;

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<string, Record<string, string>>).result.sfdxAuthUrl;
}

return authFileJson.sfdxAuthUrl;
}
}
19 changes: 19 additions & 0 deletions test/commands/auth/sfdxurl/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthFields>(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()
Expand Down

0 comments on commit 77e3de9

Please sign in to comment.