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 <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
(cherry picked from commit 25317416e6)
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
This commit is contained in:
Tomas Cohen Arazi 2022-05-03 11:20:46 -03:00 committed by Victor Grousset/tuxayo
parent 9794624aa4
commit b1404ce298
2 changed files with 55 additions and 1 deletions

View File

@ -53,6 +53,16 @@
required: true
schema:
$ref: ../definitions.json#/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:

View File

@ -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;
};