pgsql-test 2.0.3 → 2.0.5
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/README.md +78 -15
- package/connect.d.ts +3 -3
- package/connect.js +1 -1
- package/esm/connect.js +1 -1
- package/package.json +5 -5
- package/seed/types.d.ts +2 -2
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ Part of the [LaunchQL](https://github.com/launchql) ecosystem, `pgsql-test` is b
|
|
|
47
47
|
1. [Install](#install)
|
|
48
48
|
2. [Features](#features)
|
|
49
49
|
3. [Quick Start](#-quick-start)
|
|
50
|
-
4. [getConnections() Overview](#getconnections-overview)
|
|
50
|
+
4. [`getConnections()` Overview](#getconnections-overview)
|
|
51
51
|
5. [PgTestClient API Overview](#pgtestclient-api-overview)
|
|
52
52
|
6. [Usage Examples](#usage-examples)
|
|
53
53
|
* [Basic Setup](#-basic-setup)
|
|
@@ -58,7 +58,7 @@ Part of the [LaunchQL](https://github.com/launchql) ecosystem, `pgsql-test` is b
|
|
|
58
58
|
* [JSON Seeding](#️-json-seeding)
|
|
59
59
|
* [Sqitch Seeding](#️-sqitch-seeding)
|
|
60
60
|
* [LaunchQL Seeding](#-launchql-seeding)
|
|
61
|
-
7. [
|
|
61
|
+
7. [`getConnections() Options` ](#getconnections-options)
|
|
62
62
|
8. [Disclaimer](#disclaimer)
|
|
63
63
|
|
|
64
64
|
|
|
@@ -150,6 +150,25 @@ test('user count starts at 2', async () => {
|
|
|
150
150
|
|
|
151
151
|
### 🔐 Role-Based Context
|
|
152
152
|
|
|
153
|
+
|
|
154
|
+
The `pgsql-test` framework provides powerful tools to simulate authentication contexts during tests, which is particularly useful when testing Row-Level Security (RLS) policies.
|
|
155
|
+
|
|
156
|
+
#### Setting Test Context
|
|
157
|
+
|
|
158
|
+
Use `setContext()` to simulate different user roles and JWT claims:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
db.setContext({
|
|
162
|
+
role: 'authenticated',
|
|
163
|
+
'jwt.claims.user_id': '123',
|
|
164
|
+
'jwt.claims.org_id': 'acme'
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
This applies the settings using `SET LOCAL` statements, ensuring they persist only for the current transaction and maintain proper isolation between tests.
|
|
169
|
+
|
|
170
|
+
#### Testing Role-Based Access
|
|
171
|
+
|
|
153
172
|
```ts
|
|
154
173
|
describe('authenticated role', () => {
|
|
155
174
|
beforeEach(async () => {
|
|
@@ -166,6 +185,22 @@ describe('authenticated role', () => {
|
|
|
166
185
|
});
|
|
167
186
|
```
|
|
168
187
|
|
|
188
|
+
#### Database Connection Options
|
|
189
|
+
|
|
190
|
+
For non-superuser testing, use the connection options described in the [options](#getconnections-options) section. The `db.connection` property allows you to customize the non-privileged user account for your tests.
|
|
191
|
+
|
|
192
|
+
Use `setContext()` to simulate Role-Based Access Control (RBAC) during tests. This is useful when testing Row-Level Security (RLS) policies. Your actual server should manage role/user claims via secure tokens (e.g., setting `current_setting('jwt.claims.user_id')`), but this interface helps emulate those behaviors in test environments.
|
|
193
|
+
|
|
194
|
+
#### Common Testing Scenarios
|
|
195
|
+
|
|
196
|
+
This approach enables testing various access patterns:
|
|
197
|
+
- Authenticated vs. anonymous user access
|
|
198
|
+
- Per-user data filtering
|
|
199
|
+
- Admin privilege bypass behavior
|
|
200
|
+
- Custom claim-based restrictions (organization membership, admin status)
|
|
201
|
+
|
|
202
|
+
> **Note:** While this interface helps simulate RBAC for testing, your production server should manage user/role claims via secure authentication tokens, typically by setting values like `current_setting('jwt.claims.user_id')` through proper authentication middleware.
|
|
203
|
+
|
|
169
204
|
### 🔌 SQL File Seeding
|
|
170
205
|
|
|
171
206
|
Use `.sql` files to set up your database state before tests:
|
|
@@ -379,23 +414,51 @@ LaunchQL provides the best of both worlds:
|
|
|
379
414
|
|
|
380
415
|
By maintaining Sqitch compatibility while supercharging performance, LaunchQL enables you to keep your existing migration patterns while enjoying the speed benefits of our TypeScript engine.
|
|
381
416
|
|
|
382
|
-
##
|
|
417
|
+
## `getConnections` Options
|
|
418
|
+
|
|
419
|
+
This table documents the available options for the `getConnections` function. The options are passed as a combination of `pg` and `db` configuration objects.
|
|
420
|
+
|
|
421
|
+
### `db` Options (PgTestConnectionOptions)
|
|
383
422
|
|
|
384
|
-
|
|
423
|
+
| Option | Type | Default | Description |
|
|
424
|
+
| ------------------------ | ---------- | ---------------- | --------------------------------------------------------------------------- |
|
|
425
|
+
| `db.extensions` | `string[]` | `[]` | Array of PostgreSQL extensions to include in the test database |
|
|
426
|
+
| `db.cwd` | `string` | `process.cwd()` | Working directory used for LaunchQL/Sqitch projects |
|
|
427
|
+
| `db.connection.user` | `string` | `'app_user'` | User for simulating RLS via `setContext()` |
|
|
428
|
+
| `db.connection.password` | `string` | `'app_password'` | Password for RLS test user |
|
|
429
|
+
| `db.connection.role` | `string` | `'anonymous'` | Default role used during `setContext()` |
|
|
430
|
+
| `db.template` | `string` | `undefined` | Template database used for faster test DB creation |
|
|
431
|
+
| `db.rootDb` | `string` | `'postgres'` | Root database used for administrative operations (e.g., creating databases) |
|
|
432
|
+
| `db.prefix` | `string` | `'db-'` | Prefix used when generating test database names |
|
|
385
433
|
|
|
386
|
-
|
|
387
|
-
* `PGPORT`
|
|
388
|
-
* `PGUSER`
|
|
389
|
-
* `PGPASSWORD`
|
|
434
|
+
### `pg` Options (PgConfig)
|
|
390
435
|
|
|
391
|
-
|
|
436
|
+
Environment variables will override these options when available:
|
|
392
437
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
438
|
+
* `PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, `PGDATABASE`
|
|
439
|
+
|
|
440
|
+
| Option | Type | Default | Description |
|
|
441
|
+
| ------------- | -------- | ------------- | ----------------------------------------------- |
|
|
442
|
+
| `pg.user` | `string` | `'postgres'` | Superuser for PostgreSQL |
|
|
443
|
+
| `pg.password` | `string` | `'password'` | Password for the PostgreSQL superuser |
|
|
444
|
+
| `pg.host` | `string` | `'localhost'` | Hostname for PostgreSQL |
|
|
445
|
+
| `pg.port` | `number` | `5423` | Port for PostgreSQL |
|
|
446
|
+
| `pg.database` | `string` | `'postgres'` | Default database used when connecting initially |
|
|
447
|
+
|
|
448
|
+
### Usage
|
|
449
|
+
|
|
450
|
+
```ts
|
|
451
|
+
const { conn, db, teardown } = await getConnections({
|
|
452
|
+
pg: { user: 'postgres', password: 'secret' },
|
|
453
|
+
db: {
|
|
454
|
+
extensions: ['uuid-ossp'],
|
|
455
|
+
cwd: '/path/to/project',
|
|
456
|
+
connection: { user: 'test_user', password: 'secret', role: 'authenticated' },
|
|
457
|
+
template: 'test_template',
|
|
458
|
+
prefix: 'test_',
|
|
459
|
+
rootDb: 'postgres'
|
|
460
|
+
}
|
|
461
|
+
});
|
|
399
462
|
```
|
|
400
463
|
|
|
401
464
|
## Disclaimer
|
package/connect.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DbAdmin } from './admin';
|
|
2
|
-
import {
|
|
2
|
+
import { PgTestConnectionOptions, PgConfig } from '@launchql/types';
|
|
3
3
|
import { PgTestConnector } from './manager';
|
|
4
4
|
import { SeedAdapter } from './seed/types';
|
|
5
5
|
import { PgTestClient } from './test-client';
|
|
6
|
-
export declare const getPgRootAdmin: (connOpts?:
|
|
6
|
+
export declare const getPgRootAdmin: (connOpts?: PgTestConnectionOptions) => DbAdmin;
|
|
7
7
|
export interface GetConnectionOpts {
|
|
8
8
|
pg?: Partial<PgConfig>;
|
|
9
|
-
db?: Partial<
|
|
9
|
+
db?: Partial<PgTestConnectionOptions>;
|
|
10
10
|
}
|
|
11
11
|
export interface GetConnectionResult {
|
|
12
12
|
pg: PgTestClient;
|
package/connect.js
CHANGED
|
@@ -27,7 +27,7 @@ const getConnOopts = (cn = {}) => {
|
|
|
27
27
|
db: connect
|
|
28
28
|
};
|
|
29
29
|
};
|
|
30
|
-
const getConnections = async (cn = {}, seedAdapters = []) => {
|
|
30
|
+
const getConnections = async (cn = {}, seedAdapters = [seed_1.seed.launchql()]) => {
|
|
31
31
|
cn = getConnOopts(cn);
|
|
32
32
|
const config = cn.pg;
|
|
33
33
|
const connOpts = cn.db;
|
package/esm/connect.js
CHANGED
|
@@ -23,7 +23,7 @@ const getConnOopts = (cn = {}) => {
|
|
|
23
23
|
db: connect
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
|
-
export const getConnections = async (cn = {}, seedAdapters = []) => {
|
|
26
|
+
export const getConnections = async (cn = {}, seedAdapters = [seed.launchql()]) => {
|
|
27
27
|
cn = getConnOopts(cn);
|
|
28
28
|
const config = cn.pg;
|
|
29
29
|
const connOpts = cn.db;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pgsql-test",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "PostgreSQL Testing in TypeScript",
|
|
6
6
|
"main": "index.js",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"@types/pg-copy-streams": "^1.2.5"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@launchql/migrate": "^2.0.
|
|
38
|
-
"@launchql/server-utils": "^2.0.
|
|
39
|
-
"@launchql/types": "^2.0.
|
|
37
|
+
"@launchql/migrate": "^2.0.14",
|
|
38
|
+
"@launchql/server-utils": "^2.0.7",
|
|
39
|
+
"@launchql/types": "^2.0.6",
|
|
40
40
|
"chalk": "^4.1.0",
|
|
41
41
|
"deepmerge": "^4.3.1",
|
|
42
42
|
"pg": "^8.16.0",
|
|
43
43
|
"pg-copy-streams": "^6.0.6"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "5697261a8f6e252d38b02b806272a19c5cd7a578"
|
|
46
46
|
}
|
package/seed/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { PgConfig,
|
|
1
|
+
import { PgConfig, PgTestConnectionOptions } from "@launchql/types";
|
|
2
2
|
import { DbAdmin } from "../admin";
|
|
3
3
|
import { PgTestClient } from "../test-client";
|
|
4
4
|
export interface SeedContext {
|
|
5
|
-
connect:
|
|
5
|
+
connect: PgTestConnectionOptions;
|
|
6
6
|
admin: DbAdmin;
|
|
7
7
|
config: PgConfig;
|
|
8
8
|
pg: PgTestClient;
|