|
Lines 17-138
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 21; |
20 |
use Test::More tests => 5; |
| 21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
|
|
22 |
use Test::Warn; |
| 23 |
|
| 22 |
use t::lib::TestBuilder; |
24 |
use t::lib::TestBuilder; |
| 23 |
use t::lib::Mocks; |
25 |
use t::lib::Mocks; |
| 24 |
|
26 |
|
| 25 |
use C4::Auth; |
27 |
use C4::Auth; |
| 26 |
use C4::Context; |
28 |
use Koha::Cities; |
| 27 |
|
|
|
| 28 |
use Koha::Database; |
29 |
use Koha::Database; |
| 29 |
use Koha::Patron; |
|
|
| 30 |
|
30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new(); |
32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
|
|
| 34 |
$schema->storage->txn_begin; |
| 35 |
|
33 |
|
| 36 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
34 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
| 37 |
# this affects the other REST api tests |
35 |
# this affects the other REST api tests |
| 38 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
36 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
| 39 |
|
37 |
|
| 40 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
38 |
my $remote_address = '127.0.0.1'; |
| 41 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 42 |
|
40 |
|
| 43 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
41 |
subtest 'list() tests' => sub { |
| 44 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
42 |
plan tests => 2; |
| 45 |
my $guarantor = $builder->build({ |
43 |
|
| 46 |
source => 'Borrower', |
44 |
$schema->storage->txn_begin; |
| 47 |
value => { |
45 |
|
| 48 |
branchcode => $branchcode, |
46 |
unauthorized_access_tests('GET', undef, undef); |
| 49 |
categorycode => $categorycode, |
47 |
|
| 50 |
flags => 0, |
48 |
subtest 'librarian access tests' => sub { |
| 51 |
} |
49 |
plan tests => 8; |
| 52 |
}); |
50 |
|
| 53 |
my $borrower = $builder->build({ |
51 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 54 |
source => 'Borrower', |
52 |
authorized => 1 }); |
| 55 |
value => { |
53 |
my $patron = Koha::Patrons->find($borrowernumber); |
| 56 |
branchcode => $branchcode, |
54 |
Koha::Patrons->search({ |
| 57 |
categorycode => $categorycode, |
55 |
borrowernumber => { '!=' => $borrowernumber}, |
| 58 |
flags => 0, |
56 |
cardnumber => { LIKE => $patron->cardnumber . "%" } |
| 59 |
lost => 1, |
57 |
})->delete; |
| 60 |
guarantorid => $guarantor->{borrowernumber}, |
58 |
Koha::Patrons->search({ |
| 61 |
} |
59 |
borrowernumber => { '!=' => $borrowernumber}, |
| 62 |
}); |
60 |
address2 => { LIKE => $patron->address2 . "%" } |
| 63 |
|
61 |
})->delete; |
| 64 |
$t->get_ok('/api/v1/patrons') |
62 |
|
| 65 |
->status_is(401); |
63 |
my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
| 66 |
|
64 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 67 |
$t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber }) |
65 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 68 |
->status_is(401); |
66 |
$t->request_ok($tx) |
| 69 |
|
67 |
->status_is(200); |
| 70 |
my $session = C4::Auth::get_session(''); |
68 |
|
| 71 |
$session->param('number', $borrower->{ borrowernumber }); |
69 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons?cardnumber='. |
| 72 |
$session->param('id', $borrower->{ userid }); |
70 |
$patron->cardnumber); |
| 73 |
$session->param('ip', '127.0.0.1'); |
71 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 74 |
$session->param('lasttime', time()); |
72 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 75 |
$session->flush; |
73 |
$t->request_ok($tx) |
| 76 |
|
74 |
->status_is(200) |
| 77 |
my $session2 = C4::Auth::get_session(''); |
75 |
->json_is('/0/cardnumber' => $patron->cardnumber); |
| 78 |
$session2->param('number', $guarantor->{ borrowernumber }); |
76 |
|
| 79 |
$session2->param('id', $guarantor->{ userid }); |
77 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons?address2='. |
| 80 |
$session2->param('ip', '127.0.0.1'); |
78 |
$patron->address2); |
| 81 |
$session2->param('lasttime', time()); |
79 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 82 |
$session2->flush; |
80 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 83 |
|
81 |
$t->request_ok($tx) |
| 84 |
my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
82 |
->status_is(200) |
| 85 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
83 |
->json_is('/0/address2' => $patron->address2); |
| 86 |
$t->request_ok($tx) |
84 |
}; |
| 87 |
->status_is(403); |
85 |
|
| 88 |
|
86 |
$schema->storage->txn_rollback; |
| 89 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1)); |
87 |
}; |
| 90 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
88 |
|
| 91 |
$t->request_ok($tx) |
89 |
subtest 'get() tests' => sub { |
| 92 |
->status_is(403) |
90 |
plan tests => 3; |
| 93 |
->json_is('/required_permissions', {"borrowers" => "1"}); |
91 |
|
| 94 |
|
92 |
$schema->storage->txn_begin; |
| 95 |
# User without permissions, but is the owner of the object |
93 |
|
| 96 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
94 |
unauthorized_access_tests('GET', -1, undef); |
| 97 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
95 |
|
| 98 |
$t->request_ok($tx) |
96 |
subtest 'access own object tests' => sub { |
| 99 |
->status_is(200); |
97 |
plan tests => 4; |
| 100 |
|
98 |
|
| 101 |
# User without permissions, but is the guarantor of the owner of the object |
99 |
my ($patronid, $patronsessionid) = create_user_and_session({ |
| 102 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
100 |
authorized => 0 }); |
| 103 |
$tx->req->cookies({name => 'CGISESSID', value => $session2->id}); |
101 |
|
| 104 |
$t->request_ok($tx) |
102 |
# Access patron's own data even though they have no borrowers flag |
| 105 |
->status_is(200) |
103 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patronid"); |
| 106 |
->json_is('/guarantorid', $guarantor->{borrowernumber}); |
104 |
$tx->req->cookies({name => 'CGISESSID', value => $patronsessionid}); |
| 107 |
|
105 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 108 |
my $loggedinuser = $builder->build({ |
106 |
$t->request_ok($tx) |
| 109 |
source => 'Borrower', |
107 |
->status_is(200); |
| 110 |
value => { |
108 |
|
|
|
109 |
my $guarantee = $builder->build({ |
| 110 |
source => 'Borrower', |
| 111 |
value => { |
| 112 |
guarantorid => $patronid, |
| 113 |
} |
| 114 |
}); |
| 115 |
|
| 116 |
# Access guarantee's data even though guarantor has no borrowers flag |
| 117 |
my $guaranteenumber = $guarantee->{borrowernumber}; |
| 118 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/$guaranteenumber"); |
| 119 |
$tx->req->cookies({name => 'CGISESSID', value => $patronsessionid}); |
| 120 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 121 |
$t->request_ok($tx) |
| 122 |
->status_is(200); |
| 123 |
}; |
| 124 |
|
| 125 |
subtest 'librarian access tests' => sub { |
| 126 |
plan tests => 5; |
| 127 |
|
| 128 |
my ($patron_id) = create_user_and_session({ |
| 129 |
authorized => 0 }); |
| 130 |
my $patron = Koha::Patrons->find($patron_id); |
| 131 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 132 |
authorized => 1 }); |
| 133 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id"); |
| 134 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 135 |
$t->request_ok($tx) |
| 136 |
->status_is(200) |
| 137 |
->json_is('/borrowernumber' => $patron_id) |
| 138 |
->json_is('/surname' => $patron->surname) |
| 139 |
->json_is('/lost' => Mojo::JSON->false ); |
| 140 |
}; |
| 141 |
|
| 142 |
$schema->storage->txn_rollback; |
| 143 |
}; |
| 144 |
|
| 145 |
subtest 'add() tests' => sub { |
| 146 |
plan tests => 2; |
| 147 |
|
| 148 |
$schema->storage->txn_begin; |
| 149 |
|
| 150 |
my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; |
| 151 |
my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; |
| 152 |
my $newpatron = { |
| 153 |
address => 'Street', |
| 111 |
branchcode => $branchcode, |
154 |
branchcode => $branchcode, |
|
|
155 |
cardnumber => $branchcode.$categorycode, |
| 112 |
categorycode => $categorycode, |
156 |
categorycode => $categorycode, |
| 113 |
flags => 16 # borrowers flag |
157 |
city => 'Joenzoo', |
| 114 |
} |
158 |
surname => "TestUser", |
| 115 |
}); |
159 |
userid => $branchcode.$categorycode, |
| 116 |
|
160 |
}; |
| 117 |
$session = C4::Auth::get_session(''); |
161 |
|
| 118 |
$session->param('number', $loggedinuser->{ borrowernumber }); |
162 |
unauthorized_access_tests('POST', undef, $newpatron); |
| 119 |
$session->param('id', $loggedinuser->{ userid }); |
163 |
|
| 120 |
$session->param('ip', '127.0.0.1'); |
164 |
subtest 'librarian access tests' => sub { |
| 121 |
$session->param('lasttime', time()); |
165 |
plan tests => 18; |
| 122 |
$session->flush; |
166 |
|
| 123 |
|
167 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 124 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
168 |
authorized => 1 }); |
| 125 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
169 |
|
| 126 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
170 |
$newpatron->{branchcode} = "nonexistent"; # Test invalid branchcode |
| 127 |
$t->request_ok($tx) |
171 |
my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" =>json => $newpatron); |
| 128 |
->status_is(200); |
172 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 129 |
|
173 |
$t->request_ok($tx) |
| 130 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{ borrowernumber }); |
174 |
->status_is(400) |
| 131 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
175 |
->json_is('/error' => "Given branchcode does not exist"); |
| 132 |
$t->request_ok($tx) |
176 |
$newpatron->{branchcode} = $branchcode; |
| 133 |
->status_is(200) |
177 |
|
| 134 |
->json_is('/borrowernumber' => $borrower->{ borrowernumber }) |
178 |
$newpatron->{categorycode} = "nonexistent"; # Test invalid patron category |
| 135 |
->json_is('/surname' => $borrower->{ surname }) |
179 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 136 |
->json_is('/lost' => Mojo::JSON->true ); |
180 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 137 |
|
181 |
$t->request_ok($tx) |
| 138 |
$schema->storage->txn_rollback; |
182 |
->status_is(400) |
|
|
183 |
->json_is('/error' => "Given categorycode does not exist"); |
| 184 |
$newpatron->{categorycode} = $categorycode; |
| 185 |
|
| 186 |
$newpatron->{falseproperty} = "Non existent property"; |
| 187 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 188 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 189 |
$t->request_ok($tx) |
| 190 |
->status_is(400); |
| 191 |
delete $newpatron->{falseproperty}; |
| 192 |
|
| 193 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 194 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 195 |
$t->request_ok($tx) |
| 196 |
->status_is(201, 'Patron created successfully') |
| 197 |
->json_has('/borrowernumber', 'got a borrowernumber') |
| 198 |
->json_is('/cardnumber', $newpatron->{ cardnumber }) |
| 199 |
->json_is('/surname' => $newpatron->{ surname }) |
| 200 |
->json_is('/firstname' => $newpatron->{ firstname }); |
| 201 |
$newpatron->{borrowernumber} = $tx->res->json->{borrowernumber}; |
| 202 |
|
| 203 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 204 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 205 |
$t->request_ok($tx) |
| 206 |
->status_is(409) |
| 207 |
->json_has('/error', 'Fails when trying to POST duplicate'. |
| 208 |
' cardnumber or userid') |
| 209 |
->json_has('/conflict', { |
| 210 |
userid => $newpatron->{ userid }, |
| 211 |
cardnumber => $newpatron->{ cardnumber } |
| 212 |
} |
| 213 |
); |
| 214 |
}; |
| 215 |
|
| 216 |
$schema->storage->txn_rollback; |
| 217 |
}; |
| 218 |
|
| 219 |
subtest 'edit() tests' => sub { |
| 220 |
plan tests => 3; |
| 221 |
|
| 222 |
$schema->storage->txn_begin; |
| 223 |
|
| 224 |
unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'}); |
| 225 |
|
| 226 |
subtest 'patron modifying own data' => sub { |
| 227 |
plan tests => 7; |
| 228 |
|
| 229 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 230 |
authorized => 0 }); |
| 231 |
my $patron = Koha::Patrons->find($borrowernumber)->TO_JSON; |
| 232 |
|
| 233 |
t::lib::Mocks::mock_preference("OPACPatronDetails", 0); |
| 234 |
my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 235 |
$patron->{borrowernumber} => json => $patron); |
| 236 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 237 |
$t->request_ok($tx) |
| 238 |
->status_is(403, 'OPACPatronDetails off - modifications not allowed.'); |
| 239 |
|
| 240 |
t::lib::Mocks::mock_preference("OPACPatronDetails", 1); |
| 241 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 242 |
$patron->{borrowernumber} => json => $patron); |
| 243 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 244 |
$t->request_ok($tx) |
| 245 |
->status_is(204, 'Updating myself with my current data'); |
| 246 |
|
| 247 |
$patron->{'firstname'} = "noob"; |
| 248 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 249 |
$patron->{borrowernumber} => json => $patron); |
| 250 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 251 |
$t->request_ok($tx) |
| 252 |
->status_is(202, 'Updating myself with my current data'); |
| 253 |
|
| 254 |
# Approve changes |
| 255 |
Koha::Patron::Modifications->find({ |
| 256 |
borrowernumber => $patron->{borrowernumber}, |
| 257 |
firstname => "noob" |
| 258 |
})->approve; |
| 259 |
is(Koha::Patrons->find({ |
| 260 |
borrowernumber => $patron->{borrowernumber}})->firstname, |
| 261 |
"noob", "Changes approved"); |
| 262 |
}; |
| 263 |
|
| 264 |
subtest 'librarian access tests' => sub { |
| 265 |
plan tests => 20; |
| 266 |
|
| 267 |
t::lib::Mocks::mock_preference('minPasswordLength', 1); |
| 268 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 269 |
authorized => 1 }); |
| 270 |
my ($borrowernumber2, undef) = create_user_and_session({ |
| 271 |
authorized => 0 }); |
| 272 |
my $patron = Koha::Patrons->find($borrowernumber2); |
| 273 |
my $newpatron = Koha::Patrons->find($borrowernumber2)->TO_JSON; |
| 274 |
|
| 275 |
my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/-1" => |
| 276 |
json => $newpatron); |
| 277 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 278 |
$t->request_ok($tx) |
| 279 |
->status_is(404) |
| 280 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
| 281 |
|
| 282 |
$newpatron->{categorycode} = 'nonexistent'; |
| 283 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 284 |
$newpatron->{borrowernumber} => json => $newpatron |
| 285 |
); |
| 286 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 287 |
$t->request_ok($tx) |
| 288 |
->status_is(400) |
| 289 |
->json_is('/error' => "Given categorycode does not exist"); |
| 290 |
$newpatron->{categorycode} = $patron->categorycode; |
| 291 |
|
| 292 |
$newpatron->{branchcode} = 'nonexistent'; |
| 293 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 294 |
$newpatron->{borrowernumber} => json => $newpatron |
| 295 |
); |
| 296 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 297 |
$t->request_ok($tx) |
| 298 |
->status_is(400) |
| 299 |
->json_is('/error' => "Given branchcode does not exist"); |
| 300 |
$newpatron->{branchcode} = $patron->branchcode; |
| 301 |
|
| 302 |
$newpatron->{falseproperty} = "Non existent property"; |
| 303 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 304 |
$newpatron->{borrowernumber} => json => $newpatron); |
| 305 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 306 |
$t->request_ok($tx) |
| 307 |
->status_is(400) |
| 308 |
->json_is('/errors/0/message' => |
| 309 |
'Properties not allowed: falseproperty.'); |
| 310 |
delete $newpatron->{falseproperty}; |
| 311 |
|
| 312 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 313 |
$borrowernumber => json => $newpatron); |
| 314 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 315 |
$t->request_ok($tx) |
| 316 |
->status_is(409) |
| 317 |
->json_has('/error' => "Fails when trying to update to an existing" |
| 318 |
."cardnumber or userid") |
| 319 |
->json_has('/conflict', { |
| 320 |
cardnumber => $newpatron->{ cardnumber }, |
| 321 |
userid => $newpatron->{ userid } |
| 322 |
} |
| 323 |
); |
| 324 |
|
| 325 |
$newpatron->{ cardnumber } = $borrowernumber.$borrowernumber2; |
| 326 |
$newpatron->{ userid } = "user".$borrowernumber.$borrowernumber2; |
| 327 |
$newpatron->{ surname } = "user".$borrowernumber.$borrowernumber2; |
| 328 |
|
| 329 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . |
| 330 |
$newpatron->{borrowernumber} => json => $newpatron); |
| 331 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 332 |
$t->request_ok($tx) |
| 333 |
->status_is(200, 'Patron updated successfully') |
| 334 |
->json_has($newpatron); |
| 335 |
is(Koha::Patrons->find($newpatron->{borrowernumber})->cardnumber, |
| 336 |
$newpatron->{ cardnumber }, 'Patron is really updated!'); |
| 337 |
}; |
| 338 |
|
| 339 |
$schema->storage->txn_rollback; |
| 340 |
}; |
| 341 |
|
| 342 |
subtest 'delete() tests' => sub { |
| 343 |
plan tests => 2; |
| 344 |
|
| 345 |
$schema->storage->txn_begin; |
| 346 |
|
| 347 |
unauthorized_access_tests('DELETE', 123, undef); |
| 348 |
|
| 349 |
subtest 'librarian access test' => sub { |
| 350 |
plan tests => 4; |
| 351 |
|
| 352 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 353 |
authorized => 1 }); |
| 354 |
my ($borrowernumber2, $sessionid2) = create_user_and_session({ |
| 355 |
authorized => 0 }); |
| 356 |
|
| 357 |
my $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/-1"); |
| 358 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 359 |
$t->request_ok($tx) |
| 360 |
->status_is(404, 'Patron not found'); |
| 361 |
|
| 362 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/$borrowernumber2"); |
| 363 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 364 |
$t->request_ok($tx) |
| 365 |
->status_is(200, 'Patron deleted successfully'); |
| 366 |
}; |
| 367 |
|
| 368 |
$schema->storage->txn_rollback; |
| 369 |
}; |
| 370 |
|
| 371 |
# Centralized tests for 401s and 403s assuming the endpoint requires |
| 372 |
# borrowers flag for access |
| 373 |
sub unauthorized_access_tests { |
| 374 |
my ($verb, $patronid, $json) = @_; |
| 375 |
|
| 376 |
my $endpoint = '/api/v1/patrons'; |
| 377 |
$endpoint .= ($patronid) ? "/$patronid" : ''; |
| 378 |
|
| 379 |
subtest 'unauthorized access tests' => sub { |
| 380 |
plan tests => 5; |
| 381 |
|
| 382 |
my $tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
| 383 |
$t->request_ok($tx) |
| 384 |
->status_is(401); |
| 385 |
|
| 386 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
| 387 |
authorized => 0 }); |
| 388 |
|
| 389 |
$tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
| 390 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
| 391 |
$t->request_ok($tx) |
| 392 |
->status_is(403) |
| 393 |
->json_is('/required_permissions', {"borrowers" => "1"}); |
| 394 |
}; |
| 395 |
} |
| 396 |
|
| 397 |
sub create_user_and_session { |
| 398 |
|
| 399 |
my $args = shift; |
| 400 |
my $flags = ( $args->{authorized} ) ? 16 : 0; |
| 401 |
my $dbh = C4::Context->dbh; |
| 402 |
|
| 403 |
my $user = $builder->build( |
| 404 |
{ |
| 405 |
source => 'Borrower', |
| 406 |
value => { |
| 407 |
flags => $flags, |
| 408 |
gonenoaddress => 0, |
| 409 |
lost => 0, |
| 410 |
email => 'nobody@example.com', |
| 411 |
emailpro => 'nobody@example.com', |
| 412 |
B_email => 'nobody@example.com', |
| 413 |
} |
| 414 |
} |
| 415 |
); |
| 416 |
|
| 417 |
# Create a session for the authorized user |
| 418 |
my $session = C4::Auth::get_session(''); |
| 419 |
$session->param( 'number', $user->{borrowernumber} ); |
| 420 |
$session->param( 'id', $user->{userid} ); |
| 421 |
$session->param( 'ip', '127.0.0.1' ); |
| 422 |
$session->param( 'lasttime', time() ); |
| 423 |
$session->flush; |
| 424 |
|
| 425 |
return ( $user->{borrowernumber}, $session->id ); |
| 426 |
} |
| 139 |
- |
|
|