Skip to content

Commit

Permalink
fix: w-8612427 load project, if available, to establish loginurl
Browse files Browse the repository at this point in the history
  • Loading branch information
peternhale committed Feb 1, 2021
1 parent 3e85ed3 commit 2f3907a
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/commands/auth/device/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Login extends SfdxCommand {
if (await Prompts.shouldExitCommand(this.ux, this.flags.noprompt)) return {};

const oauthConfig: OAuth2Options = {
loginUrl: get(this.flags.instanceurl, 'href', null) as Optional<string>,
loginUrl: await Common.getLoginUrl(get(this.flags.instanceurl, 'href', null) as Optional<string>),
clientId: this.flags.clientid,
};

Expand Down
7 changes: 4 additions & 3 deletions src/commands/auth/jwt/grant.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, AuthRemover, Messages, SfdxError } from '@salesforce/core';
import { get, Optional } from '@salesforce/ts-types';
import { Prompts } from '../../../prompts';
import { Common } from '../../../common';

Expand Down Expand Up @@ -84,9 +85,9 @@ export default class Grant extends SfdxCommand {
privateKeyFile: this.flags.jwtkeyfile,
};

const oauth2Options = this.flags.instanceurl
? Object.assign(oauth2OptionsBase, { loginUrl: this.flags.instanceurl })
: oauth2OptionsBase;
const loginUrl = await Common.getLoginUrl(get(this.flags.instanceurl, 'href', null) as Optional<string>);

const oauth2Options = loginUrl ? Object.assign(oauth2OptionsBase, { loginUrl }) : oauth2OptionsBase;

let authInfo: AuthInfo;
try {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/auth/web/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as open from 'open';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { AuthFields, AuthInfo, Messages, OAuth2Options, SfdxError, WebOAuthServer } from '@salesforce/core';
import { Env } from '@salesforce/kit';
import { get, Optional } from '@salesforce/ts-types';
import { Prompts } from '../../../prompts';
import { Common } from '../../../common';

Expand Down Expand Up @@ -64,7 +65,7 @@ export default class Login extends SfdxCommand {
if (await Prompts.shouldExitCommand(this.ux, this.flags.noprompt)) return {};

const oauthConfig: OAuth2Options = {
loginUrl: this.flags.instanceurl,
loginUrl: await Common.getLoginUrl(get(this.flags.instanceurl, 'href', null) as Optional<string>),
clientId: this.flags.clientid,
};

Expand Down
16 changes: 15 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { AuthInfo } from '@salesforce/core';
import { AuthInfo, SfdcUrl, SfdxProject } from '@salesforce/core';
import { getString, Optional } from '@salesforce/ts-types';

interface Flags {
setalias?: string;
Expand All @@ -24,4 +25,17 @@ export class Common {
});
}
}
public static async getLoginUrl(instanceUrl: Optional<string>): Promise<Optional<string>> {
if (instanceUrl) {
return instanceUrl;
}
try {
const project = await SfdxProject.resolve();
const projectJson = await project.resolveProjectConfig();
return getString(projectJson, 'sfdcLoginUrl', SfdcUrl.PRODUCTION);
} catch (err) {
// just return production URL
return SfdcUrl.PRODUCTION;
}
}
}
114 changes: 114 additions & 0 deletions test/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* 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 { SfdcUrl, SfdxProject } from '@salesforce/core';
import sinon = require('sinon');
import { expect } from '@salesforce/command/lib/test';
import { restoreContext, testSetup } from '@salesforce/core/lib/testSetup';
import { Common } from '../src/common';

describe('common unit tests', () => {
const sandbox = sinon.createSandbox();
const $$ = testSetup();
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-explicit-any
let projectPath: any;
beforeEach(async () => {
projectPath = await $$.localPathRetriever($$.id);
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
SfdxProject.instances.clear();
});
afterEach(() => {
restoreContext($$);
sandbox.restore();
});
describe('production url', () => {
it('should return production URL if not in a dx project', async () => {
sandbox.stub(SfdxProject, 'resolve').throwsException();
const loginUrl = await Common.getLoginUrl(undefined);
expect(loginUrl).to.equal(SfdcUrl.PRODUCTION);
expect(projectPath).to.be.ok;
});
it('should return production URL if project with property sfdcLoginUrl absent', async () => {
sandbox.stub(SfdxProject.prototype, 'resolveProjectConfig').resolves({
packageDirectories: [
{
path: 'force-app',
default: true,
},
],
sourceApiVersion: '50.0',
});
const loginUrl = await Common.getLoginUrl(undefined);
expect(loginUrl).to.equal(SfdcUrl.PRODUCTION);
});
it('should return production URL if project with property sfdcLoginUrl present', async () => {
sandbox.stub(SfdxProject.prototype, 'resolveProjectConfig').resolves({
packageDirectories: [
{
path: 'force-app',
default: true,
},
],
sfdcLoginUrl: SfdcUrl.PRODUCTION,
sourceApiVersion: '50.0',
});
const loginUrl = await Common.getLoginUrl(undefined);
expect(loginUrl).to.equal(SfdcUrl.PRODUCTION);
});
});
describe('custom login url', () => {
const INSTANCE_URL_1 = 'https://example.com';
const INSTANCE_URL_2 = 'https://some.other.com';

it('should return custom login URL if not in a dx project and instanceurl given', async () => {
sandbox.stub(SfdxProject, 'resolve').throwsException();
const loginUrl = await Common.getLoginUrl(INSTANCE_URL_1);
expect(loginUrl).to.equal(INSTANCE_URL_1);
});
it('should return custom login URL if project with property sfdcLoginUrl absent and instanceurl given', async () => {
sandbox.stub(SfdxProject.prototype, 'resolveProjectConfig').resolves({
packageDirectories: [
{
path: 'force-app',
default: true,
},
],
sourceApiVersion: '50.0',
});
const loginUrl = await Common.getLoginUrl(INSTANCE_URL_1);
expect(loginUrl).to.equal(INSTANCE_URL_1);
});
it('should return custom login URL if project with property sfdcLoginUrl present and not equal to production URL', async () => {
sandbox.stub(SfdxProject.prototype, 'resolveProjectConfig').resolves({
packageDirectories: [
{
path: 'force-app',
default: true,
},
],
sfdcLoginUrl: INSTANCE_URL_2,
sourceApiVersion: '50.0',
});
const loginUrl = await Common.getLoginUrl(undefined);
expect(loginUrl).to.equal(INSTANCE_URL_2);
});
it('should return custom login URL 1 if project with property sfdcLoginUrl equal to ciustom url 2', async () => {
sandbox.stub(SfdxProject.prototype, 'resolveProjectConfig').resolves({
packageDirectories: [
{
path: 'force-app',
default: true,
},
],
sfdcLoginUrl: INSTANCE_URL_2,
sourceApiVersion: '50.0',
});
const loginUrl = await Common.getLoginUrl(INSTANCE_URL_1);
expect(loginUrl).to.equal(INSTANCE_URL_1);
});
});
});

0 comments on commit 2f3907a

Please sign in to comment.