Skip to content

Commit cd72d2f

Browse files
committed
chore: added tests
1 parent 6a6001e commit cd72d2f

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

test/utils.test.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,3 +2710,138 @@ describe('getProxyAgent', () => {
27102710
expect(result).toBeInstanceOf(PatchedHttpsProxyAgent)
27112711
})
27122712
})
2713+
2714+
describe('include-ims-credentials annotation', () => {
2715+
const fakeCode = 'fake action code'
2716+
let spy
2717+
let originalEnv
2718+
2719+
beforeEach(() => {
2720+
spy = jest.spyOn(fs, 'readFileSync')
2721+
spy.mockImplementation(() => fakeCode)
2722+
originalEnv = process.env.IMS_OAUTH_S2S
2723+
})
2724+
2725+
afterEach(() => {
2726+
spy.mockRestore()
2727+
libEnv.getCliEnv.mockReturnValue(PROD_ENV)
2728+
if (originalEnv !== undefined) {
2729+
process.env.IMS_OAUTH_S2S = originalEnv
2730+
} else {
2731+
delete process.env.IMS_OAUTH_S2S
2732+
}
2733+
})
2734+
2735+
test('action with include-ims-credentials annotation gets IMS inputs added', () => {
2736+
const imsCredentials = { client_id: 'test-client', client_secret: 'test-secret' }
2737+
process.env.IMS_OAUTH_S2S = JSON.stringify(imsCredentials)
2738+
libEnv.getCliEnv.mockReturnValue(PROD_ENV)
2739+
2740+
const packages = {
2741+
pkg1: {
2742+
actions: {
2743+
theaction: {
2744+
function: 'fake.js',
2745+
web: 'yes',
2746+
inputs: { existingInput: 'value' },
2747+
annotations: {
2748+
'include-ims-credentials': true
2749+
}
2750+
}
2751+
}
2752+
}
2753+
}
2754+
2755+
const res = utils.processPackage(packages, {}, {}, {}, false, { apihost: 'https://adobeioruntime.net' })
2756+
expect(res.actions[0].params).toEqual({
2757+
existingInput: 'value',
2758+
__ims_oauth_s2s: imsCredentials,
2759+
__ims_env: PROD_ENV
2760+
})
2761+
})
2762+
2763+
test('action with include-ims-credentials annotation and no credentials logs warning', () => {
2764+
delete process.env.IMS_OAUTH_S2S
2765+
const loggerSpy = jest.spyOn(aioLogger, 'warn')
2766+
2767+
const packages = {
2768+
pkg1: {
2769+
actions: {
2770+
theaction: {
2771+
function: 'fake.js',
2772+
web: 'yes',
2773+
annotations: {
2774+
'include-ims-credentials': true
2775+
}
2776+
}
2777+
}
2778+
}
2779+
}
2780+
2781+
utils.processPackage(packages, {}, {}, {}, false, { apihost: 'https://adobeioruntime.net' })
2782+
expect(loggerSpy).toHaveBeenCalledWith("The project has no credentials attached (missing the 'IMS_OAUTH_S2S' environment variable). The annotation 'include-ims-credentials' will be ignored.")
2783+
})
2784+
})
2785+
2786+
describe('getIncludeIMSCredentialsAnnotationInputs', () => {
2787+
afterEach(() => {
2788+
libEnv.getCliEnv.mockReturnValue(PROD_ENV)
2789+
})
2790+
2791+
test('returns undefined if annotation is not set', () => {
2792+
const action = {
2793+
annotations: {}
2794+
}
2795+
const result = utils.getIncludeIMSCredentialsAnnotationInputs(action, { client_id: 'test' })
2796+
expect(result).toBeUndefined()
2797+
})
2798+
2799+
test('returns undefined and warns if annotation is set but imsAuthObject is null', () => {
2800+
const action = {
2801+
annotations: { 'include-ims-credentials': true }
2802+
}
2803+
const loggerSpy = jest.spyOn(aioLogger, 'warn')
2804+
const result = utils.getIncludeIMSCredentialsAnnotationInputs(action, null)
2805+
expect(result).toBeUndefined()
2806+
expect(loggerSpy).toHaveBeenCalledWith("The project has no credentials attached (missing the 'IMS_OAUTH_S2S' environment variable). The annotation 'include-ims-credentials' will be ignored.")
2807+
})
2808+
2809+
test('returns inputs with ims credentials and prod env', () => {
2810+
libEnv.getCliEnv.mockReturnValue(PROD_ENV)
2811+
const action = {
2812+
annotations: { 'include-ims-credentials': true }
2813+
}
2814+
const imsAuthObject = { client_id: 'test-client', client_secret: 'test-secret' }
2815+
const result = utils.getIncludeIMSCredentialsAnnotationInputs(action, imsAuthObject)
2816+
expect(result).toEqual({
2817+
__ims_oauth_s2s: { client_id: 'test-client', client_secret: 'test-secret' },
2818+
__ims_env: PROD_ENV
2819+
})
2820+
})
2821+
2822+
test('returns inputs with ims credentials and stage env', () => {
2823+
libEnv.getCliEnv.mockReturnValue(STAGE_ENV)
2824+
const action = {
2825+
annotations: { 'include-ims-credentials': true }
2826+
}
2827+
const imsAuthObject = { client_id: 'test-client' }
2828+
const result = utils.getIncludeIMSCredentialsAnnotationInputs(action, imsAuthObject)
2829+
expect(result).toEqual({
2830+
__ims_oauth_s2s: { client_id: 'test-client' },
2831+
__ims_env: STAGE_ENV
2832+
})
2833+
})
2834+
2835+
test('returns inputs with default env when getCliEnv returns null', () => {
2836+
libEnv.getCliEnv.mockReturnValue(null)
2837+
const action = {
2838+
annotations: { 'include-ims-credentials': true }
2839+
}
2840+
const imsAuthObject = { client_id: 'test-client' }
2841+
const result = utils.getIncludeIMSCredentialsAnnotationInputs(action, imsAuthObject)
2842+
expect(result).toEqual({
2843+
__ims_oauth_s2s: { client_id: 'test-client' },
2844+
__ims_env: PROD_ENV
2845+
})
2846+
})
2847+
})

0 commit comments

Comments
 (0)