Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions supabase/tests/03-authenticate-as.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BEGIN;

CREATE EXTENSION supabase_test_helpers;

select plan(9);
select plan(11);

select tests.create_supabase_user('test');

Expand All @@ -12,10 +12,16 @@ select lives_ok($$ select tests.authenticate_as('test') $$, 'Successfully authen
select is((select tests.get_supabase_uid('test')), auth.uid(), 'Authenticates as the correct user');
select is((select current_role::text), 'authenticated', 'Sets the current role to authenticated');
select is(
current_setting('request.jwt.claims')::jsonb ?& array['sub', 'email', 'phone', 'user_metadata', 'app_metadata'],
current_setting('request.jwt.claims')::jsonb ?& array['sub', 'email', 'phone', 'user_metadata', 'app_metadata', 'exp'],
true,
'Claims should contain correct keys for authenticated user'
);
select cmp_ok((current_setting('request.jwt.claims')::jsonb->>'exp')::numeric, '>',
extract(epoch from (current_timestamp + interval '55 minutes')),
'Claim exp should expire in more than 55 minutes');
select cmp_ok((current_setting('request.jwt.claims')::jsonb->>'exp')::numeric, '<',
extract(epoch from (current_timestamp + interval '65 minutes')),
'Claim exp should expire in less than 65 minutes');

select lives_ok($$ select tests.clear_authentication() $$, 'should clear authentication');
select is((select current_role::text), 'anon', 'Sets the current role to anonymous');
Expand Down
50 changes: 50 additions & 0 deletions supabase_test_helpers--0.0.6--0.0.7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* ### tests.authenticate_as(identifier text)
* Authenticates as a user created with `tests.create_supabase_user`.
*
* Parameters:
* - `identifier` - The unique identifier for the user
*
* Returns:
* - `void`
*
* Example:
* ```sql
* SELECT tests.create_supabase_user('test_owner');
* SELECT tests.authenticate_as('test_owner');
* ```
*/
CREATE OR REPLACE FUNCTION tests.authenticate_as (identifier text)
RETURNS void
AS $$
DECLARE
user_data json;
original_auth_data text;
BEGIN
-- store the request.jwt.claims in a variable in case we need it
original_auth_data := current_setting('request.jwt.claims', true);
user_data := tests.get_supabase_user(identifier);

if user_data is null OR user_data ->> 'id' IS NULL then
RAISE EXCEPTION 'User with identifier % not found', identifier;
end if;


perform set_config('role', 'authenticated', true);
perform set_config('request.jwt.claims', json_build_object(
'sub', user_data ->> 'id',
'email', user_data ->> 'email',
'phone', user_data ->> 'phone',
'user_metadata', user_data -> 'raw_user_meta_data',
'app_metadata', user_data -> 'raw_app_meta_data',
'exp', extract(epoch from (current_timestamp + interval '1 hour'))
)::text, true);

EXCEPTION
-- revert back to original auth data
WHEN OTHERS THEN
set local role authenticated;
set local "request.jwt.claims" to original_auth_data;
RAISE;
END
$$ LANGUAGE plpgsql;
Loading