From f91ae728bf1b7f21389328de97680a9ba1a0bc53 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 3 Nov 2020 11:06:58 -0300 Subject: [PATCH] Bug 26633: (QA follow-up) Tests on error condition This patch highlights a problem with the try/catch conditions in the ad() controlled method. Tests are added, and the controller is fixed to reflect the right behaviour. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/transfer_limits.t => SUCCESS: Tests pass! 409 is returned as appropriate! Signed-off-by: Tomas Cohen Arazi Signed-off-by: Katrin Fischer Signed-off-by: Jonathan Druart --- Koha/REST/V1/TransferLimits.pm | 6 +++--- t/db_dependent/api/v1/transfer_limits.t | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Koha/REST/V1/TransferLimits.pm b/Koha/REST/V1/TransferLimits.pm index c70baf7d8f..8ce8291192 100644 --- a/Koha/REST/V1/TransferLimits.pm +++ b/Koha/REST/V1/TransferLimits.pm @@ -72,7 +72,7 @@ sub add { if ( Koha::Item::Transfer::Limits->search( $transfer_limit->attributes_from_api($params) )->count == 0 ) { $transfer_limit->store; } else { - Koha::Exceptions::TransferLimit::Duplicate->throw() + Koha::Exceptions::TransferLimit::Duplicate->throw(); } return $c->render( @@ -81,10 +81,10 @@ sub add { ); } catch { - if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) { + if ( blessed $_ && $_->isa('Koha::Exceptions::TransferLimit::Duplicate') ) { return $c->render( status => 409, - openapi => { error => $_->error, conflict => $_->duplicate_id } + openapi => { error => "$_" } ); } diff --git a/t/db_dependent/api/v1/transfer_limits.t b/t/db_dependent/api/v1/transfer_limits.t index 35eec83baa..6e05c4e1a5 100755 --- a/t/db_dependent/api/v1/transfer_limits.t +++ b/t/db_dependent/api/v1/transfer_limits.t @@ -62,7 +62,7 @@ subtest 'list() tests' => sub { subtest 'add() tests' => sub { - plan tests => 8; + plan tests => 11; $schema->storage->txn_begin; @@ -109,6 +109,14 @@ subtest 'add() tests' => sub { ->status_is( 201, 'SWAGGER3.2.1' ) ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' ); + $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref ) + ->status_is( 409, 'Conflict creating the resource' ) + ->json_is( + { + error => "[A transfer limit with the given parameters already exists!]" + } + ); + $schema->storage->txn_rollback; }; -- 2.39.5