From 1a5d559e44cb1617db35fae53f789ca9ca486dba Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 3 May 2022 11:20:46 -0300 Subject: [PATCH] [21.11.x] Bug 30663: Add x-koha-override options to /suggestions This patch adds the x-koha-override header parameter to the route that is used to create suggestions, POST /suggestions. The idea is that adding suggestions will be rejected under certain conditions unless x-koha-override is passed with appropriate values. The added overrides are: * any * max_total * max_pending Tests are added for the expected behavior. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Lucas Gass Signed-off-by: Martin Renvoize --- api/v1/swagger/paths/suggestions.yaml | 10 ++++++ t/db_dependent/api/v1/suggestions.t | 46 ++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/api/v1/swagger/paths/suggestions.yaml b/api/v1/swagger/paths/suggestions.yaml index e14ddb78ab..b1a8a9e258 100644 --- a/api/v1/swagger/paths/suggestions.yaml +++ b/api/v1/swagger/paths/suggestions.yaml @@ -53,6 +53,16 @@ required: true schema: $ref: ../definitions.yaml#/suggestion + - name: x-koha-override + in: header + required: false + description: | + Overrides list sent as a request header + Valid values: + * any + * max_total + * max_pending + type: string produces: - application/json responses: diff --git a/t/db_dependent/api/v1/suggestions.t b/t/db_dependent/api/v1/suggestions.t index 6ec1010003..9ad51e27bc 100755 --- a/t/db_dependent/api/v1/suggestions.t +++ b/t/db_dependent/api/v1/suggestions.t @@ -149,7 +149,7 @@ subtest 'get() tests' => sub { subtest 'add() tests' => sub { - plan tests => 15; + plan tests => 16; $schema->storage->txn_begin; @@ -237,6 +237,50 @@ subtest 'add() tests' => sub { ] ); + subtest 'x-koha-override tests' => sub { + + plan tests => 14; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + t::lib::Mocks::mock_preference( 'MaxTotalSuggestions', 4 ); + t::lib::Mocks::mock_preference( 'MaxOpenSuggestions', 2 ); + t::lib::Mocks::mock_preference( 'NumberOfSuggestionDays', 2 ); + + my $suggestion = $builder->build_object( + { class => 'Koha::Suggestions', + value => { suggestedby => $patron->id, STATUS => 'ACCEPTED' } + } + ); + + my $suggestion_data = $suggestion->to_api; + delete $suggestion_data->{suggestion_id}; + delete $suggestion_data->{status}; + + $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data ) + ->status_is( 201, 'First pending suggestion' ); + + $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data ) + ->status_is( 201, 'Second pending suggestion' ); + + $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data ) + ->status_is(400) + ->json_is( '/error_code' => 'max_pending_reached' ); + + $t->post_ok( "//$userid:$password@/api/v1/suggestions" + => { 'x-koha-override' => 'max_pending' } + => json => $suggestion_data ) + ->status_is( 201, 'max_pending override does the job' ); + + $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data ) + ->status_is(400) + ->json_is( '/error_code' => 'max_total_reached' ); + + $t->post_ok( + "//$userid:$password@/api/v1/suggestions" => { 'x-koha-override' => 'any' } => json => $suggestion_data ) + ->status_is( 201, 'any overrides anything' ); + }; + $schema->storage->txn_rollback; }; -- 2.34.1