-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathauth-helper.test.js
More file actions
169 lines (138 loc) · 5.81 KB
/
auth-helper.test.js
File metadata and controls
169 lines (138 loc) · 5.81 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
const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler, getTokenData } = 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('getTokenData', () => {
test('should decode JWT token and return payload', () => {
// Example JWT token with payload: {"user_id":"12345","name":"Test User"}
const exampleToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzNDUiLCJuYW1lIjoiVGVzdCBVc2VyIn0.sflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
const result = getTokenData(exampleToken)
expect(result).toEqual({ user_id: '12345', name: 'Test User' })
})
test('should return null for invalid token', () => {
const invalidToken = 'invalid.token.string'
const result = getTokenData(invalidToken)
expect(result).toBeNull()
})
test('should return null for malformed token', () => {
const malformedToken = 'malformedtoken'
const result = getTokenData(malformedToken)
expect(result).toBeNull()
})
test('should return null for non-string token', () => {
const nonStringToken = 12345
const result = getTokenData(nonStringToken)
expect(result).toBeNull()
})
})
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('setRuntimeApiHostAndAuthHandler', () => {
const DEPLOY_SERVICE_ENDPOINTS = {
prod: 'https://deploy-service.app-builder.adp.adobe.io',
stage: 'https://deploy-service.stg.app-builder.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 = setRuntimeApiHostAndAuthHandler(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 = setRuntimeApiHostAndAuthHandler(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 = setRuntimeApiHostAndAuthHandler(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 = setRuntimeApiHostAndAuthHandler(config)
expect(result.runtime.apihost).toBe(`${customUrl}/runtime`)
})
test('should return undefined when config is null or undefined', () => {
expect(setRuntimeApiHostAndAuthHandler(null)).not.toBeDefined()
expect(setRuntimeApiHostAndAuthHandler()).not.toBeDefined()
})
test('should return undefined when config has neither runtime nor ow', () => {
const config = { other: {} }
const result = setRuntimeApiHostAndAuthHandler(config)
expect(result).not.toBeDefined()
})
})