-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.test.ts
369 lines (343 loc) · 11 KB
/
env.test.ts
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { env } from 'node:process'
import test from 'ava'
import { getTestServer } from 'fixtures/seam/connect/api.js'
import jwt from 'jsonwebtoken'
import {
SeamHttp,
SeamHttpInvalidOptionsError,
SeamHttpMultiWorkspace,
} from '@seamapi/http/connect'
/*
* Tests in this file must run serially to ensure a clean environment for each test.
*/
const cleanupEnv = (): void => {
delete env.SEAM_API_KEY
delete env.SEAM_ENDPOINT
delete env.SEAM_API_URL
}
test.afterEach(cleanupEnv)
test.beforeEach(cleanupEnv)
test.serial(
'SeamHttp: constructor uses SEAM_API_KEY environment variable',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = seed.seam_apikey1_token
const seam = new SeamHttp({ endpoint })
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: constructor checks for SEAM_API_KEY environment variable',
(t) => {
t.throws(() => new SeamHttp(), {
instanceOf: SeamHttpInvalidOptionsError,
message: /SEAM_API_KEY/,
})
},
)
test.serial(
'SeamHttp: apiKey option overrides environment variables',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = 'some-invalid-api-key-1'
const seam = new SeamHttp({ apiKey: seed.seam_apikey1_token, endpoint })
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: apiKey option as first argument overrides environment variables',
(t) => {
env.SEAM_API_KEY = 'some-invalid-api-key-2'
const seam = new SeamHttp('seam_apikey_token')
t.truthy(seam)
},
)
test.serial(
'SeamHttp: constructor uses SEAM_API_KEY environment variable when passed no argument',
(t) => {
env.SEAM_API_KEY = 'seam_apikey_token'
const seam = new SeamHttp()
t.truthy(seam)
},
)
test.serial(
'SeamHttp: constructor requires SEAM_API_KEY when passed no argument',
(t) => {
t.throws(() => new SeamHttp(), {
instanceOf: SeamHttpInvalidOptionsError,
message: /apiKey/,
})
},
)
test.serial(
'SeamHttp: SEAM_ENDPOINT environment variable is used first',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_URL = 'https://example.com'
env.SEAM_ENDPOINT = endpoint
const seam = new SeamHttp({ apiKey: seed.seam_apikey1_token })
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_API_URL environment variable is used as fallback',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_URL = endpoint
const seam = new SeamHttp({ apiKey: seed.seam_apikey1_token })
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: endpoint option overrides environment variables',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_URL = 'https://example.com'
env.SEAM_ENDPOINT = 'https://example.com'
const seam = new SeamHttp({ apiKey: seed.seam_apikey1_token, endpoint })
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_ENDPOINT environment variable is used with fromApiKey',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_URL = 'https://example.com'
env.SEAM_ENDPOINT = endpoint
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: constructor ignores SEAM_API_KEY environment variable if used with clientSessionToken',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = 'some-invalid-api-key-3'
const seam = new SeamHttp({
clientSessionToken: seed.seam_cst1_token,
endpoint,
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_API_KEY environment variable is ignored with fromClientSessionToken',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = seed.seam_apikey1_token
const seam = SeamHttp.fromClientSessionToken(seed.seam_cst1_token, {
endpoint,
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_API_KEY environment variable is ignored with fromPublishableKey',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = seed.seam_apikey1_token
const seam = await SeamHttp.fromPublishableKey(
seed.seam_pk1_token,
seed.john_user_identifier_key,
{
endpoint,
},
)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_API_KEY environment variable is ignored with fromConsoleSessionToken',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = seed.seam_apikey1_token
const consoleSessionToken = jwt.sign(
{
user_id: seed.john_user_id,
key: seed.john_user_key,
},
'secret',
)
const seam = SeamHttp.fromConsoleSessionToken(
consoleSessionToken,
seed.seed_workspace_1,
{
endpoint,
},
)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_API_KEY environment variable is ignored with personalAccessToken',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_KEY = seed.seam_apikey1_token
const seam = SeamHttp.fromPersonalAccessToken(
seed.seam_at1_token,
seed.seed_workspace_1,
{
endpoint,
},
)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_ENDPOINT environment variable is used with fromPublishableKey',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_ENDPOINT = endpoint
const seam = await SeamHttp.fromPublishableKey(
seed.seam_pk1_token,
seed.john_user_identifier_key,
)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: SEAM_ENDPOINT environment variable is used with fromClientSessionToken',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env.SEAM_API_URL = 'https://example.com'
env.SEAM_ENDPOINT = endpoint
const seam = SeamHttp.fromClientSessionToken(seed.seam_cst1_token)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: constructor uses SEAM_PERSONAL_ACCESS_TOKEN and SEAM_WORKSPACE_ID environment variables',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env['SEAM_PERSONAL_ACCESS_TOKEN'] = seed.seam_at1_token
env['SEAM_WORKSPACE_ID'] = seed.seed_workspace_1
const seam = new SeamHttp({ endpoint })
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: throws error when both SEAM_API_KEY and SEAM_PERSONAL_ACCESS_TOKEN are defined',
(t) => {
env.SEAM_API_KEY = 'some-api-key'
env['SEAM_PERSONAL_ACCESS_TOKEN'] = 'some-access-token'
env['SEAM_WORKSPACE_ID'] = 'some-workspace-id'
t.throws(() => new SeamHttp(), {
instanceOf: SeamHttpInvalidOptionsError,
message:
/Both SEAM_API_KEY and SEAM_PERSONAL_ACCESS_TOKEN environment variables are defined/,
})
},
)
test.serial(
'SeamHttp: personalAccessToken option overrides environment variables',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env['SEAM_PERSONAL_ACCESS_TOKEN'] = 'some-invalid-token'
env['SEAM_WORKSPACE_ID'] = seed.seed_workspace_1
const seam = new SeamHttp({
personalAccessToken: seed.seam_at1_token,
endpoint,
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttp: workspaceId option overrides environment variables',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env['SEAM_PERSONAL_ACCESS_TOKEN'] = seed.seam_at1_token
env['SEAM_WORKSPACE_ID'] = 'some-invalid-workspace'
const seam = new SeamHttp({
workspaceId: seed.seed_workspace_1,
endpoint,
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
},
)
test.serial(
'SeamHttpMultiWorkspace: constructor uses SEAM_PERSONAL_ACCESS_TOKEN environment variable',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env['SEAM_PERSONAL_ACCESS_TOKEN'] = seed.seam_at1_token
const multiWorkspace = new SeamHttpMultiWorkspace({ endpoint })
const workspaces = await multiWorkspace.workspaces.list()
t.true(workspaces.length > 0)
},
)
test.serial(
'SeamHttpMultiWorkspace: personalAccessToken option overrides environment variables',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
env['SEAM_PERSONAL_ACCESS_TOKEN'] = 'some-invalid-token'
const multiWorkspace = new SeamHttpMultiWorkspace({
personalAccessToken: seed.seam_at1_token,
endpoint,
})
const workspaces = await multiWorkspace.workspaces.list()
t.true(workspaces.length > 0)
},
)