|
| 1 | +import { createPolicyTestClient } from '@zenstackhq/testtools'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +// https://github.com/zenstackhq/zenstack/issues/2410 |
| 5 | +describe('Regression for issue #2410', () => { |
| 6 | + it('should not generate invalid SQL when related models share identical @deny field names', async () => { |
| 7 | + const db = await createPolicyTestClient( |
| 8 | + ` |
| 9 | +model User { |
| 10 | + id String @id @default(cuid()) |
| 11 | + role String |
| 12 | +
|
| 13 | + @@allow('all', true) |
| 14 | +} |
| 15 | +
|
| 16 | +model Thread { |
| 17 | + id String @id @default(cuid()) |
| 18 | + title String |
| 19 | + apiKeyId String @deny('all', auth().role != 'ADMIN') |
| 20 | + questions Question[] |
| 21 | +
|
| 22 | + @@allow('all', true) |
| 23 | +} |
| 24 | +
|
| 25 | +model Question { |
| 26 | + id String @id @default(cuid()) |
| 27 | + content String |
| 28 | + apiKeyId String @deny('all', auth().role != 'ADMIN') |
| 29 | + threadId String |
| 30 | + thread Thread @relation(fields: [threadId], references: [id]) |
| 31 | +
|
| 32 | + @@allow('all', true) |
| 33 | +} |
| 34 | + `, |
| 35 | + ); |
| 36 | + |
| 37 | + const admin = { id: 'admin-1', role: 'ADMIN' }; |
| 38 | + const user = { id: 'user-1', role: 'USER' }; |
| 39 | + |
| 40 | + const thread = await db.$setAuth(admin).thread.create({ |
| 41 | + data: { |
| 42 | + title: 'Test Thread', |
| 43 | + apiKeyId: 'key-1', |
| 44 | + questions: { |
| 45 | + create: [{ content: 'Q1', apiKeyId: 'key-1' }], |
| 46 | + }, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + // updating a non-denied field on the Thread should succeed for any role |
| 51 | + await expect( |
| 52 | + db.$setAuth(user).thread.update({ |
| 53 | + where: { id: thread.id }, |
| 54 | + data: { title: 'Updated Thread' }, |
| 55 | + }), |
| 56 | + ).toResolveTruthy(); |
| 57 | + |
| 58 | + // updating a denied field should be rejected for non-admin |
| 59 | + await expect( |
| 60 | + db.$setAuth(user).thread.update({ |
| 61 | + where: { id: thread.id }, |
| 62 | + data: { apiKeyId: 'key-2' }, |
| 63 | + }), |
| 64 | + ).toBeRejectedByPolicy(); |
| 65 | + |
| 66 | + // updating a denied field should succeed for admin |
| 67 | + await expect( |
| 68 | + db.$setAuth(admin).thread.update({ |
| 69 | + where: { id: thread.id }, |
| 70 | + data: { apiKeyId: 'key-2' }, |
| 71 | + }), |
| 72 | + ).toResolveTruthy(); |
| 73 | + }); |
| 74 | +}); |
0 commit comments