| 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 => 10; | 
            
              | 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(403); | 
            
              | 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 |     } | 
            
              | 59 | }); | 
            
              | 60 |  | 
            
              | 61 | my $borrower2 = $builder->build({ | 
            
              | 62 |     source => 'Borrower', | 
            
              | 63 |     value => { | 
            
              | 64 |         branchcode   => $branchcode, | 
            
              | 65 |         categorycode => $categorycode, | 
            
              | 66 |     } | 
            
              | 67 | }); | 
            
              | 68 | my $borrowernumber = $borrower->{borrowernumber}; | 
            
              | 69 | my $borrowernumber2 = $borrower2->{borrowernumber}; | 
            
              | 70 |  | 
            
              | 71 | $dbh->do(q| DELETE FROM accountlines |); | 
            
              | 72 | $dbh->do(q| | 
            
              | 73 |     INSERT INTO accountlines (borrowernumber, amount, accounttype) | 
            
              | 74 |     VALUES (?, 20, 'A'), (?, 40, 'F'), (?, 80, 'F'), (?, 10, 'F') | 
            
              | 75 |     |, undef, $borrowernumber, $borrowernumber, $borrowernumber, $borrowernumber2); | 
            
              | 76 |  | 
            
              | 77 | my $session = C4::Auth::get_session(''); | 
            
              | 78 | $session->param('number', $loggedinuser->{ borrowernumber }); | 
            
              | 79 | $session->param('id', $loggedinuser->{ userid }); | 
            
              | 80 | $session->param('ip', '127.0.0.1'); | 
            
              | 81 | $session->param('lasttime', time()); | 
            
              | 82 | $session->flush; | 
            
              | 83 |  | 
            
              | 84 | my $tx = $t->ua->build_tx(GET => "/api/v1/accountlines?borrowernumber=$borrowernumber"); | 
            
              | 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 |  | 
            
              | 90 | my $json = $t->tx->res->json; | 
            
              | 91 | ok(ref $json eq 'ARRAY', 'response is a JSON array'); | 
            
              | 92 | ok(scalar @$json == 3, 'response array contains 3 elements'); | 
            
              | 93 |  | 
            
              | 94 | $tx = $t->ua->build_tx(GET => "/api/v1/accountlines"); | 
            
              | 95 | $tx->req->cookies({name => 'CGISESSID', value => $session->id}); | 
            
              | 96 | $tx->req->env({REMOTE_ADDR => '127.0.0.1'}); | 
            
              | 97 | $t->request_ok($tx) | 
            
              | 98 |   ->status_is(200); | 
            
              | 99 |  | 
            
              | 100 | $json = $t->tx->res->json; | 
            
              | 101 | ok(ref $json eq 'ARRAY', 'response is a JSON array'); | 
            
              | 102 | ok(scalar @$json == 4, 'response array contains 3 elements'); | 
            
              | 103 |  | 
            
              | 104 | $dbh->rollback; |