From 94c2afdda2846cde68164ecedbaeba566fa88d47 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Tue, 2 Feb 2021 11:18:27 -0300
Subject: [PATCH] Bug 27593: Regression tests

This patch introduces tests for the behaviour change (400 => 404) and
also adds tests for untested (error) behaviours. Notably, the 'Biblio
not found' problematic return value couldn't be triggered, because the
OpenAPI plugin returns a 500 because the response was malformed
(expected { error => 'message' } and it was returning a string).

For the above reason, running the regression tests will show a 500...
instead of the expected 400.

I covered the rest of the codepaths that trigger errors to have full
coverage.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/clubs_holds.t
=> FAIL: Tests fail loudly

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>
---
 t/db_dependent/api/v1/clubs_holds.t | 59 +++++++++++++++++++++++++++++++++++--
 1 file changed, 57 insertions(+), 2 deletions(-)

diff --git a/t/db_dependent/api/v1/clubs_holds.t b/t/db_dependent/api/v1/clubs_holds.t
index e6cfde9729..49c56e3a5f 100755
--- a/t/db_dependent/api/v1/clubs_holds.t
+++ b/t/db_dependent/api/v1/clubs_holds.t
@@ -61,7 +61,7 @@ subtest 'add() tests' => sub {
 
     subtest 'librarian access tests' => sub {
 
-        plan tests => 8;
+        plan tests => 20;
 
         $schema->storage->txn_begin;
 
@@ -71,13 +71,68 @@ subtest 'add() tests' => sub {
         my $librarian = $builder->build_object(
             {
                 class => 'Koha::Patrons',
-                value => { flags => 2**6 }    # reserveforothers flag = 6
+                value => { flags => 2 ** 6 }    # reserveforothers flag = 6
             }
         );
         my $password = 'thePassword123';
         $librarian->set_password( { password => $password, skip_validation => 1 } );
         my $userid = $librarian->userid;
 
+        my $non_existent_item   = $builder->build_sample_item;
+        my $non_existent_biblio = $non_existent_item->biblio;
+
+        my $non_existent_item_id   = $non_existent_item->id;
+        my $non_existent_biblio_id = $non_existent_biblio->id;
+        my $non_existent_item_homebranch =
+          $non_existent_item->home_branch->branchcode;
+
+        $non_existent_item->delete;
+        $non_existent_biblio->delete;
+
+        my $biblio = $builder->build_sample_biblio;
+
+        $t->post_ok(
+                "//$userid:$password@/api/v1/clubs/"
+              . $club_with_enrollments->id
+              . "/holds" => json => {
+                biblio_id         => $biblio->id,
+                item_id           => $item->id,
+                pickup_library_id => $item->home_branch->branchcode
+              }
+        )->status_is(400)
+          ->json_is( '/error' => "Item "
+              . $item->id
+              . " doesn't belong to biblio "
+              . $biblio->id );
+
+        $t->post_ok(
+                "//$userid:$password@/api/v1/clubs/"
+              . $club_with_enrollments->id
+              . "/holds" => json => {
+                pickup_library_id => $non_existent_item_homebranch
+              }
+        )->status_is(400)
+          ->json_is(
+            '/error' => 'At least one of biblio_id, item_id should be given' );
+
+        $t->post_ok(
+                "//$userid:$password@/api/v1/clubs/"
+              . $club_with_enrollments->id
+              . "/holds" => json => {
+                biblio_id         => $non_existent_biblio_id,
+                pickup_library_id => $non_existent_item_homebranch
+              }
+        )->status_is(404)->json_is( '/error' => 'Biblio not found' );
+
+        $t->post_ok(
+                "//$userid:$password@/api/v1/clubs/"
+              . $club_with_enrollments->id
+              . "/holds" => json => {
+                item_id           => $non_existent_item_id,
+                pickup_library_id => $non_existent_item_homebranch
+              }
+        )->status_is(404)->json_is( '/error' => 'Item not found' );
+
         my $data = {
             biblio_id         => $item->biblionumber,
             pickup_library_id => $item->home_branch->branchcode
-- 
2.11.0