-- 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, numerus, public;

select has_function('numerus', 'logout', array []::name[]);
select function_lang_is('numerus', 'logout', array []::name[], 'sql');
select function_returns('numerus', 'logout', array []::name[], 'void');
select is_definer('numerus', 'logout', array []::name[]);
select volatility_is('numerus', 'logout', array []::name[], 'volatile');
select function_privs_are('numerus', 'logout', array []::name[], 'guest', array []::text[]);
select function_privs_are('numerus', 'logout', array []::name[], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'logout', array []::name[], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'logout', array []::name[], 'authenticator', array []::text[]);

set client_min_messages to warning;
truncate auth."user" cascade;
reset client_min_messages;

insert into auth."user" (user_id, email, name, password, role, cookie, cookie_expires_at)
values (1, 'info@tandem.blog', 'Tandem', 'test', 'invoicer', '8c23d4a8d777775f8fc507676a0d99d3dfa54b03b1b257c838', current_timestamp + interval '1 day')
     , (12, 'admin@tandem.blog', 'Admin', 'test', 'admin', '0169e5f668eec1e6749fd25388b057997358efa8dfd697961a', current_timestamp + interval '2 day')
;

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;
