-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathsession.test.ts
More file actions
330 lines (264 loc) · 10.8 KB
/
Copy pathsession.test.ts
File metadata and controls
330 lines (264 loc) · 10.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {
ensureAuthenticatedAdmin,
ensureAuthenticatedAdminAsApp,
ensureAuthenticatedAppManagementAndBusinessPlatform,
ensureAuthenticatedBusinessPlatform,
ensureAuthenticatedPartners,
ensureAuthenticatedStorefront,
ensureAuthenticatedThemes,
setLastSeenUserId,
} from './session.js'
import {getAppAutomationToken} from './environment.js'
import {shopifyFetch} from './http.js'
import {ensureAuthenticated, setLastSeenAuthMethod, setLastSeenUserIdAfterAuth} from '../../private/node/session.js'
import {ApplicationToken} from '../../private/node/session/schema.js'
import {
exchangeCustomPartnerToken,
exchangeAppAutomationTokenForAppManagementAccessToken,
exchangeAppAutomationTokenForBusinessPlatformAccessToken,
} from '../../private/node/session/exchange.js'
import {vi, describe, expect, test} from 'vitest'
const futureDate = new Date(2022, 1, 1, 11)
const partnersToken: ApplicationToken = {
accessToken: 'custom_partners_token',
expiresAt: futureDate,
scopes: ['scope2'],
}
vi.mock('../../private/node/session.js')
vi.mock('../../private/node/session/exchange.js')
vi.mock('./environment.js')
vi.mock('./http.js')
describe('store command analytics session helpers', () => {
test('sets last seen user id through the public session helper', () => {
setLastSeenUserId('store-user-id')
expect(setLastSeenUserIdAfterAuth).toHaveBeenCalledWith('store-user-id')
})
})
describe('ensureAuthenticatedStorefront', () => {
test('returns only storefront token if success', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({storefront: 'storefront_token', userId: '1234-5678'})
// When
const got = await ensureAuthenticatedStorefront()
// Then
expect(got).toEqual('storefront_token')
expect(setLastSeenAuthMethod).not.toBeCalled()
expect(setLastSeenUserIdAfterAuth).not.toBeCalled()
})
test('returns the password if provided, and auth method is custom_app_token', async () => {
// Given/When
const got = await ensureAuthenticatedStorefront([], 'theme_access_password')
// Then
expect(got).toEqual('theme_access_password')
expect(setLastSeenAuthMethod).toBeCalledWith('custom_app_token')
expect(setLastSeenUserIdAfterAuth).toBeCalledWith('21534e73-fdc5-9bd3-8c9f-3f45815510a7')
})
test('returns the password if provided, and auth method is theme_access_token', async () => {
// Given/When
const got = await ensureAuthenticatedStorefront([], 'shptka_theme_access_password')
// Then
expect(got).toEqual('shptka_theme_access_password')
expect(setLastSeenAuthMethod).toBeCalledWith('theme_access_token')
expect(setLastSeenUserIdAfterAuth).toBeCalledWith('b7d6d99f-3f60-301f-71b8-3108eacc993e')
})
test('throws error if there is no storefront token', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({partners: 'partners_token', userId: '1234-5678'})
// When
const got = ensureAuthenticatedStorefront()
// Then
await expect(got).rejects.toThrow(`No storefront token`)
})
})
describe('ensureAuthenticatedAdmin', () => {
test('returns only admin token if success', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({
admin: {token: 'admin_token', storeFqdn: 'mystore.myshopify.com'},
userId: '1234-5678',
})
// When
const got = await ensureAuthenticatedAdmin('mystore')
// Then
expect(got).toEqual({token: 'admin_token', storeFqdn: 'mystore.myshopify.com'})
})
test('throws error if there is no token', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({partners: 'partners_token', userId: '1234-5678'})
// When
const got = ensureAuthenticatedAdmin('mystore')
// Then
await expect(got).rejects.toThrow(`No admin token`)
})
})
describe('ensureAuthenticatedPartners', () => {
test('returns only partners token if success', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({partners: 'partners_token', userId: '1234-5678'})
// When
const got = await ensureAuthenticatedPartners()
// Then
expect(got).toEqual({token: 'partners_token', userId: '1234-5678'})
})
test('throws error if there is no partners token', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({userId: '1234-5678'})
// When
const got = ensureAuthenticatedPartners()
// Then
await expect(got).rejects.toThrow(`No partners token`)
})
test('returns custom partners token if envvar is defined', async () => {
// Given
vi.mocked(exchangeCustomPartnerToken).mockResolvedValueOnce({
accessToken: partnersToken.accessToken,
userId: '575e2102-cb13-7bea-4631-ce3469eac491cdcba07d',
})
vi.mocked(getAppAutomationToken).mockReturnValue('custom_cli_token')
// When
const got = await ensureAuthenticatedPartners([])
// Then
expect(got).toEqual({token: 'custom_partners_token', userId: '575e2102-cb13-7bea-4631-ce3469eac491cdcba07d'})
expect(ensureAuthenticated).not.toHaveBeenCalled()
})
})
describe('ensureAuthenticatedTheme', () => {
test('returns admin token when no password is provided', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({
admin: {token: 'admin_token', storeFqdn: 'mystore.myshopify.com'},
userId: '1234-5678',
})
// When
const got = await ensureAuthenticatedThemes('mystore', undefined)
// Then
expect(got).toEqual({token: 'admin_token', storeFqdn: 'mystore.myshopify.com'})
expect(setLastSeenAuthMethod).not.toBeCalled()
expect(setLastSeenUserIdAfterAuth).not.toBeCalled()
})
test('throws error if there is no token when no password is provided', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({userId: ''})
// When
const got = ensureAuthenticatedThemes('mystore', undefined)
// Then
await expect(got).rejects.toThrow(`No admin token`)
})
test('returns the password when is provided and custom_app_token', async () => {
// When
const got = await ensureAuthenticatedThemes('mystore.myshopify.com', 'password')
// Then
expect(got).toEqual({token: 'password', storeFqdn: 'mystore.myshopify.com'})
expect(setLastSeenAuthMethod).toBeCalledWith('custom_app_token')
expect(setLastSeenUserIdAfterAuth).toBeCalledWith('18a8698d-f12b-f2db-4737-cecd09bb2c1e')
})
test('returns the password when is provided and theme_access_token', async () => {
// When
const got = await ensureAuthenticatedThemes('mystore.myshopify.com', 'shptka_password')
// Then
expect(got).toEqual({token: 'shptka_password', storeFqdn: 'mystore.myshopify.com'})
expect(setLastSeenAuthMethod).toBeCalledWith('theme_access_token')
expect(setLastSeenUserIdAfterAuth).toBeCalledWith('aea5e074-48e7-cb2a-4b3b-6cebbb5d6f26')
})
})
describe('ensureAuthenticatedBusinessPlatform', () => {
test('returns only business-platform token if success', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({businessPlatform: 'business_platform', userId: '1234-5678'})
// When
const got = await ensureAuthenticatedBusinessPlatform()
// Then
expect(got).toEqual('business_platform')
})
test('throws error if there is no business_platform token', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({partners: 'partners_token', userId: '1234-5678'})
// When
const got = ensureAuthenticatedBusinessPlatform()
// Then
await expect(got).rejects.toThrow(`No business-platform token`)
})
})
describe('ensureAuthenticatedAppManagementAndBusinessPlatform', () => {
test('returns app management and business platform tokens if success', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({
appManagement: 'app_management_token',
businessPlatform: 'business_platform_token',
userId: '1234-5678',
})
// When
const got = await ensureAuthenticatedAppManagementAndBusinessPlatform()
// Then
expect(got).toEqual({
appManagementToken: 'app_management_token',
businessPlatformToken: 'business_platform_token',
userId: '1234-5678',
})
})
test('throws error if there are no tokens', async () => {
// Given
vi.mocked(ensureAuthenticated).mockResolvedValueOnce({userId: '1234-5678'})
// When
const got = ensureAuthenticatedAppManagementAndBusinessPlatform()
// Then
await expect(got).rejects.toThrow('No App Management or Business Platform token found after ensuring authenticated')
})
test('returns app managment and business platform tokens if CLI token envvar is defined', async () => {
// Given
vi.mocked(getAppAutomationToken).mockReturnValue('custom_cli_token')
vi.mocked(exchangeAppAutomationTokenForAppManagementAccessToken).mockResolvedValueOnce({
accessToken: 'app-management-token',
userId: '575e2102-cb13-7bea-4631-ce3469eac491cdcba07d',
})
vi.mocked(exchangeAppAutomationTokenForBusinessPlatformAccessToken).mockResolvedValueOnce({
accessToken: 'business-platform-token',
userId: '575e2102-cb13-7bea-4631-ce3469eac491cdcba07d',
})
// When
const got = await ensureAuthenticatedAppManagementAndBusinessPlatform()
// Then
expect(got).toEqual({
appManagementToken: 'app-management-token',
userId: '575e2102-cb13-7bea-4631-ce3469eac491cdcba07d',
businessPlatformToken: 'business-platform-token',
})
expect(ensureAuthenticated).not.toHaveBeenCalled()
})
})
describe('ensureAuthenticatedAdminAsApp', () => {
test('returns admin token if success', async () => {
// Given
vi.mocked(shopifyFetch).mockResolvedValueOnce({
status: 200,
text: async () => JSON.stringify({access_token: 'app_access_token'}),
} as any)
// When
const got = await ensureAuthenticatedAdminAsApp('mystore.myshopify.com', 'client123', 'secret456')
// Then
expect(got).toEqual({token: 'app_access_token', storeFqdn: 'mystore.myshopify.com'})
})
test('throws error if app is not installed', async () => {
// Given
vi.mocked(shopifyFetch).mockResolvedValueOnce({
status: 400,
text: async () => 'error: app_not_installed',
} as any)
// When
const got = ensureAuthenticatedAdminAsApp('mystore.myshopify.com', 'client123', 'secret456')
// Then
await expect(got).rejects.toThrow(/App is not installed/)
})
test('throws error on other 400 errors', async () => {
// Given
vi.mocked(shopifyFetch).mockResolvedValueOnce({
status: 400,
statusText: 'Bad Request',
text: async () => 'invalid credentials',
} as any)
// When
const got = ensureAuthenticatedAdminAsApp('mystore.myshopify.com', 'client123', 'secret456')
// Then
await expect(got).rejects.toThrow('Failed to get access token for app client123 on store mystore.myshopify.com')
})
})