From d6773a5d60dd4da9dc8aacdf2cfd19297afd70c9 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 89ebb5862b..8434708240 100755 --- a/t/db_dependent/api/v1/checkouts.t +++ b/t/db_dependent/api/v1/checkouts.t @@ -237,7 +237,7 @@ $schema->storage->txn_rollback; subtest 'get_availability' => sub { - plan tests => 27; + plan tests => 28; $schema->storage->txn_begin; my $librarian = $builder->build_object( @@ -326,13 +326,88 @@ subtest 'get_availability' => sub { ->json_is( '/confirms' => { confirm1 => 1, confirm2 => 'please' } ) ->json_is( '/warnings' => {} ) ->json_is( '/confirmation_token' => 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maXJtMSI6MSwiY29uZmlybTIiOjF9.4QBpITwnIGOAfohyKjaFDoeBWnGmQTdyJrPn9pavArw' ); + %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( @@ -406,5 +481,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