Bug 20006: Unit tests fixes
This patch makes the holds endpoint tests pass and expect data structures that match the voted RFC. As we voted to introduce a /public namespace for unprivileged access to endpoints, this endpoint gets the ability for owners and guarantors to manipulate holds through the API. Signed-off-by: Josef Moravec <josef.moravec@gmail.com> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
This commit is contained in:
parent
62f1b2df68
commit
52bb9802dd
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
use Modern::Perl;
|
||||
|
||||
use Test::More tests => 5;
|
||||
use Test::More tests => 4;
|
||||
use Test::Mojo;
|
||||
use t::lib::TestBuilder;
|
||||
use t::lib::Mocks;
|
||||
|
|
@ -149,19 +149,20 @@ my $reserve_id = C4::Reserves::AddReserve($branchcode, $patron_1->borrowernumber
|
|||
my $reserve_id2 = C4::Reserves::AddReserve($branchcode, $patron_2->borrowernumber,
|
||||
$biblionumber, undef, 2, undef, undef, undef, '', $itemnumber);
|
||||
|
||||
my $suspend_until = DateTime->now->add(days => 10)->ymd;
|
||||
my $expirationdate = DateTime->now->add(days => 10)->ymd;
|
||||
my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
|
||||
my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
|
||||
|
||||
my $post_data = {
|
||||
borrowernumber => int($patron_1->borrowernumber),
|
||||
biblionumber => int($biblionumber),
|
||||
itemnumber => int($itemnumber),
|
||||
branchcode => $branchcode,
|
||||
expirationdate => $expirationdate,
|
||||
patron_id => int($patron_1->borrowernumber),
|
||||
biblio_id => int($biblionumber),
|
||||
item_id => int($itemnumber),
|
||||
pickup_library_id => $branchcode,
|
||||
expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
|
||||
priority => 2,
|
||||
};
|
||||
my $put_data = {
|
||||
priority => 2,
|
||||
suspend_until => $suspend_until,
|
||||
suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
|
||||
};
|
||||
|
||||
subtest "Test endpoints without authentication" => sub {
|
||||
|
|
@ -180,11 +181,11 @@ subtest "Test endpoints without authentication" => sub {
|
|||
subtest "Test endpoints without permission" => sub {
|
||||
plan tests => 10;
|
||||
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?patron_id=" . $patron_1->borrowernumber);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
|
||||
$t->request_ok($tx) # no permission
|
||||
->status_is(403);
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?patron_id=" . $patron_1->borrowernumber);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session3->id});
|
||||
$t->request_ok($tx) # reserveforothers permission
|
||||
->status_is(403);
|
||||
|
|
@ -201,47 +202,10 @@ subtest "Test endpoints without permission" => sub {
|
|||
$t->request_ok($tx) # no permission
|
||||
->status_is(403);
|
||||
};
|
||||
subtest "Test endpoints without permission, but accessing own object" => sub {
|
||||
plan tests => 16;
|
||||
|
||||
my $borrno_tmp = $post_data->{'borrowernumber'};
|
||||
$post_data->{'borrowernumber'} = int $nopermission->{'borrowernumber'};
|
||||
$tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
|
||||
$t->request_ok($tx) # create hold to myself
|
||||
->status_is(201)
|
||||
->json_has('/reserve_id');
|
||||
|
||||
$post_data->{'borrowernumber'} = $borrno_tmp;
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=".$nopermission-> { borrowernumber });
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
|
||||
$t->request_ok($tx) # get my own holds
|
||||
->status_is(200)
|
||||
->json_is('/0/borrowernumber', $nopermission->{ borrowernumber })
|
||||
->json_is('/0/biblionumber', $biblionumber)
|
||||
->json_is('/0/itemnumber', $itemnumber)
|
||||
->json_is('/0/expirationdate', $expirationdate)
|
||||
->json_is('/0/branchcode', $branchcode);
|
||||
|
||||
my $reserve_id3 = Koha::Holds->find({ borrowernumber => $nopermission->{borrowernumber} })->reserve_id;
|
||||
$tx = $t->ua->build_tx(PUT => "/api/v1/holds/$reserve_id3" => json => $put_data);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
|
||||
$t->request_ok($tx) # create hold to myself
|
||||
->status_is(200)->json_is( '/reserve_id', $reserve_id3 )->json_is(
|
||||
'/suspend_until',
|
||||
output_pref(
|
||||
{
|
||||
dateformat => 'rfc3339',
|
||||
dt => dt_from_string( $suspend_until . ' 00:00:00', 'sql' )
|
||||
}
|
||||
)
|
||||
)
|
||||
->json_is( '/priority', 2 )
|
||||
->json_is( '/itemnumber', $itemnumber );
|
||||
};
|
||||
|
||||
subtest "Test endpoints with permission" => sub {
|
||||
plan tests => 45;
|
||||
|
||||
plan tests => 44;
|
||||
|
||||
$tx = $t->ua->build_tx(GET => '/api/v1/holds');
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
|
||||
|
|
@ -249,28 +213,21 @@ subtest "Test endpoints with permission" => sub {
|
|||
->status_is(200)
|
||||
->json_has('/0')
|
||||
->json_has('/1')
|
||||
->json_has('/2')
|
||||
->json_hasnt('/3');
|
||||
->json_hasnt('/2');
|
||||
|
||||
$tx = $t->ua->build_tx(GET => '/api/v1/holds?priority=2');
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(200)
|
||||
->json_is('/0/borrowernumber', $nopermission->{borrowernumber})
|
||||
->json_is('/0/patron_id', $patron_2->borrowernumber)
|
||||
->json_hasnt('/1');
|
||||
|
||||
$tx = $t->ua->build_tx(PUT => "/api/v1/holds/$reserve_id" => json => $put_data);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session3->id});
|
||||
$t->request_ok($tx)->status_is(200)->json_is( '/reserve_id', $reserve_id )
|
||||
->json_is(
|
||||
'/suspend_until',
|
||||
output_pref(
|
||||
{
|
||||
dateformat => 'rfc3339',
|
||||
dt => dt_from_string( $suspend_until . ' 00:00:00', 'sql' )
|
||||
}
|
||||
)
|
||||
)
|
||||
$t->request_ok($tx)
|
||||
->status_is(200)
|
||||
->json_is( '/hold_id', $reserve_id )
|
||||
->json_is( '/suspended_until', output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }) )
|
||||
->json_is( '/priority', 2 );
|
||||
|
||||
$tx = $t->ua->build_tx(DELETE => "/api/v1/holds/$reserve_id");
|
||||
|
|
@ -290,14 +247,14 @@ subtest "Test endpoints with permission" => sub {
|
|||
->status_is(404)
|
||||
->json_has('/error');
|
||||
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?patron_id=" . $patron_1->borrowernumber);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session2->id}); # get with borrowers flag
|
||||
$t->request_ok($tx)
|
||||
->status_is(200)
|
||||
->json_is([]);
|
||||
|
||||
my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=$inexisting_borrowernumber");
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?patron_id=$inexisting_borrowernumber");
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(200)
|
||||
|
|
@ -312,16 +269,16 @@ subtest "Test endpoints with permission" => sub {
|
|||
$tx->req->cookies({name => 'CGISESSID', value => $session3->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(201)
|
||||
->json_has('/reserve_id');
|
||||
$reserve_id = $t->tx->res->json->{reserve_id};
|
||||
->json_has('/hold_id');
|
||||
$reserve_id = $t->tx->res->json->{hold_id};
|
||||
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?patron_id=" . $patron_1->borrowernumber);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(200)
|
||||
->json_is('/0/reserve_id', $reserve_id)
|
||||
->json_is('/0/expirationdate', $expirationdate)
|
||||
->json_is('/0/branchcode', $branchcode);
|
||||
->json_is('/0/hold_id', $reserve_id)
|
||||
->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
|
||||
->json_is('/0/pickup_library_id', $branchcode);
|
||||
|
||||
$tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session3->id});
|
||||
|
|
@ -335,18 +292,17 @@ subtest "Test endpoints with permission" => sub {
|
|||
$tx->req->cookies({name => 'CGISESSID', value => $session3->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(403)
|
||||
->json_like('/error', qr/tooManyReserves/);
|
||||
->json_like('/error', qr/itemAlreadyOnHold/);
|
||||
};
|
||||
|
||||
|
||||
subtest 'Reserves with itemtype' => sub {
|
||||
plan tests => 9;
|
||||
|
||||
my $post_data = {
|
||||
borrowernumber => int($patron_1->borrowernumber),
|
||||
biblionumber => int($biblionumber),
|
||||
branchcode => $branchcode,
|
||||
itemtype => $itemtype,
|
||||
patron_id => int($patron_1->borrowernumber),
|
||||
biblio_id => int($biblionumber),
|
||||
pickup_library_id => $branchcode,
|
||||
item_type => $itemtype,
|
||||
};
|
||||
|
||||
$tx = $t->ua->build_tx(DELETE => "/api/v1/holds/$reserve_id");
|
||||
|
|
@ -358,16 +314,16 @@ subtest 'Reserves with itemtype' => sub {
|
|||
$tx->req->cookies({name => 'CGISESSID', value => $session3->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(201)
|
||||
->json_has('/reserve_id');
|
||||
->json_has('/hold_id');
|
||||
|
||||
$reserve_id = $t->tx->res->json->{reserve_id};
|
||||
$reserve_id = $t->tx->res->json->{hold_id};
|
||||
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
|
||||
$tx = $t->ua->build_tx(GET => "/api/v1/holds?patron_id=" . $patron_1->borrowernumber);
|
||||
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
|
||||
$t->request_ok($tx)
|
||||
->status_is(200)
|
||||
->json_is('/0/reserve_id', $reserve_id)
|
||||
->json_is('/0/itemtype', $itemtype);
|
||||
->json_is('/0/hold_id', $reserve_id)
|
||||
->json_is('/0/item_type', $itemtype);
|
||||
};
|
||||
|
||||
$schema->storage->txn_rollback;
|
||||
|
|
|
|||
Loading…
Reference in New Issue