Bug 29508: Regression tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 17 Nov 2021 19:35:33 +0000 (16:35 -0300)
committerFridolin Somers <fridolin.somers@biblibre.com>
Thu, 6 Jan 2022 00:03:28 +0000 (14:03 -1000)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
t/db_dependent/api/v1/patrons.t

index 7866042..b7e65dc 100755 (executable)
@@ -195,7 +195,8 @@ subtest 'list() tests' => sub {
 };
 
 subtest 'get() tests' => sub {
-    plan tests => 2;
+
+    plan tests => 3;
 
     $schema->storage->txn_begin;
     unauthorized_access_tests('GET', -1, undef);
@@ -227,6 +228,59 @@ subtest 'get() tests' => sub {
 
         $schema->storage->txn_rollback;
     };
+
+    subtest 'search_limited() tests' => sub {
+
+        plan tests => 12;
+
+        $schema->storage->txn_begin;
+
+        my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
+        my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
+        my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
+
+        my $patron_1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } });
+        my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_2->id } });
+        my $patron_3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_3->id } });
+
+        my @libraries_where_can_see_patrons = ($library_1->id, $library_2->id);
+
+        my $mocked_patron = Test::MockModule->new('Koha::Patron');
+        $mocked_patron->mock( 'libraries_where_can_see_patrons', sub
+            {
+                return @libraries_where_can_see_patrons;
+            }
+        );
+
+        my $librarian = $builder->build_object(
+            {   class => 'Koha::Patrons',
+                value => { flags => 2**4, branchcode => $library_3->id }    # borrowers flag = 4
+            }
+        );
+        my $password = 'thePassword123';
+        $librarian->set_password( { password => $password, skip_validation => 1 } );
+        my $userid = $librarian->userid;
+
+        $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron_1->id )
+          ->status_is(200)
+          ->json_is( '/patron_id' => $patron_1->id );
+
+        $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron_2->id )
+          ->status_is(200)
+          ->json_is( '/patron_id' => $patron_2->id );
+
+        @libraries_where_can_see_patrons = ($library_1->id);
+
+        $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron_1->id )
+          ->status_is(200)
+          ->json_is( '/patron_id' => $patron_1->id );
+
+        $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron_2->id )
+          ->status_is(404)
+          ->json_is({ error => "Patron not found." });
+
+        $schema->storage->txn_rollback;
+    };
 };
 
 subtest 'add() tests' => sub {