|
1 | | -import { getConnections, PgTestClient } from 'pgsql-test'; |
| 1 | +import { getConnections, PgTestClient, seed } from 'pgsql-test'; |
2 | 2 |
|
3 | | -let db: PgTestClient; |
4 | 3 | let pg: PgTestClient; |
| 4 | +let db: PgTestClient; |
5 | 5 | let teardown: () => Promise<void>; |
6 | 6 |
|
| 7 | +let aliceId: string; |
| 8 | +let bobId: string; |
| 9 | + |
7 | 10 | beforeAll(async () => { |
8 | | - ({ pg, db, teardown } = await getConnections()); |
| 11 | + ({ pg, db, teardown } = await getConnections({}, [seed.pgpm()])); |
| 12 | + |
| 13 | + await pg.query(`GRANT authenticated TO app_user`); |
| 14 | + |
| 15 | + const patients = await pg.query(` |
| 16 | + INSERT INTO patients.patients (first_name, last_name, date_of_birth) |
| 17 | + VALUES ('Alice', 'Anderson', '1980-01-01'), |
| 18 | + ('Bob', 'Brown', '1985-05-05') |
| 19 | + RETURNING id |
| 20 | + `); |
| 21 | + aliceId = patients.rows[0].id; |
| 22 | + bobId = patients.rows[1].id; |
9 | 23 | }); |
10 | 24 |
|
11 | 25 | afterAll(async () => { |
12 | 26 | await teardown(); |
13 | 27 | }); |
14 | 28 |
|
15 | 29 | beforeEach(async () => { |
| 30 | + await pg.beforeEach(); |
16 | 31 | await db.beforeEach(); |
17 | 32 | }); |
18 | 33 |
|
19 | 34 | afterEach(async () => { |
20 | 35 | await db.afterEach(); |
| 36 | + await pg.afterEach(); |
21 | 37 | }); |
22 | 38 |
|
23 | | -describe('first test', () => { |
24 | | - it('should pass', async () => { |
25 | | - const result = await pg.query('SELECT 1 as num'); |
26 | | - expect(result.rows[0].num).toBe(1); |
| 39 | +function asPatient(userId: string) { |
| 40 | + db.setContext({ |
| 41 | + role: 'authenticated', |
| 42 | + 'app.role': 'patient', |
| 43 | + 'app.user_id': userId, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function asClinician() { |
| 48 | + db.setContext({ |
| 49 | + role: 'authenticated', |
| 50 | + 'app.role': 'clinician', |
| 51 | + 'app.user_id': '', |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +describe('conditions RLS', () => { |
| 56 | + it('Alice sees only her own conditions', async () => { |
| 57 | + asClinician(); |
| 58 | + await db.query( |
| 59 | + `INSERT INTO clinical.conditions (patient_id, description, status) |
| 60 | + VALUES ($1, 'Hypertension', 'active'), |
| 61 | + ($2, 'Type 2 Diabetes', 'active')`, |
| 62 | + [aliceId, bobId], |
| 63 | + ); |
| 64 | + |
| 65 | + asPatient(aliceId); |
| 66 | + const r = await db.query(`SELECT description FROM clinical.conditions`); |
| 67 | + expect(r.rows).toHaveLength(1); |
| 68 | + expect(r.rows[0].description).toBe('Hypertension'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('Clinician-only writes: patient cannot add a condition to themselves', async () => { |
| 72 | + asPatient(aliceId); |
| 73 | + const point = 'patient_condition_denied'; |
| 74 | + await db.savepoint(point); |
| 75 | + await expect( |
| 76 | + db.query( |
| 77 | + `INSERT INTO clinical.conditions (patient_id, description) |
| 78 | + VALUES ($1, 'Self-diagnosed fake condition')`, |
| 79 | + [aliceId], |
| 80 | + ), |
| 81 | + ).rejects.toThrow(/row-level security/i); |
| 82 | + await db.rollback(point); |
27 | 83 | }); |
28 | 84 | }); |
29 | 85 |
|
| 86 | +describe('allergies RLS', () => { |
| 87 | + it("Alice can record her own allergies but cannot see Bob's", async () => { |
| 88 | + asPatient(aliceId); |
| 89 | + await db.query( |
| 90 | + `INSERT INTO clinical.allergies (patient_id, allergen, severity) |
| 91 | + VALUES ($1, 'Penicillin', 'severe')`, |
| 92 | + [aliceId], |
| 93 | + ); |
| 94 | + |
| 95 | + asClinician(); |
| 96 | + await db.query( |
| 97 | + `INSERT INTO clinical.allergies (patient_id, allergen, severity) |
| 98 | + VALUES ($1, 'Shellfish', 'moderate')`, |
| 99 | + [bobId], |
| 100 | + ); |
| 101 | + |
| 102 | + asPatient(aliceId); |
| 103 | + const r = await db.query(`SELECT allergen FROM clinical.allergies`); |
| 104 | + expect(r.rows).toHaveLength(1); |
| 105 | + expect(r.rows[0].allergen).toBe('Penicillin'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Alice CANNOT add an allergy for Bob (WITH CHECK enforces row ownership)', async () => { |
| 109 | + asPatient(aliceId); |
| 110 | + const point = 'cross_patient_allergy'; |
| 111 | + await db.savepoint(point); |
| 112 | + await expect( |
| 113 | + db.query( |
| 114 | + `INSERT INTO clinical.allergies (patient_id, allergen, severity) |
| 115 | + VALUES ($1, 'Sneaky injection on Bob', 'moderate')`, |
| 116 | + [bobId], |
| 117 | + ), |
| 118 | + ).rejects.toThrow(/row-level security/i); |
| 119 | + await db.rollback(point); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('vitals RLS', () => { |
| 124 | + it('Clinicians record vitals; patients read their own only', async () => { |
| 125 | + asClinician(); |
| 126 | + await db.query( |
| 127 | + `INSERT INTO clinical.vitals (patient_id, heart_rate_bpm, systolic_bp, diastolic_bp, temperature_c) |
| 128 | + VALUES ($1, 72, 120, 80, 36.7), |
| 129 | + ($2, 88, 145, 92, 37.1)`, |
| 130 | + [aliceId, bobId], |
| 131 | + ); |
| 132 | + |
| 133 | + asPatient(aliceId); |
| 134 | + const aliceView = await db.query(`SELECT heart_rate_bpm FROM clinical.vitals`); |
| 135 | + expect(aliceView.rows).toHaveLength(1); |
| 136 | + expect(aliceView.rows[0].heart_rate_bpm).toBe(72); |
| 137 | + |
| 138 | + asPatient(bobId); |
| 139 | + const bobView = await db.query(`SELECT heart_rate_bpm FROM clinical.vitals`); |
| 140 | + expect(bobView.rows).toHaveLength(1); |
| 141 | + expect(bobView.rows[0].heart_rate_bpm).toBe(88); |
| 142 | + }); |
| 143 | + |
| 144 | + it('Patients cannot record their own vitals', async () => { |
| 145 | + asPatient(aliceId); |
| 146 | + const point = 'patient_vitals_denied'; |
| 147 | + await db.savepoint(point); |
| 148 | + await expect( |
| 149 | + db.query( |
| 150 | + `INSERT INTO clinical.vitals (patient_id, heart_rate_bpm) |
| 151 | + VALUES ($1, 200)`, |
| 152 | + [aliceId], |
| 153 | + ), |
| 154 | + ).rejects.toThrow(/row-level security/i); |
| 155 | + await db.rollback(point); |
| 156 | + }); |
| 157 | +}); |
0 commit comments