Browse Source

Bug 14708: (QA follow-up) Add tests

This patch adds tests for the change. It also simplifies the delete()
method structure a bit. It fixes the error 500 the tests were raising.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> FAIL: Tests fail!
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests pass! The new behaviour (code 403) is tested!
4. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
20.11.x
Tomás Cohen Arazi 3 years ago
committed by Jonathan Druart
parent
commit
b092bdd20d
  1. 47
      Koha/REST/V1/Patrons.pm
  2. 8
      t/db_dependent/api/v1/patrons.t

47
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($_);
};
}

8
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');

Loading…
Cancel
Save