|
| 1 | +import { createPolicyTestClient } from '@zenstackhq/testtools'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +describe('Regression for issue #2536', () => { |
| 5 | + it('supports currentModel and currentOperation in nested expressions', async () => { |
| 6 | + const db = await createPolicyTestClient( |
| 7 | + ` |
| 8 | + model User { |
| 9 | + id String @id |
| 10 | + groups Group[] @relation("UserGroups") |
| 11 | + } |
| 12 | +
|
| 13 | + model Group { |
| 14 | + id String @id |
| 15 | + modelName String |
| 16 | + modelOperation String |
| 17 | + users User[] @relation("UserGroups") |
| 18 | + } |
| 19 | +
|
| 20 | + // define a mixin to also check that currentModel correctly resolves to the model where the mixin is applied |
| 21 | + type AuthPolicyMixin { |
| 22 | + @@allow('all', auth().groups?[modelName == currentModel() && modelOperation == currentOperation()]) |
| 23 | + } |
| 24 | +
|
| 25 | + model Foo with AuthPolicyMixin { |
| 26 | + id String @id @default(cuid()) |
| 27 | + } |
| 28 | + `, |
| 29 | + ); |
| 30 | + |
| 31 | + const readGroup = { modelName: 'Foo', modelOperation: 'read' }; |
| 32 | + |
| 33 | + await expect(db.$setAuth({ id: 'user1', groups: [readGroup] }).foo.create({ data: {} })).toBeRejectedByPolicy(); |
| 34 | + await expect( |
| 35 | + db |
| 36 | + .$setAuth({ id: 'user1', groups: [{ modelName: 'FooBar', modelOperation: 'create' }, readGroup] }) |
| 37 | + .foo.create({ data: {} }), |
| 38 | + ).toBeRejectedByPolicy(); |
| 39 | + await expect( |
| 40 | + db |
| 41 | + .$setAuth({ id: 'user1', groups: [{ modelName: 'Foo', modelOperation: 'read' }, readGroup] }) |
| 42 | + .foo.create({ data: {} }), |
| 43 | + ).toBeRejectedByPolicy(); |
| 44 | + await expect( |
| 45 | + db |
| 46 | + .$setAuth({ id: 'user1', groups: [{ modelName: 'Foo', modelOperation: 'create' }, readGroup] }) |
| 47 | + .foo.create({ data: {} }), |
| 48 | + ).toResolveTruthy(); |
| 49 | + }); |
| 50 | +}); |
0 commit comments