@@ -49,9 +49,16 @@ describe('org:open', () => {
4949 spies . set ( 'open' , stubMethod ( $$ . SANDBOX , utils , 'openUrl' ) . resolves ( new EventEmitter ( ) ) ) ;
5050 spies . set (
5151 'requestGet' ,
52- stubMethod ( $$ . SANDBOX , Connection . prototype , 'requestGet' ) . resolves ( {
53- // eslint-disable-next-line camelcase
54- frontdoor_uri : expectedDefaultSingleUseUrl ,
52+ stubMethod ( $$ . SANDBOX , Connection . prototype , 'requestGet' ) . callsFake ( ( url : string ) => {
53+ const urlObj = new URL ( url ) ;
54+ const redirectUri = urlObj . searchParams . get ( 'redirect_uri' ) ;
55+ // see API docs: https://help.salesforce.com/s/articleView?id=xcloud.frontdoor_singleaccess.htm&type=5
56+ return Promise . resolve ( {
57+ // eslint-disable-next-line camelcase
58+ frontdoor_uri : redirectUri
59+ ? `${ expectedDefaultSingleUseUrl } &startURL=${ redirectUri } `
60+ : expectedDefaultSingleUseUrl ,
61+ } ) ;
5562 } )
5663 ) ;
5764 } ) ;
@@ -108,7 +115,9 @@ describe('org:open', () => {
108115
109116 it ( '--source-file to flexipage' , async ( ) => {
110117 $$ . SANDBOX . stub ( Connection . prototype , 'singleRecordQuery' ) . resolves ( { Id : '123' } ) ;
111- const mockMetadataUrl = 'visualEditor/appBuilder.app?pageId=123' ;
118+ const mockMetadataUrl = `${ expectedDefaultSingleUseUrl } &startURL=${ encodeURIComponent (
119+ 'visualEditor/appBuilder.app?pageId=123'
120+ ) } `;
112121 $$ . SANDBOX . stub ( Org . prototype , 'getMetadataUIURL' ) . resolves ( mockMetadataUrl ) ;
113122
114123 const response = await OrgOpenCommand . run ( [
@@ -120,11 +129,11 @@ describe('org:open', () => {
120129 flexipagePath ,
121130 ] ) ;
122131
123- expect ( response . url ) . to . include ( 'visualEditor/appBuilder.app?pageId=123' ) ;
132+ expect ( decodeURIComponent ( response . url ) ) . to . include ( 'visualEditor/appBuilder.app?pageId=123' ) ;
124133 } ) ;
125134
126135 it ( '--source-file to an ApexPage' , async ( ) => {
127- const mockMetadataUrl = '/apex/test' ;
136+ const mockMetadataUrl = ` ${ expectedDefaultSingleUseUrl } &startURL= ${ encodeURIComponent ( '/apex/test' ) } ` ;
128137 $$ . SANDBOX . stub ( Org . prototype , 'getMetadataUIURL' ) . resolves ( mockMetadataUrl ) ;
129138
130139 const response = await OrgOpenCommand . run ( [
@@ -135,10 +144,11 @@ describe('org:open', () => {
135144 '--source-file' ,
136145 apexPath ,
137146 ] ) ;
138- expect ( response . url ) . to . include ( '&startURL=/apex/test' ) ;
147+ expect ( decodeURIComponent ( response . url ) ) . to . include ( '&startURL=/apex/test' ) ;
139148 } ) ;
140149
141150 it ( '--source-file when flexipage query errors' , async ( ) => {
151+ $$ . SANDBOX . stub ( Org . prototype , 'getMetadataUIURL' ) . rejects ( new Error ( 'Metadata query failed' ) ) ;
142152 const response = await OrgOpenCommand . run ( [
143153 '--json' ,
144154 '--targetusername' ,
@@ -193,36 +203,6 @@ describe('org:open', () => {
193203 expect ( spies . get ( 'requestGet' ) . callCount ) . to . equal ( 1 ) ;
194204 expect ( spies . get ( 'requestGet' ) . args [ 0 ] [ 0 ] ) . to . deep . equal ( `${ testOrg . instanceUrl } /services/oauth2/singleaccess` ) ;
195205 } ) ;
196-
197- it ( 'handles api error' , async ( ) => {
198- $$ . SANDBOX . restore ( ) ;
199- const mockError = new Error ( 'Invalid_Scope' ) ;
200- mockError . name = 'Invalid_Scope' ;
201- $$ . SANDBOX . stub ( Connection . prototype , 'requestGet' ) . throws ( mockError ) ;
202- try {
203- await OrgOpenCommand . run ( [ '--targetusername' , testOrg . username ] ) ;
204- expect . fail ( 'should have thrown Invalid_Scope' ) ;
205- } catch ( e ) {
206- assert ( e instanceof SfError , 'should be an SfError' ) ;
207- expect ( e . name ) . to . equal ( 'Invalid_Scope' ) ;
208- expect ( e . message ) . to . equal ( sharedMessages . getMessage ( 'SingleAccessFrontdoorError' ) ) ;
209- }
210- } ) ;
211-
212- it ( 'handles invalid responde from api' , async ( ) => {
213- $$ . SANDBOX . restore ( ) ;
214- $$ . SANDBOX . stub ( Connection . prototype , 'requestGet' ) . resolves ( {
215- invalid : 'some invalid response' ,
216- } ) ;
217- try {
218- await OrgOpenCommand . run ( [ '--targetusername' , testOrg . username ] ) ;
219- expect . fail ( 'should have thrown Invalid_Scope' ) ;
220- } catch ( e ) {
221- assert ( e instanceof SfError , 'should be an SfError' ) ;
222- expect ( e . message ) . to . equal ( sharedMessages . getMessage ( 'SingleAccessFrontdoorError' ) ) ;
223- expect ( e . data ) . to . contain ( { invalid : 'some invalid response' } ) ;
224- }
225- } ) ;
226206 } ) ;
227207
228208 describe ( 'domain resolution, with callout' , ( ) => {
@@ -297,7 +277,7 @@ describe('org:open', () => {
297277
298278 await OrgOpenCommand . run ( [ '--targetusername' , testOrg . username , '--path' , testPath , '--urlonly' ] ) ;
299279
300- expect ( sfCommandUxStubs . logSuccess . firstCall . args ) . to . include (
280+ expect ( sfCommandUxStubs . logSuccess . firstCall . args [ 0 ] ) . to . deep . equal (
301281 messages . getMessage ( 'humanSuccess' , [ testOrg . orgId , testOrg . username , expectedSingleUseUrl ] )
302282 ) ;
303283 } ) ;
0 commit comments