3 # This file is part of Koha.
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
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.
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.
20 use Test::More tests => 5;
24 use t::lib::TestBuilder;
29 use Koha::Patron::Debarments qw/AddDebarment/;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
35 # this affects the other REST api tests
36 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
38 my $remote_address = '127.0.0.1';
39 my $t = Test::Mojo->new('Koha::REST::V1');
41 subtest 'list() tests' => sub {
44 $schema->storage->txn_begin;
45 unauthorized_access_tests('GET', undef, undef);
46 $schema->storage->txn_rollback;
48 subtest 'librarian access tests' => sub {
51 $schema->storage->txn_begin;
53 my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 1 });
54 my $patron = Koha::Patrons->find($patron_id);
56 my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
57 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
58 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
62 $tx = $t->ua->build_tx(GET => '/api/v1/patrons?cardnumber=' . $patron->cardnumber);
63 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
64 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
67 ->json_is('/0/cardnumber' => $patron->cardnumber);
69 $tx = $t->ua->build_tx(GET => '/api/v1/patrons?address2='.
71 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
72 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
75 ->json_is('/0/address2' => $patron->address2);
77 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
78 AddDebarment({ borrowernumber => $patron_2->id });
80 $patron_2->discard_changes;
81 my $ub = $patron_2->unblessed;
83 $tx = $t->ua->build_tx( GET => '/api/v1/patrons?restricted=' . Mojo::JSON->true );
84 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
85 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
88 ->json_has('/0/restricted')
89 ->json_is( '/0/restricted' => Mojo::JSON->true )
92 $schema->storage->txn_rollback;
96 subtest 'get() tests' => sub {
99 $schema->storage->txn_begin;
100 unauthorized_access_tests('GET', -1, undef);
101 $schema->storage->txn_rollback;
103 subtest 'librarian access tests' => sub {
106 $schema->storage->txn_begin;
108 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
109 my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
111 my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->id);
112 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
115 ->json_is('/patron_id' => $patron->id)
116 ->json_is('/category_id' => $patron->categorycode )
117 ->json_is('/surname' => $patron->surname)
118 ->json_is('/patron_card_lost' => Mojo::JSON->false );
120 $schema->storage->txn_rollback;
124 subtest 'add() tests' => sub {
127 $schema->storage->txn_begin;
129 my $patron = Koha::REST::V1::Patrons::_to_api(
130 $builder->build_object( { class => 'Koha::Patrons' } )->TO_JSON );
132 unauthorized_access_tests('POST', undef, $patron);
134 $schema->storage->txn_rollback;
136 subtest 'librarian access tests' => sub {
139 $schema->storage->txn_begin;
141 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
142 my $newpatron = Koha::REST::V1::Patrons::_to_api( $patron->TO_JSON );
143 # delete RO attributes
144 delete $newpatron->{patron_id};
145 delete $newpatron->{restricted};
147 # Create a library just to make sure its ID doesn't exist on the DB
148 my $library_to_delete = $builder->build_object({ class => 'Koha::Libraries' });
149 my $deleted_library_id = $library_to_delete->id;
151 $library_to_delete->delete;
153 my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
155 $newpatron->{library_id} = $deleted_library_id;
156 my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron );
157 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
161 ->json_is('/error' => "Duplicate ID"); }
162 qr/^DBD::mysql::st execute failed: Duplicate entry/;
164 $newpatron->{library_id} = $patron->branchcode;
166 # Create a library just to make sure its ID doesn't exist on the DB
167 my $category_to_delete = $builder->build_object({ class => 'Koha::Patron::Categories' });
168 my $deleted_category_id = $category_to_delete->id;
170 $category_to_delete->delete;
172 $newpatron->{category_id} = $deleted_category_id; # Test invalid patron category
173 $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
174 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
177 ->json_is('/error' => "Given category_id does not exist");
178 $newpatron->{category_id} = $patron->categorycode;
180 $newpatron->{falseproperty} = "Non existent property";
181 $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
182 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
185 delete $newpatron->{falseproperty};
187 my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' });
188 $newpatron = Koha::REST::V1::Patrons::_to_api($patron_to_delete->TO_JSON);
189 # delete RO attributes
190 delete $newpatron->{patron_id};
191 delete $newpatron->{restricted};
192 $patron_to_delete->delete;
194 $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
195 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
197 ->status_is(201, 'Patron created successfully')
198 ->json_has('/patron_id', 'got a patron_id')
199 ->json_is( '/cardnumber' => $newpatron->{ cardnumber })
200 ->json_is( '/surname' => $newpatron->{ surname })
201 ->json_is( '/firstname' => $newpatron->{ firstname });
203 $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
204 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
208 ->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' )
209 ->json_has( '/conflict', 'cardnumber' ); }
210 qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/;
212 $schema->storage->txn_rollback;
216 subtest 'update() tests' => sub {
219 $schema->storage->txn_begin;
220 unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'});
221 $schema->storage->txn_rollback;
223 subtest 'librarian access tests' => sub {
226 $schema->storage->txn_begin;
228 t::lib::Mocks::mock_preference('minPasswordLength', 1);
229 my ( $patron_id_1, $session_id ) = create_user_and_session({ authorized => 1 });
230 my ( $patron_id_2, undef ) = create_user_and_session({ authorized => 0 });
232 my $patron_1 = Koha::Patrons->find($patron_id_1);
233 my $patron_2 = Koha::Patrons->find($patron_id_2);
234 my $newpatron = Koha::REST::V1::Patrons::_to_api($patron_2->TO_JSON);
235 # delete RO attributes
236 delete $newpatron->{patron_id};
237 delete $newpatron->{restricted};
239 my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/-1" => json => $newpatron );
240 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
243 ->json_has('/error', 'Fails when trying to PUT nonexistent patron');
245 # Create a library just to make sure its ID doesn't exist on the DB
246 my $category_to_delete = $builder->build_object({ class => 'Koha::Patron::Categories' });
247 my $deleted_category_id = $category_to_delete->id;
249 $category_to_delete->delete;
251 $newpatron->{category_id} = $deleted_category_id;
252 $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$patron_id_2" => json => $newpatron );
253 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
256 ->json_is('/error' => "Given category_id does not exist");
257 $newpatron->{category_id} = $patron_2->categorycode;
259 # Create a library just to make sure its ID doesn't exist on the DB
260 my $library_to_delete = $builder->build_object({ class => 'Koha::Libraries' });
261 my $deleted_library_id = $library_to_delete->id;
263 $library_to_delete->delete;
265 $newpatron->{library_id} = $deleted_library_id;
266 $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron );
267 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
271 ->json_is('/error' => "Given library_id does not exist"); }
272 qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/;
273 $newpatron->{library_id} = $patron_2->branchcode;
275 $newpatron->{falseproperty} = "Non existent property";
276 $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron );
277 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
280 ->json_is('/errors/0/message' =>
281 'Properties not allowed: falseproperty.');
282 delete $newpatron->{falseproperty};
284 # Set both cardnumber and userid to already existing values
285 $newpatron->{cardnumber} = $patron_1->cardnumber;
286 $newpatron->{userid} = $patron_1->userid;
288 $tx = $t->ua->build_tx( PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron );
289 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
293 ->json_has( '/error' => "Fails when trying to update to an existing cardnumber or userid")
294 ->json_is( '/conflict', 'cardnumber' ); }
295 qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/;
297 $newpatron->{ cardnumber } = $patron_id_1.$patron_id_2;
298 $newpatron->{ userid } = "user".$patron_id_1.$patron_id_2;
299 $newpatron->{ surname } = "user".$patron_id_1.$patron_id_2;
301 $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron);
302 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
304 ->status_is(200, 'Patron updated successfully')
305 ->json_has($newpatron);
306 is(Koha::Patrons->find( $patron_2->id )->cardnumber,
307 $newpatron->{ cardnumber }, 'Patron is really updated!');
309 $schema->storage->txn_rollback;
313 subtest 'delete() tests' => sub {
316 $schema->storage->txn_begin;
317 unauthorized_access_tests('DELETE', 123, undef);
318 $schema->storage->txn_rollback;
320 subtest 'librarian access test' => sub {
323 $schema->storage->txn_begin;
325 my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
326 my ( $patron_id, undef ) = create_user_and_session({ authorized => 0 });
328 my $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/-1");
329 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
331 ->status_is(404, 'Patron not found');
333 $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/$patron_id");
334 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
336 ->status_is(200, 'Patron deleted successfully');
338 $schema->storage->txn_rollback;
342 # Centralized tests for 401s and 403s assuming the endpoint requires
343 # borrowers flag for access
344 sub unauthorized_access_tests {
345 my ($verb, $patron_id, $json) = @_;
347 my $endpoint = '/api/v1/patrons';
348 $endpoint .= ($patron_id) ? "/$patron_id" : '';
350 subtest 'unauthorized access tests' => sub {
353 my $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
357 my ($borrowernumber, $session_id) = create_user_and_session({
360 $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
361 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
364 ->json_has('/required_permissions');
368 sub create_user_and_session {
371 my $flags = ( $args->{authorized} ) ? 16 : 0;
373 my $user = $builder->build(
375 source => 'Borrower',
380 email => 'nobody@example.com',
381 emailpro => 'nobody@example.com',
382 B_email => 'nobody@example.com'
387 # Create a session for the authorized user
388 my $session = C4::Auth::get_session('');
389 $session->param( 'number', $user->{borrowernumber} );
390 $session->param( 'id', $user->{userid} );
391 $session->param( 'ip', '127.0.0.1' );
392 $session->param( 'lasttime', time() );
395 return ( $user->{borrowernumber}, $session->id );