-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathitems.test.sql
More file actions
101 lines (83 loc) · 2.1 KB
/
Copy pathitems.test.sql
File metadata and controls
101 lines (83 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
begin;
select plan(9);
select has_table('items', 'Items table exists');
select tests.rls_enabled('public', 'items');
select ok(
exists (
select 1
from pg_publication_tables
where
pubname = 'supabase_realtime'
and schemaname = 'public'
and tablename = 'items'
),
'Items table is added to supabase_realtime publication'
);
select tests.create_supabase_user('user1');
select tests.create_supabase_user('user2');
-- Create an item as user 1
select tests.authenticate_as('user1');
insert into items (
id,
user_id,
updated_at,
deleted,
name
) values (
gen_random_uuid(),
tests.get_supabase_uid('user1'),
now(),
false,
'User 1 Item'
);
select results_eq(
'select count(*) from items',
array[1::bigint],
'User 1 only sees their own items'
);
select tests.authenticate_as('user2');
select results_eq(
'select count(*) from items',
array[0::bigint],
'User 2 does not see User 1''s items'
);
select results_ne(
$$ DELETE FROM items
WHERE user_id = tests.get_supabase_uid('user1')
RETURNING 1 $$,
$$ VALUES (1) $$,
'User 2 cannot delete User 1''s items'
);
-- Update item as user 1
select tests.authenticate_as('user1');
update items
set
name = 'Updated item',
updated_at = now() + interval '1 hour'
where
user_id = tests.get_supabase_uid('user1');
select results_eq(
'SELECT name FROM items WHERE user_id = tests.get_supabase_uid(''user1'')',
array['Updated item'],
'User 1 can update their own items'
);
-- Try updating item with an older timestamp
update items
set
name = 'Older item',
updated_at = now() - interval '1 hour'
where
user_id = tests.get_supabase_uid('user1');
select results_eq(
'SELECT name FROM items WHERE user_id = tests.get_supabase_uid(''user1'')',
array['Updated item'],
'Older updates are ignored'
);
select tests.delete_supabase_user('user1');
select results_eq(
'SELECT count(*) FROM items',
array[0::bigint],
'Deleting a user deletes their items'
);
select * from finish(); --noqa
rollback;