|
Line 0
Link Here
|
| 0 |
- |
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::Auth; |
| 26 |
use C4::Context; |
| 27 |
use C4::Budgets; |
| 28 |
|
| 29 |
use Koha::Database; |
| 30 |
use Koha::Patron; |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
| 33 |
my $builder = t::lib::TestBuilder->new(); |
| 34 |
|
| 35 |
$schema->storage->txn_begin; |
| 36 |
|
| 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' ); |
| 40 |
|
| 41 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
| 42 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 43 |
|
| 44 |
my $fund1 = { |
| 45 |
budget_code => 'ABCD', |
| 46 |
budget_amount => '123.132000', |
| 47 |
budget_name => 'Periodiques', |
| 48 |
budget_notes => 'This is a note', |
| 49 |
}; |
| 50 |
my $budget_id = AddBudget($fund1); |
| 51 |
isnt( $budget_id, undef, 'AddBudget does not returns undef' ); |
| 52 |
|
| 53 |
$t->get_ok('/api/v1/acquisitions/funds') |
| 54 |
->status_is(401); |
| 55 |
|
| 56 |
$t->get_ok('/api/v1/acquisitions/funds/?name=testFund') |
| 57 |
->status_is(401); |
| 58 |
|
| 59 |
my ( $borrowernumber, $session_id ) |
| 60 |
#= create_user_and_session( { authorized => 1 } ); |
| 61 |
= create_user_and_session( ); |
| 62 |
|
| 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'}); |
| 66 |
$t->request_ok($tx) |
| 67 |
->status_is(403); |
| 68 |
|
| 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'}); |
| 72 |
$t->request_ok($tx) |
| 73 |
->status_is(403); |
| 74 |
|
| 75 |
( $borrowernumber, $session_id ) |
| 76 |
= create_user_and_session( { authorized => 1 } ); |
| 77 |
|
| 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'}); |
| 81 |
$t->request_ok($tx) |
| 82 |
->status_is(200); |
| 83 |
|
| 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'}); |
| 87 |
$t->request_ok($tx) |
| 88 |
->status_is(200) |
| 89 |
->json_like('/0/name' => qr/$fund1->{ budget_name }/); |
| 90 |
|
| 91 |
$schema->storage->txn_rollback; |
| 92 |
|
| 93 |
sub create_user_and_session { |
| 94 |
|
| 95 |
my $args = shift; |
| 96 |
my $flags = ( $args->{authorized} ) ? 2052 : 0; |
| 97 |
|
| 98 |
# my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
| 99 |
my $dbh = C4::Context->dbh; |
| 100 |
|
| 101 |
my $user = $builder->build( |
| 102 |
{ source => 'Borrower', |
| 103 |
value => { flags => $flags } |
| 104 |
} |
| 105 |
); |
| 106 |
|
| 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() ); |
| 113 |
$session->flush; |
| 114 |
|
| 115 |
if ( $args->{authorized} ) { |
| 116 |
$dbh->do( |
| 117 |
q{ |
| 118 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
| 119 |
VALUES (?,11,'budget_manage_all')}, |
| 120 |
undef, $user->{borrowernumber} |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
return ( $user->{borrowernumber}, $session->id ); |
| 125 |
} |
| 126 |
|
| 127 |
|