Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / api / v1 / clubs_holds.t
1
2 #!/usr/bin/env perl
3
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 2;
22 use Test::Mojo;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Auth;
29 use C4::Context;
30 use Koha::Database;
31 use Koha::Holds;
32 use Koha::Patrons;
33 use JSON qw( decode_json );
34
35 my $schema  = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37 my $dbh = C4::Context->dbh;
38
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' );
42
43 my $remote_address = '127.0.0.1';
44 my $t              = Test::Mojo->new('Koha::REST::V1');
45
46 subtest 'add() tests' => sub {
47     plan tests => 2;
48
49     $schema->storage->txn_begin;
50
51     my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
52
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
56     });
57
58     $schema->storage->txn_rollback;
59
60     subtest 'librarian access tests' => sub {
61         plan tests => 8;
62
63         $schema->storage->txn_begin;
64
65         my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
66
67         my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
68         my $data = {
69             biblio_id => $item->biblionumber,
70             pickup_library_id => $item->home_branch->branchcode
71         };
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 });
74         $t->request_ok($tx)
75             ->status_is(500)
76             ->json_is('/error' => "Cannot place a hold on a club without patrons.");
77
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 });
80         $t->request_ok($tx)
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);
85
86         $schema->storage->txn_rollback;
87     };
88 };
89
90 subtest "default patron home" => sub {
91     plan tests => 8;
92
93     $schema->storage->txn_begin;
94
95     my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
96
97     my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
98     my $data = {
99         biblio_id => $item->biblionumber,
100         pickup_library_id => $item->home_branch->branchcode,
101         default_patron_home => 1
102     };
103
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 });
106     $t->request_ok($tx)
107         ->status_is(201, 'Created Hold');
108
109     my $json_response = decode_json $t->tx->res->content->get_body_chunk;
110
111     my $sth = $dbh->prepare(
112         "select patron_id, hold_id from club_holds_to_patron_holds where club_hold_id = ?"
113     );
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');
119     }
120     $schema->storage->txn_rollback;
121 };
122
123 sub unauthorized_access_tests {
124     my ($verb, $endpoint, $club_hold_id, $json) = @_;
125
126     $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
127
128     subtest 'unauthorized access tests' => sub {
129         plan tests => 5;
130
131         my $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
132         $t->request_ok($tx)
133           ->status_is(401);
134
135         my ($borrowernumber, $session_id) = create_user_and_session({
136             authorized => 0 });
137
138         $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
139         $tx->req->cookies({name => 'CGISESSID', value => $session_id});
140         $t->request_ok($tx)
141           ->status_is(403)
142           ->json_has('/required_permissions');
143     };
144 }
145
146 sub create_user_and_session {
147
148     my $args  = shift;
149     my $flags = ( $args->{authorized} ) ? 64 : 0;
150
151     my $user = $builder->build(
152         {
153             source => 'Borrower',
154             value  => {
155                 flags => $flags,
156                 gonenoaddress => 0,
157                 lost => 0,
158                 email => 'nobody@example.com',
159                 emailpro => 'nobody@example.com',
160                 B_email => 'nobody@example.com'
161             }
162         }
163     );
164
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() );
171     $session->flush;
172
173     return ( $user->{borrowernumber}, $session->id );
174 }
175
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 ] );
200 }