generated from 47ng/typescript-library-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
integration.test.ts
423 lines (400 loc) · 11.3 KB
/
integration.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import { cloakedStringRegex } from '@47ng/cloak'
import fs from 'node:fs/promises'
import path from 'node:path'
import { errors } from '../errors'
import { makeExtensionClient, makeMiddlewareClient } from './prismaClient'
import * as sqlite from './sqlite'
const clients = [
{ type: 'middleware', client: makeMiddlewareClient() },
{ type: 'extension', client: makeExtensionClient() }
]
describe.each(clients)('integration ($type)', ({ client }) => {
beforeAll(() => {
// Reset database
const src = path.resolve(process.cwd(), 'prisma', 'db.test.sqlite')
const dst = path.resolve(process.cwd(), 'prisma', 'db.integration.sqlite')
return fs.copyFile(src, dst)
})
const email = '[email protected]'
test('create user', async () => {
const received = await client.user.create({
data: {
email,
name: 'James Bond'
}
})
const dbValue = await sqlite.get({ table: 'User', where: { email } })
expect(received.name).toEqual('James Bond') // clear text in returned value
expect(dbValue.name).toMatch(cloakedStringRegex) // encrypted in database
})
test('query user by encrypted field', async () => {
let received = await client.user.findFirst({
where: {
name: 'James Bond'
}
})
expect(received!.name).toEqual('James Bond')
// Should also work with long form:
received = await client.user.findFirst({
where: {
name: {
equals: 'James Bond'
}
}
})
expect(received!.name).toEqual('James Bond')
// Should also work with boolean logic:
received = await client.user.findFirst({
where: {
OR: [
{
name: 'James Bond'
},
{
name: 'Bond, James Bond.'
}
]
}
})
expect(received!.name).toEqual('James Bond')
})
test('query user by encrypted field (with equals)', async () => {
const received = await client.user.findFirst({
where: {
name: {
equals: 'James Bond'
}
}
})
expect(received!.name).toEqual('James Bond')
})
test('query user by encrypted field with complex query', async () => {
const received = await client.user.findFirst({
where: {
OR: [
{
name: {
equals: 'James Bond'
}
},
{
AND: [
{
NOT: {
name: 'Dr. No'
}
}
]
}
]
}
})
expect(received!.name).toEqual('James Bond')
})
test('delete user', async () => {
const received = await client.user.delete({ where: { email } })
expect(received.name).toEqual('James Bond')
})
test('create post & associated user', async () => {
const received = await client.post.create({
data: {
title: "I'm back",
content: 'You only live twice.',
author: {
create: {
email,
name: 'James Bond'
}
}
},
select: {
id: true,
author: true,
content: true
}
})
const user = await sqlite.get({ table: 'User', where: { email } })
const post = await sqlite.get({
table: 'Post',
where: { id: received.id.toString() }
})
expect(received.author?.name).toEqual('James Bond')
expect(received.content).toEqual('You only live twice.')
expect(user.name).toMatch(cloakedStringRegex)
expect(post.content).toMatch(cloakedStringRegex)
expect(post.title).toEqual("I'm back") // clear text in the database
})
test('update user', async () => {
const received = await client.user.update({
data: {
name: 'The name is Bond...'
},
where: {
email
}
})
const user = await sqlite.get({ table: 'User', where: { email } })
expect(received.name).toEqual('The name is Bond...')
expect(user.name).toMatch(cloakedStringRegex)
})
test('update user (with set)', async () => {
const received = await client.user.update({
data: {
name: {
set: '...James Bond.'
}
},
where: {
email
}
})
const user = await sqlite.get({ table: 'User', where: { email } })
expect(received.name).toEqual('...James Bond.')
expect(user.name).toMatch(cloakedStringRegex)
await client.user.delete({
where: {
email
}
})
})
test('complex query nesting', async () => {
const received = await client.user.create({
data: {
email: '[email protected]',
name: 'Alec Trevelyan',
posts: {
create: [
{
title: '006 - First report',
content: 'For England, James?'
},
{
title: 'Janus Quotes',
content: "I've set the timers for six minutes",
categories: {
create: {
name: 'Quotes'
}
}
}
]
}
},
include: {
posts: {
include: {
categories: true
}
}
}
})
expect(received.name).toEqual('Alec Trevelyan')
expect(received.posts[0].content).toEqual('For England, James?')
expect(received.posts[1].content).toEqual(
"I've set the timers for six minutes"
)
const user = await sqlite.get({
table: 'User',
where: { email: '[email protected]' }
})
const post1 = await sqlite.get({
table: 'Post',
where: { id: received.posts[0].id.toString() }
})
const post2 = await sqlite.get({
table: 'Post',
where: { id: received.posts[1].id.toString() }
})
const category = await sqlite.get({
table: 'Category',
where: { name: 'Quotes' }
})
expect(user.name).toMatch(cloakedStringRegex)
expect(post1.content).toMatch(cloakedStringRegex)
expect(post2.content).toMatch(cloakedStringRegex)
expect(category.name).toEqual('Quotes')
})
test('top level with no encrypted field, nested with encrypted field - using select', async () => {
const created = await client.post.create({
data: {
title: "I'm back",
content: 'You only live twice.',
categories: {
create: {
name: 'Secret agents'
}
},
author: {
create: {
email,
name: 'James Bond'
}
}
},
select: {
id: true,
author: true,
content: true,
categories: true
}
})
const category = await client.category.findFirst({
select: {
name: true,
posts: {
select: {
content: true
}
}
},
where: {
id: { equals: created.categories![0].id }
}
})
expect(category?.name).toEqual('Secret agents')
expect(category?.posts[0].content).toEqual('You only live twice.')
})
test('immutable params', async () => {
const email = '[email protected]'
const params = {
data: {
name: 'Xenia Onatop',
email
}
}
const received = await client.user.create(params)
const user = await sqlite.get({ table: 'User', where: { email } })
expect(params.data.name).toEqual('Xenia Onatop')
expect(received.name).toEqual('Xenia Onatop')
expect(user.name).toMatch(cloakedStringRegex)
})
test('orderBy is not supported', async () => {
const cer = console.error
console.error = jest.fn()
let received = await client.user.findMany({
orderBy: {
name: 'desc'
}
})
expect(received.length).toEqual(3)
// If 'desc' order was respected, those should be the other way around.
// This test verifies that the directive is dropped and natural order
// is preserved.
expect(received[0].name).toEqual('Alec Trevelyan')
expect(received[1].name).toEqual('James Bond')
expect(received[2].name).toEqual('Xenia Onatop')
expect(console.error).toHaveBeenLastCalledWith(
errors.orderByUnsupported('User', 'name')
)
// @ts-ignore
console.error.mockClear()
// Test array syntax
received = await client.user.findMany({
orderBy: [{ name: 'asc' }]
})
expect(received[0].name).toEqual('Alec Trevelyan')
expect(received[1].name).toEqual('James Bond')
expect(received[2].name).toEqual('Xenia Onatop')
expect(console.error).toHaveBeenLastCalledWith(
errors.orderByUnsupported('User', 'name')
)
console.error = cer
})
test('connect on hashed field', async () => {
const content = 'You can connect to a hashed encrypted field.'
const received = await client.post.create({
data: {
title: 'Connected',
content,
author: {
connect: {
name: 'James Bond'
}
}
},
include: {
author: true
}
})
expect(received.author?.name).toEqual('James Bond')
expect(received.content).toEqual(content)
})
test('cursor on hashed field', async () => {
const received = await client.user.findMany({
take: 1,
cursor: {
name: 'James Bond'
}
})
expect(received[0].name).toEqual('James Bond')
})
test('transactions', async () => {
const id = await client.$transaction(async tx => {
const post = await tx.post.create({
data: {
title: 'Mission orders',
author: {
connect: {
name: 'James Bond'
}
},
content: `This message will self-destruct in 5 seconds
(oops, wrong spy show)`
}
})
await tx.post.delete({ where: { id: post.id } })
return post.id
})
const post = await client.post.findUnique({ where: { id } })
expect(post).toBeNull()
})
test('transactions with rollback', async () => {
try {
await client.$transaction(async tx => {
const post = await tx.post.create({
data: {
title: 'Mission orders',
author: {
connect: {
name: 'James Bond'
}
},
content: `This message will self-destruct in 5 seconds
(oops, wrong spy show)`
}
})
// Simulate a transaction failure to test rollback
throw post.id
})
} catch (id) {
const post = await client.post.findUnique({ where: { id: id as number } })
expect(post).toBeNull()
return
}
// Should be unreachable
const reached = true
expect(reached).toBe(false)
})
test("Doesn't work with the Fluent API", async () => {
const posts = await client.user.findUnique({ where: { email } }).posts()
for (const post of posts!) {
expect(post.content).toMatch(cloakedStringRegex)
}
})
test("query entries with non-empty name", async () => {
const fakeName = 'f@keU$er'
await client.user.create({
data: {
name: '',
email: '[email protected]'
}
});
const users = await client.user.findMany();
// assume active user with nonempty name
const activeUserCount = await client.user.count({ where: { name: { not: '' } } })
// use fakeName to pretend unique name
const existingUsers = await client.user.findMany({ where: { name: { not: fakeName } } })
expect(activeUserCount).toBe(users.length - 1);
expect(existingUsers).toEqual(users);
})
})