2023-07-21 23:59:12 +00:00
|
|
|
-- Test logout
|
|
|
|
set client_min_messages to warning;
|
|
|
|
create extension if not exists pgtap;
|
|
|
|
reset client_min_messages;
|
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
|
|
select plan(17);
|
|
|
|
|
|
|
|
set search_path to auth, camper, public;
|
|
|
|
|
|
|
|
select has_function('camper', 'logout', array []::name[]);
|
|
|
|
select function_lang_is('camper', 'logout', array []::name[], 'sql');
|
|
|
|
select function_returns('camper', 'logout', array []::name[], 'void');
|
|
|
|
select is_definer('camper', 'logout', array []::name[]);
|
|
|
|
select volatility_is('camper', 'logout', array []::name[], 'volatile');
|
|
|
|
select function_privs_are('camper', 'logout', array []::name[], 'guest', array []::text[]);
|
|
|
|
select function_privs_are('camper', 'logout', array []::name[], 'employee', array ['EXECUTE']);
|
|
|
|
select function_privs_are('camper', 'logout', array []::name[], 'admin', array ['EXECUTE']);
|
|
|
|
select function_privs_are('camper', 'logout', array []::name[], 'authenticator', array []::text[]);
|
|
|
|
|
|
|
|
set client_min_messages to warning;
|
|
|
|
truncate auth."user" cascade;
|
|
|
|
reset client_min_messages;
|
|
|
|
|
Move the user role down to company_user relation
I was starting to add the public page for campsite types, creating more
granular row-level security policies for select, insert, update, and
delete, because now the guest users needed to SELECT them and they have
no related company to filter the rows with. Suddenly, i realized that
the role was wrong in the user relation: a user can be an admin to one
company, and employee to another, and guess to yet another company;
the role should be in the company_user relation instead.
That means that to know the role to set to, the user alone is not enough
and have to know the company as well. Had to change all the
cookie-related function to accept also the company’s host name, as this
is the information that the Go application has.
2023-08-08 00:22:16 +00:00
|
|
|
insert into auth."user" (user_id, email, name, password, cookie, cookie_expires_at)
|
|
|
|
values (1, 'info@tandem.blog', 'Tandem', 'test', '8c23d4a8d777775f8fc507676a0d99d3dfa54b03b1b257c838', current_timestamp + interval '1 day')
|
|
|
|
, (12, 'admin@tandem.blog', 'Admin', 'test', '0169e5f668eec1e6749fd25388b057997358efa8dfd697961a', current_timestamp + interval '2 day')
|
2023-07-21 23:59:12 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
prepare user_cookies as
|
|
|
|
select cookie, cookie_expires_at
|
|
|
|
from "user"
|
|
|
|
order by user_id
|
|
|
|
;
|
|
|
|
|
|
|
|
select set_config('request.user.cookie', '', false);
|
|
|
|
select set_config('request.user.email', '', false);
|
|
|
|
select lives_ok($$ select * from logout() $$, 'Can logout “nobody”');
|
|
|
|
|
|
|
|
select results_eq(
|
|
|
|
'user_cookies',
|
|
|
|
$$ values ('8c23d4a8d777775f8fc507676a0d99d3dfa54b03b1b257c838', current_timestamp + interval '1 day')
|
|
|
|
, ('0169e5f668eec1e6749fd25388b057997358efa8dfd697961a', current_timestamp + interval '2 day')
|
|
|
|
$$,
|
|
|
|
'Should have changed nothing'
|
|
|
|
);
|
|
|
|
|
|
|
|
select set_config('request.user.cookie', '0169e5f668eec1e6749fd25388b057997358efa8dfd697961a', false);
|
|
|
|
select set_config('request.user.email', 'info@tandem.blog', false);
|
|
|
|
select lives_ok($$ select * from logout() $$, 'Can logout even if the email and cookie does not match');
|
|
|
|
|
|
|
|
select results_eq(
|
|
|
|
'user_cookies',
|
|
|
|
$$ values ('8c23d4a8d777775f8fc507676a0d99d3dfa54b03b1b257c838', current_timestamp + interval '1 day')
|
|
|
|
, ('0169e5f668eec1e6749fd25388b057997358efa8dfd697961a', current_timestamp + interval '2 day')
|
|
|
|
$$,
|
|
|
|
'Should have changed nothing'
|
|
|
|
);
|
|
|
|
|
|
|
|
select set_config('request.user.cookie', '8c23d4a8d777775f8fc507676a0d99d3dfa54b03b1b257c838', false);
|
|
|
|
select set_config('request.user.email', 'info@tandem.blog', false);
|
|
|
|
select lives_ok($$ select * from logout() $$, 'Can logout the first user');
|
|
|
|
|
|
|
|
select results_eq(
|
|
|
|
'user_cookies',
|
|
|
|
$$ values ('', '-infinity'::timestamptz)
|
|
|
|
, ('0169e5f668eec1e6749fd25388b057997358efa8dfd697961a'::text, current_timestamp + interval '2 day')
|
|
|
|
$$,
|
|
|
|
'The first user logged out'
|
|
|
|
);
|
|
|
|
|
|
|
|
select set_config('request.user.cookie', '0169e5f668eec1e6749fd25388b057997358efa8dfd697961a', false);
|
|
|
|
select set_config('request.user.email', 'admin@tandem.blog', false);
|
|
|
|
select lives_ok($$ select * from logout() $$, 'Can logout the second user');
|
|
|
|
|
|
|
|
select results_eq(
|
|
|
|
'user_cookies',
|
|
|
|
$$ values ('', '-infinity'::timestamptz)
|
|
|
|
, ('', '-infinity'::timestamptz)
|
|
|
|
$$,
|
|
|
|
'The second user logged out'
|
|
|
|
);
|
|
|
|
|
|
|
|
select *
|
|
|
|
from finish();
|
|
|
|
|
|
|
|
rollback;
|