4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Test::More tests => 2;
25 use t::lib::TestBuilder;
33 use JSON qw( decode_json );
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37 my $dbh = C4::Context->dbh;
39 my $t = Test::Mojo->new('Koha::REST::V1');
40 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
42 subtest 'add() tests' => sub {
46 $schema->storage->txn_begin;
48 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
50 unauthorized_access_tests(
52 "/api/v1/clubs/" . $club_with_enrollments->id . "/holds",
55 biblio_id => $item->biblionumber,
56 pickup_library_id => $item->home_branch->branchcode
60 $schema->storage->txn_rollback;
62 subtest 'librarian access tests' => sub {
66 $schema->storage->txn_begin;
68 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
69 my $club_with_enrollments_id = $club_with_enrollments->id;
71 my $librarian = $builder->build_object(
73 class => 'Koha::Patrons',
74 value => { flags => 2 ** 6 } # reserveforothers flag = 6
77 my $password = 'thePassword123';
78 $librarian->set_password( { password => $password, skip_validation => 1 } );
79 my $userid = $librarian->userid;
81 my $non_existent_item = $builder->build_sample_item;
82 my $non_existent_biblio = $non_existent_item->biblio;
84 my $non_existent_item_id = $non_existent_item->id;
85 my $non_existent_biblio_id = $non_existent_biblio->id;
86 my $non_existent_item_homebranch =
87 $non_existent_item->home_branch->branchcode;
89 $non_existent_item->delete;
90 $non_existent_biblio->delete;
92 my $biblio = $builder->build_sample_biblio;
95 "//$userid:$password@/api/v1/clubs/"
96 . $club_with_enrollments->id
97 . "/holds" => json => {
98 biblio_id => $biblio->id,
100 pickup_library_id => $item->home_branch->branchcode
103 ->json_is( '/error' => "Item "
105 . " doesn't belong to biblio "
109 "//$userid:$password@/api/v1/clubs/"
110 . $club_with_enrollments->id
111 . "/holds" => json => {
112 pickup_library_id => $non_existent_item_homebranch
116 '/error' => 'At least one of biblio_id, item_id should be given' );
119 "//$userid:$password@/api/v1/clubs/"
120 . $club_with_enrollments->id
121 . "/holds" => json => {
122 biblio_id => $non_existent_biblio_id,
123 pickup_library_id => $non_existent_item_homebranch
125 )->status_is(404)->json_is( '/error' => 'Biblio not found' );
128 "//$userid:$password@/api/v1/clubs/"
129 . $club_with_enrollments->id
130 . "/holds" => json => {
131 item_id => $non_existent_item_id,
132 pickup_library_id => $non_existent_item_homebranch
134 )->status_is(404)->json_is( '/error' => 'Item not found' );
137 biblio_id => $item->biblionumber,
138 pickup_library_id => $item->home_branch->branchcode
141 $t->post_ok( "//$userid:$password@/api/v1/clubs/"
142 . $club_without_enrollments->id
143 . "/holds" => json => $data )
145 ->json_is( '/error' => "Cannot place a hold on a club without patrons." );
147 $t->post_ok( "//$userid:$password@/api/v1/clubs/"
148 . $club_with_enrollments->id
149 . "/holds" => json => $data )
150 ->status_is( 201, 'Created Hold' )
151 ->json_has( '/club_hold_id', 'got a club hold id' )
152 ->json_is( '/club_id' => $club_with_enrollments->id )
153 ->json_is( '/biblio_id' => $item->biblionumber );
155 $schema->storage->txn_rollback;
159 subtest "default patron home" => sub {
163 $schema->storage->txn_begin;
165 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
166 my $club_with_enrollments_id = $club_with_enrollments->id;
168 my $librarian = $builder->build_object(
170 class => 'Koha::Patrons',
171 value => { flags => 2**6 } # reserveforothers flag = 6
174 my $password = 'thePassword123';
175 $librarian->set_password( { password => $password, skip_validation => 1 } );
176 my $userid = $librarian->userid;
179 biblio_id => $item->biblionumber,
180 pickup_library_id => $item->home_branch->branchcode,
181 default_patron_home => 1
184 $t->post_ok( "//$userid:$password@/api/v1/clubs/"
185 . $club_with_enrollments->id
186 . "/holds" => json => $data )
187 ->status_is( 201, 'Created Hold' );
189 my $json_response = decode_json $t->tx->res->content->get_body_chunk;
191 my $sth = $dbh->prepare(
192 "select patron_id, hold_id from club_holds_to_patron_holds where club_hold_id = ?"
194 $sth->execute($json_response->{club_hold_id});
195 while (my $test = $sth->fetchrow_hashref()) {
196 my $hold = Koha::Holds->find($test->{hold_id});
197 my $patron = Koha::Patrons->find($test->{patron_id});
198 is($hold->branchcode, $patron->branchcode, 'Pickup location should be patrons home branch');
200 $schema->storage->txn_rollback;
203 sub unauthorized_access_tests {
204 my ($verb, $endpoint, $club_hold_id, $json) = @_;
206 $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
208 subtest 'unauthorized access tests' => sub {
211 my $verb_ok = lc($verb) . '_ok';
213 $t->$verb_ok($endpoint => json => $json)
216 my $unauthorized_patron = $builder->build_object(
218 class => 'Koha::Patrons',
219 value => { flags => 0 }
222 my $password = "thePassword123!";
223 $unauthorized_patron->set_password(
224 { password => $password, skip_validation => 1 } );
225 my $unauth_userid = $unauthorized_patron->userid;
227 $t->$verb_ok( "//$unauth_userid:$password\@$endpoint" => json => $json )
229 ->json_has('/required_permissions');
233 sub create_test_data {
234 my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
235 my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
236 my $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
237 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
238 my $enrollment1 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
239 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
240 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
241 my $enrollment2 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
242 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
243 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
244 my $enrollment3 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
245 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
246 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
247 my $enrollment4 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
248 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
249 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
250 my $enrollment5 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
251 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
252 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
253 my $enrollment6 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
254 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
255 my $item = $builder->build_sample_item({homebranch => $lib->branchcode});
256 return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] );