-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathimpersonation.test.ts
More file actions
68 lines (63 loc) · 2.44 KB
/
impersonation.test.ts
File metadata and controls
68 lines (63 loc) · 2.44 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
import mockAxios from 'axios'
import { Tokens, Products } from '../src'
import { makeToken } from './utils'
const apiUrl = 'https://api.ordercloud.io/v1'
const testdata = {
productID: 'my-mock-product-id',
}
beforeEach(() => {
jest.clearAllMocks() // cleans up any tracked calls before the next test
Tokens.RemoveImpersonationToken()
})
test('should use impersonation token if call As method', async () => {
const impersonationToken = makeToken()
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c3IiOiJ0ZXN0YnV5ZXIiLCJjaWQiOiI5N2JiZjJjYy01OWQxLTQ0OWEtYjY3Yy1hZTkyNjJhZGQyODQiLCJ1IjoiMTkyMDU2MyIsInVzcnR5cGUiOiJidXllciIsInJvbGUiOlsiTWVBZGRyZXNzQWRtaW4iLCJNZUFkbWluIiwiTWVDcmVkaXRDYXJkQWRtaW4iLCJNZVhwQWRtaW4iLCJTaG9wcGVyIiwiQnV5ZXJSZWFkZXIiXSwiaXNzIjoiaHR0cHM6Ly9hdXRoLm9yZGVyY2xvdWQuaW8iLCJhdWQiOiJodHRwczovL2FwaS5vcmRlcmNsb3VkLmlvIiwiZXhwIjoxNTY1NDE2Njg1LCJuYmYiOjE1NjUzODA2ODV9.Fa35Zwz3dsolWgb2X2T4119RxZAGQiE2NoeRNeLaUek'
Tokens.SetImpersonationToken(impersonationToken)
await Products.As().Delete(testdata.productID)
expect(mockAxios.delete).toHaveBeenCalledTimes(1)
expect(mockAxios.delete).toHaveBeenCalledWith(
`${apiUrl}/products/${testdata.productID}`,
{
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${impersonationToken}`,
},
}
)
})
test('should use passed in token if defined', async () => {
const token = makeToken()
await Products.Delete(testdata.productID, { accessToken: token })
expect(mockAxios.delete).toHaveBeenCalledTimes(1)
expect(mockAxios.delete).toHaveBeenCalledWith(
`${apiUrl}/products/${testdata.productID}`,
{
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
}
)
})
test('should prioritize passed in token', async () => {
const impersonationToken = makeToken()
Tokens.SetImpersonationToken(impersonationToken)
const token = makeToken()
await Products.As().Delete(testdata.productID, { accessToken: token })
expect(mockAxios.delete).toHaveBeenCalledTimes(1)
expect(mockAxios.delete).toHaveBeenCalledWith(
`${apiUrl}/products/${testdata.productID}`,
{
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
}
)
})