create SQL function "change_user_password"

This commit is contained in:
2019-10-09 12:27:20 +02:00
parent f91b25b35b
commit 20416ce108
2 changed files with 19 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
create or replace function change_user_password(resource json) returns void language plpgsql as
$$
begin
update "user"
set password = crypt(resource->>'plain_password', gen_salt('bf', 8))
where id = (resource->>'id')::uuid;
return;
end;
$$;
-- drop function if exists change_user_password(json);

View File

@@ -2,6 +2,7 @@ do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
user_with_other_password json;
selected_user json;
exist_user json;
begin
@@ -24,6 +25,12 @@ begin
assert exist_user->>'username' = 'george', format('the function check_user must be return user object with username is "george", %s is return', exist_user::text);
assert exist_user->>'password' is null, format('the function check_user must not be return the password, %s is return', exist_user::text);
-- test change password
user_with_other_password = jsonb_set(created_user::jsonb, '{plain_password}', '"qwerty"'::jsonb);
perform change_user_password(user_with_other_password);
select check_user('george', 'qwerty') into exist_user;
assert exist_user->>'username' = 'george', format('the function change_user_password must change password: %s', exist_user::text);
-- delete user and check if user is really not exists
delete from "user" where username = 'george';
select check_user('george', 'azerty') into exist_user;