-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathauth-helper.test.js
More file actions
185 lines (149 loc) · 6.33 KB
/
auth-helper.test.js
File metadata and controls
185 lines (149 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const { getAccessToken, bearerAuthHandler, setAuthHandler } = require('../../../src/lib/auth-helper')
const { getToken, context } = require('@adobe/aio-lib-ims')
const { CLI } = require('@adobe/aio-lib-ims/src/context')
const { getCliEnv } = require('@adobe/aio-lib-env')
jest.mock('@adobe/aio-lib-ims')
jest.mock('@adobe/aio-lib-env')
describe('getAccessToken', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('should get token using CLI context (default) if current context undefined', async () => {
const mockToken = 'mocked-token'
const mockEnv = 'prod'
getToken.mockResolvedValue(mockToken)
getCliEnv.mockReturnValue(mockEnv)
context.getCurrent.mockResolvedValue(undefined)
const result = await getAccessToken()
expect(context.setCli).toHaveBeenCalledWith({ 'cli.bare-output': true }, false)
expect(getCliEnv).toHaveBeenCalled()
expect(getToken).toHaveBeenCalledWith(CLI)
expect(result).toEqual({ accessToken: mockToken, env: mockEnv })
})
test('should use custom context if set', async () => {
const mockToken = 'mocked-token'
const mockEnv = 'prod'
const customContext = 'custom-context'
getToken.mockResolvedValue(mockToken)
getCliEnv.mockReturnValue(mockEnv)
context.getCurrent.mockResolvedValue(customContext)
const result = await getAccessToken()
expect(context.setCli).not.toHaveBeenCalled()
expect(getToken).toHaveBeenCalledWith(customContext)
expect(result).toEqual({ accessToken: mockToken, env: mockEnv })
})
test('should use cached token when requested', async () => {
const mockToken = 'cached-token'
const mockEnv = 'prod'
const mockContext = { data: { access_token: { token: mockToken } } }
getCliEnv.mockReturnValue(mockEnv)
context.getCurrent.mockResolvedValue(CLI)
context.get.mockResolvedValue(mockContext)
const result = await getAccessToken({ useCachedToken: true })
expect(getToken).not.toHaveBeenCalled()
expect(context.get).toHaveBeenCalledWith(CLI)
expect(result).toEqual({ accessToken: mockToken, env: mockEnv })
})
})
describe('bearerAuthHandler', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('getAuthHeader should return a Bearer token', async () => {
const mockToken = 'mocked-token'
getToken.mockResolvedValue(mockToken)
getCliEnv.mockReturnValue('prod')
context.getCurrent.mockResolvedValue(CLI)
const result = await bearerAuthHandler.getAuthHeader()
expect(result).toBe(`Bearer ${mockToken}`)
})
})
describe('setAuthHandler', () => {
const DEPLOY_SERVICE_ENDPOINTS = {
prod: 'https://deploy-service.app-builder.adp.adobe.io',
stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io'
}
beforeEach(() => {
jest.clearAllMocks()
delete process.env.AIO_DEPLOY_SERVICE_URL
})
test('should set runtime.apihost and runtime.auth_handler when config has runtime', () => {
// test both envs
{
const mockEnv = 'prod'
getCliEnv.mockReturnValue(mockEnv)
const config = { runtime: {} }
const result = setAuthHandler(config)
expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`)
expect(result.runtime.auth_handler).toBe(bearerAuthHandler)
}
{
const mockEnv = 'stage'
getCliEnv.mockReturnValue(mockEnv)
const config = { runtime: {} }
const result = setAuthHandler(config)
expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`)
expect(result.runtime.auth_handler).toBe(bearerAuthHandler)
}
})
test('should set ow.apihost and ow.auth_handler when config has ow', () => {
const mockEnv = 'unknown-env-should-use-prod'
getCliEnv.mockReturnValue(mockEnv)
const config = { ow: {} }
const result = setAuthHandler(config)
expect(result.ow.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/runtime`)
expect(result.ow.auth_handler).toBe(bearerAuthHandler)
})
test('should use custom deploy service URL from environment', () => {
const mockEnv = 'prod'
getCliEnv.mockReturnValue(mockEnv)
const customUrl = 'https://custom-deploy-service.example.com'
process.env.AIO_DEPLOY_SERVICE_URL = customUrl
const config = { runtime: {} }
const result = setAuthHandler(config)
expect(result.runtime.apihost).toBe(`${customUrl}/runtime`)
})
test('should return undefined when config is null or undefined', () => {
expect(setAuthHandler(null)).not.toBeDefined()
expect(setAuthHandler()).not.toBeDefined()
})
test('should return undefined when config has neither runtime nor ow nor web', () => {
const config = { other: {} }
const result = setAuthHandler(config)
expect(result).not.toBeDefined()
})
test('should set web.apihost and web.auth_handler and preserve namespace from ow', () => {
const mockEnv = 'stage'
getCliEnv.mockReturnValue(mockEnv)
const config = { web: {}, ow: { namespace: 'ns' } }
const result = setAuthHandler(config)
expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/cdn-api`)
expect(result.web.auth_handler).toBe(bearerAuthHandler)
expect(result.web.namespace).toBe('ns')
})
test('should use custom deploy service URL from environment for web', () => {
const customUrl = 'https://custom-deploy-service.example.com'
process.env.AIO_DEPLOY_SERVICE_URL = customUrl
getCliEnv.mockReturnValue('prod')
const config = { web: {}, ow: { namespace: 'ns' } }
const result = setAuthHandler(config)
expect(result.web.apihost).toBe(`${customUrl}/cdn-api`)
expect(result.web.auth_handler).toBe(bearerAuthHandler)
expect(result.web.namespace).toBe('ns')
})
test('falls back to prod endpoint for unknown env', () => {
getCliEnv.mockReturnValue('mystery-env')
const config = { web: {}, ow: { namespace: 'ns' } }
const result = setAuthHandler(config)
expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/cdn-api`)
expect(result.web.auth_handler).toBe(bearerAuthHandler)
expect(result.web.namespace).toBe('ns')
})
test('web.namespace=undefined if ow.namespace is not set', () => {
const config = { web: {} }
const result = setAuthHandler(config)
expect(result.web.namespace).toBe(undefined)
expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/cdn-api`)
expect(result.web.auth_handler).toBe(bearerAuthHandler)
})
})