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 => 14;
22 use t::lib::TestBuilder;
25 use JSON qw(encode_json);
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new();
34 $schema->storage->txn_begin;
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 my $t = Test::Mojo->new('Koha::REST::V1');
40 my $librarian = $builder->build_object({
41 class => 'Koha::Patrons',
42 value => { flags => 2052 }
44 my $password = 'thePassword123';
45 $librarian->set_password({ password => $password, skip_validation => 1 });
46 my $userid = $librarian->userid;
48 my $patron = $builder->build_object({
49 class => 'Koha::Patrons',
50 value => { flags => 0 }
52 my $unauth_password = 'thePassword123';
53 $patron->set_password({ password => $unauth_password, skip_validation => 1 });
54 my $unauth_userid = $patron->userid;
56 my $fund_name = 'Periodiques';
58 my $fund = $builder->build_object(
60 class => 'Koha::Acquisition::Funds',
62 budget_amount => '123.132000',
63 budget_name => $fund_name,
64 budget_notes => 'This is a note'
69 $t->get_ok('/api/v1/acquisitions/funds')
72 $t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
75 $t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds")
78 $t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds?name=" . $fund_name)
81 $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds")
84 $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds?name=" . $fund_name)
86 ->json_like('/0/name' => qr/$fund_name/);
88 $schema->storage->txn_rollback;
90 subtest 'list_owners() and list_users() tests' => sub {
94 $schema->storage->txn_begin;
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 } } );
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;
107 my $userid = $superlibrarian->userid;
109 # Restrict the query to a know list of patrons
110 my $api_filter = encode_json(
112 [ $patron_with_permission->id, $patron_without_permission->id, $superlibrarian->id ]
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 ] );
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 ] );
122 $schema->storage->txn_rollback;