@@ -1231,6 +1231,12 @@ describe('invokeSequence', () => {
12311231} )
12321232
12331233describe ( 'runDev' , ( ) => {
1234+ const originalEnv = process . env
1235+
1236+ afterEach ( ( ) => {
1237+ process . env = { ...originalEnv }
1238+ } )
1239+
12341240 test ( 'no front end, no back end' , async ( ) => {
12351241 const actionPath = fixturePath ( 'actions/successNoReturnAction.js' )
12361242 const config = createConfig ( {
@@ -1253,6 +1259,32 @@ describe('runDev', () => {
12531259 expect ( Object . keys ( actionUrls ) . length ) . toEqual ( 0 )
12541260 } )
12551261
1262+ test ( 'calls loadIMSCredentialsFromEnv for include-ims-credentials support' , async ( ) => {
1263+ const rtLib = jest . requireActual ( '@adobe/aio-lib-runtime' )
1264+ const loadIMSCredentialsFromEnvSpy = jest . spyOn ( rtLib . utils , 'loadIMSCredentialsFromEnv' )
1265+
1266+ const actionPath = fixturePath ( 'actions/successNoReturnAction.js' )
1267+ const config = createConfig ( {
1268+ hasFrontend : false ,
1269+ hasBackend : true ,
1270+ packageName : 'mypackage' ,
1271+ actions : {
1272+ myaction : {
1273+ function : actionPath
1274+ }
1275+ }
1276+ } )
1277+ const runOptions = createRunOptions ( { cert : 'my-cert' , key : 'my-key' } )
1278+ const hookRunner = ( ) => { }
1279+ const { actionUrls, serverCleanup } = await runDev ( runOptions , config , hookRunner )
1280+
1281+ await serverCleanup ( )
1282+
1283+ expect ( loadIMSCredentialsFromEnvSpy ) . toHaveBeenCalled ( )
1284+ expect ( Object . keys ( actionUrls ) . length ) . toBeGreaterThan ( 0 )
1285+ loadIMSCredentialsFromEnvSpy . mockRestore ( )
1286+ } )
1287+
12561288 test ( 'no front end, has back end' , async ( ) => {
12571289 const actionPath = fixturePath ( 'actions/successNoReturnAction.js' )
12581290 const config = createConfig ( {
@@ -1706,6 +1738,101 @@ describe('invokeAction', () => {
17061738 statusCode : 400
17071739 } )
17081740 } )
1741+
1742+ describe ( 'include-ims-credentials annotation' , ( ) => {
1743+ const rtLib = jest . requireActual ( '@adobe/aio-lib-runtime' )
1744+ let getIncludeIMSCredentialsAnnotationInputsSpy
1745+
1746+ beforeEach ( ( ) => {
1747+ getIncludeIMSCredentialsAnnotationInputsSpy = jest . spyOn ( rtLib . utils , 'getIncludeIMSCredentialsAnnotationInputs' )
1748+ } )
1749+
1750+ afterEach ( ( ) => {
1751+ getIncludeIMSCredentialsAnnotationInputsSpy . mockRestore ( )
1752+ } )
1753+
1754+ test ( 'adds IMS credentials to params when getIncludeIMSCredentialsAnnotationInputs returns inputs' , async ( ) => {
1755+ const packageName = 'foo'
1756+ const actionPath = fixturePath ( 'actions/successReturnAction.js' )
1757+ const actionLoader = createActionLoader ( actionPath )
1758+
1759+ const action = {
1760+ function : actionPath ,
1761+ annotations : {
1762+ 'include-ims-credentials' : true
1763+ }
1764+ }
1765+ const actionParams = { existingParam : 'value' }
1766+ const actionName = 'a'
1767+ const actionConfig = {
1768+ [ packageName ] : {
1769+ actions : {
1770+ [ actionName ] : action
1771+ }
1772+ }
1773+ }
1774+
1775+ // Mock the function to return IMS credentials
1776+ const mockImsInputs = {
1777+ __ims_oauth_s2s : { client_id : 'mock-access-token' , org_id : 'mock-org-id' } ,
1778+ __ims_env : 'stage'
1779+ }
1780+ getIncludeIMSCredentialsAnnotationInputsSpy . mockReturnValue ( mockImsInputs )
1781+
1782+ const actionRequestContext = {
1783+ contextActionLoader : actionLoader ,
1784+ contextItem : action ,
1785+ contextItemParams : actionParams ,
1786+ contextItemName : actionName ,
1787+ packageName,
1788+ actionConfig
1789+ }
1790+ const response = await invokeAction ( { actionRequestContext, logger : mockLogger } )
1791+
1792+ expect ( getIncludeIMSCredentialsAnnotationInputsSpy ) . toHaveBeenCalledWith ( action , expect . anything ( ) )
1793+ expect ( actionParams . __ims_oauth_s2s ) . toEqual ( { client_id : 'mock-access-token' , org_id : 'mock-org-id' } )
1794+ expect ( actionParams . __ims_env ) . toEqual ( 'stage' )
1795+ expect ( actionParams . existingParam ) . toEqual ( 'value' )
1796+ expect ( mockLogger . debug ) . toHaveBeenCalledWith ( `Added IMS credentials to action params for action '${ actionName } '.` )
1797+ expect ( response . statusCode ) . toEqual ( 200 )
1798+ } )
1799+
1800+ test ( 'does not add IMS credentials when getIncludeIMSCredentialsAnnotationInputs returns null' , async ( ) => {
1801+ const packageName = 'foo'
1802+ const actionPath = fixturePath ( 'actions/successReturnAction.js' )
1803+ const actionLoader = createActionLoader ( actionPath )
1804+
1805+ const action = { function : actionPath }
1806+ const actionParams = { existingParam : 'value' }
1807+ const actionName = 'a'
1808+ const actionConfig = {
1809+ [ packageName ] : {
1810+ actions : {
1811+ [ actionName ] : action
1812+ }
1813+ }
1814+ }
1815+
1816+ // Mock the function to return null (no annotation or no IMS auth object)
1817+ getIncludeIMSCredentialsAnnotationInputsSpy . mockReturnValue ( null )
1818+
1819+ const actionRequestContext = {
1820+ contextActionLoader : actionLoader ,
1821+ contextItem : action ,
1822+ contextItemParams : actionParams ,
1823+ contextItemName : actionName ,
1824+ packageName,
1825+ actionConfig
1826+ }
1827+ const response = await invokeAction ( { actionRequestContext, logger : mockLogger } )
1828+
1829+ expect ( getIncludeIMSCredentialsAnnotationInputsSpy ) . toHaveBeenCalledWith ( action , expect . anything ( ) )
1830+ expect ( actionParams . __ims_oauth_s2s ) . toBeUndefined ( )
1831+ expect ( actionParams . __ims_env ) . toBeUndefined ( )
1832+ expect ( actionParams . existingParam ) . toEqual ( 'value' )
1833+ expect ( response . statusCode ) . toEqual ( 200 )
1834+ } )
1835+ } )
17091836} )
17101837
17111838describe ( 'defaultActionLoader' , ( ) => {
0 commit comments