Bug 26922: Regression tests
[koha.git] / t / db_dependent / api / v1 / patrons_password.t
blob2a57516319d908538d1777bdf604db02426f5293
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 2;
22 use Test::Mojo;
23 use Test::Warn;
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
28 use Koha::Patrons;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
34 # this affects the other REST api tests
35 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37 my $remote_address = '127.0.0.1';
38 my $t = Test::Mojo->new('Koha::REST::V1');
40 subtest 'set() (authorized user tests)' => sub {
42 plan tests => 21;
44 $schema->storage->txn_begin;
46 my ( $patron, $session ) = create_user_and_session({ authorized => 1 });
48 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
49 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
51 my $new_password = 'abc';
53 my $tx
54 = $t->ua->build_tx( POST => "/api/v1/patrons/"
55 . $patron->id
56 . "/password" => json => { password => $new_password, password_2 => $new_password } );
58 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
59 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
60 $t->request_ok($tx)->status_is(200)->json_is( '' );
62 $tx
63 = $t->ua->build_tx( POST => "/api/v1/patrons/"
64 . $patron->id
65 . "/password" => json => { password => $new_password, password_2 => 'cde' } );
67 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
68 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
69 $t->request_ok($tx)->status_is(400)->json_is({ error => 'Passwords don\'t match' });
71 t::lib::Mocks::mock_preference( 'minPasswordLength', 5 );
72 $tx
73 = $t->ua->build_tx( POST => "/api/v1/patrons/"
74 . $patron->id
75 . "/password" => json => { password => $new_password, password_2 => $new_password } );
77 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
78 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
79 $t->request_ok($tx)->status_is(400)->json_is({ error => 'Password length (3) is shorter than required (5)' });
81 $new_password = 'abc ';
82 $tx
83 = $t->ua->build_tx( POST => "/api/v1/patrons/"
84 . $patron->id
85 . "/password" => json => { password => $new_password, password_2 => $new_password } );
87 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
88 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
89 $t->request_ok($tx)->status_is(400)->json_is({ error => '[Password contains leading/trailing whitespace character(s)]' });
91 $new_password = 'abcdefg';
92 $tx
93 = $t->ua->build_tx( POST => "/api/v1/patrons/"
94 . $patron->id
95 . "/password" => json => { password => $new_password, password_2 => $new_password } );
97 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
98 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
99 $t->request_ok($tx)->status_is(200)->json_is('');
101 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1);
103 = $t->ua->build_tx( POST => "/api/v1/patrons/"
104 . $patron->id
105 . "/password" => json => { password => $new_password, password_2 => $new_password } );
107 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
108 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
109 $t->request_ok($tx)->status_is(400)->json_is({ error => '[Password is too weak]' });
111 $new_password = 'ABcde123%&';
113 = $t->ua->build_tx( POST => "/api/v1/patrons/"
114 . $patron->id
115 . "/password" => json => { password => $new_password, password_2 => $new_password } );
117 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
118 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
119 $t->request_ok($tx)->status_is(200)->json_is('');
121 $schema->storage->txn_rollback;
124 subtest 'set_public() (unprivileged user tests)' => sub {
126 plan tests => 15;
128 $schema->storage->txn_begin;
130 my ( $patron, $session ) = create_user_and_session({ authorized => 0 });
131 my $other_patron = $builder->build_object({ class => 'Koha::Patrons' });
133 # Enable the public API
134 t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
136 t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 );
137 t::lib::Mocks::mock_preference( 'minPasswordLength', 3 );
138 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
140 my $new_password = 'abc';
142 my $tx = $t->ua->build_tx(
143 POST => "/api/v1/public/patrons/"
144 . $patron->id
145 . "/password" => json => {
146 password => $new_password,
147 password_repeated => $new_password,
148 old_password => 'blah'
152 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
153 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
154 $t->request_ok($tx)
155 ->status_is(403)
156 ->json_is({
157 error => 'Configuration prevents password changes by unprivileged users'
160 t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
162 my $password = 'holapassword';
163 $patron->set_password({ password => $password });
164 $tx = $t->ua->build_tx(
165 POST => "/api/v1/public/patrons/"
166 . $other_patron->id
167 . "/password" => json => {
168 password => $new_password,
169 password_repeated => $new_password,
170 old_password => $password
174 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
175 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
176 $t->request_ok($tx)
177 ->status_is(403)
178 ->json_is('/error', "Authorization failure. Missing required permission(s).");
180 $tx = $t->ua->build_tx(
181 POST => "/api/v1/public/patrons/"
182 . $patron->id
183 . "/password" => json => {
184 password => $new_password,
185 password_repeated => 'wrong_password',
186 old_password => $password
190 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
191 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
192 $t->request_ok($tx)
193 ->status_is(400)
194 ->json_is({
195 error => "Passwords don't match"
198 $tx = $t->ua->build_tx(
199 POST => "/api/v1/public/patrons/"
200 . $patron->id
201 . "/password" => json => {
202 password => $new_password,
203 password_repeated => $new_password,
204 old_password => 'badpassword'
208 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
209 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
210 $t->request_ok($tx)
211 ->status_is(400)
212 ->json_is({
213 error => "Invalid password"
216 $tx = $t->ua->build_tx(
217 POST => "/api/v1/public/patrons/"
218 . $patron->id
219 . "/password" => json => {
220 password => $new_password,
221 password_repeated => $new_password,
222 old_password => $password
226 $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
227 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
228 $t->request_ok($tx)
229 ->status_is(200)
230 ->json_is('');
232 $schema->storage->txn_rollback;
235 sub create_user_and_session {
237 my ( $args ) = @_;
238 my $flags = ( $args->{authorized} ) ? 16 : 0;
240 my $user = $builder->build_object(
242 class => 'Koha::Patrons',
243 value => { flags => $flags }
247 # Create a session for the authorized user
248 my $session = C4::Auth::get_session('');
249 $session->param( 'number', $user->borrowernumber );
250 $session->param( 'id', $user->userid );
251 $session->param( 'ip', '127.0.0.1' );
252 $session->param( 'lasttime', time() );
253 $session->flush;
255 return ( $user, $session );