Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Nov 14, 2023
1 parent cdfffbb commit 13bc041
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ export class ExpressionWriter {
this.writeFieldCondition(
expr.left,
() => {
this.write(expr.right);
// inner scope of collection expression is always compiled as non-post-guard
const innerWriter = new ExpressionWriter(this.writer, false);
innerWriter.write(expr.right);
},
operator === '?' ? 'some' : operator === '!' ? 'every' : 'none'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ export default class PolicyGenerator {
} else {
return [];
}
} else if (isInvocationExpr(expr)) {
// recurse into function arguments
return expr.args.flatMap((arg) => collectReferencePaths(arg.value));
} else {
// recurse
const children = streamContents(expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,54 @@ describe('With Policy: post update', () => {
await expect(db.model.update({ where: { id: '2' }, data: { value: 4 } })).toResolveTruthy();
});

it('functions pre-update', async () => {
const { prisma, withPolicy } = await loadSchema(
`
model Model {
id String @id @default(uuid())
value String
x Int
@@allow('create,read', true)
@@allow('update', startsWith(value, 'hello') && future().x > 0)
}
`
);

const db = withPolicy();

await prisma.model.create({ data: { id: '1', value: 'good', x: 1 } });
await expect(db.model.update({ where: { id: '1' }, data: { value: 'hello' } })).toBeRejectedByPolicy();

await prisma.model.update({ where: { id: '1' }, data: { value: 'hello world' } });
const r = await db.model.update({ where: { id: '1' }, data: { value: 'hello new world' } });
expect(r.value).toBe('hello new world');
});

it('functions post-update', async () => {
const { prisma, withPolicy } = await loadSchema(
`
model Model {
id String @id @default(uuid())
value String
x Int
@@allow('create,read', true)
@@allow('update', x > 0 && startsWith(future().value, 'hello'))
}
`,
{ logPrismaQuery: true }
);

const db = withPolicy();

await prisma.model.create({ data: { id: '1', value: 'good', x: 1 } });
await expect(db.model.update({ where: { id: '1' }, data: { value: 'nice' } })).toBeRejectedByPolicy();

const r = await db.model.update({ where: { id: '1' }, data: { x: 0, value: 'hello world' } });
expect(r.value).toBe('hello world');
});

it('collection predicate pre-update', async () => {
const { prisma, withPolicy } = await loadSchema(
`
Expand Down Expand Up @@ -109,7 +157,7 @@ describe('With Policy: post update', () => {
},
});

expect(
await expect(
db.m1.update({
where: { id: '1' },
data: { value: 1 },
Expand All @@ -124,7 +172,7 @@ describe('With Policy: post update', () => {
},
});

expect(
await expect(
db.m1.update({
where: { id: '1' },
data: { value: 1 },
Expand Down Expand Up @@ -159,14 +207,14 @@ describe('With Policy: post update', () => {
await prisma.m1.create({
data: {
id: '1',
value: 0,
value: 1,
m2: {
create: [{ id: '1', value: 1 }],
create: [{ id: '1', value: 0 }],
},
},
});

expect(
await expect(
db.m1.update({
where: { id: '1' },
data: { value: 2 },
Expand All @@ -181,7 +229,7 @@ describe('With Policy: post update', () => {
},
});

expect(
await expect(
db.m1.update({
where: { id: '1' },
data: { value: 2 },
Expand Down

0 comments on commit 13bc041

Please sign in to comment.