Lines 19-24
use Modern::Perl;
Link Here
|
19 |
|
19 |
|
20 |
use Test::More tests => 5; |
20 |
use Test::More tests => 5; |
21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
|
|
22 |
use Test::Warn; |
22 |
|
23 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
use t::lib::Mocks; |
Lines 160-166
subtest 'add() tests' => sub {
Link Here
|
160 |
unauthorized_access_tests('POST', undef, $newpatron); |
161 |
unauthorized_access_tests('POST', undef, $newpatron); |
161 |
|
162 |
|
162 |
subtest 'librarian access tests' => sub { |
163 |
subtest 'librarian access tests' => sub { |
163 |
plan tests => 18; |
164 |
plan tests => 20; |
164 |
|
165 |
|
165 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
166 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
166 |
authorized => 1 }); |
167 |
authorized => 1 }); |
Lines 168-176
subtest 'add() tests' => sub {
Link Here
|
168 |
$newpatron->{branchcode} = "nonexistent"; # Test invalid branchcode |
169 |
$newpatron->{branchcode} = "nonexistent"; # Test invalid branchcode |
169 |
my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron ); |
170 |
my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron ); |
170 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
171 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
171 |
$t->request_ok($tx) |
172 |
warning_like { |
172 |
->status_is(400) |
173 |
$t->request_ok($tx) |
173 |
->json_is('/error' => "Given branchcode does not exist"); |
174 |
->status_is(400) |
|
|
175 |
->json_is('/error' => "Given branchcode does not exist"); } |
176 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
177 |
|
174 |
$newpatron->{branchcode} = $branchcode; |
178 |
$newpatron->{branchcode} = $branchcode; |
175 |
|
179 |
|
176 |
$newpatron->{categorycode} = "nonexistent"; # Test invalid patron category |
180 |
$newpatron->{categorycode} = "nonexistent"; # Test invalid patron category |
Lines 200-214
subtest 'add() tests' => sub {
Link Here
|
200 |
|
204 |
|
201 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
205 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
202 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
206 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
203 |
$t->request_ok($tx) |
207 |
warning_like { |
204 |
->status_is(409) |
208 |
$t->request_ok($tx) |
205 |
->json_has('/error', 'Fails when trying to POST duplicate'. |
209 |
->status_is(409) |
206 |
' cardnumber or userid') |
210 |
->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' ) |
207 |
->json_has('/conflict', { |
211 |
->json_has( '/conflict', 'cardnumber' ); } |
208 |
userid => $newpatron->{ userid }, |
212 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/; |
209 |
cardnumber => $newpatron->{ cardnumber } |
|
|
210 |
} |
211 |
); |
212 |
}; |
213 |
}; |
213 |
|
214 |
|
214 |
$schema->storage->txn_rollback; |
215 |
$schema->storage->txn_rollback; |
Lines 222-228
subtest 'update() tests' => sub {
Link Here
|
222 |
unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'}); |
223 |
unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'}); |
223 |
|
224 |
|
224 |
subtest 'librarian access tests' => sub { |
225 |
subtest 'librarian access tests' => sub { |
225 |
plan tests => 20; |
226 |
plan tests => 23; |
226 |
|
227 |
|
227 |
t::lib::Mocks::mock_preference('minPasswordLength', 1); |
228 |
t::lib::Mocks::mock_preference('minPasswordLength', 1); |
228 |
my ($borrowernumber, $sessionid) = create_user_and_session({ authorized => 1 }); |
229 |
my ($borrowernumber, $sessionid) = create_user_and_session({ authorized => 1 }); |
Lines 243-259
subtest 'update() tests' => sub {
Link Here
|
243 |
$newpatron->{categorycode} = 'nonexistent'; |
244 |
$newpatron->{categorycode} = 'nonexistent'; |
244 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
245 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
245 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
246 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
246 |
$t->request_ok($tx) |
247 |
warning_like { |
247 |
->status_is(400) |
248 |
$t->request_ok($tx) |
248 |
->json_is('/error' => "Given categorycode does not exist"); |
249 |
->status_is(400) |
|
|
250 |
->json_is('/error' => "Given categorycode does not exist"); } |
251 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
249 |
$newpatron->{categorycode} = $patron_2->categorycode; |
252 |
$newpatron->{categorycode} = $patron_2->categorycode; |
250 |
|
253 |
|
251 |
$newpatron->{branchcode} = 'nonexistent'; |
254 |
$newpatron->{branchcode} = 'nonexistent'; |
252 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
255 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
253 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
256 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
254 |
$t->request_ok($tx) |
257 |
warning_like { |
255 |
->status_is(400) |
258 |
$t->request_ok($tx) |
256 |
->json_is('/error' => "Given branchcode does not exist"); |
259 |
->status_is(400) |
|
|
260 |
->json_is('/error' => "Given branchcode does not exist"); } |
261 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
257 |
$newpatron->{branchcode} = $patron_2->branchcode; |
262 |
$newpatron->{branchcode} = $patron_2->branchcode; |
258 |
|
263 |
|
259 |
$newpatron->{falseproperty} = "Non existent property"; |
264 |
$newpatron->{falseproperty} = "Non existent property"; |
Lines 271-284
subtest 'update() tests' => sub {
Link Here
|
271 |
|
276 |
|
272 |
$tx = $t->ua->build_tx( PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
277 |
$tx = $t->ua->build_tx( PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
273 |
$tx->req->cookies({ name => 'CGISESSID', value => $sessionid }); |
278 |
$tx->req->cookies({ name => 'CGISESSID', value => $sessionid }); |
274 |
$t->request_ok($tx)->status_is(409) |
279 |
warning_like { |
275 |
->json_has( '/error' => "Fails when trying to update to an existing cardnumber or userid") |
280 |
$t->request_ok($tx) |
276 |
->json_is( '/conflict', |
281 |
->status_is(409) |
277 |
{ |
282 |
->json_has( '/error' => "Fails when trying to update to an existing cardnumber or userid") |
278 |
cardnumber => $newpatron->{cardnumber}, |
283 |
->json_is( '/conflict', 'cardnumber' ); } |
279 |
userid => $newpatron->{userid} |
284 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/; |
280 |
} |
|
|
281 |
); |
282 |
|
285 |
|
283 |
$newpatron->{ cardnumber } = $borrowernumber.$borrowernumber2; |
286 |
$newpatron->{ cardnumber } = $borrowernumber.$borrowernumber2; |
284 |
$newpatron->{ userid } = "user".$borrowernumber.$borrowernumber2; |
287 |
$newpatron->{ userid } = "user".$borrowernumber.$borrowernumber2; |
285 |
- |
|
|