Bug 30719: (QA follow-up) Squash:
[koha.git] / t / db_dependent / api / v1 / illbatchstatuses.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 => 5;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::IllbatchStatus;
27 use Koha::IllbatchStatuses;
28 use Koha::Database;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 my $t = Test::Mojo->new('Koha::REST::V1');
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36 subtest 'list() tests' => sub {
37
38     plan tests => 9;
39
40     $schema->storage->txn_begin;
41
42     Koha::IllbatchStatuses->search->delete;
43
44     # Create an admin user
45     my $librarian = $builder->build_object(
46         {
47             class => 'Koha::Patrons',
48             value => {
49                 flags => 2**22    # 22 => ill
50             }
51         }
52     );
53     my $password = 'yoda4ever!';
54     $librarian->set_password( { password => $password, skip_validation => 1 } );
55     my $userid = $librarian->userid;
56
57     ## Authorized user tests
58     # No statuses, so empty array should be returned
59     $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")->status_is(200)->json_is( [] );
60
61     my $status = $builder->build_object(
62         {
63             class => 'Koha::IllbatchStatuses',
64             value => {
65                 name      => "Han Solo",
66                 code      => "SOLO",
67                 is_system => 0
68             }
69         }
70     );
71
72     # One batch created, should get returned
73     $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")->status_is(200)->json_has( '/0/id', 'ID' )
74         ->json_has( '/0/name', 'Name' )->json_has( '/0/code', 'Code' )->json_has( '/0/is_system', 'is_system' );
75
76     $schema->storage->txn_rollback;
77 };
78
79 subtest 'get() tests' => sub {
80
81     plan tests => 11;
82
83     $schema->storage->txn_begin;
84
85     my $librarian = $builder->build_object(
86         {
87             class => 'Koha::Patrons',
88             value => { flags => 2**22 }    # 22 => ill
89         }
90     );
91     my $password = 'Rebelz4DaWin';
92     $librarian->set_password( { password => $password, skip_validation => 1 } );
93     my $userid = $librarian->userid;
94
95     my $status = $builder->build_object(
96         {
97             class => 'Koha::IllbatchStatuses',
98             value => {
99                 name      => "Han Solo",
100                 code      => "SOLO",
101                 is_system => 0
102             }
103         }
104     );
105
106     # Unauthorised user
107     my $patron = $builder->build_object(
108         {
109             class => 'Koha::Patrons',
110             value => { flags => 0 }
111         }
112     );
113     $patron->set_password( { password => $password, skip_validation => 1 } );
114     my $unauth_userid = $patron->userid;
115
116     $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $status->code )->status_is(200)
117         ->json_has( '/id',        'ID' )->json_has( '/name', 'Name' )->json_has( '/code', 'Code' )
118         ->json_has( '/is_system', 'is_system' );
119
120     $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $status->id )->status_is(403);
121
122     my $status_to_delete  = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
123     my $non_existent_code = $status_to_delete->code;
124     $status_to_delete->delete;
125
126     $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code")->status_is(404)
127         ->json_is( '/error' => 'ILL batch status not found' );
128
129     $schema->storage->txn_rollback;
130 };
131
132 subtest 'add() tests' => sub {
133
134     plan tests => 14;
135
136     $schema->storage->txn_begin;
137
138     my $librarian = $builder->build_object(
139         {
140             class => 'Koha::Patrons',
141             value => { flags => 2**22 }    # 22 => ill
142         }
143     );
144     my $password = '3poRox';
145     $librarian->set_password( { password => $password, skip_validation => 1 } );
146     my $userid = $librarian->userid;
147
148     my $patron = $builder->build_object(
149         {
150             class => 'Koha::Patrons',
151             value => { flags => 0 }
152         }
153     );
154     $patron->set_password( { password => $password, skip_validation => 1 } );
155     my $unauth_userid = $patron->userid;
156
157     my $status_metadata = {
158         name      => "In a bacta tank",
159         code      => "BACTA",
160         is_system => 0
161     };
162
163     # Unauthorized attempt to write
164     $t->post_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )->status_is(403);
165
166     # Authorized attempt to write invalid data
167     my $status_with_invalid_field = {
168         %{$status_metadata},
169         doh => 1
170     };
171
172     $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_with_invalid_field )->status_is(400)
173         ->json_is(
174         "/errors" => [
175             {
176                 message => "Properties not allowed: doh.",
177                 path    => "/body"
178             }
179         ]
180         );
181
182     # Authorized attempt to write
183     my $status_id =
184         $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )->status_is(201)
185         ->json_has( '/id',        'ID' )->json_has( '/name', 'Name' )->json_has( '/code', 'Code' )
186         ->json_has( '/is_system', 'is_system' );
187
188     # Authorized attempt to create with null id
189     $status_metadata->{id} = undef;
190     $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )->status_is(400)
191         ->json_has('/errors');
192
193     $schema->storage->txn_rollback;
194 };
195
196 subtest 'update() tests' => sub {
197
198     plan tests => 13;
199
200     $schema->storage->txn_begin;
201
202     my $librarian = $builder->build_object(
203         {
204             class => 'Koha::Patrons',
205             value => { flags => 2**22 }    # 22 => ill
206         }
207     );
208     my $password = 'aw3s0m3y0d41z';
209     $librarian->set_password( { password => $password, skip_validation => 1 } );
210     my $userid = $librarian->userid;
211
212     my $patron = $builder->build_object(
213         {
214             class => 'Koha::Patrons',
215             value => { flags => 0 }
216         }
217     );
218     $patron->set_password( { password => $password, skip_validation => 1 } );
219     my $unauth_userid = $patron->userid;
220
221     my $status_code = $builder->build_object( { class => 'Koha::IllbatchStatuses' } )->code;
222
223     # Unauthorized attempt to update
224     $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/$status_code" => json =>
225             { name => 'These are not the droids you are looking for' } )->status_is(403);
226
227     # Attempt partial update on a PUT
228     my $status_with_missing_field = {
229         code      => $status_code,
230         is_system => 0
231     };
232
233     $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_missing_field )
234         ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
235
236     # Full object update on PUT
237     my $status_with_updated_field = {
238         name      => "Master Ploo Koon",
239         code      => $status_code,
240         is_system => 0
241     };
242
243     $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_updated_field )
244         ->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
245
246     # Authorized attempt to write invalid data
247     my $status_with_invalid_field = {
248         doh  => 1,
249         name => "Master Mace Windu",
250         code => $status_code
251     };
252
253     $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_invalid_field )
254         ->status_is(400)->json_is(
255         "/errors" => [
256             {
257                 message => "Properties not allowed: doh.",
258                 path    => "/body"
259             }
260         ]
261         );
262
263     my $status_to_delete  = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
264     my $non_existent_code = $status_to_delete->code;
265     $status_to_delete->delete;
266
267     $t->put_ok(
268         "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" => json => $status_with_updated_field )
269         ->status_is(404);
270
271     $schema->storage->txn_rollback;
272 };
273
274 subtest 'delete() tests' => sub {
275
276     plan tests => 9;
277
278     $schema->storage->txn_begin;
279
280     my $librarian = $builder->build_object(
281         {
282             class => 'Koha::Patrons',
283             value => { flags => 2**22 }    # 22 => ill
284         }
285     );
286     my $password = 's1th43v3r!';
287     $librarian->set_password( { password => $password, skip_validation => 1 } );
288     my $userid = $librarian->userid;
289
290     my $patron = $builder->build_object(
291         {
292             class => 'Koha::Patrons',
293             value => { flags => 0 }
294         }
295     );
296
297     $patron->set_password( { password => $password, skip_validation => 1 } );
298     my $unauth_userid = $patron->userid;
299
300     my $non_system_status = $builder->build_object(
301         {
302             class => 'Koha::IllbatchStatuses',
303             value => { is_system => 0 }
304         }
305     );
306
307     my $system_status = $builder->build_object(
308         {
309             class => 'Koha::IllbatchStatuses',
310             value => { is_system => 1 }
311         }
312     );
313
314     # Unauthorized attempt to delete
315     $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )->status_is(403);
316
317     $t->delete_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )->status_is(204);
318
319     $t->delete_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )->status_is(404);
320
321     $t->delete_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $system_status->code )->status_is(400)
322         ->json_is( "/errors" => [ { message => "ILL batch status cannot be deleted" } ] );
323
324     $schema->storage->txn_rollback;
325 };