Skip to content

Commit ecc5136

Browse files
committed
fix: update UT using frontdoorURL
1 parent 876db34 commit ecc5136

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

messages/messages.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ For additional information, please review the authorization section of the https
66

77
# SingleAccessFrontdoorError
88

9-
Failed to generate a single-use frontdoor URL.
9+
Failed to generate a frontdoor URL.

src/shared/orgOpenUtils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Duration, Env } from '@salesforce/kit';
1313

1414
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1515
const messages = Messages.loadMessages('@salesforce/plugin-org', 'open');
16+
const sharedMessages = Messages.loadMessages('@salesforce/plugin-org', 'messages');
1617

1718
export const openUrl = async (url: string, options: Options): Promise<ChildProcess> => open(url, options);
1819

@@ -23,11 +24,17 @@ export const fileCleanup = (tempFilePath: string): void =>
2324
* This method generates and returns a frontdoor url for the given org.
2425
*
2526
* @param org org for which we generate the frontdoor url.
26-
* @param conn the Connection for the given Org.
27-
* @param singleUseUrl if true returns a single-use url frontdoor url.
2827
*/
2928

30-
export const buildFrontdoorUrl = async (org: Org): Promise<string> => org.getFrontDoorUrl();
29+
export const buildFrontdoorUrl = async (org: Org): Promise<string> => {
30+
try {
31+
return await org.getFrontDoorUrl();
32+
} catch (e) {
33+
if (e instanceof SfError) throw e;
34+
const err = e as Error;
35+
throw new SfError(sharedMessages.getMessage('SingleAccessFrontdoorError'), err.message);
36+
}
37+
};
3138

