@@ -, +, @@ $ kshell k$ prove t/db_dependent/api/v1/patrons.t --- Koha/REST/V1/Patrons.pm | 47 +++++++++++++-------------------- t/db_dependent/api/v1/patrons.t | 8 +++++- 2 files changed, 26 insertions(+), 29 deletions(-) --- a/Koha/REST/V1/Patrons.pm +++ a/Koha/REST/V1/Patrons.pm @@ -304,40 +304,31 @@ Controller function that handles deleting a Koha::Patron object sub delete { my $c = shift->openapi->valid_input or return; - my $patron; + my $patron = Koha::Patrons->find( $c->validation->param('patron_id') ); + + unless ( $patron ) { + return $c->render( + status => 404, + openapi => { error => "Patron not found" } + ); + } return try { - $patron = Koha::Patrons->find( $c->validation->param('patron_id') ); - # check if loans, reservations, debarrment, etc. before deletion! - try { - $patron->delete; - return $c->render( - status => 204, - openapi => q{} - ); - } catch { - if ( $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) { - return $c->render( - status => 403, - openapi => { error => "Anonymous patron cannot be deleted" } - ); - } - else { - $c->unhandled_exception($_); - } - }; - } - catch { - unless ($patron) { + $patron->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } catch { + if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) { return $c->render( - status => 404, - openapi => { error => "Patron not found" } + status => 403, + openapi => { error => "Anonymous patron cannot be deleted" } ); } - else { - $c->unhandled_exception($_); - } + + $c->unhandled_exception($_); }; } --- a/t/db_dependent/api/v1/patrons.t +++ a/t/db_dependent/api/v1/patrons.t @@ -405,7 +405,7 @@ subtest 'delete() tests' => sub { $schema->storage->txn_rollback; subtest 'librarian access test' => sub { - plan tests => 5; + plan tests => 8; $schema->storage->txn_begin; @@ -425,6 +425,12 @@ subtest 'delete() tests' => sub { my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + t::lib::Mocks::mock_preference('AnonymousPatron', $patron->borrowernumber); + $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber) + ->status_is(403, 'Anonymous patron cannot be deleted') + ->json_is( { error => 'Anonymous patron cannot be deleted' } ); + + t::lib::Mocks::mock_preference('AnonymousPatron', 0); # back to default $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber) ->status_is(204, 'SWAGGER3.2.4') ->content_is('', 'SWAGGER3.3.4'); --