@@ -10,19 +10,15 @@ import {http, HttpResponse} from 'msw';
1010import { setupServer } from 'msw/node' ;
1111import { isolateConfig , restoreConfig } from '@salesforce/b2c-tooling-sdk/test-utils' ;
1212import OrgAudit from '../../../../src/commands/am/orgs/audit.js' ;
13- import { stubCommandConfigAndLogger , stubJsonEnabled , makeCommandThrowOnError } from '../../../helpers/test-setup.js' ;
13+ import {
14+ stubCommandConfigAndLogger ,
15+ stubJsonEnabled ,
16+ makeCommandThrowOnError ,
17+ stubImplicitOAuthStrategy ,
18+ } from '../../../helpers/test-setup.js' ;
1419
1520const TEST_HOST = 'account.test.demandware.com' ;
1621const BASE_URL = `https://${ TEST_HOST } /dw/rest/v1` ;
17- const OAUTH_URL = `https://${ TEST_HOST } /dwsso/oauth2/access_token` ;
18-
19- function createMockJWT ( payload : Record < string , unknown > = { } ) : string {
20- const header = { alg : 'HS256' , typ : 'JWT' } ;
21- const defaultPayload = { sub : 'test-client' , iat : Math . floor ( Date . now ( ) / 1000 ) , ...payload } ;
22- const headerB64 = Buffer . from ( JSON . stringify ( header ) ) . toString ( 'base64url' ) ;
23- const payloadB64 = Buffer . from ( JSON . stringify ( defaultPayload ) ) . toString ( 'base64url' ) ;
24- return `${ headerB64 } .${ payloadB64 } .signature` ;
25- }
2622
2723/**
2824 * Unit tests for org audit command CLI logic.
@@ -103,15 +99,10 @@ describe('org audit', () => {
10399
104100 stubCommandConfigAndLogger ( command ) ;
105101 stubJsonEnabled ( command , true ) ;
102+ // Mock implicit OAuth strategy to avoid browser-based flow
103+ stubImplicitOAuthStrategy ( command ) ;
106104
107105 server . use (
108- http . post ( OAUTH_URL , ( ) => {
109- return HttpResponse . json ( {
110- access_token : createMockJWT ( { sub : 'test-client' } ) ,
111- expires_in : 1800 ,
112- scope : 'sfcc.accountmanager.user.manage' ,
113- } ) ;
114- } ) ,
115106 http . get ( `${ BASE_URL } /organizations/org-123` , ( ) => {
116107 return HttpResponse . json ( mockOrg ) ;
117108 } ) ,
@@ -140,15 +131,10 @@ describe('org audit', () => {
140131 ( command as any ) . flags = { } ;
141132 stubCommandConfigAndLogger ( command ) ;
142133 stubJsonEnabled ( command , false ) ;
134+ // Mock implicit OAuth strategy to avoid browser-based flow
135+ stubImplicitOAuthStrategy ( command ) ;
143136
144137 server . use (
145- http . post ( OAUTH_URL , ( ) => {
146- return HttpResponse . json ( {
147- access_token : createMockJWT ( { sub : 'test-client' } ) ,
148- expires_in : 1800 ,
149- scope : 'sfcc.accountmanager.user.manage' ,
150- } ) ;
151- } ) ,
152138 http . get ( `${ BASE_URL } /organizations/org-123` , ( ) => {
153139 return HttpResponse . json ( mockOrg ) ;
154140 } ) ,
@@ -173,15 +159,10 @@ describe('org audit', () => {
173159
174160 stubCommandConfigAndLogger ( command ) ;
175161 stubJsonEnabled ( command , true ) ;
162+ // Mock implicit OAuth strategy to avoid browser-based flow
163+ stubImplicitOAuthStrategy ( command ) ;
176164
177165 server . use (
178- http . post ( OAUTH_URL , ( ) => {
179- return HttpResponse . json ( {
180- access_token : createMockJWT ( { sub : 'test-client' } ) ,
181- expires_in : 1800 ,
182- scope : 'sfcc.accountmanager.user.manage' ,
183- } ) ;
184- } ) ,
185166 http . get ( `${ BASE_URL } /organizations/org-123` , ( ) => {
186167 return HttpResponse . json ( mockOrg ) ;
187168 } ) ,
@@ -206,14 +187,10 @@ describe('org audit', () => {
206187 stubCommandConfigAndLogger ( command ) ;
207188 stubJsonEnabled ( command , true ) ;
208189
190+ // Mock implicit OAuth strategy to avoid browser-based flow
191+ stubImplicitOAuthStrategy ( command ) ;
192+
209193 server . use (
210- http . post ( OAUTH_URL , ( ) => {
211- return HttpResponse . json ( {
212- access_token : createMockJWT ( { sub : 'test-client' } ) ,
213- expires_in : 1800 ,
214- scope : 'sfcc.accountmanager.user.manage' ,
215- } ) ;
216- } ) ,
217194 http . get ( `${ BASE_URL } /organizations/Test%20Organization` , ( ) => {
218195 return HttpResponse . json ( { error : { message : 'Not found' } } , { status : 404 } ) ;
219196 } ) ,
@@ -241,15 +218,10 @@ describe('org audit', () => {
241218
242219 stubCommandConfigAndLogger ( command ) ;
243220 makeCommandThrowOnError ( command ) ;
221+ // Mock implicit OAuth strategy to avoid browser-based flow
222+ stubImplicitOAuthStrategy ( command ) ;
244223
245224 server . use (
246- http . post ( OAUTH_URL , ( ) => {
247- return HttpResponse . json ( {
248- access_token : createMockJWT ( { sub : 'test-client' } ) ,
249- expires_in : 1800 ,
250- scope : 'sfcc.accountmanager.user.manage' ,
251- } ) ;
252- } ) ,
253225 http . get ( `${ BASE_URL } /organizations/nonexistent-org` , ( ) => {
254226 return HttpResponse . json ( { error : { message : 'Not found' } } , { status : 404 } ) ;
255227 } ) ,
@@ -278,15 +250,10 @@ describe('org audit', () => {
278250 ( command as any ) . flags = { columns : 'timestamp,authorDisplayName,eventType' } ;
279251 stubCommandConfigAndLogger ( command ) ;
280252 stubJsonEnabled ( command , false ) ;
253+ // Mock implicit OAuth strategy to avoid browser-based flow
254+ stubImplicitOAuthStrategy ( command ) ;
281255
282256 server . use (
283- http . post ( OAUTH_URL , ( ) => {
284- return HttpResponse . json ( {
285- access_token : createMockJWT ( { sub : 'test-client' } ) ,
286- expires_in : 1800 ,
287- scope : 'sfcc.accountmanager.user.manage' ,
288- } ) ;
289- } ) ,
290257 http . get ( `${ BASE_URL } /organizations/org-123` , ( ) => {
291258 return HttpResponse . json ( mockOrg ) ;
292259 } ) ,
@@ -312,15 +279,10 @@ describe('org audit', () => {
312279 ( command as any ) . flags = { extended : true } ;
313280 stubCommandConfigAndLogger ( command ) ;
314281 stubJsonEnabled ( command , false ) ;
282+ // Mock implicit OAuth strategy to avoid browser-based flow
283+ stubImplicitOAuthStrategy ( command ) ;
315284
316285 server . use (
317- http . post ( OAUTH_URL , ( ) => {
318- return HttpResponse . json ( {
319- access_token : createMockJWT ( { sub : 'test-client' } ) ,
320- expires_in : 1800 ,
321- scope : 'sfcc.accountmanager.user.manage' ,
322- } ) ;
323- } ) ,
324286 http . get ( `${ BASE_URL } /organizations/org-123` , ( ) => {
325287 return HttpResponse . json ( mockOrg ) ;
326288 } ) ,
@@ -345,6 +307,8 @@ describe('org audit', () => {
345307
346308 stubCommandConfigAndLogger ( command ) ;
347309 stubJsonEnabled ( command , true ) ;
310+ // Mock implicit OAuth strategy to avoid browser-based flow
311+ stubImplicitOAuthStrategy ( command ) ;
348312
349313 const logsWithTimestamps = [
350314 {
@@ -357,13 +321,6 @@ describe('org audit', () => {
357321 ] ;
358322
359323 server . use (
360- http . post ( OAUTH_URL , ( ) => {
361- return HttpResponse . json ( {
362- access_token : createMockJWT ( { sub : 'test-client' } ) ,
363- expires_in : 1800 ,
364- scope : 'sfcc.accountmanager.user.manage' ,
365- } ) ;
366- } ) ,
367324 http . get ( `${ BASE_URL } /organizations/org-123` , ( ) => {
368325 return HttpResponse . json ( mockOrg ) ;
369326 } ) ,
0 commit comments