From 5820ac2a5d2fc643be7c293cb86b9e8eea8efd2b Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 28 Mar 2023 06:41:59 +0000 Subject: [PATCH] Bug 32426: (follow-up) Fix api/v1/patrons.t Content-Type: text/plain; charset=utf-8 We use another exception now. Test plan: Run t/db_dependent/api/v1/patrons.t Signed-off-by: Marcel de Rooy --- Koha/REST/V1/Patrons.pm | 12 ++++++++++++ t/db_dependent/api/v1/patrons.t | 22 +++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 576e4f8f2c..1852d7ccd2 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -147,6 +147,12 @@ sub add { openapi => { error => $_->error, conflict => $_->duplicate_id } ); } + elsif ( $_->isa('Koha::Exceptions::Patron::InvalidUserid') ) { + return $c->render( + status => 400, + openapi => { error => "Problem with ". $_->userid } + ); + } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { return $c->render( status => 400, @@ -282,6 +288,12 @@ sub update { openapi => { error => $_->error, conflict => $_->duplicate_id } ); } + elsif ( $_->isa('Koha::Exceptions::Patron::InvalidUserid') ) { + return $c->render( + status => 400, + openapi => { error => "Problem with ". $_->userid } + ); + } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { return $c->render( status => 400, diff --git a/t/db_dependent/api/v1/patrons.t b/t/db_dependent/api/v1/patrons.t index 172a04993a..8c4d9c13e1 100755 --- a/t/db_dependent/api/v1/patrons.t +++ b/t/db_dependent/api/v1/patrons.t @@ -302,7 +302,7 @@ subtest 'add() tests' => sub { $schema->storage->txn_rollback; subtest 'librarian access tests' => sub { - plan tests => 25; + plan tests => 24; $schema->storage->txn_begin; @@ -364,11 +364,9 @@ subtest 'add() tests' => sub { $newpatron->{library_id} = $deleted_library_id; - warning_like { - $t->post_ok("//$userid:$password@/api/v1/patrons" => json => $newpatron) - ->status_is(409) - ->json_is('/error' => "Duplicate ID"); } - qr/DBD::mysql::st execute failed: Duplicate entry/; + $t->post_ok("//$userid:$password@/api/v1/patrons" => json => $newpatron) + ->status_is(400) + ->json_is('/error' => "Problem with ". $newpatron->{userid} ); $newpatron->{library_id} = $patron->branchcode; @@ -418,6 +416,7 @@ subtest 'add() tests' => sub { ->json_is( '/date_of_birth' => $newpatron->{ date_of_birth }, 'Date field set (Bug 28585)' ) ->json_is( '/last_seen' => $newpatron->{ last_seen }, 'Date-time field set (Bug 28585)' ); + $newpatron->{userid} = undef; # force regeneration warning_like { $t->post_ok("//$userid:$password@/api/v1/patrons" => json => $newpatron) ->status_is(409) @@ -592,7 +591,7 @@ subtest 'update() tests' => sub { $schema->storage->txn_rollback; subtest 'librarian access tests' => sub { - plan tests => 45; + plan tests => 43; $schema->storage->txn_begin; @@ -677,12 +676,9 @@ subtest 'update() tests' => sub { $newpatron->{cardnumber} = $patron_1->cardnumber; $newpatron->{userid} = $patron_1->userid; - warning_like { - $t->put_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->borrowernumber => json => $newpatron ) - ->status_is(409) - ->json_has( '/error', "Fails when trying to update to an existing cardnumber or userid") - ->json_like( '/conflict' => qr/(borrowers\.)?cardnumber/ ); } - qr/DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key '(borrowers\.)?cardnumber'/; + $t->put_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->borrowernumber => json => $newpatron ) + ->status_is(400) + ->json_has( '/error', "Problem with userid ". $patron_1->userid ); $newpatron->{ cardnumber } = $patron_1->id . $patron_2->id; $newpatron->{ userid } = "user" . $patron_1->id.$patron_2->id; -- 2.30.2