-- Test login
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(12);

set search_path to numerus, public;

select has_function('login');
select function_lang_is('login', array ['email', 'text'], 'plpgsql');
select function_returns('login', array ['email', 'text'], 'name');
select is_definer('login', array ['email', 'text']);
select volatility_is('login', array ['email', 'text'], 'volatile');
select function_privs_are('login', array ['email', 'text'], 'guest', array ['EXECUTE']);
select function_privs_are('login', array ['email', 'text'], 'invoicer', array []::text[]);
select function_privs_are('login', array ['email', 'text'], 'admin', array []::text[]);
select function_privs_are('login', array ['email', 'text'], 'authenticator', array []::text[]);

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

insert into auth."user" (email, name, password, role)
values ('info@tandem.blog', 'Perita', 'test', 'guest');

select is(
	login('info@tandem.blog'::email, 'test'),
	'guest'::name,
	'Should find the role with the correct email and password'
);

select throws_ok(
	$$ select login('info@tandem.blog'::email, 'mah password') $$,
	'28P01',
	'invalid user or password',
	'Should not find any role with an invalid password'
);

select throws_ok(
	$$ select login('nope@tandem.blog'::email, 'test') $$,
	'28P01',
	'invalid user or password',
	'Should not find any role with an invalid email'
);


select *
from finish();

rollback;
