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;
82 biblio_id => $item->biblionumber,
83 pickup_library_id => $item->home_branch->branchcode
86 $t->post_ok( "//$userid:$password@/api/v1/clubs/"
87 . $club_without_enrollments->id
88 . "/holds" => json => $data )
90 ->json_is( '/error' => "Cannot place a hold on a club without patrons." );
92 $t->post_ok( "//$userid:$password@/api/v1/clubs/"
93 . $club_with_enrollments->id
94 . "/holds" => json => $data )
95 ->status_is( 201, 'Created Hold' )
96 ->json_has( '/club_hold_id', 'got a club hold id' )
97 ->json_is( '/club_id' => $club_with_enrollments->id )
98 ->json_is( '/biblio_id' => $item->biblionumber );
100 $schema->storage->txn_rollback;
104 subtest "default patron home" => sub {
108 $schema->storage->txn_begin;
110 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
111 my $club_with_enrollments_id = $club_with_enrollments->id;
113 my $librarian = $builder->build_object(
115 class => 'Koha::Patrons',
116 value => { flags => 2**6 } # reserveforothers flag = 6
119 my $password = 'thePassword123';
120 $librarian->set_password( { password => $password, skip_validation => 1 } );
121 my $userid = $librarian->userid;
124 biblio_id => $item->biblionumber,
125 pickup_library_id => $item->home_branch->branchcode,
126 default_patron_home => 1
129 $t->post_ok( "//$userid:$password@/api/v1/clubs/"
130 . $club_with_enrollments->id
131 . "/holds" => json => $data )
132 ->status_is( 201, 'Created Hold' );
134 my $json_response = decode_json $t->tx->res->content->get_body_chunk;
136 my $sth = $dbh->prepare(
137 "select patron_id, hold_id from club_holds_to_patron_holds where club_hold_id = ?"
139 $sth->execute($json_response->{club_hold_id});
140 while (my $test = $sth->fetchrow_hashref()) {
141 my $hold = Koha::Holds->find($test->{hold_id});
142 my $patron = Koha::Patrons->find($test->{patron_id});
143 is($hold->branchcode, $patron->branchcode, 'Pickup location should be patrons home branch');
145 $schema->storage->txn_rollback;
148 sub unauthorized_access_tests {
149 my ($verb, $endpoint, $club_hold_id, $json) = @_;
151 $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
153 subtest 'unauthorized access tests' => sub {
156 my $verb_ok = lc($verb) . '_ok';
158 $t->$verb_ok($endpoint => json => $json)
161 my $unauthorized_patron = $builder->build_object(
163 class => 'Koha::Patrons',
164 value => { flags => 0 }
167 my $password = "thePassword123!";
168 $unauthorized_patron->set_password(
169 { password => $password, skip_validation => 1 } );
170 my $unauth_userid = $unauthorized_patron->userid;
172 $t->$verb_ok( "//$unauth_userid:$password\@$endpoint" => json => $json )
174 ->json_has('/required_permissions');
178 sub create_test_data {
179 my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
180 my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
181 my $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
182 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
183 my $enrollment1 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
184 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
185 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
186 my $enrollment2 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
187 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
188 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
189 my $enrollment3 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
190 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
191 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
192 my $enrollment4 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
193 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
194 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
195 my $enrollment5 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
196 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
197 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
198 my $enrollment6 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
199 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
200 my $item = $builder->build_sample_item({homebranch => $lib->branchcode});
201 return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] );