@geekmidas/testkit 9.0.2 → 10.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{Factory-CUM2767q.d.cts → Factory-Cwzho3c8.d.cts} +2 -2
- package/dist/{Factory-ClyZLRsO.d.mts.map → Factory-Cwzho3c8.d.cts.map} +1 -1
- package/dist/{Factory-ClyZLRsO.d.mts → Factory-SFupxRC2.d.mts} +2 -2
- package/dist/{Factory-CUM2767q.d.cts.map → Factory-SFupxRC2.d.mts.map} +1 -1
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-OMwcuAX_.d.cts → KyselyFactory-hMcVtvJq.d.cts} +3 -3
- package/dist/{KyselyFactory-CTZmiGI9.d.mts.map → KyselyFactory-hMcVtvJq.d.cts.map} +1 -1
- package/dist/{KyselyFactory-CTZmiGI9.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -3
- package/dist/{KyselyFactory-OMwcuAX_.d.cts.map → KyselyFactory-vAxYodck.d.mts.map} +1 -1
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/{ObjectionFactory-DDrKwAoO.d.mts → ObjectionFactory-BWjB49-i.d.mts} +3 -3
- package/dist/{ObjectionFactory-DDrKwAoO.d.mts.map → ObjectionFactory-BWjB49-i.d.mts.map} +1 -1
- package/dist/{ObjectionFactory-DvmZVHhe.d.cts → ObjectionFactory-DW-qwnqO.d.cts} +3 -3
- package/dist/{ObjectionFactory-DvmZVHhe.d.cts.map → ObjectionFactory-DW-qwnqO.d.cts.map} +1 -1
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/{VitestKyselyTransactionIsolator-CfSMOFlN.mjs → VitestKyselyTransactionIsolator-1Saieke7.mjs} +10 -3
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-0CqOsuc9.cjs → VitestKyselyTransactionIsolator-BjJSXryR.cjs} +10 -3
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-CduJlHoT.d.cts → VitestKyselyTransactionIsolator-CwlTo6yf.d.mts} +8 -3
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-Cswnnj0k.d.mts → VitestKyselyTransactionIsolator-DJasVuIZ.d.cts} +8 -3
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/{VitestObjectionTransactionIsolator-CNo40EdC.mjs → VitestObjectionTransactionIsolator-B_VSWFum.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-CNo40EdC.mjs.map → VitestObjectionTransactionIsolator-B_VSWFum.mjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-BXoR6xdG.d.cts → VitestObjectionTransactionIsolator-CTTjvTPO.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map → VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-x6hY5j4u.d.mts → VitestObjectionTransactionIsolator-CyZG6nq4.d.mts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map → VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-B5J9jjKq.cjs → VitestObjectionTransactionIsolator-DDoJTu7e.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-B5J9jjKq.cjs.map → VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map} +1 -1
- package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
- package/dist/{VitestTransactionIsolator-BNWJqh9f.d.mts → VitestTransactionIsolator-BLaw80cx.d.mts} +20 -4
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +1 -0
- package/dist/{VitestTransactionIsolator-D6ZxLWys.mjs → VitestTransactionIsolator-CvwPecpl.mjs} +17 -4
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CtZaOUjH.cjs → VitestTransactionIsolator-DyiX-QtK.cjs} +17 -4
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CSroc7Df.d.cts → VitestTransactionIsolator-glcxyS_R.d.cts} +20 -4
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.mts +2 -2
- package/dist/{directory-DGOcVlKD.d.cts → directory-Bpz5c5pj.d.cts} +3 -3
- package/dist/{directory-DGOcVlKD.d.cts.map → directory-Bpz5c5pj.d.cts.map} +1 -1
- package/dist/{faker-BcjUfHxx.d.mts → faker-DHh7xs4u.d.mts} +3 -3
- package/dist/{faker-BcjUfHxx.d.mts.map → faker-DHh7xs4u.d.mts.map} +1 -1
- package/dist/{faker-zVCm31nU.d.cts → faker-Dg3trU4a.d.cts} +3 -3
- package/dist/{faker-zVCm31nU.d.cts.map → faker-Dg3trU4a.d.cts.map} +1 -1
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +5 -5
- package/dist/kysely.d.mts +5 -5
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +2 -2
- package/dist/objection.d.cts +5 -5
- package/dist/objection.d.mts +5 -5
- package/dist/objection.mjs +2 -2
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/requestContext.cjs +1 -1
- package/dist/requestContext.d.cts +1 -1
- package/dist/requestContext.d.mts +1 -1
- package/dist/requestContext.mjs +1 -1
- package/package.json +10 -7
- package/CHANGELOG.md +0 -151
- package/dist/VitestKyselyTransactionIsolator-0CqOsuc9.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CfSMOFlN.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +0 -1
- package/dist/VitestTransactionIsolator-CtZaOUjH.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-D6ZxLWys.mjs.map +0 -1
- package/src/Factory.ts +0 -174
- package/src/KyselyFactory.ts +0 -391
- package/src/ObjectionFactory.ts +0 -412
- package/src/PostgresKyselyMigrator.ts +0 -104
- package/src/PostgresMigrator.ts +0 -193
- package/src/PostgresObjectionMigrator.ts +0 -138
- package/src/VitestKyselyTransactionIsolator.ts +0 -72
- package/src/VitestObjectionTransactionIsolator.ts +0 -75
- package/src/VitestTransactionIsolator.ts +0 -348
- package/src/__tests__/Factory.spec.ts +0 -178
- package/src/__tests__/KyselyFactory.spec.ts +0 -458
- package/src/__tests__/ObjectionFactory.spec.ts +0 -586
- package/src/__tests__/PostgresKyselyMigrator.spec.ts +0 -794
- package/src/__tests__/PostgresMigrator.spec.ts +0 -471
- package/src/__tests__/PostgresObjectionMigrator.spec.ts +0 -634
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -138
- package/src/__tests__/benchmark.spec.ts +0 -140
- package/src/__tests__/better-auth.spec.ts +0 -21
- package/src/__tests__/faker.spec.ts +0 -231
- package/src/__tests__/initScript.spec.ts +0 -332
- package/src/__tests__/integration.spec.ts +0 -610
- package/src/__tests__/requestContext.spec.ts +0 -113
- package/src/__tests__/utilities.spec.ts +0 -211
- package/src/aws.ts +0 -131
- package/src/benchmark.ts +0 -48
- package/src/better-auth.ts +0 -310
- package/src/faker.ts +0 -349
- package/src/helpers.ts +0 -45
- package/src/initScript.ts +0 -122
- package/src/kysely.ts +0 -166
- package/src/logger.ts +0 -18
- package/src/objection.ts +0 -157
- package/src/os/directory.ts +0 -26
- package/src/os/index.ts +0 -1
- package/src/requestContext.ts +0 -119
- package/src/timer.ts +0 -3
- package/test/globalSetup.ts +0 -54
- package/test/helpers.ts +0 -268
- package/test/migrations/1749664623372_user.ts +0 -22
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -8
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
import { writeFileSync } from 'node:fs';
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import pg from 'pg';
|
|
5
|
-
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
6
|
-
import { parseInitScript, runInitScript } from '../initScript';
|
|
7
|
-
|
|
8
|
-
describe('parseInitScript', () => {
|
|
9
|
-
it('should extract SQL from EOSQL heredoc blocks', () => {
|
|
10
|
-
const script = `#!/bin/bash
|
|
11
|
-
set -e
|
|
12
|
-
|
|
13
|
-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
14
|
-
CREATE USER api WITH PASSWORD '$API_DB_PASSWORD';
|
|
15
|
-
GRANT ALL ON SCHEMA public TO api;
|
|
16
|
-
EOSQL
|
|
17
|
-
`;
|
|
18
|
-
|
|
19
|
-
const blocks = parseInitScript(script, {
|
|
20
|
-
POSTGRES_USER: 'app',
|
|
21
|
-
POSTGRES_DB: 'mydb',
|
|
22
|
-
API_DB_PASSWORD: 'secret123',
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
expect(blocks).toHaveLength(1);
|
|
26
|
-
expect(blocks[0]).toContain("CREATE USER api WITH PASSWORD 'secret123'");
|
|
27
|
-
expect(blocks[0]).toContain('GRANT ALL ON SCHEMA public TO api');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should extract multiple heredoc blocks', () => {
|
|
31
|
-
const script = `#!/bin/bash
|
|
32
|
-
set -e
|
|
33
|
-
|
|
34
|
-
psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
35
|
-
CREATE USER api WITH PASSWORD '$API_DB_PASSWORD';
|
|
36
|
-
EOSQL
|
|
37
|
-
|
|
38
|
-
psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
39
|
-
CREATE USER auth WITH PASSWORD '$AUTH_DB_PASSWORD';
|
|
40
|
-
CREATE SCHEMA auth AUTHORIZATION auth;
|
|
41
|
-
EOSQL
|
|
42
|
-
|
|
43
|
-
echo "Done!"
|
|
44
|
-
`;
|
|
45
|
-
|
|
46
|
-
const blocks = parseInitScript(script, {
|
|
47
|
-
POSTGRES_USER: 'app',
|
|
48
|
-
POSTGRES_DB: 'mydb',
|
|
49
|
-
API_DB_PASSWORD: 'apipass',
|
|
50
|
-
AUTH_DB_PASSWORD: 'authpass',
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
expect(blocks).toHaveLength(2);
|
|
54
|
-
expect(blocks[0]).toContain("CREATE USER api WITH PASSWORD 'apipass'");
|
|
55
|
-
expect(blocks[1]).toContain("CREATE USER auth WITH PASSWORD 'authpass'");
|
|
56
|
-
expect(blocks[1]).toContain('CREATE SCHEMA auth AUTHORIZATION auth');
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should substitute $VAR_NAME syntax', () => {
|
|
60
|
-
const script = `psql <<-EOSQL
|
|
61
|
-
CREATE USER $APP_USER WITH PASSWORD '$APP_PASSWORD';
|
|
62
|
-
EOSQL`;
|
|
63
|
-
|
|
64
|
-
const blocks = parseInitScript(script, {
|
|
65
|
-
APP_USER: 'myuser',
|
|
66
|
-
APP_PASSWORD: 'mypass',
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
expect(blocks[0]).toContain("CREATE USER myuser WITH PASSWORD 'mypass'");
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should substitute ${VAR_NAME} syntax', () => {
|
|
73
|
-
const script = `psql <<-EOSQL
|
|
74
|
-
CREATE USER \${APP_USER} WITH PASSWORD '\${APP_PASSWORD}';
|
|
75
|
-
EOSQL`;
|
|
76
|
-
|
|
77
|
-
const blocks = parseInitScript(script, {
|
|
78
|
-
APP_USER: 'myuser',
|
|
79
|
-
APP_PASSWORD: 'mypass',
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
expect(blocks[0]).toContain("CREATE USER myuser WITH PASSWORD 'mypass'");
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('should replace missing vars with empty string', () => {
|
|
86
|
-
const script = `psql <<-EOSQL
|
|
87
|
-
CREATE USER api WITH PASSWORD '$MISSING_VAR';
|
|
88
|
-
EOSQL`;
|
|
89
|
-
|
|
90
|
-
const blocks = parseInitScript(script, {});
|
|
91
|
-
|
|
92
|
-
expect(blocks[0]).toContain("CREATE USER api WITH PASSWORD ''");
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should unescape bash-escaped dollar signs for PL/pgSQL blocks', () => {
|
|
96
|
-
const script = `psql <<-EOSQL
|
|
97
|
-
DO \\$\\$
|
|
98
|
-
BEGIN
|
|
99
|
-
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'api') THEN
|
|
100
|
-
CREATE USER api WITH PASSWORD '$API_DB_PASSWORD';
|
|
101
|
-
ELSE
|
|
102
|
-
ALTER USER api WITH PASSWORD '$API_DB_PASSWORD';
|
|
103
|
-
END IF;
|
|
104
|
-
END
|
|
105
|
-
\\$\\$;
|
|
106
|
-
EOSQL`;
|
|
107
|
-
|
|
108
|
-
const blocks = parseInitScript(script, {
|
|
109
|
-
API_DB_PASSWORD: 'secret',
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
expect(blocks).toHaveLength(1);
|
|
113
|
-
expect(blocks[0]).toContain('DO $$');
|
|
114
|
-
expect(blocks[0]).toContain('END\n $$;');
|
|
115
|
-
expect(blocks[0]).toContain("CREATE USER api WITH PASSWORD 'secret'");
|
|
116
|
-
expect(blocks[0]).not.toContain('\\$');
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('should return empty array for script with no heredocs', () => {
|
|
120
|
-
const script = `#!/bin/bash
|
|
121
|
-
echo "Hello world"
|
|
122
|
-
`;
|
|
123
|
-
|
|
124
|
-
const blocks = parseInitScript(script, {});
|
|
125
|
-
|
|
126
|
-
expect(blocks).toEqual([]);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('should handle the full generated init.sh format', () => {
|
|
130
|
-
const script = `#!/bin/bash
|
|
131
|
-
set -e
|
|
132
|
-
|
|
133
|
-
# Auto-generated PostgreSQL init script
|
|
134
|
-
# Creates per-app users with separate schemas in a single database
|
|
135
|
-
# - api: uses public schema
|
|
136
|
-
# - auth: uses auth schema (search_path=auth)
|
|
137
|
-
|
|
138
|
-
# Create api user (uses public schema)
|
|
139
|
-
echo "Creating user api..."
|
|
140
|
-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
141
|
-
CREATE USER api WITH PASSWORD '$API_DB_PASSWORD';
|
|
142
|
-
GRANT ALL ON SCHEMA public TO api;
|
|
143
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO api;
|
|
144
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO api;
|
|
145
|
-
EOSQL
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
# Create auth user with dedicated schema
|
|
149
|
-
echo "Creating user auth with schema auth..."
|
|
150
|
-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
151
|
-
CREATE USER auth WITH PASSWORD '$AUTH_DB_PASSWORD';
|
|
152
|
-
CREATE SCHEMA auth AUTHORIZATION auth;
|
|
153
|
-
ALTER USER auth SET search_path TO auth;
|
|
154
|
-
GRANT USAGE ON SCHEMA auth TO auth;
|
|
155
|
-
GRANT ALL ON ALL TABLES IN SCHEMA auth TO auth;
|
|
156
|
-
GRANT ALL ON ALL SEQUENCES IN SCHEMA auth TO auth;
|
|
157
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT ALL ON TABLES TO auth;
|
|
158
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT ALL ON SEQUENCES TO auth;
|
|
159
|
-
EOSQL
|
|
160
|
-
|
|
161
|
-
echo "Database initialization complete!"
|
|
162
|
-
`;
|
|
163
|
-
|
|
164
|
-
const blocks = parseInitScript(script, {
|
|
165
|
-
POSTGRES_USER: 'app',
|
|
166
|
-
POSTGRES_DB: 'residentman_dev_test',
|
|
167
|
-
API_DB_PASSWORD: 'apipass123',
|
|
168
|
-
AUTH_DB_PASSWORD: 'authpass456',
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
expect(blocks).toHaveLength(2);
|
|
172
|
-
|
|
173
|
-
// API block
|
|
174
|
-
expect(blocks[0]).toContain("CREATE USER api WITH PASSWORD 'apipass123'");
|
|
175
|
-
expect(blocks[0]).toContain('GRANT ALL ON SCHEMA public TO api');
|
|
176
|
-
expect(blocks[0]).toContain('ALTER DEFAULT PRIVILEGES IN SCHEMA public');
|
|
177
|
-
|
|
178
|
-
// Auth block
|
|
179
|
-
expect(blocks[1]).toContain("CREATE USER auth WITH PASSWORD 'authpass456'");
|
|
180
|
-
expect(blocks[1]).toContain('CREATE SCHEMA auth AUTHORIZATION auth');
|
|
181
|
-
expect(blocks[1]).toContain('ALTER USER auth SET search_path TO auth');
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const PG_CONFIG = {
|
|
186
|
-
host: 'localhost',
|
|
187
|
-
port: 5432,
|
|
188
|
-
user: 'geekmidas',
|
|
189
|
-
password: 'geekmidas',
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Helper to run queries against the postgres admin database.
|
|
194
|
-
*/
|
|
195
|
-
async function adminQuery(...queries: string[]): Promise<void> {
|
|
196
|
-
const client = new pg.Client({ ...PG_CONFIG, database: 'postgres' });
|
|
197
|
-
try {
|
|
198
|
-
await client.connect();
|
|
199
|
-
for (const sql of queries) {
|
|
200
|
-
await client.query(sql);
|
|
201
|
-
}
|
|
202
|
-
} finally {
|
|
203
|
-
await client.end();
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Force-drop a role by reassigning owned objects and revoking privileges first.
|
|
209
|
-
*/
|
|
210
|
-
async function forceDropRole(role: string): Promise<void> {
|
|
211
|
-
// Find all databases where this role might own objects
|
|
212
|
-
const client = new pg.Client({ ...PG_CONFIG, database: 'postgres' });
|
|
213
|
-
try {
|
|
214
|
-
await client.connect();
|
|
215
|
-
const result = await client.query(
|
|
216
|
-
"SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres'",
|
|
217
|
-
);
|
|
218
|
-
for (const row of result.rows) {
|
|
219
|
-
const dbClient = new pg.Client({
|
|
220
|
-
...PG_CONFIG,
|
|
221
|
-
database: row.datname,
|
|
222
|
-
});
|
|
223
|
-
try {
|
|
224
|
-
await dbClient.connect();
|
|
225
|
-
await dbClient.query(`REASSIGN OWNED BY ${role} TO ${PG_CONFIG.user}`);
|
|
226
|
-
await dbClient.query(`DROP OWNED BY ${role}`);
|
|
227
|
-
} catch {
|
|
228
|
-
// Role might not exist in this database, ignore
|
|
229
|
-
} finally {
|
|
230
|
-
await dbClient.end();
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
await client.query(`DROP ROLE IF EXISTS ${role}`);
|
|
234
|
-
} finally {
|
|
235
|
-
await client.end();
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
describe('runInitScript', () => {
|
|
240
|
-
const dbName = `test_init_script_${Date.now()}`;
|
|
241
|
-
const dbUrl = `postgresql://${PG_CONFIG.user}:${PG_CONFIG.password}@${PG_CONFIG.host}:${PG_CONFIG.port}/${dbName}`;
|
|
242
|
-
|
|
243
|
-
beforeAll(async () => {
|
|
244
|
-
// Clean up stale state from previous failed runs
|
|
245
|
-
await forceDropRole('test_api');
|
|
246
|
-
await forceDropRole('test_auth');
|
|
247
|
-
await adminQuery(
|
|
248
|
-
`DROP DATABASE IF EXISTS "${dbName}"`,
|
|
249
|
-
`CREATE DATABASE "${dbName}"`,
|
|
250
|
-
);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
afterAll(async () => {
|
|
254
|
-
// Drop the database first (removes object dependencies), then roles
|
|
255
|
-
await adminQuery(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
256
|
-
await forceDropRole('test_api');
|
|
257
|
-
await forceDropRole('test_auth');
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
it('should create users and schemas from init script', async () => {
|
|
261
|
-
// Write a test init script to a temp file
|
|
262
|
-
const scriptPath = join(tmpdir(), `init-${Date.now()}.sh`);
|
|
263
|
-
writeFileSync(
|
|
264
|
-
scriptPath,
|
|
265
|
-
`#!/bin/bash
|
|
266
|
-
set -e
|
|
267
|
-
|
|
268
|
-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
269
|
-
CREATE USER test_api WITH PASSWORD '$API_DB_PASSWORD';
|
|
270
|
-
GRANT ALL ON SCHEMA public TO test_api;
|
|
271
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO test_api;
|
|
272
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO test_api;
|
|
273
|
-
EOSQL
|
|
274
|
-
|
|
275
|
-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
276
|
-
CREATE USER test_auth WITH PASSWORD '$AUTH_DB_PASSWORD';
|
|
277
|
-
CREATE SCHEMA IF NOT EXISTS test_auth AUTHORIZATION test_auth;
|
|
278
|
-
ALTER USER test_auth SET search_path TO test_auth;
|
|
279
|
-
GRANT USAGE ON SCHEMA test_auth TO test_auth;
|
|
280
|
-
GRANT ALL ON ALL TABLES IN SCHEMA test_auth TO test_auth;
|
|
281
|
-
GRANT ALL ON ALL SEQUENCES IN SCHEMA test_auth TO test_auth;
|
|
282
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA test_auth GRANT ALL ON TABLES TO test_auth;
|
|
283
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA test_auth GRANT ALL ON SEQUENCES TO test_auth;
|
|
284
|
-
EOSQL
|
|
285
|
-
|
|
286
|
-
echo "Done!"
|
|
287
|
-
`,
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
// Run the init script against the test database
|
|
291
|
-
await runInitScript(scriptPath, dbUrl, {
|
|
292
|
-
POSTGRES_USER: PG_CONFIG.user,
|
|
293
|
-
POSTGRES_DB: dbName,
|
|
294
|
-
API_DB_PASSWORD: 'apipass',
|
|
295
|
-
AUTH_DB_PASSWORD: 'authpass',
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
// Verify: connect as test_api and check access to public schema
|
|
299
|
-
const apiClient = new pg.Client({
|
|
300
|
-
...PG_CONFIG,
|
|
301
|
-
user: 'test_api',
|
|
302
|
-
password: 'apipass',
|
|
303
|
-
database: dbName,
|
|
304
|
-
});
|
|
305
|
-
await apiClient.connect();
|
|
306
|
-
await apiClient.query(
|
|
307
|
-
'CREATE TABLE IF NOT EXISTS api_test_table (id serial PRIMARY KEY)',
|
|
308
|
-
);
|
|
309
|
-
const apiResult = await apiClient.query(
|
|
310
|
-
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'api_test_table'",
|
|
311
|
-
);
|
|
312
|
-
expect(apiResult.rowCount).toBe(1);
|
|
313
|
-
await apiClient.end();
|
|
314
|
-
|
|
315
|
-
// Verify: connect as test_auth and check dedicated schema
|
|
316
|
-
const authClient = new pg.Client({
|
|
317
|
-
...PG_CONFIG,
|
|
318
|
-
user: 'test_auth',
|
|
319
|
-
password: 'authpass',
|
|
320
|
-
database: dbName,
|
|
321
|
-
});
|
|
322
|
-
await authClient.connect();
|
|
323
|
-
const schemaResult = await authClient.query(
|
|
324
|
-
"SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'test_auth'",
|
|
325
|
-
);
|
|
326
|
-
expect(schemaResult.rowCount).toBe(1);
|
|
327
|
-
await authClient.query(
|
|
328
|
-
'CREATE TABLE IF NOT EXISTS test_auth.auth_test_table (id serial PRIMARY KEY)',
|
|
329
|
-
);
|
|
330
|
-
await authClient.end();
|
|
331
|
-
});
|
|
332
|
-
});
|