@@ -15,8 +15,6 @@ const { Command } = require('@oclif/core')
1515const { PropertyEnv } = require ( '../src/properties' )
1616const RuntimeLib = require ( '@adobe/aio-lib-runtime' )
1717const OpenWhiskError = require ( 'openwhisk/lib/openwhisk_error' )
18- const { getToken, context } = require ( '@adobe/aio-lib-ims' )
19- const { getCliEnv } = require ( '@adobe/aio-lib-env' )
2018
2119jest . mock ( '@adobe/aio-lib-ims' , ( ) => ( {
2220 getToken : jest . fn ( ) ,
@@ -223,13 +221,26 @@ describe('instance methods', () => {
223221 } )
224222
225223 describe ( 'ow' , ( ) => {
224+ beforeEach ( ( ) => {
225+ RuntimeLib . init . mockClear ( )
226+ } )
227+
226228 test ( 'is a function' , async ( ) => {
227229 expect ( command . wsk ) . toBeInstanceOf ( Function )
228230 } )
229231
230232 test ( 'returns a promise' , ( ) => {
233+ RuntimeLib . init . mockReturnValue ( { } )
231234 return command . wsk ( ) . then ( ( ow ) => {
232- expect ( ow ) . toBe ( ow )
235+ expect ( ow ) . toBeDefined ( )
236+ } )
237+ } )
238+
239+ test ( 'returns a promise (pass options)' , ( ) => {
240+ RuntimeLib . init . mockReturnValue ( { } )
241+ const options = { useRuntimeAuth : true }
242+ return command . wsk ( options ) . then ( ( ow ) => {
243+ expect ( ow ) . toBeDefined ( )
233244 } )
234245 } )
235246
@@ -334,158 +345,4 @@ describe('instance methods', () => {
334345 expect ( command . error ) . toHaveBeenCalledWith ( 'msg' + suffix )
335346 } )
336347 } )
337-
338- describe ( 'authHandler' , ( ) => {
339- describe ( 'when IS_DEPLOY_SERVICE_ENABLED = true' , ( ) => {
340- beforeEach ( ( ) => {
341- process . env . IS_DEPLOY_SERVICE_ENABLED = true
342- } )
343-
344- afterEach ( ( ) => {
345- process . env . IS_DEPLOY_SERVICE_ENABLED = false
346- } )
347- test ( 'No Options : should return the correct Authorization header using getAuthHeader' , async ( ) => {
348- const mockToken = 'mock-access-token'
349- getToken . mockResolvedValue ( mockToken )
350-
351- // Spy on runtimeLib.init to capture options before it's used
352- let capturedOptions
353- RuntimeLib . init . mockImplementation ( async ( options ) => {
354- capturedOptions = options // Store options for later verification
355- return { } // Mock runtimeLib.init() return value
356- } )
357-
358- // Call wsk() which internally sets auth_handler
359- await command . wsk ( )
360-
361- // Ensure options were captured
362- expect ( capturedOptions ) . toBeDefined ( )
363- expect ( capturedOptions . auth_handler ) . toBeDefined ( )
364- expect ( capturedOptions . apihost ) . toBeDefined ( )
365- expect ( capturedOptions . apihost ) . toBe ( 'some.host' )
366-
367- // Call getAuthHeader() from captured options
368- const authHeader = await capturedOptions . auth_handler . getAuthHeader ( )
369-
370- expect ( context . setCli ) . toHaveBeenCalledWith ( { 'cli.bare-output' : true } , false )
371- expect ( getCliEnv ) . toHaveBeenCalled ( )
372- expect ( getToken ) . toHaveBeenCalled ( )
373- expect ( authHeader ) . toBe ( `Bearer ${ mockToken } ` )
374- } )
375-
376- test ( 'With Options : should return the correct Authorization header using getAuthHeader' , async ( ) => {
377- const mockToken = 'mock-access-token'
378- getToken . mockResolvedValue ( mockToken )
379-
380- const options = {
381- auth_handler : {
382- getAuthHeader : async ( ) => `Bearer ${ mockToken } `
383- } ,
384- apihost : 'https://custom-api.adobe.com'
385- }
386-
387- await command . wsk ( options ) // Call wsk() with an existing options object
388-
389- expect ( RuntimeLib . init ) . toHaveBeenCalledWith ( options )
390- } )
391-
392- test ( 'Default OW Host testing' , async ( ) => {
393- delete process . env [ PropertyEnv . APIHOST ]
394-
395- const mockToken = 'mock-access-token'
396- getToken . mockResolvedValue ( mockToken )
397-
398- command . getOptions = jest . fn ( ) . mockResolvedValue ( { } )
399-
400- // Mock runtimeLib.init to track its calls
401- const mockInit = jest . fn ( ) . mockResolvedValue ( { } )
402- RuntimeLib . init = mockInit
403-
404- // Call wsk() without options
405- await command . wsk ( )
406-
407- // Assertions
408- expect ( RuntimeLib . init ) . toHaveBeenCalled ( )
409-
410- // Verify the passed options contain the default apihost
411- const optionsPassedToInit = mockInit . mock . calls [ 0 ] [ 0 ] // Get the options passed to init
412- expect ( optionsPassedToInit . apihost ) . toBe ( 'https://adobeioruntime.net' )
413-
414- // Ensure the Authorization header is set correctly
415- expect ( optionsPassedToInit . auth_handler ) . toBeDefined ( )
416- const authHeader = await optionsPassedToInit . auth_handler . getAuthHeader ( )
417- expect ( authHeader ) . toBe ( `Bearer ${ mockToken } ` )
418- } )
419- } )
420-
421- describe ( 'when IS_DEPLOY_SERVICE_ENABLED = false' , ( ) => {
422- beforeEach ( ( ) => {
423- process . env . IS_DEPLOY_SERVICE_ENABLED = false
424- } )
425-
426- test ( 'No Options : should return the correct Authorization header using getAuthHeader' , async ( ) => {
427- const mockToken = 'mock-access-token'
428- getToken . mockResolvedValue ( mockToken )
429-
430- // Spy on runtimeLib.init to capture options before it's used
431- let capturedOptions
432- RuntimeLib . init . mockImplementation ( async ( options ) => {
433- capturedOptions = options // Store options for later verification
434- return { } // Mock runtimeLib.init() return value
435- } )
436-
437- // Call wsk() which internally sets auth_handler
438- await command . wsk ( )
439-
440- // Ensure options were captured
441- expect ( capturedOptions ) . toBeDefined ( )
442- expect ( capturedOptions . auth_handler ) . not . toBeDefined ( )
443- expect ( capturedOptions . apihost ) . toBeDefined ( )
444- expect ( capturedOptions . apihost ) . toBe ( 'some.host' )
445- } )
446-
447- test ( 'With Options : should return the correct Authorization header using getAuthHeader' , async ( ) => {
448- const mockToken = 'mock-access-token'
449- getToken . mockResolvedValue ( mockToken )
450-
451- const options = {
452- auth_handler : {
453- getAuthHeader : async ( ) => `Bearer ${ mockToken } `
454- } ,
455- apihost : 'https://custom-api.adobe.com'
456- }
457-
458- await command . wsk ( options ) // Call wsk() with an existing options object
459-
460- expect ( RuntimeLib . init ) . toHaveBeenCalledWith ( options )
461- } )
462-
463- test ( 'Default OW Host testing' , async ( ) => {
464- delete process . env [ PropertyEnv . APIHOST ]
465-
466- const mockToken = 'mock-access-token'
467- getToken . mockResolvedValue ( mockToken )
468-
469- // command.getOptions = jest.fn().mockResolvedValue({})
470-
471- // Mock runtimeLib.init to track its calls
472- const mockInit = jest . fn ( ) . mockResolvedValue ( { } )
473- RuntimeLib . init = mockInit
474-
475- // Call wsk() without options
476- await command . wsk ( )
477-
478- // Assertions
479- expect ( RuntimeLib . init ) . toHaveBeenCalled ( )
480-
481- // Verify the passed options contain the default apihost
482- const optionsPassedToInit = mockInit . mock . calls [ 0 ] [ 0 ] // Get the options passed to init
483- expect ( optionsPassedToInit . apihost ) . toBe ( 'some.host' )
484- expect ( optionsPassedToInit . namespace ) . toBe ( 'some_namespace' )
485-
486- // Ensure the Authorization header is set correctly
487- expect ( optionsPassedToInit . auth_handler ) . not . toBeDefined ( )
488- } )
489- } )
490- } )
491348} )
0 commit comments