Bug 23517: (follow-up) More test cases
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 C4::Budgets;
26
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new();
31
32 $schema->storage->txn_begin;
33
34 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36 my $t = Test::Mojo->new('Koha::REST::V1');
37
38 my $librarian = $builder->build_object({
39     class => 'Koha::Patrons',
40     value => { flags => 2052 }
41 });
42 my $password = 'thePassword123';
43 $librarian->set_password({ password => $password, skip_validation => 1 });
44 my $userid = $librarian->userid;
45
46 my $patron = $builder->build_object({
47     class => 'Koha::Patrons',
48     value => { flags => 0 }
49 });
50 my $unauth_password = 'thePassword123';
51 $patron->set_password({ password => $unauth_password, skip_validation => 1 });
52 my $unauth_userid = $patron->userid;
53
54 my $fund1 = {
55     budget_code      => 'ABCD',
56     budget_amount    => '123.132000',
57     budget_name      => 'Periodiques',
58     budget_notes     => 'This is a note',
59 };
60 my $budget_id = AddBudget($fund1);
61 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
62
63 $t->get_ok('/api/v1/acquisitions/funds')
64   ->status_is(401);
65
66 $t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
67   ->status_is(401);
68
69 $t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds")
70   ->status_is(403);
71
72 $t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name })
73   ->status_is(403);
74
75 $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds")
76   ->status_is(200);
77
78 $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name })
79   ->status_is(200)
80   ->json_like('/0/name' => qr/$fund1->{ budget_name }/);
81
82 $schema->storage->txn_rollback;
83
84 1;