3239
export const handleDomainError = (err: unknown, url: string, env: Env): string => {
3340
if (err instanceof Error) {

test/unit/org/open.test.ts

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from 'node:fs';
99
import { join } from 'node:path';
1010
import * as process from 'node:process';
1111
import { assert, expect } from 'chai';
12-
import { Connection, Messages, SfdcUrl, SfError } from '@salesforce/core';
12+
import { Connection, Messages, Org, SfdcUrl, SfError } from '@salesforce/core';
1313
import { stubMethod } from '@salesforce/ts-sinon';
1414
import { MockTestOrgData, shouldThrow, TestContext } from '@salesforce/core/testSetup';
1515
import { stubSfCommandUx, stubSpinner, stubUx } from '@salesforce/sf-plugins-core';
@@ -27,10 +27,9 @@ describe('org:open', () => {
2727

2828
const testBrowser = 'firefox';
2929
const testPath = '/lightning/whatever';
30-
const expectedDefaultUrl = `${testOrg.instanceUrl}/secur/frontdoor.jsp?sid=${testOrg.accessToken}`;
31-
const expectedUrl = `${expectedDefaultUrl}&retURL=${encodeURIComponent(testPath)}`;
3230
const singleUseToken = (Math.random() + 1).toString(36).substring(2); // random string to simulate a single-use token
3331
const expectedDefaultSingleUseUrl = `${testOrg.instanceUrl}/secur/frontdoor.jsp?otp=${singleUseToken}`;
32+
const expectedSingleUseUrl = `${expectedDefaultSingleUseUrl}&startURL=${encodeURIComponent(testPath)}`;
3433

3534
let sfCommandUxStubs: ReturnType<typeof stubSfCommandUx>;
3635

@@ -66,7 +65,7 @@ describe('org:open', () => {
6665
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--urlonly']);
6766
assert(response);
6867
testJsonStructure(response);
69-
expect(response.url).to.equal(expectedDefaultUrl);
68+
expect(response.url).to.equal(expectedDefaultSingleUseUrl);
7069
});
7170

7271
it('org with a url is built correctly', async () => {
@@ -79,7 +78,7 @@ describe('org:open', () => {
7978
testPath,
8079
]);
8180
assert(response);
82-
expect(response.url).to.equal(expectedUrl);
81+
expect(response.url).to.equal(expectedSingleUseUrl);
8382
});
8483

8584
describe('--source-file', () => {
@@ -109,6 +108,8 @@ describe('org:open', () => {
109108

110109
it('--source-file to flexipage', async () => {
111110
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({ Id: '123' });
111+
const mockMetadataUrl = 'visualEditor/appBuilder.app?pageId=123';
112+
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL').resolves(mockMetadataUrl);
112113

113114
const response = await OrgOpenCommand.run([
114115
'--json',
@@ -118,10 +119,14 @@ describe('org:open', () => {
118119
'--source-file',
119120
flexipagePath,
120121
]);
122+
121123
expect(response.url).to.include('visualEditor/appBuilder.app?pageId=123');
122124
});
123125

124126
it('--source-file to an ApexPage', async () => {
127+
const mockMetadataUrl = '/apex/test';
128+
$$.SANDBOX.stub(Org.prototype, 'getMetadataUIURL').resolves(mockMetadataUrl);
129+
125130
const response = await OrgOpenCommand.run([
126131
'--json',
127132
'--targetusername',
@@ -130,31 +135,41 @@ describe('org:open', () => {
130135
'--source-file',
131136
apexPath,
132137
]);
133-
expect(response.url).to.include('&retURL=/apex/test');
138+
expect(response.url).to.include('&startURL=/apex/test');
134139
});
135140

136141
it('--source-file when flexipage query errors', async () => {
137-
const response = await OrgOpenCommand.run([
138-
'--json',
139-
'--targetusername',
140-
testOrg.username,
141-
'--urlonly',
142-
'--source-file',
143-
flexipagesDir,
144-
]);
145-
expect(response.url).to.include('lightning/setup/FlexiPageList/home');
142+
try {
143+
await OrgOpenCommand.run([
144+
'--json',
145+
'--targetusername',
146+
testOrg.username,
147+
'--urlonly',
148+
'--source-file',
149+
flexipagesDir,
150+
]);
151+
expect.fail('should have thrown an error');
152+
} catch (e) {
153+
assert(e instanceof Error);
154+
expect(e.message).to.include(`Unable to generate metadata URL for file: ${flexipagesDir}`);
155+
}
146156
});
147157

148158
it('--source-file to neither flexipage or apexpage', async () => {
149-
const response = await OrgOpenCommand.run([
150-
'--json',
151-
'--targetusername',
152-
testOrg.username,
153-
'--urlonly',
154-
'--source-file',
155-
apexDir,
156-
]);
157-
expect(response.url).to.include('lightning/setup/FlexiPageList/home');
159+
try {
160+
await OrgOpenCommand.run([
161+
'--json',
162+
'--targetusername',
163+
testOrg.username,
164+
'--urlonly',
165+
'--source-file',
166+
apexDir,
167+
]);
168+
expect.fail('should have thrown an error');
169+
} catch (e) {
170+
assert(e instanceof Error);
171+
expect(e.message).to.include(`Unable to generate metadata URL for file: ${apexDir}`);
172+
}
158173
});
159174
});
160175

@@ -164,7 +179,8 @@ describe('org:open', () => {
164179
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--urlonly']);
165180
assert(response);
166181
testJsonStructure(response);
167-
expect(response.url).to.equal(expectedUrl);
182+
expect(response.url).to.equal(expectedSingleUseUrl);
183+
expect(spies.get('requestGet').callCount).to.equal(1);
168184
delete process.env.FORCE_OPEN_URL;
169185
});
170186

@@ -174,7 +190,7 @@ describe('org:open', () => {
174190
expect(response.url).to.equal(expectedDefaultSingleUseUrl);
175191
// verify we called to the correct endpoint to generate the single-use AT
176192
expect(spies.get('requestGet').callCount).to.equal(1);
177-
expect(spies.get('requestGet').args[0][0]).to.deep.equal('/services/oauth2/singleaccess');
193+
expect(spies.get('requestGet').args[0][0]).to.deep.equal(`${testOrg.instanceUrl}/services/oauth2/singleaccess`);
178194
});
179195

180196
it('generates a single-use frontdoor url even if --url-only or --json flag are passed in', async () => {
@@ -183,12 +199,13 @@ describe('org:open', () => {
183199
expect(response.url).to.equal(expectedDefaultSingleUseUrl);
184200
// verify we called to the correct endpoint to generate the single-use AT
185201
expect(spies.get('requestGet').callCount).to.equal(1);
186-
expect(spies.get('requestGet').args[0][0]).to.deep.equal('/services/oauth2/singleaccess');
202+
expect(spies.get('requestGet').args[0][0]).to.deep.equal(`${testOrg.instanceUrl}/services/oauth2/singleaccess`);
187203
});
188204

189205
it('handles api error', async () => {
190206
$$.SANDBOX.restore();
191207
const mockError = new Error('Invalid_Scope');
208+
mockError.name = 'Invalid_Scope';
192209
$$.SANDBOX.stub(Connection.prototype, 'requestGet').throws(mockError);
193210
try {
194211
await OrgOpenCommand.run(['--targetusername', testOrg.username]);
@@ -223,7 +240,7 @@ describe('org:open', () => {
223240
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
224241
assert(response);
225242
testJsonStructure(response);
226-
expect(response.url).to.equal(expectedUrl);
243+
expect(response.url).to.equal(expectedSingleUseUrl);
227244

228245
expect(spies.get('resolver').callCount).to.equal(1);
229246
});
@@ -254,7 +271,7 @@ describe('org:open', () => {
254271
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
255272
assert(response);
256273
testJsonStructure(response);
257-
expect(response.url).to.equal(expectedUrl);
274+
expect(response.url).to.equal(expectedSingleUseUrl);
258275
expect(spies.get('resolver').callCount).to.equal(0);
259276
delete process.env.SFDX_CONTAINER_MODE;
260277
});
@@ -265,6 +282,7 @@ describe('org:open', () => {
265282
const response = await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
266283
assert(response);
267284
testJsonStructure(response);
285+
expect(response.url).to.equal(expectedSingleUseUrl);
268286
expect(spies.get('resolver').callCount).to.equal(1);
269287
delete process.env.SF_DOMAIN_RETRY;
270288
});
@@ -273,7 +291,7 @@ describe('org:open', () => {
273291
describe('human output', () => {
274292
it('calls open and outputs proper success message (no url)', async () => {
275293
spies.set('resolver', stubMethod($$.SANDBOX, SfdcUrl.prototype, 'checkLightningDomain').resolves('1.1.1.1'));
276-
await OrgOpenCommand.run(['--targetusername', testOrg.username, '--path', testPath]);
294+
await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
277295

278296
expect(sfCommandUxStubs.logSuccess.firstCall.args).to.include(
279297
messages.getMessage('humanSuccessNoUrl', [testOrg.orgId, testOrg.username])
@@ -288,7 +306,7 @@ describe('org:open', () => {
288306
await OrgOpenCommand.run(['--targetusername', testOrg.username, '--path', testPath, '--urlonly']);
289307

290308
expect(sfCommandUxStubs.logSuccess.firstCall.args).to.include(
291-
messages.getMessage('humanSuccess', [testOrg.orgId, testOrg.username, expectedUrl])
309+
messages.getMessage('humanSuccess', [testOrg.orgId, testOrg.username, expectedSingleUseUrl])
292310
);
293311
});
294312

@@ -313,7 +331,7 @@ describe('org:open', () => {
313331
it('calls open with no browser argument', async () => {
314332
spies.set('resolver', stubMethod($$.SANDBOX, SfdcUrl.prototype, 'checkLightningDomain').resolves('1.1.1.1'));
315333

316-
await OrgOpenCommand.run(['--targetusername', testOrg.username, '--path', testPath]);
334+
await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath]);
317335
expect(
318336
sfCommandUxStubs.logSuccess.calledOnceWith(
319337
messages.getMessage('humanSuccessNoUrl', [testOrg.orgId, testOrg.username])
@@ -329,9 +347,9 @@ describe('org:open', () => {
329347
it('calls open with a browser argument', async () => {
330348
spies.set('resolver', stubMethod($$.SANDBOX, SfdcUrl.prototype, 'checkLightningDomain').resolves('1.1.1.1'));
331349

332-
await OrgOpenCommand.run(['--targetusername', testOrg.username, '--path', testPath, '-b', testBrowser]);
350+
await OrgOpenCommand.run(['--json', '--targetusername', testOrg.username, '--path', testPath, '-b', testBrowser]);
333351

334-
expect(sfCommandUxStubs.warn(sharedMessages.getMessage('SecurityWarning')));
352+
expect(sfCommandUxStubs.warn.calledOnceWith(sharedMessages.getMessage('SecurityWarning')));
335353
expect(
336354
sfCommandUxStubs.logSuccess.calledOnceWith(
337355
messages.getMessage('humanSuccessNoUrl', [testOrg.orgId, testOrg.username])

0 commit comments

Comments
 (0)