Bug 30596: Prevent api/v1/acquisitions_baskets.t and api/v1/acquisitions_funds.t...
[koha.git] / t / db_dependent / api / v1 / acquisitions_funds.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 => 14;
21 use Test::Mojo;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use JSON qw(encode_json);
26
27 use C4::Budgets;
28
29 use Koha::Database;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new();
33
34 $schema->storage->txn_begin;
35
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38 my $t = Test::Mojo->new('Koha::REST::V1');
39
40 my $librarian = $builder->build_object({
41     class => 'Koha::Patrons',
42     value => { flags => 2052 }
43 });
44 my $password = 'thePassword123';
45 $librarian->set_password({ password => $password, skip_validation => 1 });
46 my $userid = $librarian->userid;
47
48 my $patron = $builder->build_object({
49     class => 'Koha::Patrons',
50     value => { flags => 0 }
51 });
52 my $unauth_password = 'thePassword123';
53 $patron->set_password({ password => $unauth_password, skip_validation => 1 });
54 my $unauth_userid = $patron->userid;
55
56 my $fund_name = 'Periodiques';
57
58 my $fund = $builder->build_object(
59     {
60         class => 'Koha::Acquisition::Funds',
61         value => {
62             budget_amount => '123.132000',
63             budget_name   => $fund_name,
64             budget_notes  => 'This is a note'
65         }
66     }
67 );
68
69 $t->get_ok('/api/v1/acquisitions/funds')
70   ->status_is(401);
71
72 $t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
73   ->status_is(401);
74
75 $t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds")
76   ->status_is(403);
77
78 $t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds?name=" . $fund_name)
79   ->status_is(403);
80
81 $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds")
82   ->status_is(200);
83
84 $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds?name=" . $fund_name)
85   ->status_is(200)
86   ->json_like('/0/name' => qr/$fund_name/);
87
88 $schema->storage->txn_rollback;
89
90 subtest 'list_owners() and list_users() tests' => sub {
91
92     plan tests => 6;
93
94     $schema->storage->txn_begin;
95
96     my $patron_with_permission =
97       $builder->build_object( { class => 'Koha::Patrons', value => { flags => 2**11 } } )
98       ;    ## 11 == acquisition
99     my $patron_without_permission =
100       $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
101     my $superlibrarian =
102       $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } );
103     my $password = 'thePassword123';
104     $superlibrarian->set_password( { password => $password, skip_validation => 1 } );
105     $superlibrarian->discard_changes;
106
107     my $userid = $superlibrarian->userid;
108
109     # Restrict the query to a know list of patrons
110     my $api_filter = encode_json(
111         {   'me.patron_id' =>
112               [ $patron_with_permission->id, $patron_without_permission->id, $superlibrarian->id ]
113         }
114     );
115
116     $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds/owners?q=$api_filter")
117       ->status_is(200)->json_is( [ $patron_with_permission->to_api, $superlibrarian->to_api ] );
118
119     $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds/users?q=$api_filter")
120       ->status_is(200)->json_is( [ $patron_with_permission->to_api, $superlibrarian->to_api ] );
121
122     $schema->storage->txn_rollback;
123 };