|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { createPolicyTestClient } from './utils'; |
| 3 | + |
| 4 | +describe('Reference Equality Tests', () => { |
| 5 | + it('works with create and auth equality', async () => { |
| 6 | + const db = await createPolicyTestClient( |
| 7 | + ` |
| 8 | +model User { |
| 9 | + id1 Int |
| 10 | + id2 Int |
| 11 | + posts Post[] |
| 12 | + @@id([id1, id2]) |
| 13 | + @@allow('all', auth() == this) |
| 14 | + @@allow('read', true) |
| 15 | +} |
| 16 | +
|
| 17 | +model Post { |
| 18 | + id Int @id @default(autoincrement()) |
| 19 | + title String |
| 20 | + authorId1 Int |
| 21 | + authorId2 Int |
| 22 | + author User @relation(fields: [authorId1, authorId2], references: [id1, id2]) |
| 23 | + @@allow('all', auth() == author) |
| 24 | +} |
| 25 | + `, |
| 26 | + ); |
| 27 | + |
| 28 | + await expect( |
| 29 | + db.user.create({ |
| 30 | + data: { id1: 1, id2: 2 }, |
| 31 | + }), |
| 32 | + ).toBeRejectedByPolicy(); |
| 33 | + |
| 34 | + await expect( |
| 35 | + db.$setAuth({ id1: 1, id2: 2 }).user.create({ |
| 36 | + data: { id1: 1, id2: 2 }, |
| 37 | + }), |
| 38 | + ).resolves.toMatchObject({ id1: 1, id2: 2 }); |
| 39 | + |
| 40 | + await expect( |
| 41 | + db.post.create({ |
| 42 | + data: { authorId1: 1, authorId2: 2, title: 'Post 1' }, |
| 43 | + }), |
| 44 | + ).toBeRejectedByPolicy(); |
| 45 | + await expect( |
| 46 | + db.post.create({ |
| 47 | + data: { author: { connect: { id1_id2: { id1: 1, id2: 2 } } }, title: 'Post 1' }, |
| 48 | + }), |
| 49 | + ).toBeRejectedByPolicy(); |
| 50 | + |
| 51 | + await expect( |
| 52 | + db.$setAuth({ id1: 1, id2: 2 }).post.create({ |
| 53 | + data: { authorId1: 1, authorId2: 2, title: 'Post 1' }, |
| 54 | + }), |
| 55 | + ).resolves.toMatchObject({ title: 'Post 1' }); |
| 56 | + await expect( |
| 57 | + db.$setAuth({ id1: 1, id2: 2 }).post.create({ |
| 58 | + data: { author: { connect: { id1_id2: { id1: 1, id2: 2 } } }, title: 'Post 2' }, |
| 59 | + }), |
| 60 | + ).resolves.toMatchObject({ title: 'Post 2' }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('works with create and auth inequality', async () => { |
| 64 | + const db = await createPolicyTestClient( |
| 65 | + ` |
| 66 | +model User { |
| 67 | + id1 Int |
| 68 | + id2 Int |
| 69 | + posts Post[] |
| 70 | + @@id([id1, id2]) |
| 71 | + @@allow('all', auth() != this) |
| 72 | + @@allow('read', true) |
| 73 | +} |
| 74 | +
|
| 75 | +model Post { |
| 76 | + id Int @id @default(autoincrement()) |
| 77 | + title String |
| 78 | + authorId1 Int |
| 79 | + authorId2 Int |
| 80 | + author User @relation(fields: [authorId1, authorId2], references: [id1, id2]) |
| 81 | + @@allow('all', auth() != author) |
| 82 | + @@allow('read', true) |
| 83 | +} |
| 84 | + `, |
| 85 | + ); |
| 86 | + |
| 87 | + await expect( |
| 88 | + db.$setAuth({ id1: 1, id2: 2 }).user.create({ |
| 89 | + data: { id1: 1, id2: 2 }, |
| 90 | + }), |
| 91 | + ).toBeRejectedByPolicy(); |
| 92 | + await expect( |
| 93 | + db.$setAuth({ id1: 2, id2: 2 }).user.create({ |
| 94 | + data: { id1: 1, id2: 2 }, |
| 95 | + }), |
| 96 | + ).toResolveTruthy(); |
| 97 | + |
| 98 | + await expect( |
| 99 | + db.$setAuth({ id1: 1, id2: 2 }).post.create({ |
| 100 | + data: { authorId1: 1, authorId2: 2, title: 'Post 1' }, |
| 101 | + }), |
| 102 | + ).toBeRejectedByPolicy(); |
| 103 | + await expect( |
| 104 | + db.$setAuth({ id1: 2, id2: 2 }).post.create({ |
| 105 | + data: { authorId1: 1, authorId2: 2, title: 'Post 1' }, |
| 106 | + }), |
| 107 | + ).resolves.toMatchObject({ title: 'Post 1' }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments