According to PostgreSQL’s manual[0]: “STABLE indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same argument values, but that its result could change across SQL statements.” This definition matches both functions. Moreover, find_user_role did not need to be written in plpgsql, that i assume—but did not test—are slower than sql functions. [0]: https://www.postgresql.org/docs/14/sql-createfunction.html
54 lines
1.5 KiB
PL/PgSQL
54 lines
1.5 KiB
PL/PgSQL
-- 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'], 'stable');
|
|
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;
|