|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { createPolicyTestClient } from '../utils'; |
| 3 | + |
| 4 | +describe('Update policy tests', () => { |
| 5 | + it('works with scalar field check', async () => { |
| 6 | + const db = await createPolicyTestClient( |
| 7 | + ` |
| 8 | +model Foo { |
| 9 | + id Int @id |
| 10 | + x Int |
| 11 | + @@allow('update', x > 0) |
| 12 | + @@allow('create,read', true) |
| 13 | +} |
| 14 | +`, |
| 15 | + ); |
| 16 | + |
| 17 | + await db.foo.create({ data: { id: 1, x: 0 } }); |
| 18 | + await expect(db.foo.update({ where: { id: 1 }, data: { x: 1 } })).toBeRejectedNotFound(); |
| 19 | + await db.foo.create({ data: { id: 2, x: 1 } }); |
| 20 | + await expect(db.foo.update({ where: { id: 2 }, data: { x: 2 } })).resolves.toMatchObject({ x: 2 }); |
| 21 | + |
| 22 | + await expect( |
| 23 | + db.$qb.updateTable('Foo').set({ x: 1 }).where('id', '=', 1).executeTakeFirst(), |
| 24 | + ).resolves.toMatchObject({ numUpdatedRows: 0n }); |
| 25 | + await expect( |
| 26 | + db.$qb.updateTable('Foo').set({ x: 3 }).where('id', '=', 2).returningAll().execute(), |
| 27 | + ).resolves.toMatchObject([{ id: 2, x: 3 }]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('works with this scalar member check', async () => { |
| 31 | + const db = await createPolicyTestClient( |
| 32 | + ` |
| 33 | +model Foo { |
| 34 | + id Int @id |
| 35 | + x Int |
| 36 | + @@allow('update', this.x > 0) |
| 37 | + @@allow('create,read', true) |
| 38 | +} |
| 39 | +`, |
| 40 | + ); |
| 41 | + |
| 42 | + await db.foo.create({ data: { id: 1, x: 0 } }); |
| 43 | + await expect(db.foo.update({ where: { id: 1 }, data: { x: 1 } })).toBeRejectedNotFound(); |
| 44 | + await db.foo.create({ data: { id: 2, x: 1 } }); |
| 45 | + await expect(db.foo.update({ where: { id: 2 }, data: { x: 2 } })).resolves.toMatchObject({ x: 2 }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('denies by default', async () => { |
| 49 | + const db = await createPolicyTestClient( |
| 50 | + ` |
| 51 | +model Foo { |
| 52 | + id Int @id |
| 53 | + x Int |
| 54 | + @@allow('create,read', true) |
| 55 | +} |
| 56 | +`, |
| 57 | + ); |
| 58 | + |
| 59 | + await db.foo.create({ data: { id: 1, x: 0 } }); |
| 60 | + await expect(db.foo.update({ where: { id: 1 }, data: { x: 1 } })).toBeRejectedNotFound(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('works with deny rule', async () => { |
| 64 | + const db = await createPolicyTestClient( |
| 65 | + ` |
| 66 | +model Foo { |
| 67 | + id Int @id |
| 68 | + x Int |
| 69 | + @@deny('update', x <= 0) |
| 70 | + @@allow('create,read,update', true) |
| 71 | +} |
| 72 | +`, |
| 73 | + ); |
| 74 | + await db.foo.create({ data: { id: 1, x: 0 } }); |
| 75 | + await expect(db.foo.update({ where: { id: 1 }, data: { x: 1 } })).toBeRejectedNotFound(); |
| 76 | + await db.foo.create({ data: { id: 2, x: 1 } }); |
| 77 | + await expect(db.foo.update({ where: { id: 2 }, data: { x: 2 } })).resolves.toMatchObject({ x: 2 }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('works with mixed allow and deny rules', async () => { |
| 81 | + const db = await createPolicyTestClient( |
| 82 | + ` |
| 83 | +model Foo { |
| 84 | + id Int @id |
| 85 | + x Int |
| 86 | + @@deny('update', x <= 0) |
| 87 | + @@allow('update', x <= 0 || x > 1) |
| 88 | + @@allow('create,read', true) |
| 89 | +} |
| 90 | +`, |
| 91 | + ); |
| 92 | + |
| 93 | + await db.foo.create({ data: { id: 1, x: 0 } }); |
| 94 | + await expect(db.foo.update({ where: { id: 1 }, data: { x: 1 } })).toBeRejectedNotFound(); |
| 95 | + await db.foo.create({ data: { id: 2, x: 1 } }); |
| 96 | + await expect(db.foo.update({ where: { id: 2 }, data: { x: 2 } })).toBeRejectedNotFound(); |
| 97 | + await db.foo.create({ data: { id: 3, x: 2 } }); |
| 98 | + await expect(db.foo.update({ where: { id: 3 }, data: { x: 3 } })).resolves.toMatchObject({ x: 3 }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('works with auth check', async () => { |
| 102 | + const db = await createPolicyTestClient( |
| 103 | + ` |
| 104 | +type Auth { |
| 105 | + x Int |
| 106 | + @@auth |
| 107 | +} |
| 108 | +
|
| 109 | +model Foo { |
| 110 | + id Int @id |
| 111 | + x Int |
| 112 | + @@allow('update', x == auth().x) |
| 113 | + @@allow('create,read', true) |
| 114 | +} |
| 115 | +`, |
| 116 | + ); |
| 117 | + await db.foo.create({ data: { id: 1, x: 1 } }); |
| 118 | + await expect(db.$setAuth({ x: 0 }).foo.update({ where: { id: 1 }, data: { x: 2 } })).toBeRejectedNotFound(); |
| 119 | + await expect(db.$setAuth({ x: 1 }).foo.update({ where: { id: 1 }, data: { x: 2 } })).resolves.toMatchObject({ |
| 120 | + x: 2, |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments