|
| 1 | +import { getConnections, PgTestClient } from 'pgsql-test'; |
| 2 | + |
| 3 | +let pg: PgTestClient; |
| 4 | +let db: PgTestClient; |
| 5 | +let teardown: () => Promise<void>; |
| 6 | + |
| 7 | +beforeAll(async () => { |
| 8 | + ({ pg, db, teardown } = await getConnections()); |
| 9 | +}); |
| 10 | + |
| 11 | +afterAll(async () => { |
| 12 | + await teardown(); |
| 13 | +}); |
| 14 | + |
| 15 | +beforeEach(async () => { |
| 16 | + await db.beforeEach(); |
| 17 | +}); |
| 18 | + |
| 19 | +afterEach(async () => { |
| 20 | + await db.afterEach(); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('RLS Demo - Data Insertion', () => { |
| 24 | + it('should insert users and products', async () => { |
| 25 | + // Insert users |
| 26 | + const user1 = await pg.one( |
| 27 | + `INSERT INTO rls_test.users (email, name) |
| 28 | + VALUES ($1, $2) |
| 29 | + RETURNING id, email, name`, |
| 30 | + ['[email protected]', 'Alice Johnson'] |
| 31 | + ); |
| 32 | + |
| 33 | + const user2 = await pg.one( |
| 34 | + `INSERT INTO rls_test.users (email, name) |
| 35 | + VALUES ($1, $2) |
| 36 | + RETURNING id, email, name`, |
| 37 | + ['[email protected]', 'Bob Smith'] |
| 38 | + ); |
| 39 | + |
| 40 | + // Insert products |
| 41 | + const product1 = await pg.one( |
| 42 | + `INSERT INTO rls_test.products (name, description, price, owner_id) |
| 43 | + VALUES ($1, $2, $3, $4) |
| 44 | + RETURNING id, name, price, owner_id`, |
| 45 | + ['Laptop Pro', 'High-performance laptop', 1299.99, user1.id] |
| 46 | + ); |
| 47 | + |
| 48 | + const product2 = await pg.one( |
| 49 | + `INSERT INTO rls_test.products (name, description, price, owner_id) |
| 50 | + VALUES ($1, $2, $3, $4) |
| 51 | + RETURNING id, name, price, owner_id`, |
| 52 | + ['Wireless Mouse', 'Ergonomic mouse', 49.99, user1.id] |
| 53 | + ); |
| 54 | + |
| 55 | + expect(user1.email).toBe('[email protected]'); |
| 56 | + expect(product1.name).toBe('Laptop Pro'); |
| 57 | + expect(product1.owner_id).toBe(user1.id); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should query user products with joins', async () => { |
| 61 | + const result = await pg.many( |
| 62 | + `SELECT u.name, p.name as product_name, p.price |
| 63 | + FROM rls_test.users u |
| 64 | + JOIN rls_test.products p ON u.id = p.owner_id |
| 65 | + ORDER BY u.name, p.name` |
| 66 | + ); |
| 67 | + |
| 68 | + expect(result.length).toBeGreaterThan(0); |
| 69 | + expect(result[0]).toHaveProperty('name'); |
| 70 | + expect(result[0]).toHaveProperty('product_name'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should test RLS context switching', async () => { |
| 74 | + // Get a user ID for context |
| 75 | + const user = await pg.one(`SELECT id FROM rls_test.users LIMIT 1`); |
| 76 | + |
| 77 | + // Set context to simulate authenticated user with JWT claims |
| 78 | + db.setContext({ |
| 79 | + role: 'authenticated', |
| 80 | + 'jwt.claims.user_id': user.id |
| 81 | + }); |
| 82 | + |
| 83 | + // Test auth.uid() function |
| 84 | + const uid = await db.one(`SELECT auth.uid() as uid`); |
| 85 | + expect(uid.uid).toBe(user.id); |
| 86 | + |
| 87 | + // Test auth.role() function |
| 88 | + const role = await db.one(`SELECT auth.role() as role`); |
| 89 | + expect(role.role).toBe('authenticated'); |
| 90 | + |
| 91 | + // Query should work with RLS policies |
| 92 | + const userData = await db.one( |
| 93 | + `SELECT id, email FROM rls_test.users WHERE id = $1`, |
| 94 | + [user.id] |
| 95 | + ); |
| 96 | + |
| 97 | + expect(userData.id).toBe(user.id); |
| 98 | + }); |
| 99 | +}); |
0 commit comments