Bug 31028: (follow-up) Fix executable bit
[koha.git] / t / db_dependent / api / v1 / ticket_updates.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::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::Tickets;
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 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34 t::lib::Mocks::mock_preference( 'NotifyPasswordChange', 0 );
35
36 subtest 'list_updates() tests' => sub {
37
38     plan tests => 14;
39
40     $schema->storage->txn_begin;
41
42     Koha::Tickets->search->delete;
43
44     my $librarian = $builder->build_object(
45         {
46             class => 'Koha::Patrons',
47             value => { flags => 2**2 }    # catalogue flag = 2
48         }
49     );
50     my $password = 'thePassword123';
51     $librarian->set_password( { password => $password, skip_validation => 1 } );
52     my $userid = $librarian->userid;
53
54     my $patron = $builder->build_object(
55         {
56             class => 'Koha::Patrons',
57             value => { flags => 0 }
58         }
59     );
60
61     $patron->set_password( { password => $password, skip_validation => 1 } );
62     my $unauth_userid = $patron->userid;
63
64     my $ticket    = $builder->build_object( { class => 'Koha::Tickets' } );
65     my $ticket_id = $ticket->id;
66
67     ## Authorized user tests
68     # No updates, so empty array should be returned
69     $t->get_ok("//$userid:$password@/api/v1/tickets/$ticket_id/updates")
70       ->status_is(200)->json_is( [] );
71
72     my $update = $builder->build_object(
73         {
74             class => 'Koha::Ticket::Updates',
75             value => { ticket_id => $ticket_id }
76         }
77     );
78
79     # One ticket update added, should get returned
80     $t->get_ok("//$userid:$password@/api/v1/tickets/$ticket_id/updates")
81       ->status_is(200)->json_is( [ $update->to_api ] );
82
83     my $update_2 = $builder->build_object(
84         {
85             class => 'Koha::Ticket::Updates',
86             value => { ticket_id => $ticket_id }
87         }
88     );
89     my $update_3 = $builder->build_object(
90         {
91             class => 'Koha::Ticket::Updates',
92             value => { ticket_id => $ticket_id }
93         }
94     );
95
96     # Two ticket updates added, they should both be returned
97     $t->get_ok("//$userid:$password@/api/v1/tickets/$ticket_id/updates")
98       ->status_is(200)
99       ->json_is( [ $update->to_api, $update_2->to_api, $update_3->to_api, ] );
100
101     # Warn on unsupported query parameter
102     $t->get_ok(
103 "//$userid:$password@/api/v1/tickets/$ticket_id/updates?ticket_blah=blah"
104     )->status_is(400)->json_is(
105         [
106             {
107                 path    => '/query/ticket_blah',
108                 message => 'Malformed query string'
109             }
110         ]
111     );
112
113     # Unauthorized access
114     $t->get_ok("//$unauth_userid:$password@/api/v1/tickets")->status_is(403);
115
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'add_update() tests' => sub {
120
121     plan tests => 34;
122
123     $schema->storage->txn_begin;
124
125     my $librarian = $builder->build_object(
126         {
127             class => 'Koha::Patrons',
128             value => { flags => 2**9 }    # editcatalogue flag = 9
129         }
130     );
131     my $password = 'thePassword123';
132     $librarian->set_password( { password => $password, skip_validation => 1 } );
133     my $userid = $librarian->userid;
134
135     my $patron = $builder->build_object(
136         {
137             class => 'Koha::Patrons',
138             value => { flags => 0 }
139         }
140     );
141
142     $patron->set_password( { password => $password, skip_validation => 1 } );
143     my $unauth_userid = $patron->userid;
144
145     my $ticket = $builder->build_object(
146         {
147             class => 'Koha::Tickets',
148             value => { reporter_id => $patron->id }
149         }
150     );
151     my $ticket_id = $ticket->id;
152
153     my $update = {
154         message => "First ticket update",
155         public  => Mojo::JSON->false
156     };
157
158     # Unauthorized attempt to write
159     $t->post_ok(
160         "//$unauth_userid:$password@/api/v1/tickets/$ticket_id/updates" =>
161           json => $update )->status_is(403);
162
163     # Authorized attempt to write
164     my $update_id =
165       $t->post_ok(
166         "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
167           $update )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
168         Location => qr|^\/api\/v1\/tickets/\d*|,
169         'SWAGGER3.4.1'
170     )->json_is( '/message' => $update->{message} )
171       ->json_is( '/public'  => $update->{public} )
172       ->json_is( '/user_id' => $librarian->id )->tx->res->json->{update_id};
173
174     # Check that notice trigger didn't fire for non-public update
175     my $notices =
176       Koha::Notice::Messages->search( { borrowernumber => $patron->id } );
177     is( $notices->count, 0,
178         'No notices queued when the update is marked as not public' );
179
180     # Authorized attempt to create with null id
181     $update->{update_id} = undef;
182     $t->post_ok(
183         "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
184           $update )->status_is(400)->json_has('/errors');
185
186     # Authorized attempt to create with existing id
187     $update->{update_id} = $update_id;
188     $t->post_ok(
189         "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
190           $update )->status_is(400)->json_is(
191         "/errors" => [
192             {
193                 message => "Read-only.",
194                 path    => "/body/update_id"
195             }
196         ]
197           );
198     delete $update->{update_id};
199
200     # Authorized attempt to write missing data
201     my $update_with_missing_field = { message => "Another ticket update" };
202
203     $t->post_ok(
204         "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
205           $update_with_missing_field )->status_is(400)->json_is(
206         "/errors" => [
207             {
208                 message => "Missing property.",
209                 path    => "/body/public"
210             }
211         ]
212           );
213
214     # Check that notice trigger fired for public update
215     $update->{public} = Mojo::JSON->true;
216     $update_id =
217       $t->post_ok(
218         "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
219           $update )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
220         Location => qr|^\/api\/v1\/tickets/\d*|,
221         'SWAGGER3.4.1'
222     )->json_is( '/message' => $update->{message} )
223       ->json_is( '/public'  => $update->{public} )
224       ->json_is( '/user_id' => $librarian->id )->tx->res->json->{update_id};
225
226     $notices =
227       Koha::Notice::Messages->search( { borrowernumber => $patron->id } );
228     is( $notices->count, 1,
229         'One notice queued when the update is marked as public' );
230     my $THE_notice = $notices->next;
231     is( $THE_notice->letter_code, 'TICKET_UPDATE',
232         'Notice queued was a TICKET_UPDATE for non-status changing update'
233     );
234     $THE_notice->delete;
235
236     $update->{state} = 'resolved';
237     $update_id =
238       $t->post_ok(
239         "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
240           $update )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
241         Location => qr|^\/api\/v1\/tickets/\d*|,
242         'SWAGGER3.4.1'
243     )->json_is( '/message' => $update->{message} )
244       ->json_is( '/public'  => $update->{public} )
245       ->json_is( '/user_id' => $librarian->id )->tx->res->json->{update_id};
246
247     $notices =
248       Koha::Notice::Messages->search( { borrowernumber => $patron->id } );
249     is( $notices->count, 1,
250         'One notice queued when the update is marked as public' );
251     $THE_notice = $notices->next;
252     is( $THE_notice->letter_code, 'TICKET_RESOLVE',
253         'Notice queued was a TICKET_RESOLVED for status changing update'
254     );
255
256     $schema->storage->txn_rollback;
257 };