create SQL function "change_user_password"
This commit is contained in:
@@ -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);
|
||||||
@@ -2,6 +2,7 @@ do
|
|||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||||
|
user_with_other_password json;
|
||||||
selected_user json;
|
selected_user json;
|
||||||
exist_user json;
|
exist_user json;
|
||||||
begin
|
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->>'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);
|
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 user and check if user is really not exists
|
||||||
delete from "user" where username = 'george';
|
delete from "user" where username = 'george';
|
||||||
select check_user('george', 'azerty') into exist_user;
|
select check_user('george', 'azerty') into exist_user;
|
||||||
|
|||||||
Reference in New Issue
Block a user