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 => 1;
25 use t::lib::TestBuilder;
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 'add() tests' => sub {
44 $schema->storage->txn_begin;
46 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
48 unauthorized_access_tests('POST', "/api/v1/clubs/".$club_with_enrollments->id."/holds", undef, {
49 biblio_id => $item->biblionumber,
50 pickup_library_id => $item->home_branch->branchcode
53 $schema->storage->txn_rollback;
55 subtest 'librarian access tests' => sub {
58 $schema->storage->txn_begin;
60 my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
62 my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
64 biblio_id => $item->biblionumber,
65 pickup_library_id => $item->home_branch->branchcode
67 my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_without_enrollments->id."/holds" => json => $data);
68 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
71 ->json_is('/error' => "Cannot place a hold on a club without patrons.");
73 $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data);
74 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
76 ->status_is(201, 'Created Hold')
77 ->json_has('/club_hold_id', 'got a club hold id')
78 ->json_is( '/club_id' => $club_with_enrollments->id)
79 ->json_is( '/biblio_id' => $item->biblionumber);
81 $schema->storage->txn_rollback;
85 sub unauthorized_access_tests {
86 my ($verb, $endpoint, $club_hold_id, $json) = @_;
88 $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
90 subtest 'unauthorized access tests' => sub {
93 my $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
97 my ($borrowernumber, $session_id) = create_user_and_session({
100 $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
101 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
104 ->json_has('/required_permissions');
108 sub create_user_and_session {
111 my $flags = ( $args->{authorized} ) ? 64 : 0;
113 my $user = $builder->build(
115 source => 'Borrower',
120 email => 'nobody@example.com',
121 emailpro => 'nobody@example.com',
122 B_email => 'nobody@example.com'
127 # Create a session for the authorized user
128 my $session = C4::Auth::get_session('');
129 $session->param( 'number', $user->{borrowernumber} );
130 $session->param( 'id', $user->{userid} );
131 $session->param( 'ip', '127.0.0.1' );
132 $session->param( 'lasttime', time() );
135 return ( $user->{borrowernumber}, $session->id );
138 sub create_test_data {
139 my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
140 my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } );
141 my $enrollment1 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
142 my $enrollment2 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
143 my $enrollment3 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
144 my $enrollment4 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
145 my $enrollment5 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
146 my $enrollment6 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } );
147 my $item = $builder->build_sample_item();
148 return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] );