-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix(policy): relation access via this, more test cases
#252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createPolicyTestClient } from '../utils'; | ||
|
|
||
| describe('Read policy tests', () => { | ||
| describe('Find tests', () => { | ||
| it('works with top-level find', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| x Int | ||
| @@allow('create', true) | ||
| @@allow('read', x > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.$unuseAll().foo.create({ data: { id: 1, x: 0 } }); | ||
| await expect(db.foo.findUnique({ where: { id: 1 } })).toResolveNull(); | ||
|
|
||
| await db.$unuseAll().foo.update({ where: { id: 1 }, data: { x: 1 } }); | ||
| await expect(db.foo.findUnique({ where: { id: 1 } })).toResolveTruthy(); | ||
| }); | ||
|
|
||
| it('works with mutation read-back', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| x Int | ||
| @@allow('create,update', true) | ||
| @@allow('read', x > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await expect(db.foo.create({ data: { id: 1, x: 0 } })).toBeRejectedByPolicy(); | ||
| await expect(db.$unuseAll().foo.count()).resolves.toBe(1); | ||
| await expect(db.foo.update({ where: { id: 1 }, data: { x: 1 } })).resolves.toMatchObject({ x: 1 }); | ||
| }); | ||
|
|
||
| it('works with to-one relation optional owner-side read', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bar Bar? @relation(fields: [barId], references: [id]) | ||
| barId Int? @unique | ||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo? | ||
| @@allow('create,update', true) | ||
| @@allow('read', y > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.foo.create({ data: { id: 1, bar: { create: { id: 1, y: 0 } } } }); | ||
| await expect(db.foo.findFirst({ include: { bar: true } })).resolves.toMatchObject({ id: 1, bar: null }); | ||
| await db.bar.update({ where: { id: 1 }, data: { y: 1 } }); | ||
| await expect(db.foo.findFirst({ include: { bar: true } })).resolves.toMatchObject({ | ||
| id: 1, | ||
| bar: { id: 1 }, | ||
| }); | ||
| }); | ||
|
|
||
| // TODO: check if we should be consistent with v2 and filter out the parent entity | ||
| // if a non-optional child relation is included but not readable | ||
| it('works with to-one relation non-optional owner-side read', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bar Bar @relation(fields: [barId], references: [id]) | ||
| barId Int @unique | ||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo? | ||
| @@allow('create,update', true) | ||
| @@allow('read', y > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.foo.create({ data: { id: 1, bar: { create: { id: 1, y: 0 } } } }); | ||
| await expect(db.foo.findFirst({ include: { bar: true } })).resolves.toMatchObject({ id: 1, bar: null }); | ||
| await db.bar.update({ where: { id: 1 }, data: { y: 1 } }); | ||
| await expect(db.foo.findFirst({ include: { bar: true } })).resolves.toMatchObject({ | ||
| id: 1, | ||
| bar: { id: 1 }, | ||
| }); | ||
| }); | ||
|
|
||
| it('works with to-one relation non-owner-side read', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bar Bar? | ||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo @relation(fields: [fooId], references: [id]) | ||
| fooId Int @unique | ||
| @@allow('create,update', true) | ||
| @@allow('read', y > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.foo.create({ data: { id: 1, bar: { create: { id: 1, y: 0 } } } }); | ||
| await expect(db.foo.findFirst({ include: { bar: true } })).resolves.toMatchObject({ id: 1, bar: null }); | ||
| await db.bar.update({ where: { id: 1 }, data: { y: 1 } }); | ||
| await expect(db.foo.findFirst({ include: { bar: true } })).resolves.toMatchObject({ | ||
| id: 1, | ||
| bar: { id: 1 }, | ||
| }); | ||
| }); | ||
|
|
||
| it('works with to-many relation read', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bars Bar[] | ||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo? @relation(fields: [fooId], references: [id]) | ||
| fooId Int? | ||
| @@allow('create,update', true) | ||
| @@allow('read', y > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.foo.create({ | ||
| data: { | ||
| id: 1, | ||
| bars: { | ||
| create: [ | ||
| { id: 1, y: 0 }, | ||
| { id: 2, y: 1 }, | ||
| ], | ||
| }, | ||
| }, | ||
| }); | ||
| await expect(db.foo.findFirst({ include: { bars: true } })).resolves.toMatchObject({ | ||
| id: 1, | ||
| bars: [{ id: 2 }], | ||
| }); | ||
| }); | ||
|
|
||
| it('works with filtered by to-one relation field', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bar Bar? @relation(fields: [barId], references: [id]) | ||
| barId Int? @unique | ||
| @@allow('create', true) | ||
| @@allow('read', bar.y > 0) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo? | ||
| @@allow('all', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.$unuseAll().foo.create({ data: { id: 1, bar: { create: { id: 1, y: 0 } } } }); | ||
| await expect(db.foo.findMany()).resolves.toHaveLength(0); | ||
| await db.bar.update({ where: { id: 1 }, data: { y: 1 } }); | ||
| await expect(db.foo.findMany()).resolves.toHaveLength(1); | ||
| }); | ||
|
|
||
| it('works with filtered by to-one relation non-null', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bar Bar? @relation(fields: [barId], references: [id]) | ||
| barId Int? @unique | ||
| @@allow('create,update', true) | ||
| @@allow('read', bar != null) | ||
| @@allow('read', this.bar != null) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo? | ||
| @@allow('all', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.$unuseAll().foo.create({ data: { id: 1 } }); | ||
| await expect(db.foo.findMany()).resolves.toHaveLength(0); | ||
| await db.foo.update({ where: { id: 1 }, data: { bar: { create: { id: 1, y: 0 } } } }); | ||
| await expect(db.foo.findMany()).resolves.toHaveLength(1); | ||
| }); | ||
|
|
||
| it('works with filtered by to-many relation', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Foo { | ||
| id Int @id | ||
| bars Bar[] | ||
| @@allow('create,update', true) | ||
| @@allow('read', bars?[y > 0]) | ||
| @@allow('read', this.bars?[y > 0]) | ||
| } | ||
|
|
||
| model Bar { | ||
| id Int @id | ||
| y Int | ||
| foo Foo? @relation(fields: [fooId], references: [id]) | ||
| fooId Int? | ||
| @@allow('all', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await db.$unuseAll().foo.create({ data: { id: 1, bars: { create: [{ id: 1, y: 0 }] } } }); | ||
| await expect(db.foo.findMany()).resolves.toHaveLength(0); | ||
| await db.foo.update({ where: { id: 1 }, data: { bars: { create: { id: 2, y: 1 } } } }); | ||
| await expect(db.foo.findMany()).resolves.toHaveLength(1); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.