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 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
40 # this affects the other REST api tests
41 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
43 my $remote_address = '127.0.0.1';
44 my $t = Test::Mojo->new('Koha::REST::V1');
46 subtest 'add() tests' => sub {
49 $schema->storage->txn_begin;
51 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
53 unauthorized_access_tests('POST', "/api/v1/clubs/".$club_with_enrollments->id."/holds", undef, {
54 biblio_id => $item->biblionumber,
55 pickup_library_id => $item->home_branch->branchcode
58 $schema->storage->txn_rollback;
60 subtest 'librarian access tests' => sub {
63 $schema->storage->txn_begin;
65 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
67 my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
69 biblio_id => $item->biblionumber,
70 pickup_library_id => $item->home_branch->branchcode
72 my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_without_enrollments->id."/holds" => json => $data);
73 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
76 ->json_is('/error' => "Cannot place a hold on a club without patrons.");
78 $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data);
79 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
81 ->status_is(201, 'Created Hold')
82 ->json_has('/club_hold_id', 'got a club hold id')
83 ->json_is( '/club_id' => $club_with_enrollments->id)
84 ->json_is( '/biblio_id' => $item->biblionumber);
86 $schema->storage->txn_rollback;
90 subtest "default patron home" => sub {
93 $schema->storage->txn_begin;
95 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
97 my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
99 biblio_id => $item->biblionumber,
100 pickup_library_id => $item->home_branch->branchcode,
101 default_patron_home => 1
104 my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data);
105 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
107 ->status_is(201, 'Created Hold');
109 my $json_response = decode_json $t->tx->res->content->get_body_chunk;
111 my $sth = $dbh->prepare(
112 "select patron_id, hold_id from club_holds_to_patron_holds where club_hold_id = ?"
114 $sth->execute($json_response->{club_hold_id});
115 while (my $test = $sth->fetchrow_hashref()) {
116 my $hold = Koha::Holds->find($test->{hold_id});
117 my $patron = Koha::Patrons->find($test->{patron_id});
118 is($hold->branchcode, $patron->branchcode, 'Pickup location should be patrons home branch');
120 $schema->storage->txn_rollback;
123 sub unauthorized_access_tests {
124 my ($verb, $endpoint, $club_hold_id, $json) = @_;
126 $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
128 subtest 'unauthorized access tests' => sub {
131 my $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
135 my ($borrowernumber, $session_id) = create_user_and_session({
138 $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
139 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
142 ->json_has('/required_permissions');
146 sub create_user_and_session {
149 my $flags = ( $args->{authorized} ) ? 64 : 0;
151 my $user = $builder->build(
153 source => 'Borrower',
158 email => 'nobody@example.com',
159 emailpro => 'nobody@example.com',
160 B_email => 'nobody@example.com'
165 # Create a session for the authorized user
166 my $session = C4::Auth::get_session('');
167 $session->param( 'number', $user->{borrowernumber} );
168 $session->param( 'id', $user->{userid} );
169 $session->param( 'ip', '127.0.0.1' );
170 $session->param( 'lasttime', time() );
173 return ( $user->{borrowernumber}, $session->id );
176 sub create_test_data {
177 my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
178 my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
179 my $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
180 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
181 my $enrollment1 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
182 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
183 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
184 my $enrollment2 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
185 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
186 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
187 my $enrollment3 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
188 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
189 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
190 my $enrollment4 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
191 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
192 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
193 my $enrollment5 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
194 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
195 $patron = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib->branchcode}});
196 my $enrollment6 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef, borrowernumber => $patron->borrowernumber } } );
197 $lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}});
198 my $item = $builder->build_sample_item({homebranch => $lib->branchcode});
199 return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] );