3 # This file is part of Koha.
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
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.
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.
20 use Test::More tests => 14;
22 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new();
35 $schema->storage->txn_begin;
37 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
38 # this affects the other REST api tests
39 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
41 $ENV{REMOTE_ADDR} = '127.0.0.1';
42 my $t = Test::Mojo->new('Koha::REST::V1');
45 budget_code => 'ABCD',
46 budget_amount => '123.132000',
47 budget_name => 'Periodiques',
48 budget_notes => 'This is a note',
50 my $budget_id = AddBudget($fund1);
51 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
53 $t->get_ok('/api/v1/acquisitions/funds')
56 $t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
59 my ( $borrowernumber, $session_id )
60 #= create_user_and_session( { authorized => 1 } );
61 = create_user_and_session( );
63 my $tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/funds');
64 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
65 $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
69 $tx = $t->ua->build_tx(GET => "/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name });
70 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
71 $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
75 ( $borrowernumber, $session_id )
76 = create_user_and_session( { authorized => 1 } );
78 $tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/funds');
79 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
80 $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
84 $tx = $t->ua->build_tx(GET => "/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name });
85 $tx->req->cookies({name => 'CGISESSID', value => $session_id});
86 $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
89 ->json_like('/0/name' => qr/$fund1->{ budget_name }/);
91 $schema->storage->txn_rollback;
93 sub create_user_and_session {
96 my $flags = ( $args->{authorized} ) ? 2052 : 0;
98 # my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
99 my $dbh = C4::Context->dbh;
101 my $user = $builder->build(
102 { source => 'Borrower',
103 value => { flags => $flags }
107 # Create a session for the authorized user
108 my $session = C4::Auth::get_session('');
109 $session->param( 'number', $user->{borrowernumber} );
110 $session->param( 'id', $user->{userid} );
111 $session->param( 'ip', '127.0.0.1' );
112 $session->param( 'lasttime', time() );
115 if ( $args->{authorized} ) {
118 INSERT INTO user_permissions (borrowernumber,module_bit,code)
119 VALUES (?,11,'budget_manage_all')},
120 undef, $user->{borrowernumber}
124 return ( $user->{borrowernumber}, $session->id );