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 => 12; |
21 |
use Test::Mojo; |
22 |
use t::lib::TestBuilder; |
23 |
|
24 |
use C4::Auth; |
25 |
use C4::Context; |
26 |
|
27 |
use Koha::Database; |
28 |
|
29 |
my $builder = t::lib::TestBuilder->new(); |
30 |
|
31 |
my $dbh = C4::Context->dbh; |
32 |
$dbh->{AutoCommit} = 0; |
33 |
$dbh->{RaiseError} = 1; |
34 |
|
35 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
36 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
37 |
|
38 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
39 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
40 |
|
41 |
$t->get_ok('/api/v1/accountlines') |
42 |
->status_is(401); |
43 |
|
44 |
my $loggedinuser = $builder->build({ |
45 |
source => 'Borrower', |
46 |
value => { |
47 |
branchcode => $branchcode, |
48 |
categorycode => $categorycode, |
49 |
flags => 1024 |
50 |
} |
51 |
}); |
52 |
|
53 |
my $borrower = $builder->build({ |
54 |
source => 'Borrower', |
55 |
value => { |
56 |
branchcode => $branchcode, |
57 |
categorycode => $categorycode, |
58 |
flags => 0, |
59 |
} |
60 |
}); |
61 |
|
62 |
my $borrower2 = $builder->build({ |
63 |
source => 'Borrower', |
64 |
value => { |
65 |
branchcode => $branchcode, |
66 |
categorycode => $categorycode, |
67 |
} |
68 |
}); |
69 |
my $borrowernumber = $borrower->{borrowernumber}; |
70 |
my $borrowernumber2 = $borrower2->{borrowernumber}; |
71 |
|
72 |
$dbh->do(q| DELETE FROM accountlines |); |
73 |
$dbh->do(q| |
74 |
INSERT INTO accountlines (borrowernumber, amount, accounttype) |
75 |
VALUES (?, 20, 'A'), (?, 40, 'F'), (?, 80, 'F'), (?, 10, 'F') |
76 |
|, undef, $borrowernumber, $borrowernumber, $borrowernumber, $borrowernumber2); |
77 |
|
78 |
my $session = C4::Auth::get_session(''); |
79 |
$session->param('number', $loggedinuser->{ borrowernumber }); |
80 |
$session->param('id', $loggedinuser->{ userid }); |
81 |
$session->param('ip', '127.0.0.1'); |
82 |
$session->param('lasttime', time()); |
83 |
$session->flush; |
84 |
|
85 |
my $borrowersession = C4::Auth::get_session(''); |
86 |
$borrowersession->param('number', $borrower->{ borrowernumber }); |
87 |
$borrowersession->param('id', $borrower->{ userid }); |
88 |
$borrowersession->param('ip', '127.0.0.1'); |
89 |
$borrowersession->param('lasttime', time()); |
90 |
$borrowersession->flush; |
91 |
|
92 |
my $tx = $t->ua->build_tx(GET => "/api/v1/accountlines?borrowernumber=$borrowernumber2"); |
93 |
$tx->req->cookies({name => 'CGISESSID', value => $borrowersession->id}); |
94 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
95 |
$t->request_ok($tx) |
96 |
->status_is(403); |
97 |
|
98 |
$tx = $t->ua->build_tx(GET => "/api/v1/accountlines?borrowernumber=$borrowernumber"); |
99 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
100 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
101 |
$t->request_ok($tx) |
102 |
->status_is(200); |
103 |
|
104 |
my $json = $t->tx->res->json; |
105 |
ok(ref $json eq 'ARRAY', 'response is a JSON array'); |
106 |
ok(scalar @$json == 3, 'response array contains 3 elements'); |
107 |
|
108 |
$tx = $t->ua->build_tx(GET => "/api/v1/accountlines"); |
109 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
110 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
111 |
$t->request_ok($tx) |
112 |
->status_is(200); |
113 |
|
114 |
$json = $t->tx->res->json; |
115 |
ok(ref $json eq 'ARRAY', 'response is a JSON array'); |
116 |
ok(scalar @$json == 4, 'response array contains 3 elements'); |
117 |
|
118 |
$dbh->rollback; |