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 $patron = $builder->build_object( { class => 'Koha::Patrons' } );
125 my $password = 'thePassword123';
126 $patron->set_password( { password => $password, skip_validation => 1 } );
127 my $userid = $patron->userid;
128 my $other_patron = $builder->build_object( { class => 'Koha::Patrons' } );
130 # Enable the public API
131 t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
133 t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 );
134 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
135 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
137 my $new_password = 'abc';
140 "//$userid:$password@/api/v1/public/patrons/"
142 . "/password" => json => {
143 password => $new_password,
144 password_repeated => $new_password,
145 old_password => 'blah'
147 )->status_is(403)->json_is(
150 'Configuration prevents password changes by unprivileged users'
154 t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
157 "//$userid:$password@/api/v1/public/patrons/"
159 . "/password" => json => {
160 password => $new_password,
161 password_repeated => $new_password,
162 old_password => $password
166 "Authorization failure. Missing required permission(s)." );
169 "//$userid:$password@/api/v1/public/patrons/"
171 . "/password" => json => {
172 password => $new_password,
173 password_repeated => 'wrong_password',
174 old_password => $password
176 )->status_is(400)->json_is(
178 error => "Passwords don't match"
183 "//$userid:$password@/api/v1/public/patrons/"
185 . "/password" => json => {
186 password => $new_password,
187 password_repeated => $new_password,
188 old_password => 'badpassword'
190 )->status_is(400)->json_is(
192 error => "Invalid password"
197 "//$userid:$password@/api/v1/public/patrons/"
199 . "/password" => json => {
200 password => $new_password,
201 password_repeated => $new_password,
202 old_password => $password
204 )->status_is(200)->json_is('');
206 $schema->storage->txn_rollback;