Skip to content

Commit 24acc67

Browse files
committed
add JSON seeding
1 parent f62a0ec commit 24acc67

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

packages/hello-world/__tests__/seeding/data/seed-data.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
export const users = [
22
{
33
id: '550e8400-e29b-41d4-a716-446655440001',
4-
5-
name: 'Tutorial User 1'
4+
65
},
76
{
87
id: '550e8400-e29b-41d4-a716-446655440002',
9-
10-
name: 'Tutorial User 2'
8+
119
},
1210
{
1311
id: '550e8400-e29b-41d4-a716-446655440003',
14-
15-
name: 'Tutorial User 3'
12+
1613
}
1714
];
1815

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { getConnections, PgTestClient, seed } from 'supabase-test';
2+
import path from 'path';
3+
import { pets, users } from './data/seed-data';
4+
5+
let db: PgTestClient;
6+
let teardown: () => Promise<void>;
7+
8+
const sql = (f: string) => path.join(__dirname, 'data', f);
9+
10+
const cwd = path.resolve(__dirname, '../../');
11+
12+
beforeAll(async () => {
13+
({ db, teardown } = await getConnections(
14+
{}, [
15+
seed.launchql(cwd),
16+
seed.json({
17+
'auth.users': users,
18+
'rls_test.pets': pets
19+
})
20+
]
21+
));
22+
});
23+
24+
afterAll(async () => {
25+
await teardown();
26+
});
27+
28+
beforeEach(async () => {
29+
await db.beforeEach();
30+
});
31+
32+
afterEach(async () => {
33+
await db.afterEach();
34+
});
35+
36+
describe('tutorial: testing with sql file seeding', () => {
37+
it('should work with sql file seed function', async () => {
38+
39+
db.setContext({
40+
role: 'authenticated',
41+
'request.jwt.claim.sub': users[0].id
42+
});
43+
44+
const verifiedPets = await db.any(
45+
`SELECT id FROM rls_test.pets WHERE user_id = $1`,
46+
[users[0].id]
47+
);
48+
expect(verifiedPets.length).toBe(1);
49+
50+
db.clearContext();
51+
52+
const anonPets = await db.any(
53+
`SELECT id FROM rls_test.pets WHERE user_id = $1`,
54+
[users[0].id]
55+
);
56+
expect(anonPets.length).toBe(0);
57+
58+
});
59+
it('should enforce RLS - users can only see their own pets', async () => {
60+
// set context to first user
61+
db.setContext({
62+
role: 'authenticated',
63+
'request.jwt.claim.sub': users[0].id
64+
});
65+
66+
// user1 should only see their own pet (Fido)
67+
const user1Pets = await db.many(
68+
`SELECT id, name, breed, user_id FROM rls_test.pets ORDER BY name`
69+
);
70+
71+
expect(user1Pets.length).toBe(1);
72+
expect(user1Pets[0].user_id).toBe(users[0].id);
73+
expect(user1Pets[0].name).toBe('Fido');
74+
75+
// set context to second user
76+
db.setContext({
77+
role: 'authenticated',
78+
'request.jwt.claim.sub': users[1].id
79+
});
80+
81+
// user2 should only see their own pet (Buddy)
82+
const user2Pets = await db.many(
83+
`SELECT id, name, breed, user_id FROM rls_test.pets ORDER BY name`
84+
);
85+
86+
expect(user2Pets.length).toBe(1);
87+
expect(user2Pets[0].user_id).toBe(users[1].id);
88+
expect(user2Pets[0].name).toBe('Buddy');
89+
});
90+
});
91+

0 commit comments

Comments
 (0)