View | Details | Raw Unified | Return to bug 23415
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/checkouts.t (-2 / +103 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 111;
21
use Test::More tests => 112;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Mojo;
23
use Test::Mojo;
24
use t::lib::Mocks;
24
use t::lib::Mocks;
Lines 599-601 subtest 'add checkout' => sub { Link Here
599
599
600
    $schema->storage->txn_rollback;
600
    $schema->storage->txn_rollback;
601
};
601
};
602
- 
602
603
subtest 'renew with fine override (x-koha-override: debt_limit)' => sub {
604
    plan tests => 15;
605
606
    $schema->storage->txn_begin;
607
608
    # Create a librarian with circulate permission
609
    my $librarian = $builder->build_object(
610
        {
611
            class => 'Koha::Patrons',
612
            value => { flags => 2 }     # Superlibrarian
613
        }
614
    );
615
    my $password = 'thePassword123';
616
    $librarian->set_password( { password => $password, skip_validation => 1 } );
617
    my $userid = $librarian->userid;
618
619
    # Create a regular patron
620
    my $patron = $builder->build_object(
621
        {
622
            class => 'Koha::Patrons',
623
            value => { flags => 0 }
624
        }
625
    );
626
627
    # Create a branch and set up userenv
628
    my $branch = $builder->build( { source => 'Branch' } );
629
    t::lib::Mocks::mock_userenv(
630
        { branchcode => $branch->{branchcode}, borrowernumber => $librarian->borrowernumber } );
631
632
    # Set up circulation rules
633
    Koha::CirculationRules->set_rules(
634
        {
635
            categorycode => undef,
636
            itemtype     => undef,
637
            branchcode   => undef,
638
            rules        => {
639
                renewalperiod   => 7,
640
                renewalsallowed => 10,
641
                issuelength     => 5,
642
            }
643
        }
644
    );
645
646
    my $item = $builder->build_sample_item;
647
648
    # Check out the item
649
    my $checkout = AddIssue( $patron, $item->barcode );
650
651
    # Set FineNoRenewals to a low amount
652
    t::lib::Mocks::mock_preference( 'FineNoRenewals', 5 );
653
654
    # Add a fine that exceeds the limit
655
    my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
656
    $account->add_debit(
657
        {
658
            amount      => 10.00,
659
            type        => 'OVERDUE',
660
            interface   => 'test',
661
            description => 'Test fine',
662
            item_id     => $item->itemnumber,
663
        }
664
    );
665
666
    my $checkout_id = $checkout->id;
667
668
    # Test 1: Renewal should be blocked due to excessive fines
669
    $t->post_ok("//$userid:$password@/api/v1/checkouts/$checkout_id/renewal")->status_is(403)
670
        ->json_like( '/error' => qr/too_much_owing/ );
671
672
    # Test 2: Override should fail when AllowFineOverrideRenewing is disabled
673
    t::lib::Mocks::mock_preference( 'AllowFineOverrideRenewing', 0 );
674
675
    $t->post_ok( "//$userid:$password@/api/v1/checkouts/$checkout_id/renewal" => { 'x-koha-override' => 'debt_limit' } )
676
        ->status_is(403)->json_like( '/error' => qr/too_much_owing/ );
677
678
    # Test 3: Override should succeed when AllowFineOverrideRenewing is enabled
679
    t::lib::Mocks::mock_preference( 'AllowFineOverrideRenewing', 1 );
680
681
    $t->post_ok( "//$userid:$password@/api/v1/checkouts/$checkout_id/renewal" => { 'x-koha-override' => 'debt_limit' } )
682
        ->status_is(201)->json_has('/due_date');
683
684
    # Refresh checkout
685
    $checkout = Koha::Checkouts->find($checkout_id);
686
687
    # Test 4: Test with /renewals endpoint (alternative endpoint)
688
    # Add another checkout to test the other endpoint
689
    my $item2        = $builder->build_sample_item;
690
    my $checkout2    = AddIssue( $patron, $item2->barcode );
691
    my $checkout2_id = $checkout2->id;
692
693
    # Should be blocked without override
694
    $t->post_ok("//$userid:$password@/api/v1/checkouts/$checkout2_id/renewals")->status_is(403)
695
        ->json_like( '/error' => qr/too_much_owing/ );
696
697
    # Should succeed with override
698
    $t->post_ok(
699
        "//$userid:$password@/api/v1/checkouts/$checkout2_id/renewals" => { 'x-koha-override' => 'debt_limit' } )
700
        ->status_is(201)->json_has('/due_date');
701
702
    $schema->storage->txn_rollback;
703
};

Return to bug 23415