Skip to content

Commit fc8a1a6

Browse files
ymc9Copilot
andauthored
test: policy + client extension interaction (#56)
* test: policy + client extension interaction * Update packages/runtime/test/policy/client-extensions.test.ts Co-authored-by: Copilot <[email protected]> * remove log --------- Co-authored-by: Copilot <[email protected]>
1 parent 5c39145 commit fc8a1a6

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { definePlugin } from '../../src/client';
3+
import { createPolicyTestClient } from './utils';
4+
5+
describe('client extensions tests for policies', () => {
6+
it('query override one model', async () => {
7+
const db = await createPolicyTestClient(
8+
`
9+
model Model {
10+
id String @id @default(uuid())
11+
x Int
12+
y Int
13+
14+
@@allow('read', x > 0)
15+
}
16+
`,
17+
);
18+
19+
const rawDb = db.$unuseAll();
20+
await rawDb.model.create({ data: { x: 0, y: 100 } });
21+
await rawDb.model.create({ data: { x: 1, y: 200 } });
22+
await rawDb.model.create({ data: { x: 2, y: 300 } });
23+
24+
const ext = definePlugin({
25+
id: 'prisma-extension-queryOverride',
26+
onQuery: {
27+
model: {
28+
findMany({ args, query }: any) {
29+
args = args ?? {};
30+
args.where = { ...args.where, y: { lt: 300 } };
31+
return query(args);
32+
},
33+
},
34+
},
35+
});
36+
37+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
38+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
39+
});
40+
41+
it('query override all models', async () => {
42+
const db = await createPolicyTestClient(
43+
`
44+
model Model {
45+
id String @id @default(uuid())
46+
x Int
47+
y Int
48+
49+
@@allow('read', x > 0)
50+
}
51+
`,
52+
);
53+
54+
const rawDb = db.$unuseAll();
55+
await rawDb.model.create({ data: { x: 0, y: 100 } });
56+
await rawDb.model.create({ data: { x: 1, y: 200 } });
57+
await rawDb.model.create({ data: { x: 2, y: 300 } });
58+
59+
const ext = definePlugin({
60+
id: 'prisma-extension-queryOverride',
61+
onQuery: {
62+
$allModels: {
63+
async findMany({ args, query }: any) {
64+
args = args ?? {};
65+
args.where = { ...args.where, y: { lt: 300 } };
66+
return query(args);
67+
},
68+
},
69+
},
70+
});
71+
72+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
73+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
74+
});
75+
76+
it('query override all operations', async () => {
77+
const db = await createPolicyTestClient(
78+
`
79+
model Model {
80+
id String @id @default(uuid())
81+
x Int
82+
y Int
83+
84+
@@allow('read', x > 0)
85+
}
86+
`,
87+
);
88+
89+
const rawDb = db.$unuseAll();
90+
await rawDb.model.create({ data: { x: 0, y: 100 } });
91+
await rawDb.model.create({ data: { x: 1, y: 200 } });
92+
await rawDb.model.create({ data: { x: 2, y: 300 } });
93+
94+
const ext = definePlugin({
95+
id: 'prisma-extension-queryOverride',
96+
onQuery: {
97+
model: {
98+
async $allOperations({ args, query }: any) {
99+
args = args ?? {};
100+
args.where = { ...args.where, y: { lt: 300 } };
101+
return query(args);
102+
},
103+
},
104+
},
105+
});
106+
107+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
108+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
109+
});
110+
111+
it('query override everything', async () => {
112+
const db = await createPolicyTestClient(
113+
`
114+
model Model {
115+
id String @id @default(uuid())
116+
x Int
117+
y Int
118+
119+
@@allow('read', x > 0)
120+
}
121+
`,
122+
);
123+
124+
const rawDb = db.$unuseAll();
125+
await rawDb.model.create({ data: { x: 0, y: 100 } });
126+
await rawDb.model.create({ data: { x: 1, y: 200 } });
127+
await rawDb.model.create({ data: { x: 2, y: 300 } });
128+
129+
const ext = definePlugin({
130+
id: 'prisma-extension-queryOverride',
131+
onQuery: {
132+
$allModels: {
133+
$allOperations({ args, query }: any) {
134+
args = args ?? {};
135+
args.where = { ...args.where, y: { lt: 300 } };
136+
return query(args);
137+
},
138+
},
139+
},
140+
});
141+
142+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
143+
await expect(db.$use(ext).model.findMany()).resolves.toHaveLength(1);
144+
});
145+
146+
it('result mutation', async () => {
147+
const db = await createPolicyTestClient(
148+
`
149+
model Model {
150+
id String @id @default(uuid())
151+
value Int
152+
153+
@@allow('read', value > 0)
154+
}
155+
`,
156+
);
157+
158+
const rawDb = db.$unuseAll();
159+
await rawDb.model.create({ data: { value: 0 } });
160+
await rawDb.model.create({ data: { value: 1 } });
161+
162+
const ext = definePlugin({
163+
id: 'prisma-extension-resultMutation',
164+
onQuery: {
165+
model: {
166+
async findMany({ args, query }: any) {
167+
const r: any = await query(args);
168+
for (let i = 0; i < r.length; i++) {
169+
r[i].value = r[i].value + 1;
170+
}
171+
return r;
172+
},
173+
},
174+
},
175+
});
176+
177+
const expected = [expect.objectContaining({ value: 2 })];
178+
await expect(db.$use(ext).model.findMany()).resolves.toEqual(expected);
179+
await expect(db.$use(ext).model.findMany()).resolves.toEqual(expected);
180+
});
181+
});

0 commit comments

Comments
 (0)