From 79b43bf03d9f2f36b1ac2f6d440610e384ba9285 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 10 May 2023 11:24:17 +0100 Subject: [PATCH] Bug 30979: Unit tests --- t/db_dependent/api/v1/checkouts.t | 132 +++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/api/v1/checkouts.t b/t/db_dependent/api/v1/checkouts.t index 2e749aa874..2be4d9b421 100755 --- a/t/db_dependent/api/v1/checkouts.t +++ b/t/db_dependent/api/v1/checkouts.t @@ -238,7 +238,7 @@ $schema->storage->txn_rollback; subtest 'get_availability' => sub { - plan tests => 28; + plan tests => 29; $schema->storage->txn_begin; my $librarian = $builder->build_object( @@ -340,13 +340,88 @@ subtest 'get_availability' => sub { ), 'Correct token' ); + %needsconfirmation = (); + + subtest 'public availability' => sub { + plan tests => 18; + + # Available, Not authentication required + $t->get_ok( +"/api/v1/public/checkouts/availability?item_id=$item1_id&patron_id=$patron_id" + )->status_is(200)->json_is( '/blockers' => {} ) + ->json_is( '/confirms' => {} )->json_is( '/warnings' => {} ) + ->json_is( '/confirmation_token' => undef ); + + # Needs confirmation upgrade to blocker + %needsconfirmation = ( TOO_MANY => 1, ISSUED_TO_ANOTHER => 1 ); + $t->get_ok( +"/api/v1/public/checkouts/availability?item_id=$item1_id&patron_id=$patron_id" + )->status_is(200) + ->json_is( '/blockers' => { TOO_MANY => 1, ISSUED_TO_ANOTHER => 1 } ) + ->json_is( '/confirms' => {} )->json_is( '/warnings' => {} ) + ->json_is( '/confirmation_token' => undef ); + %needsconfirmation = (); + + # Remove personal information from public endpoint + %issuingimpossible = ( + issued_borrowernumber => 'private', + issued_cardnumber => 'private', + issued_firstname => 'private', + issued_surname => 'private', + resborrowernumber => 'private', + resbranchcode => 'private', + rescardnumber => 'private', + reserve_id => 'private', + resfirstname => 'private', + resreservedate => 'private', + ressurname => 'private', + item_notforloan => 'private' + ); + %alerts = ( + issued_borrowernumber => 'private', + issued_cardnumber => 'private', + issued_firstname => 'private', + issued_surname => 'private', + resborrowernumber => 'private', + resbranchcode => 'private', + rescardnumber => 'private', + reserve_id => 'private', + resfirstname => 'private', + resreservedate => 'private', + ressurname => 'private', + item_notforloan => 'private' + ); + + %needsconfirmation = ( + issued_borrowernumber => 'private', + issued_cardnumber => 'private', + issued_firstname => 'private', + issued_surname => 'private', + resborrowernumber => 'private', + resbranchcode => 'private', + rescardnumber => 'private', + reserve_id => 'private', + resfirstname => 'private', + resreservedate => 'private', + ressurname => 'private', + item_notforloan => 'private' + ); + $t->get_ok( +"/api/v1/public/checkouts/availability?item_id=$item1_id&patron_id=$patron_id" + )->status_is(200)->json_is( '/blockers' => {} ) + ->json_is( '/confirms' => {} )->json_is( '/warnings' => {} ) + ->json_is( '/confirmation_token' => undef ); + %issuingimpossible = (); + %alerts = (); + %needsconfirmation = (); + }; $schema->storage->txn_rollback; }; subtest 'add checkout' => sub { - plan tests => 9; + plan tests => 10; $schema->storage->txn_begin; my $librarian = $builder->build_object( @@ -426,5 +501,58 @@ subtest 'add checkout' => sub { )->status_is(201)->or(sub { diag $t->tx->res->body }); %needsconfirmation = (); + subtest 'public add' => sub { + plan tests => 14; + + my $useridp = $patron->userid; + $patron->set_password( + { password => $password, skip_validation => 1 } ); + + # Feature disabled + t::lib::Mocks::mock_preference( 'OpacTrustedCheckout', 0 ); + + $t->post_ok( "/api/v1/public/patrons/$patron_id/checkouts" => json => + { item_id => $item1_id, patron_id => $patron_id } ) + ->status_is(401)->json_is( + { + error => "Authentication failure." + } + ); + + $t->post_ok( + "//$useridp:$password@/api/v1/public/patrons/$patron_id/checkouts" + => json => { item_id => $item1_id, patron_id => $patron_id } ) + ->status_is(405) + ->json_is( + { error => "Feature disabled", error_code => "FEATURE_DISABLED" } ); + + # Feature enabled + t::lib::Mocks::mock_preference( 'OpacTrustedCheckout', 1 ); + + $t->post_ok( "/api/v1/public/patrons/$patron_id/checkouts" => json => + { item_id => $item1_id, patron_id => $patron_id } ) + ->status_is(401)->json_is( + { + error => "Authentication failure." + } + ); + + $t->post_ok( + "//$userid:$password@/api/v1/public/patrons/$patron_id/checkouts" + => json => { item_id => $item1_id, patron_id => $patron_id } ) + ->status_is(403)->json_is( + { + error => + "Authorization failure. Missing required permission(s).", + required_permissions => undef + } + ); + + $t->post_ok( + "//$useridp:$password@/api/v1/public/patrons/$patron_id/checkouts" + => json => { item_id => $item1_id, patron_id => $patron_id } ) + ->status_is(201); + }; + $schema->storage->txn_rollback; }; -- 2.40.1