3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 2;
24 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34 my $t = Test::Mojo->new('Koha::REST::V1');
36 subtest 'set() (authorized user tests)' => sub {
40 $schema->storage->txn_begin;
42 my $privileged_patron = $builder->build_object(
44 class => 'Koha::Patrons',
45 value => { flags => 1 }
48 my $password = 'thePassword123';
49 $privileged_patron->set_password(
50 { password => $password, skip_validation => 1 } );
51 my $userid = $privileged_patron->userid;
53 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
55 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
56 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
58 my $new_password = 'abc';
60 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
62 . "/password" => json =>
63 { password => $new_password, password_2 => $new_password } )
64 ->status_is(200)->json_is('');
66 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
68 . "/password" => json =>
69 { password => $new_password, password_2 => 'cde' } )->status_is(400)
70 ->json_is( { error => 'Passwords don\'t match' } );
72 t::lib::Mocks::mock_preference( 'minPasswordLength', 5 );
74 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
76 . "/password" => json =>
77 { password => $new_password, password_2 => $new_password } )
80 { error => 'Password length (3) is shorter than required (5)' } );
82 $new_password = 'abc ';
83 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
85 . "/password" => json =>
86 { password => $new_password, password_2 => $new_password } )
87 ->status_is(400)->json_is(
90 '[Password contains leading/trailing whitespace character(s)]'
94 $new_password = 'abcdefg';
95 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
97 . "/password" => json =>
98 { password => $new_password, password_2 => $new_password } )
99 ->status_is(200)->json_is('');
101 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1 );
102 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
104 . "/password" => json =>
105 { password => $new_password, password_2 => $new_password } )
106 ->status_is(400)->json_is( { error => '[Password is too weak]' } );
108 $new_password = 'ABcde123%&';
109 $t->post_ok( "//$userid:$password@/api/v1/patrons/"
111 . "/password" => json =>
112 { password => $new_password, password_2 => $new_password } )
113 ->status_is(200)->json_is('');
115 $schema->storage->txn_rollback;
118 subtest 'set_public() (unprivileged user tests)' => sub {
122 $schema->storage->txn_begin;
124 my $category = $builder->build_object(
126 class => 'Koha::Patron::Categories',
127 value => { change_password => 0 } # disallow changing password for the patron category
130 my $patron = $builder->build_object(
132 class => 'Koha::Patrons',
133 value => { categorycode => $category->id }
137 my $password = 'thePassword123';
138 $patron->set_password( { password => $password, skip_validation => 1 } );
139 my $userid = $patron->userid;
140 my $other_patron = $builder->build_object( { class => 'Koha::Patrons' } );
142 # Enable the public API
143 t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
145 t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 );
146 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
147 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
149 my $new_password = 'abc';
152 "//$userid:$password@/api/v1/public/patrons/"
154 . "/password" => json => {
155 password => $new_password,
156 password_repeated => $new_password,
157 old_password => 'blah'
159 )->status_is(403)->json_is(
161 error => 'Changing password is forbidden'
165 t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
168 "//$userid:$password@/api/v1/public/patrons/"
170 . "/password" => json => {
171 password => $new_password,
172 password_repeated => $new_password,
173 old_password => $password
177 "Authorization failure. Missing required permission(s)." );
180 "//$userid:$password@/api/v1/public/patrons/"
182 . "/password" => json => {
183 password => $new_password,
184 password_repeated => $new_password,
185 old_password => $password
187 )->status_is(403)->json_is({ error => 'Changing password is forbidden' });
189 # Allow password changing to the patron category
190 $category->change_password(1)->store;
193 "//$userid:$password@/api/v1/public/patrons/"
195 . "/password" => json => {
196 password => $new_password,
197 password_repeated => 'wrong_password',
198 old_password => $password
200 )->status_is(400)->json_is(
202 error => "Passwords don't match"
207 "//$userid:$password@/api/v1/public/patrons/"
209 . "/password" => json => {
210 password => $new_password,
211 password_repeated => $new_password,
212 old_password => 'badpassword'
214 )->status_is(400)->json_is(
216 error => "Invalid password"
221 "//$userid:$password@/api/v1/public/patrons/"
223 . "/password" => json => {
224 password => $new_password,
225 password_repeated => $new_password,
226 old_password => $password
228 )->status_is(200)->json_is('');
230 $schema->storage->txn_rollback;