Skip to content

Commit

Permalink
fix: return all info from AuthInfo.listAllAuthorizations (#449)
Browse files Browse the repository at this point in the history
* fix: return all info from AuthInfo.listAllAuthorizations

* chore: code review

* chore: code review

* chore: code review
  • Loading branch information
mdonnalley authored Aug 2, 2021
1 parent 6514ad4 commit 17e60ea
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/exported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export { ConfigPropertyMeta, ConfigPropertyMetaInput, Config, SfdxPropertyKeys }

export { ConfigInfo, ConfigAggregator } from './config/configAggregator';

export { AuthFields, AuthInfo, OAuth2WithVerifier, SfdcUrl, getJwtAudienceUrl } from './org/authInfo';
export { AuthFields, AuthInfo, OAuth2WithVerifier, OrgAuthorization, SfdcUrl, getJwtAudienceUrl } from './org/authInfo';

export { AuthRemover } from './org/authRemover';

Expand Down
11 changes: 4 additions & 7 deletions src/globalInfo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { JsonMap, Optional } from '@salesforce/ts-types';
import { JsonMap } from '@salesforce/ts-types';

export enum SfInfoKeys {
ORGS = 'orgs',
Expand All @@ -16,13 +16,10 @@ export type Timestamp = { timestamp: string };
export type SfEntry = JsonMap;

export type SfOrg = {
alias: Optional<string>;
username: Optional<string>;
orgId: Optional<string>;
instanceUrl: Optional<string>;
username: string;
orgId: string;
instanceUrl: string;
accessToken?: string;
oauthMethod?: 'jwt' | 'web' | 'token' | 'unknown';
error?: string;
} & SfEntry;

export interface SfOrgs {
Expand Down
44 changes: 32 additions & 12 deletions src/org/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ export type AuthFields = {
expirationDate?: string;
};

export type OrgAuthorization = {
orgId: string;
username: string;
oauthMethod: 'jwt' | 'web' | 'token' | 'unknown';
aliases: Nullable<string[]>;
configs: Nullable<string[]>;
isScratchOrg?: boolean;
instanceUrl?: string;
accessToken?: string;
error?: string;
};

/**
* Options for access token flow.
*/
Expand Down Expand Up @@ -373,40 +385,48 @@ export class AuthInfo extends AsyncOptionalCreatable<AuthInfo.Options> {
/**
* Get a list of all authorizations based on auth files stored in the global directory.
*
* @returns {Promise<SfOrg[]>}
* @returns {Promise<OrgAuthorization[]>}
*/
public static async listAllAuthorizations(): Promise<SfOrg[]> {
public static async listAllAuthorizations(): Promise<OrgAuthorization[]> {
const globalInfo = await GlobalInfo.getInstance();
const config = (await ConfigAggregator.create()).getConfigInfo();
const auths = Object.values(globalInfo.orgs.getAll());
const final: SfOrg[] = [];
const final: OrgAuthorization[] = [];
for (const auth of auths) {
const username = ensureString(auth.username);
const alias = globalInfo.aliases.get(username) ?? undefined;
const aliases = globalInfo.aliases.getAll(username) ?? undefined;
// Get a list of configuration values that are set to either the username or one
// of the aliases
const configs = config
.filter((c) => aliases.includes(c.value as string) || c.value === username)
.map((c) => c.key);
try {
const authInfo = await AuthInfo.create({ username });
const { orgId, instanceUrl } = authInfo.getFields();
const { orgId, instanceUrl, devHubUsername } = authInfo.getFields();
final.push({
alias,
aliases,
configs,
username,
orgId,
instanceUrl,
isScratchOrg: Boolean(devHubUsername),
orgId: orgId as string,
accessToken: authInfo.getConnectionOptions().accessToken,
oauthMethod: authInfo.isJwt() ? 'jwt' : authInfo.isOauth() ? 'web' : 'token',
timestamp: auth.timestamp,
});
} catch (err) {
final.push({
alias,
aliases,
configs,
username,
orgId: auth.orgId as string,
instanceUrl: auth.instanceUrl as string,
orgId: auth.orgId,
instanceUrl: auth.instanceUrl,
accessToken: undefined,
oauthMethod: 'unknown',
error: err.message,
timestamp: auth.timestamp,
});
}
}

return final;
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/globalInfo/accessors/orgAccessorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { testSetup } from '../../../../src/testSetup';
describe('OrgAccessor', () => {
const username = '[email protected]';
const alias = 'MyAlias';
const org = { username, orgId: '12345', alias };
const org = { username, orgId: '12345', aliases: [alias] };
const token = { token: '123', url: 'https://login.salesforce.com', user: username };
const $$ = testSetup();

Expand Down
12 changes: 6 additions & 6 deletions test/unit/globalInfo/sfdxDataHandlerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('SfdxDataHandler', () => {
username,
timestamp,
instanceUrl: 'https://login.salesforce.com',
alias: 'myOrg',
aliases: ['myOrg'],
},
};
const merged: SfInfo = GlobalInfo.emptyDataModel;
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('SfdxDataHandler', () => {
orgId: '12345_NEW_SFDX',
username: newSfdxAuthUsername,
instanceUrl: 'https://login.salesforce.com',
alias: 'newUser',
aliases: ['newUser'],
},
},
});
Expand All @@ -141,7 +141,7 @@ describe('SfdxDataHandler', () => {
orgId: '12345_NEW_SFDX',
username: newSfdxAuthUsername,
instanceUrl: 'https://login.salesforce.com',
alias: 'newUser',
aliases: ['newUser'],
},
},
});
Expand All @@ -167,7 +167,7 @@ describe('SfdxDataHandler', () => {
orgId: '12345_NEW_SFDX',
username: newSfdxAuthUsername,
instanceUrl: 'https://login.salesforce.com',
alias: 'newUser',
aliases: ['newUser'],
},
},
tokens: {
Expand All @@ -180,7 +180,7 @@ describe('SfdxDataHandler', () => {
orgId: '12345_NEW_SFDX',
username: newSfdxAuthUsername,
instanceUrl: 'https://login.salesforce.com',
alias: 'newUser',
aliases: ['newUser'],
},
},
});
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('AuthHandler', () => {
accessToken: 'token_12345',
username,
instanceUrl: 'https://login.salesforce.com',
alias: 'myOrg',
aliases: ['myOrg'],
};

beforeEach(() => {
Expand Down
63 changes: 46 additions & 17 deletions test/unit/org/authInfoTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,6 @@ describe('AuthInfo', () => {
describe('listAllAuthorizations', () => {
describe('with no AuthInfo.create errors', () => {
const username = '[email protected]';
let timestamp;

beforeEach(async () => {
stubMethod($$.SANDBOX, ConfigAggregator.prototype, 'loadProperties').callsFake(async () => {});
Expand Down Expand Up @@ -1843,21 +1842,21 @@ describe('AuthInfo', () => {
authCode: testMetadata.authCode,
},
});
timestamp = (await GlobalInfo.getInstance()).orgs.get(username).timestamp;
stubMethod($$.SANDBOX, AuthInfo, 'create').withArgs({ username }).returns(Promise.resolve(authInfo));
});

it('should return list of authorizations with web oauthMethod', async () => {
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
alias: undefined,
aliases: [],
configs: [],
isScratchOrg: false,
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: 'authInfoTest_access_token',
oauthMethod: 'web',
timestamp,
},
]);
});
Expand All @@ -1867,13 +1866,14 @@ describe('AuthInfo', () => {
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
alias: undefined,
aliases: [],
configs: [],
isScratchOrg: false,
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: 'authInfoTest_access_token',
oauthMethod: 'jwt',
timestamp,
},
]);
});
Expand All @@ -1884,29 +1884,58 @@ describe('AuthInfo', () => {
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
alias: undefined,
aliases: [],
configs: [],
isScratchOrg: false,
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: 'authInfoTest_access_token',
oauthMethod: 'token',
timestamp,
},
]);
});

it('should return list of authorizations with alias', async () => {
stubMethod($$.SANDBOX, AliasAccessor.prototype, 'get').returns('MyAlias');
it('should return list of authorizations with aliases', async () => {
stubMethod($$.SANDBOX, AliasAccessor.prototype, 'getAll').returns(['MyAlias']);
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
alias: 'MyAlias',
aliases: ['MyAlias'],
configs: [],
isScratchOrg: false,
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: 'authInfoTest_access_token',
oauthMethod: 'web',
},
]);
});

it('should return list of authorizations with configs', async () => {
stubMethod($$.SANDBOX, AliasAccessor.prototype, 'getAll').returns(['MyAlias']);
stubMethod($$.SANDBOX, ConfigAggregator.prototype, 'getConfigInfo').returns([
{
value: 'MyAlias',
key: OrgConfigProperties.TARGET_ORG,
},
{
value: username,
key: OrgConfigProperties.TARGET_DEV_HUB,
},
]);
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
aliases: ['MyAlias'],
configs: [OrgConfigProperties.TARGET_ORG, OrgConfigProperties.TARGET_DEV_HUB],
isScratchOrg: false,
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: 'authInfoTest_access_token',
oauthMethod: 'web',
timestamp,
},
]);
});
Expand Down Expand Up @@ -1934,31 +1963,31 @@ describe('AuthInfo', () => {
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
alias: undefined,
aliases: [],
configs: [],
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: undefined,
oauthMethod: 'unknown',
error: 'FAIL!',
timestamp: undefined,
},
]);
});

it('should return list of authorizations with unknown oauthMethod and alias', async () => {
stubMethod($$.SANDBOX, AliasAccessor.prototype, 'get').returns('MyAlias');
stubMethod($$.SANDBOX, AliasAccessor.prototype, 'getAll').returns(['MyAlias']);
const auths = await AuthInfo.listAllAuthorizations();
expect(auths).to.deep.equal([
{
alias: 'MyAlias',
aliases: ['MyAlias'],
configs: [],
username: '[email protected]',
orgId: '00DAuthInfoTest_orgId',
instanceUrl: 'http://mydevhub.localhost.internal.salesforce.com:6109',
accessToken: undefined,
oauthMethod: 'unknown',
error: 'FAIL!',
timestamp: undefined,
},
]);
});
Expand Down

0 comments on commit 17e60ea

Please sign in to comment.