|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
|
| 5 |
use Test::NoWarnings; |
| 6 |
use Test::More tests => 5; |
| 7 |
use Test::MockModule; |
| 8 |
use Test::Mojo; |
| 9 |
|
| 10 |
use t::lib::TestBuilder; |
| 11 |
use t::lib::Mocks; |
| 12 |
|
| 13 |
use Koha::Database; |
| 14 |
|
| 15 |
my $schema = Koha::Database->new->schema; |
| 16 |
my $builder = t::lib::TestBuilder->new; |
| 17 |
|
| 18 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 19 |
|
| 20 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 21 |
|
| 22 |
subtest 'list()' => sub { |
| 23 |
plan tests => 9; |
| 24 |
|
| 25 |
$schema->storage->txn_begin; |
| 26 |
|
| 27 |
my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 28 |
my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 29 |
my $item = $builder->build_sample_item( |
| 30 |
{ |
| 31 |
homebranch => $library1->branchcode, |
| 32 |
holdingbranch => $library1->branchcode, |
| 33 |
} |
| 34 |
); |
| 35 |
|
| 36 |
my $transfer = $item->request_transfer( { to => $library2, reason => 'Manual' } ); |
| 37 |
|
| 38 |
my $patron = $builder->build_object( |
| 39 |
{ |
| 40 |
class => 'Koha::Patrons', |
| 41 |
value => { flags => 1 } |
| 42 |
} |
| 43 |
); |
| 44 |
|
| 45 |
my $nonprivilegedpatron = $builder->build_object( |
| 46 |
{ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 0 } |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
my $password = 'thePassword123'; |
| 53 |
|
| 54 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
| 55 |
my $userid = $nonprivilegedpatron->userid; |
| 56 |
|
| 57 |
$t->get_ok("//$userid:$password@/api/v1/items/transfers")->status_is(403) |
| 58 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
| 59 |
|
| 60 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 61 |
$userid = $patron->userid; |
| 62 |
|
| 63 |
$t->get_ok("//$userid:$password@/api/v1/items/transfers")->status_is( 200, 'Transfer list retrieved successfully' ); |
| 64 |
|
| 65 |
my $res = $t->tx->res->json; |
| 66 |
my $json = pop @$res; |
| 67 |
is( $json->{item_id}, $item->itemnumber, 'Item number matches' ); |
| 68 |
is( $json->{from_library_id}, $library1->branchcode, 'From library matches' ); |
| 69 |
is( $json->{to_library_id}, $library2->branchcode, 'To library matches' ); |
| 70 |
is( $json->{reason}, 'Manual', 'Reason matches' ); |
| 71 |
|
| 72 |
$schema->storage->txn_rollback; |
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'get()' => sub { |
| 76 |
plan tests => 9; |
| 77 |
|
| 78 |
$schema->storage->txn_begin; |
| 79 |
|
| 80 |
my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 81 |
my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 82 |
my $item = $builder->build_sample_item( |
| 83 |
{ |
| 84 |
homebranch => $library1->branchcode, |
| 85 |
holdingbranch => $library1->branchcode, |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
my $transfer = $item->request_transfer( { to => $library2, reason => 'Manual' } ); |
| 90 |
|
| 91 |
my $patron = $builder->build_object( |
| 92 |
{ |
| 93 |
class => 'Koha::Patrons', |
| 94 |
value => { flags => 1 } |
| 95 |
} |
| 96 |
); |
| 97 |
|
| 98 |
my $nonprivilegedpatron = $builder->build_object( |
| 99 |
{ |
| 100 |
class => 'Koha::Patrons', |
| 101 |
value => { flags => 0 } |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
my $password = 'thePassword123'; |
| 106 |
|
| 107 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
| 108 |
my $userid = $nonprivilegedpatron->userid; |
| 109 |
|
| 110 |
$t->get_ok("//$userid:$password@/api/v1/items/transfers")->status_is(403) |
| 111 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
| 112 |
|
| 113 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 114 |
$userid = $patron->userid; |
| 115 |
my $itemnumber = $item->itemnumber; |
| 116 |
$t->get_ok("//$userid:$password@/api/v1/items/$itemnumber/transfers") |
| 117 |
->status_is( 200, 'Transfer retrieved successfully' ); |
| 118 |
|
| 119 |
my $res = $t->tx->res->json; |
| 120 |
is( $res->{item_id}, $item->itemnumber, 'Item number matches' ); |
| 121 |
is( $res->{from_library_id}, $library1->branchcode, 'From library matches' ); |
| 122 |
is( $res->{to_library_id}, $library2->branchcode, 'To library matches' ); |
| 123 |
is( $res->{reason}, 'Manual', 'Reason matches' ); |
| 124 |
|
| 125 |
$schema->storage->txn_rollback; |
| 126 |
}; |
| 127 |
|
| 128 |
subtest 'add()' => sub { |
| 129 |
plan tests => 33; |
| 130 |
|
| 131 |
$schema->storage->txn_begin; |
| 132 |
|
| 133 |
my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 134 |
my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 135 |
my $library3 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 136 |
my $item = $builder->build_sample_item( |
| 137 |
{ |
| 138 |
homebranch => $library1->branchcode, |
| 139 |
holdingbranch => $library1->branchcode, |
| 140 |
} |
| 141 |
); |
| 142 |
|
| 143 |
my $patron = $builder->build_object( |
| 144 |
{ |
| 145 |
class => 'Koha::Patrons', |
| 146 |
value => { flags => 1 } |
| 147 |
} |
| 148 |
); |
| 149 |
|
| 150 |
my $nonprivilegedpatron = $builder->build_object( |
| 151 |
{ |
| 152 |
class => 'Koha::Patrons', |
| 153 |
value => { flags => 0 } |
| 154 |
} |
| 155 |
); |
| 156 |
|
| 157 |
my $password = 'thePassword123'; |
| 158 |
|
| 159 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
| 160 |
my $userid = $nonprivilegedpatron->userid; |
| 161 |
my $itemnumber = $item->itemnumber; |
| 162 |
|
| 163 |
my $params = { |
| 164 |
to_library_id => $library2->branchcode, |
| 165 |
reason => 'Manual', |
| 166 |
}; |
| 167 |
|
| 168 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers" => json => $params )->status_is(403) |
| 169 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
| 170 |
|
| 171 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 172 |
$userid = $patron->userid; |
| 173 |
|
| 174 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers" => json => $params ) |
| 175 |
->status_is( 201, 'Transfer is created successfully' ); |
| 176 |
|
| 177 |
my $res = $t->tx->res->json; |
| 178 |
is( $res->{item_id}, $item->itemnumber, 'Item number matches' ); |
| 179 |
is( $res->{from_library_id}, $library1->branchcode, 'From library matches' ); |
| 180 |
is( $res->{to_library_id}, $library2->branchcode, 'To library matches' ); |
| 181 |
is( $res->{reason}, 'Manual', 'Reason matches' ); |
| 182 |
|
| 183 |
# Check transfer with limits |
| 184 |
my $limit = $builder->build_object( { class => 'Koha::Item::Transfer::Limits' } ); |
| 185 |
$limit->set( { toBranch => $library3->branchcode, fromBranch => $library1->branchcode } )->store; |
| 186 |
|
| 187 |
$params->{to_library_id} = $library3->branchcode; |
| 188 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers" => json => $params ) |
| 189 |
->status_is( 409, 'Transfer is not created due to transfer limit' ); |
| 190 |
|
| 191 |
$t->post_ok( |
| 192 |
"//$userid:$password@/api/v1/items/$itemnumber/transfers?ignore_limits=1&enqueue=1" => json => $params ) |
| 193 |
->status_is( 201, 'Transfer is created successfully ignoring transfer limit' ); |
| 194 |
|
| 195 |
# Duplicate transfer |
| 196 |
$params->{to_library_id} = $library1->branchcode; |
| 197 |
$params->{comments} = 'Test comment'; |
| 198 |
|
| 199 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers" => json => $params )->status_is(409) |
| 200 |
->json_is( '/error' => 'Item is already in transfer queue' ); |
| 201 |
|
| 202 |
# Add to enqueue even if already in transfer queue |
| 203 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers?enqueue=1" => json => $params ) |
| 204 |
->status_is( 201, 'Transfer is created successfully to enqueue up transfer' ); |
| 205 |
$res = $t->tx->res->json; |
| 206 |
|
| 207 |
is( $res->{to_library_id}, $library1->branchcode, 'To library matches' ); |
| 208 |
is( $res->{reason}, 'Manual', 'Reason matches' ); |
| 209 |
is( $res->{comments}, 'Test comment', 'Comments matches' ); |
| 210 |
|
| 211 |
my $library_transfer_id = $res->{library_transfer_id}; |
| 212 |
|
| 213 |
# Add to enqueue with replace |
| 214 |
$params->{to_library_id} = $library2->branchcode; |
| 215 |
$params->{reason} = 'Reserve'; |
| 216 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers?replace=WrongTransfer" => json => $params ) |
| 217 |
->status_is( 201, 'Transfer is created successfully to replace existing transfer' ); |
| 218 |
$res = $t->tx->res->json; |
| 219 |
is( $res->{to_library_id}, $library2->branchcode, 'To library matches after replace' ); |
| 220 |
is( $res->{reason}, 'Reserve', 'Reason matches after replace' ); |
| 221 |
|
| 222 |
# Invalid item number |
| 223 |
my $invalid_itemnumber = $itemnumber + 1; |
| 224 |
$t->post_ok( "//$userid:$password@/api/v1/items/$invalid_itemnumber/transfers" => json => $params )->status_is(404) |
| 225 |
->json_is( '/error' => 'Item not found' ); |
| 226 |
|
| 227 |
# Invalid library |
| 228 |
$params->{to_library_id} = 'InvalidLibrary'; |
| 229 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers" => json => $params )->status_is(404) |
| 230 |
->json_is( '/error' => 'Library not found' ); |
| 231 |
|
| 232 |
# Invalid reason |
| 233 |
$params->{reason} = 'InvalidReason'; |
| 234 |
$t->post_ok( "//$userid:$password@/api/v1/items/$itemnumber/transfers" => json => $params )->status_is(400); |
| 235 |
|
| 236 |
$schema->storage->txn_rollback; |
| 237 |
}; |
| 238 |
|
| 239 |
subtest 'delete()' => sub { |
| 240 |
plan tests => 7; |
| 241 |
|
| 242 |
$schema->storage->txn_begin; |
| 243 |
|
| 244 |
my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 245 |
my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 246 |
my $item = $builder->build_sample_item( |
| 247 |
{ |
| 248 |
homebranch => $library1->branchcode, |
| 249 |
holdingbranch => $library1->branchcode, |
| 250 |
} |
| 251 |
); |
| 252 |
|
| 253 |
my $transfer = $item->request_transfer( { to => $library2, reason => 'Manual' } ); |
| 254 |
|
| 255 |
my $patron = $builder->build_object( |
| 256 |
{ |
| 257 |
class => 'Koha::Patrons', |
| 258 |
value => { flags => 1 } |
| 259 |
} |
| 260 |
); |
| 261 |
|
| 262 |
my $nonprivilegedpatron = $builder->build_object( |
| 263 |
{ |
| 264 |
class => 'Koha::Patrons', |
| 265 |
value => { flags => 0 } |
| 266 |
} |
| 267 |
); |
| 268 |
|
| 269 |
my $password = 'thePassword123'; |
| 270 |
|
| 271 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
| 272 |
my $userid = $nonprivilegedpatron->userid; |
| 273 |
my $itemnumber = $item->itemnumber; |
| 274 |
$t->delete_ok("//$userid:$password@/api/v1/items/$itemnumber/transfers")->status_is(403) |
| 275 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
| 276 |
|
| 277 |
is( $item->transfer->branchtransfer_id, $transfer->branchtransfer_id, 'Item transfer exists' ); |
| 278 |
|
| 279 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 280 |
$userid = $patron->userid; |
| 281 |
|
| 282 |
$t->delete_ok("//$userid:$password@/api/v1/items/$itemnumber/transfers") |
| 283 |
->status_is( 204, 'Transfer deleted successfully' ); |
| 284 |
|
| 285 |
is( $item->transfer, undef, 'Item transfer is removed' ); |
| 286 |
|
| 287 |
$schema->storage->txn_rollback; |
| 288 |
}; |