From 9fac64d4b80f12dba3362624c77e904a01b42a9f Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Tue, 3 May 2022 11:20:46 -0300
Subject: [PATCH] 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>
---
 api/v1/swagger/paths/suggestions.yaml | 18 ++++++++++-
 t/db_dependent/api/v1/suggestions.t   | 46 ++++++++++++++++++++++++++-
 2 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/api/v1/swagger/paths/suggestions.yaml b/api/v1/swagger/paths/suggestions.yaml
index 7a0650a328..5438188591 100644
--- a/api/v1/swagger/paths/suggestions.yaml
+++ b/api/v1/swagger/paths/suggestions.yaml
@@ -57,6 +57,18 @@
         required: true
         schema:
           $ref: "../swagger.yaml#/definitions/suggestion"
+      - name: x-koha-override
+        in: header
+        required: false
+        description: Overrides list sent as a request header
+        type: array
+        items:
+          type: string
+          enum:
+            - any
+            - max_total
+            - max_pending
+        collectionFormat: csv
     produces:
       - application/json
     responses:
@@ -65,7 +77,11 @@
         schema:
           $ref: "../swagger.yaml#/definitions/suggestion"
       "400":
-        description: Bad request
+        description: |
+          Bad request. Possible `error_code` attribute values:
+
+          * `max_total_reached`
+          * `max_pending_reached`
         schema:
           $ref: "../swagger.yaml#/definitions/error"
       "403":
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.20.1