|
| 1 | +import { loadSchema } from '@zenstackhq/testtools'; |
| 2 | + |
| 3 | +describe('Test API updateManyAndReturn', () => { |
| 4 | + it('model-level policies', async () => { |
| 5 | + const { prisma, enhance } = await loadSchema( |
| 6 | + ` |
| 7 | + model User { |
| 8 | + id Int @id @default(autoincrement()) |
| 9 | + posts Post[] |
| 10 | + level Int |
| 11 | +
|
| 12 | + @@allow('read', level > 0) |
| 13 | + } |
| 14 | +
|
| 15 | + model Post { |
| 16 | + id Int @id @default(autoincrement()) |
| 17 | + title String |
| 18 | + published Boolean @default(false) |
| 19 | + userId Int |
| 20 | + user User @relation(fields: [userId], references: [id]) |
| 21 | +
|
| 22 | + @@allow('read', published) |
| 23 | + @@allow('update', contains(title, 'hello')) |
| 24 | + } |
| 25 | + ` |
| 26 | + ); |
| 27 | + |
| 28 | + await prisma.user.createMany({ |
| 29 | + data: [{ id: 1, level: 1 }], |
| 30 | + }); |
| 31 | + await prisma.user.createMany({ |
| 32 | + data: [{ id: 2, level: 0 }], |
| 33 | + }); |
| 34 | + |
| 35 | + await prisma.post.createMany({ |
| 36 | + data: [ |
| 37 | + { id: 1, title: 'hello1', userId: 1, published: true }, |
| 38 | + { id: 2, title: 'world1', userId: 1, published: false }, |
| 39 | + ], |
| 40 | + }); |
| 41 | + |
| 42 | + const db = enhance(); |
| 43 | + |
| 44 | + // only post#1 is updated |
| 45 | + let r = await db.post.updateManyAndReturn({ |
| 46 | + data: { title: 'foo' }, |
| 47 | + }); |
| 48 | + expect(r).toHaveLength(1); |
| 49 | + expect(r[0].id).toBe(1); |
| 50 | + |
| 51 | + // post#2 is excluded from update |
| 52 | + await expect( |
| 53 | + db.post.updateManyAndReturn({ |
| 54 | + where: { id: 2 }, |
| 55 | + data: { title: 'foo' }, |
| 56 | + }) |
| 57 | + ).resolves.toHaveLength(0); |
| 58 | + |
| 59 | + // reset |
| 60 | + await prisma.post.update({ where: { id: 1 }, data: { title: 'hello1' } }); |
| 61 | + |
| 62 | + // post#1 is updated |
| 63 | + await expect( |
| 64 | + db.post.updateManyAndReturn({ |
| 65 | + where: { id: 1 }, |
| 66 | + data: { title: 'foo' }, |
| 67 | + }) |
| 68 | + ).resolves.toHaveLength(1); |
| 69 | + |
| 70 | + // reset |
| 71 | + await prisma.post.update({ where: { id: 1 }, data: { title: 'hello1' } }); |
| 72 | + |
| 73 | + // read-back check |
| 74 | + // post#1 updated but can't be read back |
| 75 | + await expect( |
| 76 | + db.post.updateManyAndReturn({ |
| 77 | + data: { published: false }, |
| 78 | + }) |
| 79 | + ).toBeRejectedByPolicy(['result is not allowed to be read back']); |
| 80 | + // but the update should have been applied |
| 81 | + await expect(prisma.post.findUnique({ where: { id: 1 } })).resolves.toMatchObject({ published: false }); |
| 82 | + |
| 83 | + // reset |
| 84 | + await prisma.post.update({ where: { id: 1 }, data: { published: true } }); |
| 85 | + |
| 86 | + // return relation |
| 87 | + r = await db.post.updateManyAndReturn({ |
| 88 | + include: { user: true }, |
| 89 | + data: { title: 'hello2' }, |
| 90 | + }); |
| 91 | + expect(r[0]).toMatchObject({ user: { id: 1 } }); |
| 92 | + |
| 93 | + // relation filtered |
| 94 | + await prisma.post.create({ data: { id: 3, title: 'hello3', userId: 2, published: true } }); |
| 95 | + await expect( |
| 96 | + db.post.updateManyAndReturn({ |
| 97 | + where: { id: 3 }, |
| 98 | + include: { user: true }, |
| 99 | + data: { title: 'hello4' }, |
| 100 | + }) |
| 101 | + ).toBeRejectedByPolicy(['result is not allowed to be read back']); |
| 102 | + // update is applied |
| 103 | + await expect(prisma.post.findUnique({ where: { id: 3 } })).resolves.toMatchObject({ title: 'hello4' }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('field-level policies', async () => { |
| 107 | + const { prisma, enhance } = await loadSchema( |
| 108 | + ` |
| 109 | + model Post { |
| 110 | + id Int @id @default(autoincrement()) |
| 111 | + title String @allow('read', published) |
| 112 | + published Boolean @default(false) |
| 113 | +
|
| 114 | + @@allow('all', true) |
| 115 | + } |
| 116 | + ` |
| 117 | + ); |
| 118 | + |
| 119 | + const db = enhance(); |
| 120 | + |
| 121 | + // update should succeed but one result's title field can't be read back |
| 122 | + await prisma.post.createMany({ |
| 123 | + data: [ |
| 124 | + { id: 1, title: 'post1', published: true }, |
| 125 | + { id: 2, title: 'post2', published: false }, |
| 126 | + ], |
| 127 | + }); |
| 128 | + |
| 129 | + const r = await db.post.updateManyAndReturn({ |
| 130 | + data: { title: 'foo' }, |
| 131 | + }); |
| 132 | + |
| 133 | + expect(r.length).toBe(2); |
| 134 | + expect(r[0].title).toBeTruthy(); |
| 135 | + expect(r[1].title).toBeUndefined(); |
| 136 | + |
| 137 | + // check posts are updated |
| 138 | + await expect(prisma.post.findMany({ where: { title: 'foo' } })).resolves.toHaveLength(2); |
| 139 | + }); |
| 140 | +}); |
0 commit comments