From 35db7c78cca1f5ab433ef095eb3cd5c0796b0bba Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 23 Oct 2025 12:23:31 +0100 Subject: [PATCH] Bug 23415: Add API tests for fine override on renewal This commit adds comprehensive REST API tests to verify the x-koha-override: debt_limit functionality for renewals. Tests cover: - Renewal blocked due to excessive fines - Override fails when AllowFineOverrideRenewing is disabled - Override succeeds when AllowFineOverrideRenewing is enabled - Both /renewal and /renewals endpoints Test plan: 1. Run: prove t/db_dependent/api/v1/checkouts.t 2. All 110 tests should pass --- t/db_dependent/api/v1/checkouts.t | 104 +++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/checkouts.t b/t/db_dependent/api/v1/checkouts.t index 7a00bd50ed6..85577c75c33 100755 --- a/t/db_dependent/api/v1/checkouts.t +++ b/t/db_dependent/api/v1/checkouts.t @@ -18,7 +18,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 109; +use Test::More tests => 110; use Test::MockModule; use Test::Mojo; use t::lib::Mocks; @@ -527,3 +527,105 @@ subtest 'add checkout' => sub { $schema->storage->txn_rollback; }; + +subtest 'renew with fine override (x-koha-override: debt_limit)' => sub { + plan tests => 15; + + $schema->storage->txn_begin; + + # Create a librarian with circulate permission + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2 } # Superlibrarian + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + # Create a regular patron + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + # Create a branch and set up userenv + my $branch = $builder->build( { source => 'Branch' } ); + t::lib::Mocks::mock_userenv( + { branchcode => $branch->{branchcode}, borrowernumber => $librarian->borrowernumber } ); + + # Set up circulation rules + Koha::CirculationRules->set_rules( + { + categorycode => undef, + itemtype => undef, + branchcode => undef, + rules => { + renewalperiod => 7, + renewalsallowed => 10, + issuelength => 5, + } + } + ); + + my $item = $builder->build_sample_item; + + # Check out the item + my $checkout = AddIssue( $patron, $item->barcode ); + + # Set FineNoRenewals to a low amount + t::lib::Mocks::mock_preference( 'FineNoRenewals', 5 ); + + # Add a fine that exceeds the limit + my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } ); + $account->add_debit( + { + amount => 10.00, + type => 'OVERDUE', + interface => 'test', + description => 'Test fine', + item_id => $item->itemnumber, + } + ); + + my $checkout_id = $checkout->id; + + # Test 1: Renewal should be blocked due to excessive fines + $t->post_ok("//$userid:$password@/api/v1/checkouts/$checkout_id/renewal")->status_is(403) + ->json_like( '/error' => qr/too_much_owing/ ); + + # Test 2: Override should fail when AllowFineOverrideRenewing is disabled + t::lib::Mocks::mock_preference( 'AllowFineOverrideRenewing', 0 ); + + $t->post_ok( "//$userid:$password@/api/v1/checkouts/$checkout_id/renewal" => { 'x-koha-override' => 'debt_limit' } ) + ->status_is(403)->json_like( '/error' => qr/too_much_owing/ ); + + # Test 3: Override should succeed when AllowFineOverrideRenewing is enabled + t::lib::Mocks::mock_preference( 'AllowFineOverrideRenewing', 1 ); + + $t->post_ok( "//$userid:$password@/api/v1/checkouts/$checkout_id/renewal" => { 'x-koha-override' => 'debt_limit' } ) + ->status_is(201)->json_has('/due_date'); + + # Refresh checkout + $checkout = Koha::Checkouts->find($checkout_id); + + # Test 4: Test with /renewals endpoint (alternative endpoint) + # Add another checkout to test the other endpoint + my $item2 = $builder->build_sample_item; + my $checkout2 = AddIssue( $patron, $item2->barcode ); + my $checkout2_id = $checkout2->id; + + # Should be blocked without override + $t->post_ok("//$userid:$password@/api/v1/checkouts/$checkout2_id/renewals")->status_is(403) + ->json_like( '/error' => qr/too_much_owing/ ); + + # Should succeed with override + $t->post_ok( + "//$userid:$password@/api/v1/checkouts/$checkout2_id/renewals" => { 'x-koha-override' => 'debt_limit' } ) + ->status_is(201)->json_has('/due_date'); + + $schema->storage->txn_rollback; +}; -- 2.51.0