-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-framework.sql
143 lines (133 loc) · 4.23 KB
/
test-framework.sql
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
drop table if exists public.test_users cascade;
create table
public.test_users
(
email text not null,
password text not null,
is_member boolean not null,
constraint test_users_pkey primary key (email)
) tablespace pg_default;
alter table public.test_users
enable row level security;
create policy "test admin can add a user"
on public.test_users
for insert WITH CHECK (
auth.jwt() ->> 'email' = '[email protected]'
);
CREATE OR REPLACE FUNCTION public.insert_test_user()
RETURNS trigger AS
$BODY$
declare
new_id uuid := uuid_generate_v4();
BEGIN
insert into "auth"."users" ("instance_id",
"id",
"aud",
"role",
"email",
"encrypted_password",
"email_confirmed_at",
"invited_at",
"confirmation_token",
"confirmation_sent_at",
"recovery_token",
"recovery_sent_at",
"email_change_token_new",
"email_change",
"email_change_sent_at",
"last_sign_in_at",
"raw_app_meta_data",
"raw_user_meta_data",
"is_super_admin",
"created_at",
"updated_at",
"phone",
"phone_confirmed_at",
"phone_change",
"phone_change_token",
"phone_change_sent_at",
"email_change_token_current",
"email_change_confirm_status",
"banned_until",
"reauthentication_token",
"reauthentication_sent_at",
"is_sso_user")
values ('00000000-0000-0000-0000-000000000000',
new_id,
'authenticated',
'authenticated',
NEW.email,
crypt(NEW.password, gen_salt('bf')),
now(),
null,
'',
now(),
'',
null,
'',
'',
null,
now(),
'{"provider": "email", "providers": ["email"]}',
'{}',
null,
now(),
now(),
null,
null,
'',
'',
null,
'',
0,
null,
'',
null,
'f');
IF NEW.is_member THEN
UPDATE public.profiles SET membership_level = 'member' WHERE id = new_id;
END IF;
RETURN NULL;
END;
$BODY$
security definer
LANGUAGE 'plpgsql';
create trigger on_auth_user_created
BEFORE insert
on public.test_users
for each row
EXECUTE PROCEDURE insert_test_user();
--- empty test users every day
create or replace function delete_old_test_accounts()
returns void as
$$
declare
num_failed_test_users smallint := 0;
begin
SELECT COUNT(email) INTO num_failed_test_users FROM public.test_users;
IF num_failed_test_users = 0 THEN
delete
from auth.users
where id in (select id
from auth.users
where email <> '[email protected]'
and created_at < now() - interval '24 hours');
ELSE
RAISE EXCEPTION '% test users have been badly added', num_failed_test_users;
END IF;
end;
$$
language plpgsql;
DO
$$
declare
jid bigint;
BEGIN
SELECT jobid from cron.job where jobname = 'daily-test-users-cleanup' into jid;
EXECUTE 'SELECT cron.unschedule($1)' USING jid;
END
$$ language plpgsql;
select cron.schedule(
'daily-test-users-cleanup',
'30 4 * * *',
$$ select delete_old_test_accounts() $$);