|
Lines 26-31
use t::lib::Mocks;
Link Here
|
| 26 |
|
26 |
|
| 27 |
use C4::Auth; |
27 |
use C4::Auth; |
| 28 |
use Koha::Database; |
28 |
use Koha::Database; |
|
|
29 |
use Koha::Patron::Debarments qw/AddDebarment/; |
| 29 |
|
30 |
|
| 30 |
my $schema = Koha::Database->new->schema; |
31 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
32 |
my $builder = t::lib::TestBuilder->new; |
|
Lines 41-144
subtest 'list() tests' => sub {
Link Here
|
| 41 |
plan tests => 2; |
42 |
plan tests => 2; |
| 42 |
|
43 |
|
| 43 |
$schema->storage->txn_begin; |
44 |
$schema->storage->txn_begin; |
| 44 |
|
|
|
| 45 |
unauthorized_access_tests('GET', undef, undef); |
45 |
unauthorized_access_tests('GET', undef, undef); |
|
|
46 |
$schema->storage->txn_rollback; |
| 46 |
|
47 |
|
| 47 |
subtest 'librarian access tests' => sub { |
48 |
subtest 'librarian access tests' => sub { |
| 48 |
plan tests => 8; |
49 |
plan tests => 13; |
| 49 |
|
50 |
|
| 50 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
51 |
$schema->storage->txn_begin; |
| 51 |
authorized => 1 }); |
52 |
|
| 52 |
my $patron = Koha::Patrons->find($borrowernumber); |
53 |
Koha::Patrons->search->delete; |
| 53 |
Koha::Patrons->search({ |
54 |
|
| 54 |
borrowernumber => { '!=' => $borrowernumber}, |
55 |
my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 1 }); |
| 55 |
cardnumber => { LIKE => $patron->cardnumber . "%" } |
56 |
my $patron = Koha::Patrons->find($patron_id); |
| 56 |
})->delete; |
|
|
| 57 |
Koha::Patrons->search({ |
| 58 |
borrowernumber => { '!=' => $borrowernumber}, |
| 59 |
address2 => { LIKE => $patron->address2 . "%" } |
| 60 |
})->delete; |
| 61 |
|
57 |
|
| 62 |
my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
58 |
my $tx = $t->ua->build_tx(GET => '/api/v1/patrons'); |
| 63 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
59 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 64 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
60 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
| 65 |
$t->request_ok($tx) |
61 |
$t->request_ok($tx) |
| 66 |
->status_is(200); |
62 |
->status_is(200); |
| 67 |
|
63 |
|
| 68 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons?cardnumber='. |
64 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons?cardnumber=' . $patron->cardnumber); |
| 69 |
$patron->cardnumber); |
65 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 70 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
66 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
| 71 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
|
|
| 72 |
$t->request_ok($tx) |
67 |
$t->request_ok($tx) |
| 73 |
->status_is(200) |
68 |
->status_is(200) |
| 74 |
->json_is('/0/cardnumber' => $patron->cardnumber); |
69 |
->json_is('/0/cardnumber' => $patron->cardnumber); |
| 75 |
|
70 |
|
| 76 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons?address2='. |
71 |
$tx = $t->ua->build_tx(GET => '/api/v1/patrons?address2='. |
| 77 |
$patron->address2); |
72 |
$patron->address2); |
| 78 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
73 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 79 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
74 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
| 80 |
$t->request_ok($tx) |
75 |
$t->request_ok($tx) |
| 81 |
->status_is(200) |
76 |
->status_is(200) |
| 82 |
->json_is('/0/address2' => $patron->address2); |
77 |
->json_is('/0/address2' => $patron->address2); |
| 83 |
}; |
|
|
| 84 |
|
78 |
|
| 85 |
$schema->storage->txn_rollback; |
79 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
|
|
80 |
AddDebarment({ borrowernumber => $patron_2->id }); |
| 81 |
# re-read from DB |
| 82 |
$patron_2->discard_changes; |
| 83 |
my $ub = $patron_2->unblessed; |
| 84 |
|
| 85 |
$tx = $t->ua->build_tx( GET => '/api/v1/patrons?restricted=' . Mojo::JSON->true ); |
| 86 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 87 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
| 88 |
$t->request_ok($tx) |
| 89 |
->status_is(200) |
| 90 |
->json_has('/0/restricted') |
| 91 |
->json_is( '/0/restricted' => Mojo::JSON->true ) |
| 92 |
->json_hasnt('/1'); |
| 93 |
|
| 94 |
$schema->storage->txn_rollback; |
| 95 |
}; |
| 86 |
}; |
96 |
}; |
| 87 |
|
97 |
|
| 88 |
subtest 'get() tests' => sub { |
98 |
subtest 'get() tests' => sub { |
| 89 |
plan tests => 3; |
99 |
plan tests => 3; |
| 90 |
|
100 |
|
| 91 |
$schema->storage->txn_begin; |
101 |
$schema->storage->txn_begin; |
| 92 |
|
|
|
| 93 |
unauthorized_access_tests('GET', -1, undef); |
102 |
unauthorized_access_tests('GET', -1, undef); |
|
|
103 |
$schema->storage->txn_rollback; |
| 94 |
|
104 |
|
| 95 |
subtest 'access own object tests' => sub { |
105 |
subtest 'access own object tests' => sub { |
| 96 |
plan tests => 4; |
106 |
plan tests => 4; |
| 97 |
|
107 |
|
| 98 |
my ($patronid, $patronsessionid) = create_user_and_session({ |
108 |
$schema->storage->txn_begin; |
| 99 |
authorized => 0 }); |
109 |
|
|
|
110 |
my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 }); |
| 100 |
|
111 |
|
| 101 |
# Access patron's own data even though they have no borrowers flag |
112 |
# Access patron's own data even though they have no borrowers flag |
| 102 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patronid"); |
113 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron_id); |
| 103 |
$tx->req->cookies({name => 'CGISESSID', value => $patronsessionid}); |
114 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 104 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
115 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
| 105 |
$t->request_ok($tx) |
116 |
$t->request_ok($tx) |
| 106 |
->status_is(200); |
117 |
->status_is(200); |
| 107 |
|
118 |
|
| 108 |
my $guarantee = $builder->build({ |
119 |
my $guarantee = $builder->build_object({ |
| 109 |
source => 'Borrower', |
120 |
class => 'Koha::Patrons', |
| 110 |
value => { |
121 |
value => { |
| 111 |
guarantorid => $patronid, |
122 |
guarantorid => $patron_id, |
| 112 |
} |
123 |
} |
| 113 |
}); |
124 |
}); |
| 114 |
|
125 |
|
| 115 |
# Access guarantee's data even though guarantor has no borrowers flag |
126 |
# Access guarantee's data even though guarantor has no borrowers flag |
| 116 |
my $guaranteenumber = $guarantee->{borrowernumber}; |
127 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $guarantee->id ); |
| 117 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/$guaranteenumber"); |
128 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 118 |
$tx->req->cookies({name => 'CGISESSID', value => $patronsessionid}); |
129 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
| 119 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
|
|
| 120 |
$t->request_ok($tx) |
130 |
$t->request_ok($tx) |
| 121 |
->status_is(200); |
131 |
->status_is(200); |
|
|
132 |
|
| 133 |
$schema->storage->txn_rollback; |
| 122 |
}; |
134 |
}; |
| 123 |
|
135 |
|
| 124 |
subtest 'librarian access tests' => sub { |
136 |
subtest 'librarian access tests' => sub { |
| 125 |
plan tests => 5; |
137 |
plan tests => 6; |
| 126 |
|
138 |
|
| 127 |
my ($patron_id) = create_user_and_session({ |
139 |
$schema->storage->txn_begin; |
| 128 |
authorized => 0 }); |
140 |
|
| 129 |
my $patron = Koha::Patrons->find($patron_id); |
141 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 130 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
142 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
| 131 |
authorized => 1 }); |
143 |
|
| 132 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id"); |
144 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->id); |
| 133 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
145 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 134 |
$t->request_ok($tx) |
146 |
$t->request_ok($tx) |
| 135 |
->status_is(200) |
147 |
->status_is(200) |
| 136 |
->json_is('/borrowernumber' => $patron_id) |
148 |
->json_is('/patron_id' => $patron->id) |
| 137 |
->json_is('/surname' => $patron->surname) |
149 |
->json_is('/category_id' => $patron->categorycode ) |
| 138 |
->json_is('/lost' => Mojo::JSON->false ); |
150 |
->json_is('/surname' => $patron->surname) |
| 139 |
}; |
151 |
->json_is('/patron_card_lost' => Mojo::JSON->false ); |
| 140 |
|
152 |
|
| 141 |
$schema->storage->txn_rollback; |
153 |
$schema->storage->txn_rollback; |
|
|
154 |
}; |
| 142 |
}; |
155 |
}; |
| 143 |
|
156 |
|
| 144 |
subtest 'add() tests' => sub { |
157 |
subtest 'add() tests' => sub { |
|
Lines 146-269
subtest 'add() tests' => sub {
Link Here
|
| 146 |
|
159 |
|
| 147 |
$schema->storage->txn_begin; |
160 |
$schema->storage->txn_begin; |
| 148 |
|
161 |
|
| 149 |
my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; |
162 |
my $patron = Koha::REST::V1::Patrons::_to_api( |
| 150 |
my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; |
163 |
$builder->build_object( { class => 'Koha::Patrons' } )->TO_JSON ); |
| 151 |
my $newpatron = { |
|
|
| 152 |
address => 'Street', |
| 153 |
branchcode => $branchcode, |
| 154 |
cardnumber => $branchcode.$categorycode, |
| 155 |
categorycode => $categorycode, |
| 156 |
city => 'Joenzoo', |
| 157 |
surname => "TestUser", |
| 158 |
userid => $branchcode.$categorycode, |
| 159 |
}; |
| 160 |
|
164 |
|
| 161 |
unauthorized_access_tests('POST', undef, $newpatron); |
165 |
unauthorized_access_tests('POST', undef, $patron); |
|
|
166 |
|
| 167 |
$schema->storage->txn_rollback; |
| 162 |
|
168 |
|
| 163 |
subtest 'librarian access tests' => sub { |
169 |
subtest 'librarian access tests' => sub { |
| 164 |
plan tests => 20; |
170 |
plan tests => 20; |
| 165 |
|
171 |
|
| 166 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
172 |
$schema->storage->txn_begin; |
| 167 |
authorized => 1 }); |
173 |
|
|
|
174 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 175 |
my $newpatron = Koha::REST::V1::Patrons::_to_api( $patron->TO_JSON ); |
| 176 |
# delete RO attributes |
| 177 |
delete $newpatron->{patron_id}; |
| 178 |
delete $newpatron->{restricted}; |
| 179 |
|
| 180 |
# Create a library just to make sure its ID doesn't exist on the DB |
| 181 |
my $library_to_delete = $builder->build_object({ class => 'Koha::Libraries' }); |
| 182 |
my $deleted_library_id = $library_to_delete->id; |
| 183 |
# Delete library |
| 184 |
$library_to_delete->delete; |
| 168 |
|
185 |
|
| 169 |
$newpatron->{branchcode} = "nonexistent"; # Test invalid branchcode |
186 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
|
|
187 |
|
| 188 |
$newpatron->{library_id} = $deleted_library_id; |
| 170 |
my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron ); |
189 |
my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron ); |
| 171 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
190 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 172 |
warning_like { |
191 |
warning_like { |
| 173 |
$t->request_ok($tx) |
192 |
$t->request_ok($tx) |
| 174 |
->status_is(400) |
193 |
->status_is(409) |
| 175 |
->json_is('/error' => "Given branchcode does not exist"); } |
194 |
->json_is('/error' => "Duplicate ID"); } |
| 176 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
195 |
qr/^DBD::mysql::st execute failed: Duplicate entry/; |
|
|
196 |
|
| 197 |
$newpatron->{library_id} = $patron->branchcode; |
| 177 |
|
198 |
|
| 178 |
$newpatron->{branchcode} = $branchcode; |
199 |
# Create a library just to make sure its ID doesn't exist on the DB |
|
|
200 |
my $category_to_delete = $builder->build_object({ class => 'Koha::Patron::Categories' }); |
| 201 |
my $deleted_category_id = $category_to_delete->id; |
| 202 |
# Delete library |
| 203 |
$category_to_delete->delete; |
| 179 |
|
204 |
|
| 180 |
$newpatron->{categorycode} = "nonexistent"; # Test invalid patron category |
205 |
$newpatron->{category_id} = $deleted_category_id; # Test invalid patron category |
| 181 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
206 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 182 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
207 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 183 |
$t->request_ok($tx) |
208 |
$t->request_ok($tx) |
| 184 |
->status_is(400) |
209 |
->status_is(400) |
| 185 |
->json_is('/error' => "Given categorycode does not exist"); |
210 |
->json_is('/error' => "Given category_id does not exist"); |
| 186 |
$newpatron->{categorycode} = $categorycode; |
211 |
$newpatron->{category_id} = $patron->categorycode; |
| 187 |
|
212 |
|
| 188 |
$newpatron->{falseproperty} = "Non existent property"; |
213 |
$newpatron->{falseproperty} = "Non existent property"; |
| 189 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
214 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 190 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
215 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 191 |
$t->request_ok($tx) |
216 |
$t->request_ok($tx) |
| 192 |
->status_is(400); |
217 |
->status_is(400); |
| 193 |
delete $newpatron->{falseproperty}; |
218 |
delete $newpatron->{falseproperty}; |
| 194 |
|
219 |
|
|
|
220 |
my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' }); |
| 221 |
$newpatron = Koha::REST::V1::Patrons::_to_api($patron_to_delete->TO_JSON); |
| 222 |
# delete RO attributes |
| 223 |
delete $newpatron->{patron_id}; |
| 224 |
delete $newpatron->{restricted}; |
| 225 |
$patron_to_delete->delete; |
| 226 |
|
| 195 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
227 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 196 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
228 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 197 |
$t->request_ok($tx) |
229 |
$t->request_ok($tx) |
| 198 |
->status_is(201, 'Patron created successfully') |
230 |
->status_is(201, 'Patron created successfully') |
| 199 |
->json_has('/borrowernumber', 'got a borrowernumber') |
231 |
->json_has('/patron_id', 'got a patron_id') |
| 200 |
->json_is('/cardnumber', $newpatron->{ cardnumber }) |
232 |
->json_is( '/cardnumber' => $newpatron->{ cardnumber }) |
| 201 |
->json_is('/surname' => $newpatron->{ surname }) |
233 |
->json_is( '/surname' => $newpatron->{ surname }) |
| 202 |
->json_is('/firstname' => $newpatron->{ firstname }); |
234 |
->json_is( '/firstname' => $newpatron->{ firstname }); |
| 203 |
$newpatron->{borrowernumber} = $tx->res->json->{borrowernumber}; |
|
|
| 204 |
|
235 |
|
| 205 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
236 |
$tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron); |
| 206 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
237 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 207 |
warning_like { |
238 |
warning_like { |
| 208 |
$t->request_ok($tx) |
239 |
$t->request_ok($tx) |
| 209 |
->status_is(409) |
240 |
->status_is(409) |
| 210 |
->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' ) |
241 |
->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' ) |
| 211 |
->json_has( '/conflict', 'cardnumber' ); } |
242 |
->json_has( '/conflict', 'cardnumber' ); } |
| 212 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/; |
243 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/; |
| 213 |
}; |
|
|
| 214 |
|
244 |
|
| 215 |
$schema->storage->txn_rollback; |
245 |
$schema->storage->txn_rollback; |
|
|
246 |
}; |
| 216 |
}; |
247 |
}; |
| 217 |
|
248 |
|
| 218 |
subtest 'update() tests' => sub { |
249 |
subtest 'update() tests' => sub { |
| 219 |
plan tests => 2; |
250 |
plan tests => 2; |
| 220 |
|
251 |
|
| 221 |
$schema->storage->txn_begin; |
252 |
$schema->storage->txn_begin; |
| 222 |
|
|
|
| 223 |
unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'}); |
253 |
unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'}); |
|
|
254 |
$schema->storage->txn_rollback; |
| 224 |
|
255 |
|
| 225 |
subtest 'librarian access tests' => sub { |
256 |
subtest 'librarian access tests' => sub { |
| 226 |
plan tests => 23; |
257 |
plan tests => 23; |
| 227 |
|
258 |
|
|
|
259 |
$schema->storage->txn_begin; |
| 260 |
|
| 228 |
t::lib::Mocks::mock_preference('minPasswordLength', 1); |
261 |
t::lib::Mocks::mock_preference('minPasswordLength', 1); |
| 229 |
my ($borrowernumber, $sessionid) = create_user_and_session({ authorized => 1 }); |
262 |
my ( $patron_id_1, $session_id ) = create_user_and_session({ authorized => 1 }); |
| 230 |
my ($borrowernumber2, undef) = create_user_and_session({ authorized => 0 }); |
263 |
my ( $patron_id_2, undef ) = create_user_and_session({ authorized => 0 }); |
| 231 |
|
264 |
|
| 232 |
my $patron_1 = Koha::Patrons->find($borrowernumber); |
265 |
my $patron_1 = Koha::Patrons->find($patron_id_1); |
| 233 |
my $patron_2 = Koha::Patrons->find($borrowernumber2); |
266 |
my $patron_2 = Koha::Patrons->find($patron_id_2); |
| 234 |
my $newpatron = $patron_2->TO_JSON; |
267 |
my $newpatron = Koha::REST::V1::Patrons::_to_api($patron_2->TO_JSON); |
| 235 |
# borrowernumber should not be passed in the request body for PUT |
268 |
# delete RO attributes |
| 236 |
delete $newpatron->{borrowernumber}; |
269 |
delete $newpatron->{patron_id}; |
|
|
270 |
delete $newpatron->{restricted}; |
| 237 |
|
271 |
|
| 238 |
my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/-1" => json => $newpatron ); |
272 |
my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/-1" => json => $newpatron ); |
| 239 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
273 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 240 |
$t->request_ok($tx) |
274 |
$t->request_ok($tx) |
| 241 |
->status_is(404) |
275 |
->status_is(404) |
| 242 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
276 |
->json_has('/error', 'Fails when trying to PUT nonexistent patron'); |
| 243 |
|
277 |
|
| 244 |
$newpatron->{categorycode} = 'nonexistent'; |
278 |
# Create a library just to make sure its ID doesn't exist on the DB |
| 245 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
279 |
my $category_to_delete = $builder->build_object({ class => 'Koha::Patron::Categories' }); |
| 246 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
280 |
my $deleted_category_id = $category_to_delete->id; |
|
|
281 |
# Delete library |
| 282 |
$category_to_delete->delete; |
| 283 |
|
| 284 |
$newpatron->{category_id} = $deleted_category_id; |
| 285 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$patron_id_2" => json => $newpatron ); |
| 286 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 247 |
warning_like { |
287 |
warning_like { |
| 248 |
$t->request_ok($tx) |
288 |
$t->request_ok($tx) |
| 249 |
->status_is(400) |
289 |
->status_is(400) |
| 250 |
->json_is('/error' => "Given categorycode does not exist"); } |
290 |
->json_is('/error' => "Given category_id does not exist"); } |
| 251 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
291 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
| 252 |
$newpatron->{categorycode} = $patron_2->categorycode; |
292 |
$newpatron->{category_id} = $patron_2->categorycode; |
|
|
293 |
|
| 294 |
# Create a library just to make sure its ID doesn't exist on the DB |
| 295 |
my $library_to_delete = $builder->build_object({ class => 'Koha::Libraries' }); |
| 296 |
my $deleted_library_id = $library_to_delete->id; |
| 297 |
# Delete library |
| 298 |
$library_to_delete->delete; |
| 253 |
|
299 |
|
| 254 |
$newpatron->{branchcode} = 'nonexistent'; |
300 |
$newpatron->{library_id} = $deleted_library_id; |
| 255 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
301 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron ); |
| 256 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
302 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 257 |
warning_like { |
303 |
warning_like { |
| 258 |
$t->request_ok($tx) |
304 |
$t->request_ok($tx) |
| 259 |
->status_is(400) |
305 |
->status_is(400) |
| 260 |
->json_is('/error' => "Given branchcode does not exist"); } |
306 |
->json_is('/error' => "Given library_id does not exist"); } |
| 261 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
307 |
qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/; |
| 262 |
$newpatron->{branchcode} = $patron_2->branchcode; |
308 |
$newpatron->{library_id} = $patron_2->branchcode; |
| 263 |
|
309 |
|
| 264 |
$newpatron->{falseproperty} = "Non existent property"; |
310 |
$newpatron->{falseproperty} = "Non existent property"; |
| 265 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
311 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron ); |
| 266 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
312 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 267 |
$t->request_ok($tx) |
313 |
$t->request_ok($tx) |
| 268 |
->status_is(400) |
314 |
->status_is(400) |
| 269 |
->json_is('/errors/0/message' => |
315 |
->json_is('/errors/0/message' => |
|
Lines 274-281
subtest 'update() tests' => sub {
Link Here
|
| 274 |
$newpatron->{cardnumber} = $patron_1->cardnumber; |
320 |
$newpatron->{cardnumber} = $patron_1->cardnumber; |
| 275 |
$newpatron->{userid} = $patron_1->userid; |
321 |
$newpatron->{userid} = $patron_1->userid; |
| 276 |
|
322 |
|
| 277 |
$tx = $t->ua->build_tx( PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron ); |
323 |
$tx = $t->ua->build_tx( PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron ); |
| 278 |
$tx->req->cookies({ name => 'CGISESSID', value => $sessionid }); |
324 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 279 |
warning_like { |
325 |
warning_like { |
| 280 |
$t->request_ok($tx) |
326 |
$t->request_ok($tx) |
| 281 |
->status_is(409) |
327 |
->status_is(409) |
|
Lines 283-340
subtest 'update() tests' => sub {
Link Here
|
| 283 |
->json_is( '/conflict', 'cardnumber' ); } |
329 |
->json_is( '/conflict', 'cardnumber' ); } |
| 284 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/; |
330 |
qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/; |
| 285 |
|
331 |
|
| 286 |
$newpatron->{ cardnumber } = $borrowernumber.$borrowernumber2; |
332 |
$newpatron->{ cardnumber } = $patron_id_1.$patron_id_2; |
| 287 |
$newpatron->{ userid } = "user".$borrowernumber.$borrowernumber2; |
333 |
$newpatron->{ userid } = "user".$patron_id_1.$patron_id_2; |
| 288 |
$newpatron->{ surname } = "user".$borrowernumber.$borrowernumber2; |
334 |
$newpatron->{ surname } = "user".$patron_id_1.$patron_id_2; |
| 289 |
|
335 |
|
| 290 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron); |
336 |
$tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron); |
| 291 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
337 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 292 |
$t->request_ok($tx) |
338 |
$t->request_ok($tx) |
| 293 |
->status_is(200, 'Patron updated successfully') |
339 |
->status_is(200, 'Patron updated successfully') |
| 294 |
->json_has($newpatron); |
340 |
->json_has($newpatron); |
| 295 |
is(Koha::Patrons->find( $patron_2->id )->cardnumber, |
341 |
is(Koha::Patrons->find( $patron_2->id )->cardnumber, |
| 296 |
$newpatron->{ cardnumber }, 'Patron is really updated!'); |
342 |
$newpatron->{ cardnumber }, 'Patron is really updated!'); |
| 297 |
}; |
|
|
| 298 |
|
343 |
|
| 299 |
$schema->storage->txn_rollback; |
344 |
$schema->storage->txn_rollback; |
|
|
345 |
}; |
| 300 |
}; |
346 |
}; |
| 301 |
|
347 |
|
| 302 |
subtest 'delete() tests' => sub { |
348 |
subtest 'delete() tests' => sub { |
| 303 |
plan tests => 2; |
349 |
plan tests => 2; |
| 304 |
|
350 |
|
| 305 |
$schema->storage->txn_begin; |
351 |
$schema->storage->txn_begin; |
| 306 |
|
|
|
| 307 |
unauthorized_access_tests('DELETE', 123, undef); |
352 |
unauthorized_access_tests('DELETE', 123, undef); |
|
|
353 |
$schema->storage->txn_rollback; |
| 308 |
|
354 |
|
| 309 |
subtest 'librarian access test' => sub { |
355 |
subtest 'librarian access test' => sub { |
| 310 |
plan tests => 4; |
356 |
plan tests => 4; |
| 311 |
|
357 |
|
| 312 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
358 |
$schema->storage->txn_begin; |
| 313 |
authorized => 1 }); |
359 |
|
| 314 |
my ($borrowernumber2, $sessionid2) = create_user_and_session({ |
360 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
| 315 |
authorized => 0 }); |
361 |
my ( $patron_id, undef ) = create_user_and_session({ authorized => 0 }); |
| 316 |
|
362 |
|
| 317 |
my $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/-1"); |
363 |
my $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/-1"); |
| 318 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
364 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 319 |
$t->request_ok($tx) |
365 |
$t->request_ok($tx) |
| 320 |
->status_is(404, 'Patron not found'); |
366 |
->status_is(404, 'Patron not found'); |
| 321 |
|
367 |
|
| 322 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/$borrowernumber2"); |
368 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/$patron_id"); |
| 323 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
369 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
| 324 |
$t->request_ok($tx) |
370 |
$t->request_ok($tx) |
| 325 |
->status_is(200, 'Patron deleted successfully'); |
371 |
->status_is(200, 'Patron deleted successfully'); |
| 326 |
}; |
|
|
| 327 |
|
372 |
|
| 328 |
$schema->storage->txn_rollback; |
373 |
$schema->storage->txn_rollback; |
|
|
374 |
}; |
| 329 |
}; |
375 |
}; |
| 330 |
|
376 |
|
| 331 |
# Centralized tests for 401s and 403s assuming the endpoint requires |
377 |
# Centralized tests for 401s and 403s assuming the endpoint requires |
| 332 |
# borrowers flag for access |
378 |
# borrowers flag for access |
| 333 |
sub unauthorized_access_tests { |
379 |
sub unauthorized_access_tests { |
| 334 |
my ($verb, $patronid, $json) = @_; |
380 |
my ($verb, $patron_id, $json) = @_; |
| 335 |
|
381 |
|
| 336 |
my $endpoint = '/api/v1/patrons'; |
382 |
my $endpoint = '/api/v1/patrons'; |
| 337 |
$endpoint .= ($patronid) ? "/$patronid" : ''; |
383 |
$endpoint .= ($patron_id) ? "/$patron_id" : ''; |
| 338 |
|
384 |
|
| 339 |
subtest 'unauthorized access tests' => sub { |
385 |
subtest 'unauthorized access tests' => sub { |
| 340 |
plan tests => 5; |
386 |
plan tests => 5; |
|
Lines 343-353
sub unauthorized_access_tests {
Link Here
|
| 343 |
$t->request_ok($tx) |
389 |
$t->request_ok($tx) |
| 344 |
->status_is(401); |
390 |
->status_is(401); |
| 345 |
|
391 |
|
| 346 |
my ($borrowernumber, $sessionid) = create_user_and_session({ |
392 |
my ($borrowernumber, $session_id) = create_user_and_session({ |
| 347 |
authorized => 0 }); |
393 |
authorized => 0 }); |
| 348 |
|
394 |
|
| 349 |
$tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
395 |
$tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
| 350 |
$tx->req->cookies({name => 'CGISESSID', value => $sessionid}); |
396 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
| 351 |
$t->request_ok($tx) |
397 |
$t->request_ok($tx) |
| 352 |
->status_is(403) |
398 |
->status_is(403) |
| 353 |
->json_has('/required_permissions'); |
399 |
->json_has('/required_permissions'); |
| 354 |
- |
|
|