Bug 23666: (follow-up) Adjust to new exceptions
[koha.git] / t / db_dependent / api / v1 / acquisitions_vendors.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
22 use Test::Mojo;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Acquisition::Booksellers;
28 use Koha::Database;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35 my $t = Test::Mojo->new('Koha::REST::V1');
36
37 subtest 'list() and delete() tests | authorized user' => sub {
38
39     plan tests => 35;
40
41     $schema->storage->txn_begin;
42
43     $schema->resultset('Aqbasket')->search->delete;
44     Koha::Acquisition::Booksellers->search->delete;
45
46     my $patron = $builder->build_object({
47         class => 'Koha::Patrons',
48         value => { flags => 2 ** 11 } ## 11 => acquisitions
49     });
50     my $password = 'thePassword123';
51     $patron->set_password({ password => $password, skip_validation => 1 });
52     my $userid = $patron->userid;
53
54     ## Authorized user tests
55     # No vendors, so empty array should be returned
56     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" )
57       ->status_is(200)
58       ->json_is( [] );
59
60     my $vendor_name = 'Ruben libros';
61     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers', value => { name => $vendor_name } });
62
63     # One vendor created, should get returned
64     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" )
65       ->status_is(200)
66       ->json_like( '/0/name' => qr/$vendor_name/ );
67
68     my $other_vendor_name = 'Amerindia';
69     my $other_vendor
70         = $builder->build_object({ class => 'Koha::Acquisition::Booksellers', value => { name => $other_vendor_name } });
71
72     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" )
73       ->status_is(200)
74       ->json_like( '/0/name' => qr/Ruben/ )
75       ->json_like( '/1/name' => qr/Amerindia/ );
76
77     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors?name=$vendor_name" )
78       ->status_is(200)
79       ->json_like( '/0/name' => qr/Ruben/ );
80
81     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors?name=$other_vendor_name" )
82       ->status_is(200)
83       ->json_like( '/0/name' => qr/Amerindia/ );
84
85
86     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors?accountnumber=" . $vendor->accountnumber )
87       ->status_is(200)
88       ->json_like( '/0/name' => qr/Ruben/ );
89
90     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors?accountnumber=" . $other_vendor->accountnumber )
91       ->status_is(200)
92       ->json_like( '/0/name' => qr/Amerindia/ );
93
94     $t->delete_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )
95       ->status_is(204, 'SWAGGER3.2.4')
96       ->content_is('', 'SWAGGER3.3.4');
97
98     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" )
99       ->status_is(200)
100       ->json_like( '/0/name' => qr/$other_vendor_name/ )
101       ->json_hasnt( '/1', 'Only one vendor' );
102
103     $t->delete_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $other_vendor->id )
104       ->status_is(204, 'SWAGGER3.2.4')
105       ->content_is('', 'SWAGGER3.3.4');
106
107     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors" )
108       ->status_is(200)
109       ->json_is( [] );
110
111     $schema->storage->txn_rollback;
112 };
113
114 subtest 'get() test' => sub {
115
116     plan tests => 9;
117
118     $schema->storage->txn_begin;
119
120     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
121     my $nonexistent_vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
122     my $non_existent_id = $nonexistent_vendor->id;
123     $nonexistent_vendor->delete;
124
125     my $patron = $builder->build_object({
126         class => 'Koha::Patrons',
127         value => { flags => 2 ** 11 } ## 11 => acquisitions
128     });
129     my $password = 'thePassword123';
130     $patron->set_password({ password => $password, skip_validation => 1 });
131     my $userid = $patron->userid;
132
133     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )
134       ->status_is(200)
135       ->json_is( $vendor->to_api );
136
137     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $non_existent_id )
138       ->status_is(404)
139       ->json_is( '/error' => 'Vendor not found' );
140
141     # remove permissions
142     $patron->set({ flags => 0 })->store;
143
144     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )
145       ->status_is(403)
146       ->json_is( '/error', 'Authorization failure. Missing required permission(s).' );
147
148     $schema->storage->txn_rollback;
149 };
150
151 subtest 'add() tests' => sub {
152
153     plan tests => 16;
154
155     $schema->storage->txn_begin;
156
157     my $authorized_patron = $builder->build_object({
158         class => 'Koha::Patrons',
159         value => { flags => 2 ** 11 }
160     });
161     my $password = 'thePassword123';
162     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
163     my $auth_userid = $authorized_patron->userid;
164
165     my $unauthorized_patron = $builder->build_object({
166         class => 'Koha::Patrons',
167         value => { flags => 0 }
168     });
169     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
170     my $unauth_userid = $unauthorized_patron->userid;
171
172     my $vendor = { name => 'Ruben libros' };
173
174     # Unauthorized attempt to write
175     $t->post_ok( "//$unauth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor )
176       ->status_is(403);
177
178     # Authorized attempt to write invalid data
179     my $vendor_with_invalid_field = {
180         name     => 'Amerindia',
181         address5 => 'An address'
182     };
183
184     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor_with_invalid_field )
185       ->status_is(400)
186       ->json_is(
187         "/errors" => [
188             {   message => "Properties not allowed: address5.",
189                 path    => "/body"
190             }
191           ]
192         );
193
194     # Authorized attempt to write
195     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor )
196       ->status_is( 201, 'SWAGGER3 .2.1' )
197       ->header_like( Location => qr|^\/api\/v1\/acquisitions\/vendors/\d*|, 'SWAGGER3.4.1')
198       ->json_is( '/name' => $vendor->{name} )
199       ->json_is( '/address1' => $vendor->{address1} );
200
201     # read the response vendor id for later use
202     my $vendor_id = $t->tx->res->json('/id');
203
204     # Authorized attempt to create with null id
205     $vendor->{id} = undef;
206     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor )
207       ->status_is(400)
208       ->json_has('/errors');
209
210     # Authorized attempt to create with existing id
211     $vendor->{id} = $vendor_id;
212     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor )
213       ->status_is(400)
214       ->json_is(
215         "/errors" => [
216             {   message => "Read-only.",
217                 path    => "/body/id"
218             }
219         ]
220     );
221
222     $schema->storage->txn_rollback;
223 };
224
225 subtest 'update() tests' => sub {
226
227     plan tests => 15;
228
229     $schema->storage->txn_begin;
230
231     my $authorized_patron = $builder->build_object({
232         class => 'Koha::Patrons',
233         value => { flags => 2 ** 11 }
234     });
235     my $password = 'thePassword123';
236     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
237     my $auth_userid = $authorized_patron->userid;
238
239     my $unauthorized_patron = $builder->build_object({
240         class => 'Koha::Patrons',
241         value => { flags => 0 }
242     });
243     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
244     my $unauth_userid = $unauthorized_patron->userid;
245
246     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' } );
247
248     # Unauthorized attempt to update
249     $t->put_ok( "//$unauth_userid:$password@/api/v1/acquisitions/vendors/"
250           . $vendor->id => json =>
251           { city_name => 'New unauthorized name change' } )
252       ->status_is(403);
253
254     # Attempt partial update on a PUT
255     my $vendor_without_mandatory_field = { address1 => 'New address' };
256
257     $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/"
258           . $vendor->id => json => $vendor_without_mandatory_field )
259       ->status_is(400)
260       ->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
261
262     # Full object update on PUT
263     my $vendor_with_updated_field = { name => "London books", };
264
265     $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/"
266           . $vendor->id => json => $vendor_with_updated_field )
267       ->status_is(200)
268       ->json_is( '/name' => 'London books' );
269
270     # Authorized attempt to write invalid data
271     my $vendor_with_invalid_field = {
272         blah     => "Blah",
273         address1 => "Address 1",
274         address2 => "Address 2",
275         address3 => "Address 3"
276     };
277
278     $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/"
279           . $vendor->id => json => $vendor_with_invalid_field )
280       ->status_is(400)
281       ->json_is(
282         "/errors" => [
283             {   message => "Properties not allowed: blah.",
284                 path    => "/body"
285             }
286         ]
287       );
288
289     my $nonexistent_vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' } );
290     my $non_existent_id = $nonexistent_vendor->id;
291     $nonexistent_vendor->delete;
292
293     $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/"
294           . $non_existent_id => json => $vendor_with_updated_field )
295       ->status_is(404);
296
297     $schema->storage->txn_rollback;
298
299     # Wrong method (POST)
300     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/"
301           . $vendor->id => json => $vendor_with_updated_field )
302       ->status_is(404);
303 };
304
305 subtest 'delete() tests' => sub {
306
307     plan tests => 7;
308
309     $schema->storage->txn_begin;
310
311     my $authorized_patron = $builder->build_object({
312         class => 'Koha::Patrons',
313         value => { flags => 2 ** 11 }
314     });
315     my $password = 'thePassword123';
316     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
317     my $auth_userid = $authorized_patron->userid;
318
319     my $unauthorized_patron = $builder->build_object({
320         class => 'Koha::Patrons',
321         value => { flags => 0 }
322     });
323     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
324     my $unauth_userid = $unauthorized_patron->userid;
325
326     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' } );
327
328     # Unauthorized attempt to delete
329     $t->delete_ok( "//$unauth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )
330       ->status_is(403);
331
332     $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )
333       ->status_is(204, 'SWAGGER3.2.4')
334       ->content_is('', 'SWAGGER3.3.4');
335
336     $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id )
337       ->status_is(404);
338
339     $schema->storage->txn_rollback;
340 };