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 |
|
21 |
use Test::More tests => 2; |
22 |
use Test::Mojo; |
23 |
use Test::MockModule; |
24 |
|
25 |
use t::lib::Mocks; |
26 |
use t::lib::TestBuilder; |
27 |
|
28 |
use C4::Auth; |
29 |
use C4::Context; |
30 |
|
31 |
use Koha::Database; |
32 |
use Koha::Notice::Messages; |
33 |
|
34 |
use Crypt::Eksblowfish::Bcrypt qw(en_base64); |
35 |
|
36 |
my $schema = Koha::Database->new->schema; |
37 |
my $builder = t::lib::TestBuilder->new; |
38 |
|
39 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
40 |
# this affects the other REST api tests |
41 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
42 |
|
43 |
my $remote_address = '127.0.0.1'; |
44 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
45 |
|
46 |
subtest 'recovery() tests' => sub { |
47 |
plan tests => 29; |
48 |
|
49 |
$schema->storage->txn_begin; |
50 |
|
51 |
my $url = '/api/v1/patrons/password/recovery'; |
52 |
|
53 |
my ($patron, $session) = create_user_and_session(); |
54 |
|
55 |
my $tx = $t->ua->build_tx(POST => $url => json => {}); |
56 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
57 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
58 |
$t->request_ok($tx) |
59 |
->status_is(400); |
60 |
|
61 |
t::lib::Mocks::mock_preference('OpacPasswordReset', 0); |
62 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 0); |
63 |
$tx = $t->ua->build_tx(POST => $url => json => { |
64 |
email => $patron->email, |
65 |
cardnumber => $patron->cardnumber |
66 |
}); |
67 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
68 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
69 |
$t->request_ok($tx) |
70 |
->status_is(403); |
71 |
|
72 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 1); |
73 |
$tx = $t->ua->build_tx(POST => $url => json => { |
74 |
email => $patron->email, |
75 |
cardnumber => $patron->cardnumber |
76 |
}); |
77 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
78 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
79 |
$t->request_ok($tx) |
80 |
->status_is(403); |
81 |
|
82 |
t::lib::Mocks::mock_preference('OpacPasswordReset', 1); |
83 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 0); |
84 |
$tx = $t->ua->build_tx(POST => $url => json => { |
85 |
email => $patron->email, |
86 |
cardnumber => $patron->cardnumber |
87 |
}); |
88 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
89 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
90 |
$t->request_ok($tx) |
91 |
->status_is(403); |
92 |
|
93 |
t::lib::Mocks::mock_preference('OpacPasswordChange', 1); |
94 |
|
95 |
$tx = $t->ua->build_tx(POST => $url => json => { |
96 |
email => 'nonexistent', |
97 |
cardnumber => $patron->cardnumber |
98 |
}); |
99 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
100 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
101 |
$t->request_ok($tx) |
102 |
->status_is(404); |
103 |
|
104 |
$tx = $t->ua->build_tx(POST => $url => json => { |
105 |
email => $patron->email, |
106 |
cardnumber => 'nonexistent' |
107 |
}); |
108 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
109 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
110 |
$t->request_ok($tx) |
111 |
->status_is(404); |
112 |
|
113 |
$tx = $t->ua->build_tx(POST => $url => json => { |
114 |
email => 'nonexistent', |
115 |
userid => $patron->userid |
116 |
}); |
117 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
118 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
119 |
$t->request_ok($tx) |
120 |
->status_is(404); |
121 |
|
122 |
$tx = $t->ua->build_tx(POST => $url => json => { |
123 |
email => $patron->email, |
124 |
userid => 'nonexistent' |
125 |
}); |
126 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
127 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
128 |
$t->request_ok($tx) |
129 |
->status_is(404); |
130 |
|
131 |
$tx = $t->ua->build_tx(POST => $url => json => { |
132 |
email => $patron->email, |
133 |
cardnumber => $patron->cardnumber |
134 |
}); |
135 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
136 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
137 |
$t->request_ok($tx) |
138 |
->status_is(201) |
139 |
->json_is('/status' => 1); |
140 |
|
141 |
my $rs = Koha::Database->new->schema->resultset('BorrowerPasswordRecovery'); |
142 |
is( |
143 |
$rs->search({ borrowernumber => $patron->borrowernumber })->count, 1, |
144 |
'Password modification request found in database' |
145 |
); |
146 |
|
147 |
$tx = $t->ua->build_tx(POST => $url => json => { |
148 |
email => $patron->email, |
149 |
userid => $patron->userid |
150 |
}); |
151 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
152 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
153 |
$t->request_ok($tx) |
154 |
->status_is(201) |
155 |
->json_is('/status' => 1); |
156 |
|
157 |
is( |
158 |
$rs->search({ borrowernumber => $patron->borrowernumber })->count, 1, |
159 |
'Password modification request found in database' |
160 |
); |
161 |
|
162 |
$tx = $t->ua->build_tx(POST => $url => json => { |
163 |
email => $patron->email, |
164 |
userid => $patron->userid, |
165 |
cardnumber => $patron->cardnumber, |
166 |
}); |
167 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
168 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
169 |
$t->request_ok($tx) |
170 |
->status_is(201) |
171 |
->json_is('/status' => 1); |
172 |
|
173 |
is( |
174 |
$rs->search({ borrowernumber => $patron->borrowernumber })->count, 1, |
175 |
'Password modification request found in database' |
176 |
); |
177 |
|
178 |
my $notice = Koha::Notice::Messages->search({ |
179 |
borrowernumber => $patron->borrowernumber, |
180 |
letter_code => 'PASSWORD_RESET', |
181 |
message_transport_type => 'email' |
182 |
})->count; |
183 |
is($notice, 3, 'Found password reset letters in message queue.'); |
184 |
|
185 |
$schema->storage->txn_rollback; |
186 |
}; |
187 |
|
188 |
subtest 'complete_recovery() tests' => sub { |
189 |
plan tests => 7; |
190 |
|
191 |
$schema->storage->txn_begin; |
192 |
|
193 |
my $rs = Koha::Database->new->schema->resultset('BorrowerPasswordRecovery'); |
194 |
|
195 |
my ($patron, $session) = create_user_and_session(); |
196 |
my $uuid_str; |
197 |
do { |
198 |
$uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16)); |
199 |
} while ( substr ( $uuid_str, -1, 1 ) eq '.' ); |
200 |
my $recovery = $builder->build({ |
201 |
source => 'BorrowerPasswordRecovery', |
202 |
value => { |
203 |
borrowernumber => $patron->borrowernumber, |
204 |
uuid => $uuid_str |
205 |
} |
206 |
}); |
207 |
|
208 |
my $url = '/api/v1/patrons/password/recovery/complete'; |
209 |
|
210 |
my $tx = $t->ua->build_tx(POST => $url => json => {}); |
211 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
212 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
213 |
$t->request_ok($tx) |
214 |
->status_is(400); |
215 |
|
216 |
$tx = $t->ua->build_tx(POST => $url.'notfound' => json => { |
217 |
uuid => $uuid_str, |
218 |
new_password => 'test', |
219 |
confirm_new_password => 'test', |
220 |
}); |
221 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
222 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
223 |
$t->request_ok($tx) |
224 |
->status_is(404); |
225 |
|
226 |
t::lib::Mocks::mock_preference('minPasswordLength', 4); |
227 |
$tx = $t->ua->build_tx(POST => $url => json => { |
228 |
uuid => $uuid_str, |
229 |
new_password => '1234', |
230 |
confirm_new_password => '1234', |
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(200); |
236 |
|
237 |
my $stored_pw = Koha::Patrons->find($patron->borrowernumber)->password; |
238 |
is( |
239 |
$stored_pw, |
240 |
Koha::AuthUtils::hash_password('1234', $stored_pw), 'Password changed' |
241 |
); |
242 |
|
243 |
$schema->storage->txn_rollback; |
244 |
}; |
245 |
|
246 |
sub create_user_and_session { |
247 |
my ($flags) = @_; |
248 |
|
249 |
my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; |
250 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
251 |
|
252 |
my $borrower = $builder->build({ |
253 |
source => 'Borrower', |
254 |
value => { |
255 |
branchcode => $branchcode, |
256 |
categorycode => $categorycode, |
257 |
lost => 0, |
258 |
} |
259 |
}); |
260 |
|
261 |
my $session = C4::Auth::get_session(''); |
262 |
$session->param('number', $borrower->{ borrowernumber }); |
263 |
$session->param('id', $borrower->{ userid }); |
264 |
$session->param('ip', '127.0.0.1'); |
265 |
$session->param('lasttime', time()); |
266 |
$session->flush; |
267 |
my $patron = Koha::Patrons->find($borrower->{borrowernumber}); |
268 |
if ( $flags ) { |
269 |
Koha::Auth::PermissionManager->grantPermissions($patron, $flags); |
270 |
} |
271 |
|
272 |
return ($patron, $session); |
273 |
} |