-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathfixtures.sql
67 lines (58 loc) · 1.68 KB
/
fixtures.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
CREATE ROLE test_user_role;
CREATE ROLE test_admin_role;
GRANT authenticated TO test_user_role;
GRANT postgres TO test_admin_role;
INSERT INTO auth.users (id, "role", email)
VALUES (gen_random_uuid (), 'test_user_role', '[email protected]')
RETURNING
* \gset bob_
INSERT INTO auth.users (id, "role", email)
VALUES (gen_random_uuid (), 'test_user_role', '[email protected]')
RETURNING
* \gset alice_
INSERT INTO auth.users (id, "role", email)
VALUES (gen_random_uuid (), 'test_admin_role', '[email protected]')
RETURNING
* \gset admin_
CREATE OR REPLACE FUNCTION test_logout ()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM
set_config('request.jwt.claim.sub', NULL, TRUE);
PERFORM
set_config('request.jwt.claim.role', NULL, TRUE);
PERFORM
set_config('request.jwt.claim.email', NULL, TRUE);
RESET ROLE;
END;
$$;
CREATE OR REPLACE FUNCTION test_login (user_email text, logout_first boolean = TRUE)
RETURNS auth.users
LANGUAGE plpgsql
AS $$
DECLARE
auth_user auth.users;
BEGIN
IF logout_first THEN
PERFORM
test_logout ();
END IF;
SELECT
* INTO auth_user
FROM
auth.users
WHERE
email = user_email;
PERFORM
set_config('request.jwt.claim.sub', (auth_user).id::text, TRUE);
PERFORM
set_config('request.jwt.claim.role', (auth_user).ROLE, TRUE);
PERFORM
set_config('request.jwt.claim.email', (auth_user).email, TRUE);
RAISE NOTICE '%', format( 'SET ROLE %I; -- Logging in as %L (%L)', (auth_user).ROLE, (auth_user).id, (auth_user).email);
EXECUTE format('SET ROLE %I', (auth_user).ROLE);
RETURN auth_user;
END;
$$;