3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 2;
23 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
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 );
36 subtest 'list_updates() tests' => sub {
40 $schema->storage->txn_begin;
42 Koha::Tickets->search->delete;
44 my $librarian = $builder->build_object(
46 class => 'Koha::Patrons',
47 value => { flags => 2**2 } # catalogue flag = 2
50 my $password = 'thePassword123';
51 $librarian->set_password( { password => $password, skip_validation => 1 } );
52 my $userid = $librarian->userid;
54 my $patron = $builder->build_object(
56 class => 'Koha::Patrons',
57 value => { flags => 0 }
61 $patron->set_password( { password => $password, skip_validation => 1 } );
62 my $unauth_userid = $patron->userid;
64 my $ticket = $builder->build_object( { class => 'Koha::Tickets' } );
65 my $ticket_id = $ticket->id;
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( [] );
72 my $update = $builder->build_object(
74 class => 'Koha::Ticket::Updates',
75 value => { ticket_id => $ticket_id }
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 ] );
83 my $update_2 = $builder->build_object(
85 class => 'Koha::Ticket::Updates',
86 value => { ticket_id => $ticket_id }
89 my $update_3 = $builder->build_object(
91 class => 'Koha::Ticket::Updates',
92 value => { ticket_id => $ticket_id }
96 # Two ticket updates added, they should both be returned
97 $t->get_ok("//$userid:$password@/api/v1/tickets/$ticket_id/updates")
99 ->json_is( [ $update->to_api, $update_2->to_api, $update_3->to_api, ] );
101 # Warn on unsupported query parameter
103 "//$userid:$password@/api/v1/tickets/$ticket_id/updates?ticket_blah=blah"
104 )->status_is(400)->json_is(
107 path => '/query/ticket_blah',
108 message => 'Malformed query string'
113 # Unauthorized access
114 $t->get_ok("//$unauth_userid:$password@/api/v1/tickets")->status_is(403);
116 $schema->storage->txn_rollback;
119 subtest 'add_update() tests' => sub {
123 $schema->storage->txn_begin;
125 my $librarian = $builder->build_object(
127 class => 'Koha::Patrons',
128 value => { flags => 2**9 } # editcatalogue flag = 9
131 my $password = 'thePassword123';
132 $librarian->set_password( { password => $password, skip_validation => 1 } );
133 my $userid = $librarian->userid;
135 my $patron = $builder->build_object(
137 class => 'Koha::Patrons',
138 value => { flags => 0 }
142 $patron->set_password( { password => $password, skip_validation => 1 } );
143 my $unauth_userid = $patron->userid;
145 my $ticket = $builder->build_object(
147 class => 'Koha::Tickets',
148 value => { reporter_id => $patron->id }
151 my $ticket_id = $ticket->id;
154 message => "First ticket update",
155 public => Mojo::JSON->false
158 # Unauthorized attempt to write
160 "//$unauth_userid:$password@/api/v1/tickets/$ticket_id/updates" =>
161 json => $update )->status_is(403);
163 # Authorized attempt to write
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*|,
170 )->json_is( '/message' => $update->{message} )
171 ->json_is( '/public' => $update->{public} )
172 ->json_is( '/user_id' => $librarian->id )->tx->res->json->{update_id};
174 # Check that notice trigger didn't fire for non-public update
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' );
180 # Authorized attempt to create with null id
181 $update->{update_id} = undef;
183 "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
184 $update )->status_is(400)->json_has('/errors');
186 # Authorized attempt to create with existing id
187 $update->{update_id} = $update_id;
189 "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
190 $update )->status_is(400)->json_is(
193 message => "Read-only.",
194 path => "/body/update_id"
198 delete $update->{update_id};
200 # Authorized attempt to write missing data
201 my $update_with_missing_field = { message => "Another ticket update" };
204 "//$userid:$password@/api/v1/tickets/$ticket_id/updates" => json =>
205 $update_with_missing_field )->status_is(400)->json_is(
208 message => "Missing property.",
209 path => "/body/public"
214 # Check that notice trigger fired for public update
215 $update->{public} = Mojo::JSON->true;
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*|,
222 )->json_is( '/message' => $update->{message} )
223 ->json_is( '/public' => $update->{public} )
224 ->json_is( '/user_id' => $librarian->id )->tx->res->json->{update_id};
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'
236 $update->{state} = 'resolved';
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*|,
243 )->json_is( '/message' => $update->{message} )
244 ->json_is( '/public' => $update->{public} )
245 ->json_is( '/user_id' => $librarian->id )->tx->res->json->{update_id};
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'
256 $schema->storage->txn_rollback;