Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 2; |
21 |
use Test::Mojo; |
22 |
use Test::MockModule; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use C4::Auth; |
28 |
use C4::Context; |
29 |
|
30 |
use Koha::Database; |
31 |
use Koha::Notice::Messages; |
32 |
|
33 |
use Crypt::Eksblowfish::Bcrypt qw(en_base64); |
34 |
|
35 |
my $schema = Koha::Database->new->schema; |
36 |
my $builder = t::lib::TestBuilder->new; |
37 |
|
38 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
39 |
# this affects the other REST api tests |
40 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
41 |
|
42 |
my $remote_address = '127.0.0.1'; |
43 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
44 |
|
45 |
subtest 'recovery() tests' => sub { |
46 |
plan tests => 29; |
47 |
|
48 |
$schema->storage->txn_begin; |
49 |
|
50 |
my $url = '/api/v1/public/patrons/password/recovery'; |
51 |
|
52 |
my ($patron, $session) = create_user_and_session(); |
53 |
|
54 |
my $tx = $t->ua->build_tx(POST => $url => json => {}); |
55 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
56 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
57 |
$t->request_ok($tx) |
58 |
->status_is(400); |
59 |
|
60 |
t::lib::Mocks::mock_preference('OpacPasswordReset', 0); |
61 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 0); |
62 |
$tx = $t->ua->build_tx(POST => $url => json => { |
63 |
email => $patron->email, |
64 |
cardnumber => $patron->cardnumber |
65 |
}); |
66 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
67 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
68 |
$t->request_ok($tx) |
69 |
->status_is(403); |
70 |
|
71 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 1); |
72 |
$tx = $t->ua->build_tx(POST => $url => json => { |
73 |
email => $patron->email, |
74 |
cardnumber => $patron->cardnumber |
75 |
}); |
76 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
77 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
78 |
$t->request_ok($tx) |
79 |
->status_is(403); |
80 |
|
81 |
t::lib::Mocks::mock_preference('OpacPasswordReset', 1); |
82 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 0); |
83 |
$tx = $t->ua->build_tx(POST => $url => json => { |
84 |
email => $patron->email, |
85 |
cardnumber => $patron->cardnumber |
86 |
}); |
87 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
88 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
89 |
$t->request_ok($tx) |
90 |
->status_is(403); |
91 |
|
92 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 1); |
93 |
|
94 |
$tx = $t->ua->build_tx(POST => $url => json => { |
95 |
email => 'nonexistent', |
96 |
cardnumber => $patron->cardnumber |
97 |
}); |
98 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
99 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
100 |
$t->request_ok($tx) |
101 |
->status_is(404); |
102 |
|
103 |
$tx = $t->ua->build_tx(POST => $url => json => { |
104 |
email => $patron->email, |
105 |
cardnumber => 'nonexistent' |
106 |
}); |
107 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
108 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
109 |
$t->request_ok($tx) |
110 |
->status_is(404); |
111 |
|
112 |
$tx = $t->ua->build_tx(POST => $url => json => { |
113 |
email => 'nonexistent', |
114 |
userid => $patron->userid |
115 |
}); |
116 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
117 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
118 |
$t->request_ok($tx) |
119 |
->status_is(404); |
120 |
|
121 |
$tx = $t->ua->build_tx(POST => $url => json => { |
122 |
email => $patron->email, |
123 |
userid => 'nonexistent' |
124 |
}); |
125 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
126 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
127 |
$t->request_ok($tx) |
128 |
->status_is(404); |
129 |
|
130 |
$tx = $t->ua->build_tx(POST => $url => json => { |
131 |
email => $patron->email, |
132 |
cardnumber => $patron->cardnumber |
133 |
}); |
134 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
135 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
136 |
$t->request_ok($tx) |
137 |
->status_is(201) |
138 |
->json_is('/status' => 1); |
139 |
|
140 |
my $rs = Koha::Database->new->schema->resultset('BorrowerPasswordRecovery'); |
141 |
is( |
142 |
$rs->search({ borrowernumber => $patron->borrowernumber })->count, 1, |
143 |
'Password modification request found in database' |
144 |
); |
145 |
|
146 |
$tx = $t->ua->build_tx(POST => $url => json => { |
147 |
email => $patron->email, |
148 |
userid => $patron->userid |
149 |
}); |
150 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
151 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
152 |
$t->request_ok($tx) |
153 |
->status_is(201) |
154 |
->json_is('/status' => 1); |
155 |
|
156 |
is( |
157 |
$rs->search({ borrowernumber => $patron->borrowernumber })->count, 1, |
158 |
'Password modification request found in database' |
159 |
); |
160 |
|
161 |
$tx = $t->ua->build_tx(POST => $url => json => { |
162 |
email => $patron->email, |
163 |
userid => $patron->userid, |
164 |
cardnumber => $patron->cardnumber, |
165 |
}); |
166 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
167 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
168 |
$t->request_ok($tx) |
169 |
->status_is(201) |
170 |
->json_is('/status' => 1); |
171 |
|
172 |
is( |
173 |
$rs->search({ borrowernumber => $patron->borrowernumber })->count, 1, |
174 |
'Password modification request found in database' |
175 |
); |
176 |
|
177 |
my $notice = Koha::Notice::Messages->search({ |
178 |
borrowernumber => $patron->borrowernumber, |
179 |
letter_code => 'PASSWORD_RESET', |
180 |
message_transport_type => 'email' |
181 |
})->count; |
182 |
is($notice, 3, 'Found password reset letters in message queue.'); |
183 |
|
184 |
$schema->storage->txn_rollback; |
185 |
}; |
186 |
|
187 |
subtest 'complete_recovery() tests' => sub { |
188 |
plan tests => 16; |
189 |
|
190 |
$schema->storage->txn_begin; |
191 |
|
192 |
my $rs = Koha::Database->new->schema->resultset('BorrowerPasswordRecovery'); |
193 |
|
194 |
my ($patron, $session) = create_user_and_session(); |
195 |
my $uuid_str; |
196 |
do { |
197 |
$uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16)); |
198 |
} while ( substr ( $uuid_str, -1, 1 ) eq '.' ); |
199 |
my $recovery = $builder->build({ |
200 |
source => 'BorrowerPasswordRecovery', |
201 |
value => { |
202 |
borrowernumber => $patron->borrowernumber, |
203 |
uuid => $uuid_str |
204 |
} |
205 |
}); |
206 |
|
207 |
my $url = '/api/v1/public/patrons/password/recovery/complete'; |
208 |
|
209 |
my $tx = $t->ua->build_tx(POST => $url => json => {}); |
210 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
211 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
212 |
$t->request_ok($tx) |
213 |
->status_is(400); |
214 |
|
215 |
$tx = $t->ua->build_tx(POST => $url.'notfound' => json => { |
216 |
uuid => $uuid_str, |
217 |
new_password => 'test', |
218 |
confirm_new_password => 'test', |
219 |
}); |
220 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
221 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
222 |
$t->request_ok($tx) |
223 |
->status_is(404); |
224 |
|
225 |
t::lib::Mocks::mock_preference('minPasswordLength', 6); |
226 |
my $new_pass = 'short'; |
227 |
$tx = $t->ua->build_tx(POST => $url => json => { |
228 |
uuid => $uuid_str, |
229 |
new_password => $new_pass, |
230 |
confirm_new_password => $new_pass, |
231 |
}); |
232 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
233 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
234 |
$t->request_ok($tx) |
235 |
->status_is(400) |
236 |
->json_like('/error', qr/too short/); |
237 |
|
238 |
$new_pass = ' wh1te Space! '; |
239 |
$tx = $t->ua->build_tx(POST => $url => json => { |
240 |
uuid => $uuid_str, |
241 |
new_password => $new_pass, |
242 |
confirm_new_password => $new_pass, |
243 |
}); |
244 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
245 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
246 |
$t->request_ok($tx) |
247 |
->status_is(400) |
248 |
->json_like('/error', qr/trailing/); |
249 |
|
250 |
$new_pass = 'tooweak'; |
251 |
$tx = $t->ua->build_tx(POST => $url => json => { |
252 |
uuid => $uuid_str, |
253 |
new_password => $new_pass, |
254 |
confirm_new_password => $new_pass, |
255 |
}); |
256 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
257 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
258 |
$t->request_ok($tx) |
259 |
->status_is(400) |
260 |
->json_like('/error', qr/too weak/); |
261 |
|
262 |
t::lib::Mocks::mock_preference('minPasswordLength', 4); |
263 |
$new_pass = 'STR 0NG ENOugh P@@ssword'; |
264 |
$tx = $t->ua->build_tx(POST => $url => json => { |
265 |
uuid => $uuid_str, |
266 |
new_password => $new_pass, |
267 |
confirm_new_password => $new_pass, |
268 |
}); |
269 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
270 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
271 |
$t->request_ok($tx) |
272 |
->status_is(200); |
273 |
|
274 |
my $stored_pw = Koha::Patrons->find($patron->borrowernumber)->password; |
275 |
is( |
276 |
$stored_pw, |
277 |
Koha::AuthUtils::hash_password($new_pass, $stored_pw), 'Password changed' |
278 |
); |
279 |
|
280 |
$schema->storage->txn_rollback; |
281 |
}; |
282 |
|
283 |
sub create_user_and_session { |
284 |
my $args = shift; |
285 |
|
286 |
my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
287 |
my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; |
288 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
289 |
my $dbh = C4::Context->dbh; |
290 |
|
291 |
my $borrower = $builder->build({ |
292 |
source => 'Borrower', |
293 |
value => { |
294 |
branchcode => $branchcode, |
295 |
categorycode => $categorycode, |
296 |
lost => 0, |
297 |
flags => $flags, |
298 |
} |
299 |
}); |
300 |
|
301 |
my $session = C4::Auth::get_session(''); |
302 |
$session->param('number', $borrower->{ borrowernumber }); |
303 |
$session->param('id', $borrower->{ userid }); |
304 |
$session->param('ip', '127.0.0.1'); |
305 |
$session->param('lasttime', time()); |
306 |
$session->flush; |
307 |
my $patron = Koha::Patrons->find($borrower->{borrowernumber}); |
308 |
if ( $args->{authorized} ) { |
309 |
$dbh->do( " |
310 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
311 |
VALUES (?,4,'get_password_reset_uuid')", undef, |
312 |
$borrower->{borrowernumber} ); |
313 |
} |
314 |
|
315 |
return ($patron, $session); |
316 |
} |