Skip to content

Commit 48cc5a9

Browse files
committed
test(policy): move raw sql regression to e2e
1 parent 41a503f commit 48cc5a9

4 files changed

Lines changed: 39 additions & 57 deletions

File tree

packages/plugins/policy/package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"build": "tsc --noEmit && tsup-node",
88
"watch": "tsup-node --watch",
99
"lint": "eslint src --ext ts",
10-
"test": "vitest run",
1110
"pack": "pnpm pack"
1211
},
1312
"keywords": [],
@@ -47,10 +46,7 @@
4746
},
4847
"devDependencies": {
4948
"@types/better-sqlite3": "catalog:",
50-
"@types/pg": "^8.0.0",
5149
"@zenstackhq/eslint-config": "workspace:*",
52-
"@zenstackhq/testtools": "workspace:*",
53-
"@zenstackhq/typescript-config": "workspace:*",
54-
"@zenstackhq/vitest-config": "workspace:*"
50+
"@zenstackhq/typescript-config": "workspace:*"
5551
}
5652
}

packages/plugins/policy/vitest.config.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/plugins/policy/test/raw-sql.test.ts renamed to tests/e2e/orm/policy/raw-sql.test.ts

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { PolicyPlugin } from '@zenstackhq/plugin-policy';
12
import type { ClientContract } from '@zenstackhq/orm';
23
import type { SchemaDef } from '@zenstackhq/orm/schema';
34
import { createTestClient } from '@zenstackhq/testtools';
4-
import { beforeAll, describe, expect, it } from 'vitest';
5-
import { PolicyPlugin } from '../src/plugin';
5+
import { sql } from 'kysely';
6+
import { afterEach, describe, expect, it } from 'vitest';
67

78
const schema = `
89
model User {
@@ -24,49 +25,42 @@ model Secret {
2425
}
2526
`;
2627

27-
describe('PolicyPlugin raw SQL', () => {
28-
let unsafeClient: ClientContract<SchemaDef>;
29-
let rawClient: ClientContract<SchemaDef>;
30-
let adminClient: ClientContract<SchemaDef>;
31-
let defaultClient: ClientContract<SchemaDef>;
32-
let defaultRawClient: ClientContract<SchemaDef>;
33-
let defaultAdminClient: ClientContract<SchemaDef>;
34-
35-
beforeAll(async () => {
36-
unsafeClient = await createTestClient(schema, {
37-
plugins: [new PolicyPlugin({ dangerouslyAllowRawSql: true })],
38-
provider: 'postgresql',
39-
dbName: 'policy_raw_sql_dangerous',
40-
});
41-
rawClient = unsafeClient.$unuseAll();
42-
adminClient = unsafeClient.$setAuth({ id: 'admin', role: 'admin' });
28+
describe('Policy raw SQL tests', () => {
29+
const clients: ClientContract<SchemaDef>[] = [];
4330

44-
await rawClient.user.create({
45-
data: {
46-
id: 'admin',
47-
role: 'admin',
48-
},
49-
});
31+
afterEach(async () => {
32+
await Promise.all(clients.splice(0).map((client) => client.$disconnect()));
33+
});
5034

51-
defaultClient = await createTestClient(schema, {
52-
plugins: [new PolicyPlugin()],
53-
provider: 'postgresql',
54-
dbName: 'policy_raw_sql_default',
35+
function ref(client: ClientContract<SchemaDef>, col: string) {
36+
return client.$schema.provider.type === 'mysql' ? sql.raw(`\`${col}\``) : sql.raw(`"${col}"`);
37+
}
38+
39+
async function createPolicyClient(options?: { dangerouslyAllowRawSql?: boolean; dbName: string }) {
40+
const unsafeClient = await createTestClient(schema, {
41+
dbName: options?.dbName,
42+
plugins: [new PolicyPlugin({ dangerouslyAllowRawSql: options?.dangerouslyAllowRawSql })],
5543
});
56-
defaultRawClient = defaultClient.$unuseAll();
57-
defaultAdminClient = defaultClient.$setAuth({ id: 'admin', role: 'admin' });
44+
clients.push(unsafeClient);
45+
46+
const rawClient = unsafeClient.$unuseAll();
47+
const adminClient = unsafeClient.$setAuth({ id: 'admin', role: 'admin' });
5848

59-
await defaultRawClient.user.create({
49+
await rawClient.user.create({
6050
data: {
6151
id: 'admin',
6252
role: 'admin',
6353
},
6454
});
65-
});
55+
56+
return { adminClient };
57+
}
6658

6759
it('keeps rejecting raw SQL by default', async () => {
60+
const { adminClient } = await createPolicyClient({ dbName: 'policy_raw_sql_default' });
61+
6862
await expect(
69-
defaultAdminClient.$transaction(async (tx) => {
63+
adminClient.$transaction(async (tx) => {
7064
await tx.secret.create({
7165
data: {
7266
id: 'secret-default',
@@ -76,15 +70,20 @@ describe('PolicyPlugin raw SQL', () => {
7670
});
7771

7872
await tx.$queryRaw<{ value: string }[]>`
79-
SELECT "value"
80-
FROM "Secret"
81-
WHERE "id" = ${'secret-default'}
73+
SELECT ${ref(tx, 'Secret')}.${ref(tx, 'value')}
74+
FROM ${ref(tx, 'Secret')}
75+
WHERE ${ref(tx, 'Secret')}.${ref(tx, 'id')} = ${'secret-default'}
8276
`;
8377
}),
8478
).rejects.toThrow('non-CRUD queries are not allowed');
8579
});
8680

8781
it('allows raw SQL inside a transaction when dangerous raw SQL is enabled', async () => {
82+
const { adminClient } = await createPolicyClient({
83+
dangerouslyAllowRawSql: true,
84+
dbName: 'policy_raw_sql_dangerous',
85+
});
86+
8887
await adminClient.$transaction(async (tx) => {
8988
await tx.secret.create({
9089
data: {
@@ -95,9 +94,9 @@ describe('PolicyPlugin raw SQL', () => {
9594
});
9695

9796
const rows = await tx.$queryRaw<{ value: string }[]>`
98-
SELECT "value"
99-
FROM "Secret"
100-
WHERE "id" = ${'secret-1'}
97+
SELECT ${ref(tx, 'Secret')}.${ref(tx, 'value')}
98+
FROM ${ref(tx, 'Secret')}
99+
WHERE ${ref(tx, 'Secret')}.${ref(tx, 'id')} = ${'secret-1'}
101100
`;
102101

103102
expect(rows).toEqual([{ value: 'top-secret' }]);

0 commit comments

Comments
 (0)