Skip to content

Commit

Permalink
fix: test for lightning instance url
Browse files Browse the repository at this point in the history
  • Loading branch information
maggiben committed Apr 5, 2021
1 parent a8a8f33 commit d997e96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion messages/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"warnAuth": "Logging in to a business or production org is not recommended on a demo or shared machine. Please run \"sfdx auth:logout --targetusername <your username> --noprompt\" when finished using this org, which is similar to logging out of the org in the browser.\n\nDo you want to authorize this org for use with the Salesforce CLI (y/n)?",
"noPromptAuth": "do not prompt for auth confirmation in demo mode",
"disableMasking": "disable masking of user input (for use with problematic terminals)",
"clientSecretStdin": "OAuth client secret of personal connected app?"
"clientSecretStdin": "OAuth client secret of personal connected app?",
"invalidInstanceUrl": "Invalid instance URL. Specify a Salesforce instance URL using the format <domainname>.salesforce.com"
}
9 changes: 8 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

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

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');

interface Flags {
setalias?: string;
setdefaultdevhubusername?: boolean;
Expand Down Expand Up @@ -40,6 +43,10 @@ export class Common {
logger.debug(`error occurred while trying to determine loginUrl: ${message}`);
loginUrl = SfdcUrl.PRODUCTION;
}
if (loginUrl.includes('lightning.force.com')) {
logger.warn(messages.getMessage('invalidInstanceUrl'));
throw new SfdxError(messages.getMessage('invalidInstanceUrl'), 'URL_WARNING');
}
logger.debug(`loginUrl: ${loginUrl}`);
return loginUrl;
}
Expand Down
20 changes: 19 additions & 1 deletion test/common.test.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 { SfdcUrl, SfdxProject } from '@salesforce/core';
import { SfdcUrl, SfdxProject, SfdxError } from '@salesforce/core';
import sinon = require('sinon');
import { expect } from '@salesforce/command/lib/test';
import { restoreContext, testSetup } from '@salesforce/core/lib/testSetup';
Expand Down Expand Up @@ -56,6 +56,24 @@ describe('common unit tests', () => {
const loginUrl = await Common.resolveLoginUrl(undefined);
expect(loginUrl).to.equal(SfdcUrl.PRODUCTION);
});
it('should throw on lightning login URL', async () => {
sandbox.stub(SfdxProject.prototype, 'resolveProjectConfig').resolves({
packageDirectories: [
{
path: 'force-app',
default: true,
},
],
sfdcLoginUrl: 'https://shanedevhub.lightning.force.com',
sourceApiVersion: '50.0',
});
try {
await Common.resolveLoginUrl(undefined);
} catch (error) {
const err = error as SfdxError;
expect(err.name).to.equal('URL_WARNING');
}
});
});
describe('custom login url', () => {
const INSTANCE_URL_1 = 'https://example.com';
Expand Down

0 comments on commit d997e96

Please sign in to comment.