Skip to content

Commit

Permalink
fix: cleanup code and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RodEsp committed Mar 15, 2021
1 parent a9dfc1b commit 882014d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 7 additions & 8 deletions src/commands/auth/sfdxurl/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as os from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { AuthFields, AuthInfo, fs, Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import { Prompts } from '../../../prompts';
import { Common } from '../../../common';

Expand All @@ -18,6 +19,10 @@ const commonMessages = Messages.loadMessages('@salesforce/plugin-auth', 'message
const AUTH_URL_FORMAT1 = 'force://<refreshToken>@<instanceUrl>';
const AUTH_URL_FORMAT2 = 'force://<clientId>:<clientSecret>:<refreshToken>@<instanceUrl>';

type AuthJson = AnyJson & {
result?: AnyJson & { sfdxAuthUrl: string };
sfdxAuthUrl: string;
};
export default class Store extends SfdxCommand {
public static readonly description = messages.getMessage('description', [AUTH_URL_FORMAT1, AUTH_URL_FORMAT2]);
public static readonly examples = messages.getMessage('examples').split(os.EOL);
Expand Down Expand Up @@ -71,13 +76,7 @@ export default class Store extends SfdxCommand {
}

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;
const authFileJson = (await fs.readJson(authFile)) as AuthJson;
return authFileJson.result?.sfdxAuthUrl || authFileJson.sfdxAuthUrl;

This comment has been minimized.

Copy link
@peternhale

peternhale Mar 15, 2021

Contributor

There is a chance that both might be undefined. Will that condition be handled elsewhere?

This comment has been minimized.

Copy link
@RodEsp

RodEsp Mar 15, 2021

Author Contributor

Just updated this to use a try/catch and print out a friendly error message.
It also prints out the description of the command which states how the auth file should be formatted but not sure if that's too much. Lemme know what you think.

}
}
12 changes: 6 additions & 6 deletions test/commands/auth/sfdxurl/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ describe('auth:sfdxurl:store', async () => {
test
.do(async () => {
await prepareStubs({ fileDoesNotExist: true });
$$.SANDBOX.stub(fs, 'readFile').callsFake(
async () => '{"sfdxAuthUrl": "force://PlatformCLI::[email protected]"}'
);
$$.SANDBOX.stub(fs, 'readJson').callsFake(async () => ({
sfdxAuthUrl: 'force://PlatformCLI::[email protected]',
}));
})
.stdout()
.command(['auth:sfdxurl:store', '-f', 'path/to/key.json', '--json'])
Expand All @@ -77,9 +77,9 @@ describe('auth:sfdxurl:store', async () => {
test
.do(async () => {
await prepareStubs({ fileDoesNotExist: true });
$$.SANDBOX.stub(fs, 'readFile').callsFake(
async () => '{"result": {"sfdxAuthUrl": "force://PlatformCLI::[email protected]"}}'
);
$$.SANDBOX.stub(fs, 'readJson').callsFake(async () => ({
result: { sfdxAuthUrl: 'force://PlatformCLI::[email protected]' },
}));
})
.stdout()
.command(['auth:sfdxurl:store', '-f', 'path/to/key.json', '--json'])
Expand Down

0 comments on commit 882014d

Please sign in to comment.