Bug 27330: Regression tests
[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 my $t = Test::Mojo->new('Koha::REST::V1');
40 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
41
42 subtest 'add() tests' => sub {
43
44     plan tests => 2;
45
46     $schema->storage->txn_begin;
47
48     my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
49
50     unauthorized_access_tests(
51         'POST',
52         "/api/v1/clubs/" . $club_with_enrollments->id . "/holds",
53         undef,
54         {
55             biblio_id         => $item->biblionumber,
56             pickup_library_id => $item->home_branch->branchcode
57         }
58     );
59
60     $schema->storage->txn_rollback;
61
62     subtest 'librarian access tests' => sub {
63
64         plan tests => 8;
65
66         $schema->storage->txn_begin;
67
68         my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
69         my $club_with_enrollments_id = $club_with_enrollments->id;
70
71         my $librarian = $builder->build_object(
72             {
73                 class => 'Koha::Patrons',
74                 value => { flags => 2**6 }    # reserveforothers flag = 6
75             }
76         );
77         my $password = 'thePassword123';
78         $librarian->set_password( { password => $password, skip_validation => 1 } );
79         my $userid = $librarian->userid;
80
81         my $data = {
82             biblio_id         => $item->biblionumber,
83             pickup_library_id => $item->home_branch->branchcode
84         };
85
86         $t->post_ok( "//$userid:$password@/api/v1/clubs/"
87               . $club_without_enrollments->id
88               . "/holds" => json => $data )
89           ->status_is(403)
90           ->json_is( '/error' => "Cannot place a hold on a club without patrons." );
91
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 );
99
100         $schema->storage->txn_rollback;
101     };
102 };
103
104 subtest "default patron home" => sub {
105
106     plan tests => 8;
107
108     $schema->storage->txn_begin;
109
110     my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data();
111     my $club_with_enrollments_id = $club_with_enrollments->id;
112
113     my $librarian = $builder->build_object(
114         {
115             class => 'Koha::Patrons',
116             value => { flags => 2**6 }    # reserveforothers flag = 6
117         }
118     );
119     my $password = 'thePassword123';
120     $librarian->set_password( { password => $password, skip_validation => 1 } );
121     my $userid = $librarian->userid;
122
123     my $data = {
124         biblio_id           => $item->biblionumber,
125         pickup_library_id   => $item->home_branch->branchcode,
126         default_patron_home => 1
127     };
128
129     $t->post_ok( "//$userid:$password@/api/v1/clubs/"
130           . $club_with_enrollments->id
131           . "/holds" => json => $data )
132       ->status_is( 201, 'Created Hold' );
133
134     my $json_response = decode_json $t->tx->res->content->get_body_chunk;
135
136     my $sth = $dbh->prepare(
137         "select patron_id, hold_id from club_holds_to_patron_holds where club_hold_id = ?"
138     );
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');
144     }
145     $schema->storage->txn_rollback;
146 };
147
148 sub unauthorized_access_tests {
149     my ($verb, $endpoint, $club_hold_id, $json) = @_;
150
151     $endpoint .= ($club_hold_id) ? "/$club_hold_id" : '';
152
153     subtest 'unauthorized access tests' => sub {
154         plan tests => 5;
155
156         my $verb_ok = lc($verb) . '_ok';
157
158         $t->$verb_ok($endpoint => json => $json)
159           ->status_is(401);
160
161         my $unauthorized_patron = $builder->build_object(
162             {
163                 class => 'Koha::Patrons',
164                 value => { flags => 0 }
165             }
166         );
167         my $password = "thePassword123!";
168         $unauthorized_patron->set_password(
169             { password => $password, skip_validation => 1 } );
170         my $unauth_userid = $unauthorized_patron->userid;
171
172         $t->$verb_ok( "//$unauth_userid:$password\@$endpoint" => json => $json )
173           ->status_is(403)
174           ->json_has('/required_permissions');
175     };
176 }
177
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 ] );
202 }