From 0283b5a4853a672e5b684db80ac01f5a3a6de79a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 2 Jul 2024 10:12:35 -0300 Subject: [PATCH] Bug 36641: Make tests cover all code paths Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Katrin Fischer --- Koha/REST/V1/CirculationRules.pm | 6 +-- t/db_dependent/api/v1/circulation_rules.t | 54 +++++++++++++++++++++-- t/db_dependent/api/v1/responses.t | 2 +- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/Koha/REST/V1/CirculationRules.pm b/Koha/REST/V1/CirculationRules.pm index 0a112ff03c..45d3038ca3 100644 --- a/Koha/REST/V1/CirculationRules.pm +++ b/Koha/REST/V1/CirculationRules.pm @@ -58,7 +58,7 @@ sub list_effective_rules { my $type = Koha::ItemTypes->find($item_type); return $c->render_invalid_parameter_value( { - path => '/query/item_type', + path => '/query/item_type_id', values => { uri => '/api/v1/item_types', field => 'item_type_id' @@ -71,7 +71,7 @@ sub list_effective_rules { my $library = Koha::Libraries->find($branchcode); return $c->render_invalid_parameter_value( { - path => '/query/library', + path => '/query/library_id', values => { uri => '/api/v1/libraries', field => 'library_id' @@ -84,7 +84,7 @@ sub list_effective_rules { my $category = Koha::Patron::Categories->find($patron_category); return $c->render_invalid_parameter_value( { - path => '/query/patron_category', + path => '/query/patron_category_id', values => { uri => '/api/v1/patron_categories', field => 'patron_category_id' diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t index 1c5e0a04ac..a24cd68949 100755 --- a/t/db_dependent/api/v1/circulation_rules.t +++ b/t/db_dependent/api/v1/circulation_rules.t @@ -34,7 +34,7 @@ t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); subtest 'list_effective_rules() tests' => sub { - plan tests => 26; + plan tests => 32; $schema->storage->txn_begin; @@ -140,19 +140,65 @@ subtest 'list_effective_rules() tests' => sub { $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400) ->json_is( [ { path => '/query/rules_blah', message => 'Malformed query string' } ] ); + # Make sure we have a non-existent library + my $library_to_delete = $builder->build_object( { class => 'Koha::Libraries' } ); + my $non_existent_library = $library_to_delete->branchcode; + $library_to_delete->delete; + # Warn on incorrect query parameter value - $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=SMITH")->status_is(400)->json_is( + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$non_existent_library")->status_is(400) + ->json_is( '' => { error => 'Invalid parameter value', error_code => 'invalid_parameter_value', - path => '/query/library', + path => '/query/library_id', values => { uri => '/api/v1/libraries', field => 'library_id' } }, "Invalid parameter value handled correctly" - ); + ); + + # Make sure we have a non-existent category + my $category_to_delete = $builder->build_object( { class => 'Koha::Patron::Categories' } ); + my $non_existent_category = $category_to_delete->categorycode; + $category_to_delete->delete; + + # Warn on incorrect query parameter value + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?patron_category_id=$non_existent_category") + ->status_is(400)->json_is( + '' => { + error => 'Invalid parameter value', + error_code => 'invalid_parameter_value', + path => '/query/patron_category_id', + values => { + uri => '/api/v1/patron_categories', + field => 'patron_category_id' + } + }, + "Invalid parameter value handled correctly" + ); + + # Make sure we have a non-existent itemtype + my $itemtype_to_delete = $builder->build_object( { class => 'Koha::ItemTypes' } ); + my $non_existent_itemtype = $itemtype_to_delete->itemtype; + $itemtype_to_delete->delete; + + # Warn on incorrect query parameter value + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?item_type_id=$non_existent_itemtype")->status_is(400) + ->json_is( + '' => { + error => 'Invalid parameter value', + error_code => 'invalid_parameter_value', + path => '/query/item_type_id', + values => { + uri => '/api/v1/item_types', + field => 'item_type_id' + } + }, + "Invalid parameter value handled correctly" + ); # Unauthorized access $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403); diff --git a/t/db_dependent/api/v1/responses.t b/t/db_dependent/api/v1/responses.t index 575fc33a0c..8404ec86fc 100755 --- a/t/db_dependent/api/v1/responses.t +++ b/t/db_dependent/api/v1/responses.t @@ -154,7 +154,7 @@ subtest 'render_invalid_parameter_value() tests' => sub { my $t = Test::Mojo->new('Koha::REST::V1'); - $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library=SOMETHING")->status_is('400')->json_is( + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=SOMETHING")->status_is('400')->json_is( { error => 'Invalid parameter value', error_code => 'invalid_parameter_value', -- 2.39.5