Bug 24545: Fix license statements
[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 use Test::Mojo;
22 use Test::Warn;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use C4::Auth;
28 use Koha::Acquisition::Booksellers;
29 use Koha::Database;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
35 # this affects the other REST api tests
36 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37
38 my $remote_address = '127.0.0.1';
39 my $t              = Test::Mojo->new('Koha::REST::V1');
40
41 subtest 'list() and delete() tests | authorized user' => sub {
42
43     plan tests => 35;
44
45     $schema->storage->txn_begin;
46
47     $schema->resultset('Aqbasket')->search->delete;
48     Koha::Acquisition::Booksellers->search->delete;
49     my ( $borrowernumber, $session_id )
50         = create_user_and_session( { authorized => 1 } );
51
52     ## Authorized user tests
53     # No vendors, so empty array should be returned
54     my $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' );
55     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
56     $tx->req->env( { REMOTE_ADDR => $remote_address } );
57     $t->request_ok($tx)
58       ->status_is(200)
59       ->json_is( [] );
60
61     my $vendor_name = 'Ruben libros';
62     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers', value => { name => $vendor_name } });
63
64     # One vendor created, should get returned
65     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' );
66     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
67     $tx->req->env( { REMOTE_ADDR => $remote_address } );
68     $t->request_ok($tx)
69       ->status_is(200)
70       ->json_like( '/0/name' => qr/$vendor_name/ );
71
72     my $other_vendor_name = 'Amerindia';
73     my $other_vendor
74         = $builder->build_object({ class => 'Koha::Acquisition::Booksellers', value => { name => $other_vendor_name } });
75
76     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' );
77     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
78     $tx->req->env( { REMOTE_ADDR => $remote_address } );
79     $t->request_ok($tx)
80       ->status_is(200)
81       ->json_like( '/0/name' => qr/Ruben/ )
82       ->json_like( '/1/name' => qr/Amerindia/ );
83
84     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?name=' . $vendor_name );
85     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
86     $tx->req->env( { REMOTE_ADDR => $remote_address } );
87     $t->request_ok($tx)
88       ->status_is(200)
89       ->json_like( '/0/name' => qr/Ruben/ );
90
91     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?name=' . $other_vendor_name );
92     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
93     $tx->req->env( { REMOTE_ADDR => $remote_address } );
94     $t->request_ok($tx)
95       ->status_is(200)
96       ->json_like( '/0/name' => qr/Amerindia/ );
97
98     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?accountnumber=' . $vendor->accountnumber );
99     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
100     $tx->req->env( { REMOTE_ADDR => $remote_address } );
101     $t->request_ok($tx)
102       ->status_is(200)
103       ->json_like( '/0/name' => qr/Ruben/ );
104
105     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?accountnumber=' . $other_vendor->accountnumber );
106     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
107     $tx->req->env( { REMOTE_ADDR => $remote_address } );
108     $t->request_ok($tx)
109       ->status_is(200)
110       ->json_like( '/0/name' => qr/Amerindia/ );
111
112     $tx = $t->ua->build_tx( DELETE => '/api/v1/acquisitions/vendors/' . $vendor->id );
113     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
114     $tx->req->env( { REMOTE_ADDR => $remote_address } );
115     $t->request_ok($tx)
116       ->status_is(200)
117       ->content_is(q{""});
118
119     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' );
120     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
121     $tx->req->env( { REMOTE_ADDR => $remote_address } );
122     $t->request_ok($tx)
123       ->status_is(200)
124       ->json_like( '/0/name' => qr/$other_vendor_name/ )
125       ->json_hasnt( '/1', 'Only one vendor' );
126
127     $tx = $t->ua->build_tx( DELETE => '/api/v1/acquisitions/vendors/' . $other_vendor->id );
128     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
129     $tx->req->env( { REMOTE_ADDR => $remote_address } );
130     $t->request_ok($tx)
131       ->status_is(200)
132       ->content_is(q{""});
133
134     $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' );
135     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
136     $tx->req->env( { REMOTE_ADDR => $remote_address } );
137     $t->request_ok($tx)
138       ->status_is(200)
139       ->json_is( [] );
140
141     $schema->storage->txn_rollback;
142 };
143
144 subtest 'get() test' => sub {
145
146     plan tests => 9;
147
148     $schema->storage->txn_begin;
149
150     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
151     my ( $borrowernumber, $session_id )
152         = create_user_and_session( { authorized => 1 } );
153
154     my $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $vendor->id );
155     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
156     $tx->req->env( { REMOTE_ADDR => $remote_address } );
157     $t->request_ok($tx)
158       ->status_is(200)
159       ->json_is( $vendor->to_api );
160
161     my $non_existent_id = $vendor->id + 1;
162     $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $non_existent_id );
163     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
164     $tx->req->env( { REMOTE_ADDR => $remote_address } );
165     $t->request_ok($tx)
166       ->status_is(404)
167       ->json_is( '/error' => 'Vendor not found' );
168
169     my ( $unauthorized_borrowernumber, $unauthorized_session_id )
170         = create_user_and_session( { authorized => 0 } );
171     $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $vendor->id );
172     $tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } );
173     $tx->req->env( { REMOTE_ADDR => $remote_address } );
174     $t->request_ok($tx)
175       ->status_is(403)
176       ->json_is( '/error', 'Authorization failure. Missing required permission(s).' );
177
178     $schema->storage->txn_rollback;
179 };
180
181 subtest 'add() tests' => sub {
182
183     plan tests => 16;
184
185     $schema->storage->txn_begin;
186
187     my ( $unauthorized_borrowernumber, $unauthorized_session_id )
188         = create_user_and_session( { authorized => 0 } );
189     my ( $authorized_borrowernumber, $authorized_session_id )
190         = create_user_and_session( { authorized => 1 } );
191     my $vendor = { name => 'Ruben libros' };
192
193     # Unauthorized attempt to write
194     my $tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor );
195     $tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } );
196     $tx->req->env( { REMOTE_ADDR => $remote_address } );
197     $t->request_ok($tx)
198       ->status_is(403);
199
200     # Authorized attempt to write invalid data
201     my $vendor_with_invalid_field = {
202         name     => 'Amerindia',
203         address5 => 'An address'
204     };
205
206     $tx = $t->ua->build_tx(
207         POST => "/api/v1/acquisitions/vendors" => json => $vendor_with_invalid_field );
208     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
209     $tx->req->env( { REMOTE_ADDR => $remote_address } );
210     $t->request_ok($tx)
211       ->status_is(400)
212       ->json_is(
213         "/errors" => [
214             {   message => "Properties not allowed: address5.",
215                 path    => "/body"
216             }
217           ]
218         );
219
220     # Authorized attempt to write
221     $tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor );
222     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
223     $tx->req->env( { REMOTE_ADDR => $remote_address } );
224     my $vendor_id = $t->request_ok($tx)
225                       ->status_is( 201, 'SWAGGER3 .2.1' )
226                       ->header_like( Location => qr|^\/api\/v1\/acquisitions\/vendors/\d*|, 'SWAGGER3.4.1')
227                       ->json_is( '/name' => $vendor->{name} )
228                       ->json_is( '/address1' => $vendor->{address1} )->tx->res->json('/id')
229         ;    # read the response vendor id for later use
230
231     # Authorized attempt to create with null id
232     $vendor->{id} = undef;
233     $tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor );
234     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
235     $tx->req->env( { REMOTE_ADDR => $remote_address } );
236     $t->request_ok($tx)
237       ->status_is(400)
238       ->json_has('/errors');
239
240     # Authorized attempt to create with existing id
241     $vendor->{id} = $vendor_id;
242     $tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor );
243     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
244     $tx->req->env( { REMOTE_ADDR => $remote_address } );
245     $t->request_ok($tx)
246       ->status_is(400)
247       ->json_is(
248         "/errors" => [
249             {   message => "Read-only.",
250                 path    => "/body/id"
251             }
252         ]
253     );
254
255     $schema->storage->txn_rollback;
256 };
257
258 subtest 'update() tests' => sub {
259
260     plan tests => 15;
261
262     $schema->storage->txn_begin;
263
264     my ( $unauthorized_borrowernumber, $unauthorized_session_id )
265         = create_user_and_session( { authorized => 0 } );
266     my ( $authorized_borrowernumber, $authorized_session_id )
267         = create_user_and_session( { authorized => 1 } );
268
269     my $vendor_id = $builder->build( { source => 'Aqbookseller' } )->{id};
270
271     # Unauthorized attempt to update
272     my $tx = $t->ua->build_tx( PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json =>
273             { city_name => 'New unauthorized name change' } );
274     $tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } );
275     $tx->req->env( { REMOTE_ADDR => $remote_address } );
276     $t->request_ok($tx)
277       ->status_is(403);
278
279     # Attempt partial update on a PUT
280     my $vendor_without_mandatory_field = { address1 => 'New address' };
281
282     $tx = $t->ua->build_tx( PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json =>
283             $vendor_without_mandatory_field );
284     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
285     $tx->req->env( { REMOTE_ADDR => $remote_address } );
286     $t->request_ok($tx)
287       ->status_is(400)
288       ->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
289
290     # Full object update on PUT
291     my $vendor_with_updated_field = { name => "London books", };
292
293     $tx = $t->ua->build_tx(
294         PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json => $vendor_with_updated_field );
295     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
296     $tx->req->env( { REMOTE_ADDR => $remote_address } );
297     $t->request_ok($tx)
298       ->status_is(200)
299       ->json_is( '/name' => 'London books' );
300
301     # Authorized attempt to write invalid data
302     my $vendor_with_invalid_field = {
303         blah     => "Blah",
304         address1 => "Address 1",
305         address2 => "Address 2",
306         address3 => "Address 3"
307     };
308
309     $tx = $t->ua->build_tx(
310         PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json => $vendor_with_invalid_field );
311     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
312     $tx->req->env( { REMOTE_ADDR => $remote_address } );
313     $t->request_ok($tx)
314       ->status_is(400)
315       ->json_is(
316         "/errors" => [
317             {   message => "Properties not allowed: blah.",
318                 path    => "/body"
319             }
320         ]
321       );
322
323     my $non_existent_id = $vendor_id + 1;
324     $tx = $t->ua->build_tx( PUT => "/api/v1/acquisitions/vendors/$non_existent_id" => json =>
325             $vendor_with_updated_field );
326     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
327     $tx->req->env( { REMOTE_ADDR => $remote_address } );
328     $t->request_ok($tx)->status_is(404);
329
330     $schema->storage->txn_rollback;
331
332     # Wrong method (POST)
333     $vendor_with_updated_field->{id} = 2;
334
335     $tx = $t->ua->build_tx(
336         POST => "/api/v1/acquisitions/vendors/$vendor_id" => json => $vendor_with_updated_field );
337     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
338     $tx->req->env( { REMOTE_ADDR => $remote_address } );
339     $t->request_ok($tx)
340       ->status_is(404);
341 };
342
343 subtest 'delete() tests' => sub {
344
345     plan tests => 7;
346
347     $schema->storage->txn_begin;
348
349     my ( $unauthorized_borrowernumber, $unauthorized_session_id )
350         = create_user_and_session( { authorized => 0 } );
351     my ( $authorized_borrowernumber, $authorized_session_id )
352         = create_user_and_session( { authorized => 1 } );
353
354     my $vendor_id = $builder->build( { source => 'Aqbookseller' } )->{id};
355
356     # Unauthorized attempt to update
357     my $tx = $t->ua->build_tx( DELETE => "/api/v1/acquisitions/vendors/$vendor_id" );
358     $tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } );
359     $tx->req->env( { REMOTE_ADDR => $remote_address } );
360     $t->request_ok($tx)
361       ->status_is(403);
362
363     $tx = $t->ua->build_tx( DELETE => "/api/v1/acquisitions/vendors/$vendor_id" );
364     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
365     $tx->req->env( { REMOTE_ADDR => $remote_address } );
366     $t->request_ok($tx)
367       ->status_is(200)
368       ->content_is(q{""});
369
370     $tx = $t->ua->build_tx( DELETE => "/api/v1/acquisitions/vendors/$vendor_id" );
371     $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
372     $tx->req->env( { REMOTE_ADDR => $remote_address } );
373     $t->request_ok($tx)
374       ->status_is(404);
375
376     $schema->storage->txn_rollback;
377 };
378
379 sub create_user_and_session {
380
381     my $args = shift;
382     my $flags = ( $args->{authorized} ) ? 2052 : 0;
383
384     # my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
385     my $dbh = C4::Context->dbh;
386
387     my $user = $builder->build(
388         {   source => 'Borrower',
389             value  => { flags => $flags }
390         }
391     );
392
393     # Create a session for the authorized user
394     my $session = C4::Auth::get_session('');
395     $session->param( 'number',   $user->{borrowernumber} );
396     $session->param( 'id',       $user->{userid} );
397     $session->param( 'ip',       '127.0.0.1' );
398     $session->param( 'lasttime', time() );
399     $session->flush;
400
401     if ( $args->{authorized} ) {
402         $dbh->do(
403             q{
404             INSERT INTO user_permissions (borrowernumber,module_bit,code)
405             VALUES (?,11,'vendors_manage')},
406             undef, $user->{borrowernumber}
407         );
408     }
409
410     return ( $user->{borrowernumber}, $session->id );
411 }
412
413 1;