|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
| 3 |
# Copyright 2016 Koha-Suomi |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 7; |
| 23 |
use Test::Mojo; |
| 24 |
use t::lib::TestBuilder; |
| 25 |
|
| 26 |
use C4::Auth; |
| 27 |
use C4::Biblio; |
| 28 |
use C4::Context; |
| 29 |
use C4::Items; |
| 30 |
|
| 31 |
use Koha::Database; |
| 32 |
use Koha::Patron; |
| 33 |
use Koha::Items; |
| 34 |
|
| 35 |
my $builder = t::lib::TestBuilder->new(); |
| 36 |
|
| 37 |
my $dbh = C4::Context->dbh; |
| 38 |
$dbh->{AutoCommit} = 0; |
| 39 |
$dbh->{RaiseError} = 1; |
| 40 |
|
| 41 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
| 42 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 43 |
|
| 44 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
| 45 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
| 46 |
my $borrower = $builder->build({ |
| 47 |
source => 'Borrower', |
| 48 |
value => { |
| 49 |
branchcode => $branchcode, |
| 50 |
categorycode => $categorycode, |
| 51 |
flags => 16, |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
my $librarian = $builder->build({ |
| 56 |
source => "Borrower", |
| 57 |
value => { |
| 58 |
categorycode => $categorycode, |
| 59 |
branchcode => $branchcode, |
| 60 |
flags => 4, |
| 61 |
}, |
| 62 |
}); |
| 63 |
|
| 64 |
my ($session) = create_session($borrower); |
| 65 |
|
| 66 |
my $biblio = $builder->build({ |
| 67 |
source => 'Biblio' |
| 68 |
}); |
| 69 |
my $biblionumber = $biblio->{biblionumber}; |
| 70 |
my $item1 = $builder->build({ |
| 71 |
source => 'Item', |
| 72 |
value => { |
| 73 |
biblionumber => $biblionumber, |
| 74 |
} |
| 75 |
}); |
| 76 |
my $item2 = $builder->build({ |
| 77 |
source => 'Item', |
| 78 |
value => { |
| 79 |
biblionumber => $biblionumber, |
| 80 |
} |
| 81 |
}); |
| 82 |
my $item1number = $item1->{itemnumber}; |
| 83 |
my $item2number = $item2->{itemnumber}; |
| 84 |
|
| 85 |
my $nonExistentBiblionumber = -14362719; |
| 86 |
my $tx = $t->ua->build_tx(GET => "/api/v1/biblios/$nonExistentBiblionumber"); |
| 87 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 88 |
$t->request_ok($tx) |
| 89 |
->status_is(404); |
| 90 |
|
| 91 |
$tx = $t->ua->build_tx(GET => "/api/v1/biblios/$biblionumber"); |
| 92 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 93 |
$t->request_ok($tx) |
| 94 |
->status_is(200) |
| 95 |
->json_is('/items/0/itemnumber' => $item1number) |
| 96 |
->json_is('/items/1/itemnumber' => $item2number) |
| 97 |
->json_is('/biblionumber' => $biblionumber); |
| 98 |
|
| 99 |
$dbh->rollback; |
| 100 |
|
| 101 |
sub create_session { |
| 102 |
my (@borrowers) = @_; |
| 103 |
|
| 104 |
my @sessions; |
| 105 |
foreach $borrower (@borrowers) { |
| 106 |
my $session = C4::Auth::get_session(''); |
| 107 |
$session->param('number', $borrower->{borrowernumber}); |
| 108 |
$session->param('id', $borrower->{userid}); |
| 109 |
$session->param('ip', '127.0.0.1'); |
| 110 |
$session->param('lasttime', time()); |
| 111 |
$session->flush; |
| 112 |
push @sessions, $session; |
| 113 |
} |
| 114 |
|
| 115 |
return @sessions; |
| 116 |
} |