Bug 35353: Add REST API endpoint to retrieve old holds
[koha.git] / t / db_dependent / api / v1 / patrons_holds.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use Test::MockModule;
22 use Test::Mojo;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new();
31
32 my $t = Test::Mojo->new('Koha::REST::V1');
33
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36 subtest 'list() tests' => sub {
37
38     plan tests => 18;
39
40     $schema->storage->txn_begin;
41
42     my $patron = $builder->build_object({
43         class => 'Koha::Patrons',
44         value => { flags => 2 ** 4 } # 'borrowers' flag == 4
45     });
46     my $password = 'thePassword123';
47     $patron->set_password({ password => $password, skip_validation => 1 });
48     my $userid = $patron->userid;
49
50     $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds')
51       ->status_is( 200, 'SWAGGER3.2.2' )
52       ->json_is( [] );
53
54     my $hold_1 = $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->id } } );
55     my $hold_2 = $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->id } } );
56     my $hold_3 = $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->id } } );
57
58     $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?_order_by=+me.hold_id' )
59         ->status_is( 200, 'SWAGGER3.2.2' )
60         ->json_is( '' => [ $hold_1->to_api, $hold_2->to_api, $hold_3->to_api ], 'Holds retrieved' );
61
62     $hold_1->fill;
63     $hold_3->fill;
64
65     $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?_order_by=+me.hold_id' )
66         ->status_is( 200, 'SWAGGER3.2.2' )->json_is( '' => [ $hold_2->to_api ], 'Only current holds retrieved' );
67
68     $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?old=1&_order_by=+me.hold_id' )
69         ->status_is( 200, 'SWAGGER3.2.2' )
70         ->json_is( '' => [ $hold_1->to_api, $hold_3->to_api ], 'Only old holds retrieved' );
71
72     my $old_hold_1 = Koha::Old::Holds->find( $hold_1->id );
73     $old_hold_1->item->delete;
74     $old_hold_1->pickup_library->delete;
75
76     $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/holds?old=1&_order_by=+me.hold_id' )
77         ->status_is( 200, 'SWAGGER3.2.2' )->json_is(
78         '' => [ $old_hold_1->get_from_storage->to_api, $hold_3->to_api ],
79         'Old holds even after item and library removed'
80         );
81
82     my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' });
83     my $non_existent_patron_id = $non_existent_patron->id;
84     # get rid of the patron
85     $non_existent_patron->delete;
86
87     $t->get_ok("//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/holds')
88       ->status_is( 404 )
89       ->json_is( '/error' => 'Patron not found' );
90
91     $schema->storage->txn_rollback;
92 };
93
94 subtest 'delete_public() tests' => sub {
95
96     plan tests => 13;
97
98     $schema->storage->txn_begin;
99
100     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
101
102     my $patron = $builder->build_object(
103         {
104             class => 'Koha::Patrons',
105             value => { flags => 0 },
106         }
107     );
108     my $password = 'thePassword123';
109     $patron->set_password( { password => $password, skip_validation => 1 } );
110     my $userid = $patron->userid;
111
112     my $hold_to_delete  = $builder->build_object( { class => 'Koha::Holds' } );
113     my $deleted_hold_id = $hold_to_delete->id;
114     $hold_to_delete->delete;
115
116     $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $deleted_hold_id )
117         ->status_is(404);
118
119     my $another_user_hold = $builder->build_object( { class => 'Koha::Holds' } );
120
121     $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $another_user_hold->id )
122         ->status_is( 404, 'Invalid patron_id and hold_id combination yields 404' );
123
124     my $non_waiting_hold = $builder->build_object(
125         {
126             class => 'Koha::Holds',
127             value => {
128                 borrowernumber => $patron->id,
129                 found          => undef,
130                 itemnumber     => undef
131             }
132         }
133     );
134
135     $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $non_waiting_hold->id )
136         ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
137
138     my $cancellation_requestable;
139
140     my $hold_mock = Test::MockModule->new('Koha::Hold');
141     $hold_mock->mock( 'cancellation_requestable_from_opac', sub { return $cancellation_requestable; } );
142
143     my $item         = $builder->build_sample_item;
144     my $waiting_hold = $builder->build_object(
145         {
146             class => 'Koha::Holds',
147             value => {
148                 borrowernumber => $patron->id,
149                 found          => 'W',
150                 itemnumber     => $item->id,
151             }
152         }
153     );
154
155     $cancellation_requestable = 0;
156
157     $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $waiting_hold->id )
158         ->status_is(403)->json_is( { error => 'Cancellation forbidden' } );
159
160     $cancellation_requestable = 1;
161
162     $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/" . $patron->id . '/holds/' . $waiting_hold->id )
163         ->status_is(202);
164
165     my $cancellation_requests = $waiting_hold->cancellation_requests;
166     is( $cancellation_requests->count, 1, 'Cancellation request recorded' );
167
168     $schema->storage->txn_rollback;
169 };