|
Lines 1-248
Link Here
|
| 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 |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 2; |
| 21 |
|
| 22 |
use Test::MockModule; |
| 23 |
use Test::MockObject; |
| 24 |
use Test::Mojo; |
| 25 |
|
| 26 |
use JSON qw(encode_json); |
| 27 |
|
| 28 |
use t::lib::TestBuilder; |
| 29 |
use t::lib::Mocks; |
| 30 |
|
| 31 |
use Koha::Illrequests; |
| 32 |
use Koha::DateUtils qw( format_sqldatetime ); |
| 33 |
|
| 34 |
my $schema = Koha::Database->new->schema; |
| 35 |
my $builder = t::lib::TestBuilder->new; |
| 36 |
|
| 37 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 38 |
|
| 39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 40 |
|
| 41 |
subtest 'list_legacy() tests' => sub { |
| 42 |
|
| 43 |
plan tests => 30; |
| 44 |
|
| 45 |
# Mock ILLBackend (as object) |
| 46 |
my $backend = Test::MockObject->new; |
| 47 |
$backend->set_isa('Koha::Illbackends::Mock'); |
| 48 |
$backend->set_always('name', 'Mock'); |
| 49 |
$backend->set_always('capabilities', sub { return 'bar'; } ); |
| 50 |
$backend->mock( |
| 51 |
'metadata', |
| 52 |
sub { |
| 53 |
my ( $self, $rq ) = @_; |
| 54 |
return { |
| 55 |
ID => $rq->illrequest_id, |
| 56 |
Title => $rq->patron->borrowernumber |
| 57 |
} |
| 58 |
} |
| 59 |
); |
| 60 |
$backend->mock( |
| 61 |
'status_graph', sub {}, |
| 62 |
); |
| 63 |
|
| 64 |
# Mock Koha::Illrequest::load_backend (to load Mocked Backend) |
| 65 |
my $illreqmodule = Test::MockModule->new('Koha::Illrequest'); |
| 66 |
$illreqmodule->mock( 'load_backend', |
| 67 |
sub { my $self = shift; $self->{_my_backend} = $backend; return $self } |
| 68 |
); |
| 69 |
|
| 70 |
$schema->storage->txn_begin; |
| 71 |
|
| 72 |
Koha::Illrequests->search->delete; |
| 73 |
|
| 74 |
# create an authorized user |
| 75 |
my $patron = $builder->build_object({ |
| 76 |
class => 'Koha::Patrons', |
| 77 |
value => { flags => 2 ** 22 } # 22 => ill |
| 78 |
}); |
| 79 |
my $password = 'thePassword123'; |
| 80 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
| 81 |
my $userid = $patron->userid; |
| 82 |
|
| 83 |
## Authorized user tests |
| 84 |
# No requests, so empty array should be returned |
| 85 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests" ) |
| 86 |
->status_is(200) |
| 87 |
->json_is( [] ); |
| 88 |
|
| 89 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 90 |
my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 91 |
my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 92 |
|
| 93 |
# Create an ILL request |
| 94 |
my $illrequest = $builder->build_object( |
| 95 |
{ |
| 96 |
class => 'Koha::Illrequests', |
| 97 |
value => { |
| 98 |
backend => 'Mock', |
| 99 |
biblio_id => undef, |
| 100 |
branchcode => $library->branchcode, |
| 101 |
borrowernumber => $patron_1->borrowernumber, |
| 102 |
status => 'STATUS1', |
| 103 |
} |
| 104 |
} |
| 105 |
); |
| 106 |
|
| 107 |
# The api response is always augmented with the id_prefix |
| 108 |
my $response = $illrequest->unblessed; |
| 109 |
$response->{id_prefix} = $illrequest->id_prefix; |
| 110 |
|
| 111 |
my $req_formatted = add_formatted($response); |
| 112 |
|
| 113 |
# One illrequest created, should get returned |
| 114 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests" ) |
| 115 |
->status_is(200) |
| 116 |
->json_is( [ $req_formatted ] ); |
| 117 |
|
| 118 |
# One illrequest created, returned with augmented data |
| 119 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners" ) |
| 120 |
->status_is(200) |
| 121 |
->json_has( '/0/patron', 'patron embedded' ) |
| 122 |
->json_is( '/0/patron/patron_id', $patron_1->borrowernumber, 'The right patron is embeded') |
| 123 |
->json_has( '/0/requested_partners', 'requested_partners embedded' ) |
| 124 |
->json_has( '/0/capabilities', 'capabilities embedded' ) |
| 125 |
->json_has( '/0/library', 'library embedded' ) |
| 126 |
->json_has( '/0/metadata', 'metadata embedded' ) |
| 127 |
->json_hasnt( '/1', 'Only one request was created' ); |
| 128 |
|
| 129 |
# Create another ILL request |
| 130 |
my $illrequest2 = $builder->build_object( |
| 131 |
{ |
| 132 |
class => 'Koha::Illrequests', |
| 133 |
value => { |
| 134 |
backend => 'Mock', |
| 135 |
biblio_id => undef, |
| 136 |
branchcode => $library->branchcode, |
| 137 |
borrowernumber => $patron_2->borrowernumber, |
| 138 |
status => 'STATUS2', |
| 139 |
} |
| 140 |
} |
| 141 |
); |
| 142 |
|
| 143 |
# The api response is always augmented with the id_prefix |
| 144 |
my $response2 = $illrequest2->unblessed; |
| 145 |
$response2->{id_prefix} = $illrequest2->id_prefix; |
| 146 |
|
| 147 |
my $req2_formatted = add_formatted($response2); |
| 148 |
|
| 149 |
# Two illrequest created, should get returned |
| 150 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests" ) |
| 151 |
->status_is(200) |
| 152 |
->json_is( [ $req_formatted, $req2_formatted ] ); |
| 153 |
|
| 154 |
# Warn on unsupported query parameter |
| 155 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests?request_blah=blah" ) |
| 156 |
->status_is(400) |
| 157 |
->json_is( |
| 158 |
[{ path => '/query/request_blah', message => 'Malformed query string'}] |
| 159 |
); |
| 160 |
|
| 161 |
# Test the borrowernumber parameter |
| 162 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests?borrowernumber=" . $patron_2->borrowernumber ) |
| 163 |
->status_is(200) |
| 164 |
->json_is( [ $response2 ] ); |
| 165 |
|
| 166 |
# Test the ILLHiddenRequestStatuses syspref |
| 167 |
t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS1' ); |
| 168 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests" ) |
| 169 |
->status_is(200) |
| 170 |
->json_is( [ $req2_formatted ] ); |
| 171 |
|
| 172 |
t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS2' ); |
| 173 |
$t->get_ok( "//$userid:$password@/api/v1/illrequests" ) |
| 174 |
->status_is(200) |
| 175 |
->json_is( [ $req_formatted ] ); |
| 176 |
|
| 177 |
$schema->storage->txn_rollback; |
| 178 |
}; |
| 179 |
|
| 180 |
subtest 'list() tests' => sub { |
| 181 |
|
| 182 |
plan tests => 9; |
| 183 |
|
| 184 |
$schema->storage->txn_begin; |
| 185 |
|
| 186 |
Koha::Illrequests->search->delete; |
| 187 |
|
| 188 |
my $librarian = $builder->build_object( |
| 189 |
{ |
| 190 |
class => 'Koha::Patrons', |
| 191 |
value => { flags => 2 ** 22 } # 22 => ill |
| 192 |
} |
| 193 |
); |
| 194 |
my $password = 'thePassword123'; |
| 195 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 196 |
my $userid = $librarian->userid; |
| 197 |
|
| 198 |
my $patron = $builder->build_object( |
| 199 |
{ |
| 200 |
class => 'Koha::Patrons', |
| 201 |
value => { flags => 0 } |
| 202 |
} |
| 203 |
); |
| 204 |
|
| 205 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 206 |
my $unauth_userid = $patron->userid; |
| 207 |
|
| 208 |
$t->get_ok("//$userid:$password@/api/v1/ill_requests") |
| 209 |
->status_is(200) |
| 210 |
->json_is( [] ); |
| 211 |
|
| 212 |
my $req_1 = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => undef, status => 'REQ' } }); |
| 213 |
my $req_2 = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => undef, status => 'REQ' } } ); |
| 214 |
my $ret = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => undef, status => 'RET' } } ); |
| 215 |
|
| 216 |
$t->get_ok("//$userid:$password@/api/v1/ill_requests") |
| 217 |
->status_is(200) |
| 218 |
->json_is( [ $req_1->to_api, $req_2->to_api, $ret->to_api ]); |
| 219 |
|
| 220 |
my $query = encode_json({ status => 'REQ' }); |
| 221 |
|
| 222 |
# Filtering works |
| 223 |
$t->get_ok("//$userid:$password@/api/v1/ill_requests?q=$query" ) |
| 224 |
->status_is(200) |
| 225 |
->json_is( [ $req_1->to_api, $req_2->to_api ]); |
| 226 |
|
| 227 |
$schema->storage->txn_rollback; |
| 228 |
}; |
| 229 |
|
| 230 |
sub add_formatted { |
| 231 |
my $req = shift; |
| 232 |
my @format_dates = ( 'placed', 'updated', 'completed' ); |
| 233 |
# We need to embellish the request with properties that the API |
| 234 |
# controller calculates on the fly |
| 235 |
# Create new "formatted" columns for each date column |
| 236 |
# that needs formatting |
| 237 |
foreach my $field(@format_dates) { |
| 238 |
if (defined $req->{$field}) { |
| 239 |
$req->{$field . "_formatted"} = format_sqldatetime( |
| 240 |
$req->{$field}, |
| 241 |
undef, |
| 242 |
undef, |
| 243 |
1 |
| 244 |
); |
| 245 |
} |
| 246 |
} |
| 247 |
return $req; |
| 248 |
} |
| 249 |
- |