Bug 30982: Add tests and implement GET /background_jobs/$id
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 29 Jun 2022 09:28:31 +0000 (11:28 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 23 Sep 2022 11:57:48 +0000 (08:57 -0300)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/REST/V1/BackgroundJobs.pm
api/v1/swagger/paths/background_jobs.yaml
t/db_dependent/api/v1/background_jobs.t [new file with mode: 0755]

index e7deaa3..55f121b 100644 (file)
@@ -55,4 +55,40 @@ sub list {
 
 }
 
+sub get {
+    my $c = shift->openapi->valid_input or return;
+
+    return try {
+
+        my $background_job_id = $c->validation->param('background_job_id');
+        my $patron            = $c->stash('koha.user');
+
+        my $can_manage_background_jobs =
+          $patron->has_permission( { parameters => 'manage_background_jobs' } );
+
+        my $background_job = Koha::BackgroundJobs->find($background_job_id);
+
+        return $c->render(
+            status  => 404,
+            openapi => { error => "Object not found" }
+        ) unless $background_job;
+
+        return $c->render(
+            status  => 403,
+            openapi => { error => "Cannot see background job info" }
+          )
+          if !$can_manage_background_jobs
+          && $background_job->borrowernumber != $patron->borrowernumber;
+
+        return $c->render(
+            status  => 200,
+            openapi => $background_job->to_api
+        );
+    }
+    catch {
+        $c->unhandled_exception($_);
+    };
+}
+
+
 1;
index 320ef81..c386dfc 100644 (file)
         description: A background job
         schema:
           $ref: "../swagger.yaml#/definitions/background_job"
+      "403":
+        description: Access forbidden
+        schema:
+          $ref: "../swagger.yaml#/definitions/error"
       "404":
         description: Background job not found
         schema:
diff --git a/t/db_dependent/api/v1/background_jobs.t b/t/db_dependent/api/v1/background_jobs.t
new file mode 100755 (executable)
index 0000000..11181b1
--- /dev/null
@@ -0,0 +1,125 @@
+#!/usr/bin/env perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 25;
+use Test::Mojo;
+
+use t::lib::TestBuilder;
+use t::lib::Mocks;
+
+use Koha::BackgroundJobs;
+use Koha::Database;
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+my $t = Test::Mojo->new('Koha::REST::V1');
+#use t::lib::Mojo;
+#my $t = t::lib::Mojo->new('Koha::REST::V1');
+t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
+
+$schema->storage->txn_begin;
+
+Koha::BackgroundJobs->delete;
+my $superlibrarian = $builder->build_object(
+    {
+        class => 'Koha::Patrons',
+        value => { flags => 1 },
+    }
+);
+my $password = 'thePassword123';
+$superlibrarian->set_password( { password => $password, skip_validation => 1 } );
+my $superlibrarian_userid = $superlibrarian->userid;
+
+my $librarian = $builder->build_object(
+    {
+        class => 'Koha::Patrons',
+        value => { flags => 2 ** 2 }, # catalogue flag = 2
+    }
+);
+$librarian->set_password( { password => $password, skip_validation => 1 } );
+my $librarian_userid = $librarian->userid;
+
+my $patron = $builder->build_object(
+    {
+        class => 'Koha::Patrons',
+        value => { flags => 0 },
+    }
+);
+$patron->set_password( { password => $password, skip_validation => 1 } );
+my $patron_userid = $patron->userid;
+
+$t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
+  ->status_is(200)
+  ->json_is( [] );
+
+my $background_job = $builder->build_object(
+    {
+        class => 'Koha::BackgroundJobs',
+        value => {
+            status         => 'finished',
+            progress       => 100,
+            size           => 100,
+            borrowernumber => $patron->borrowernumber,
+            type           => 'batch_item_record_modification',
+            queue => 'default',
+            #data => '{"record_ids":["1"],"regex_mod":null,"exclude_from_local_holds_priority":null,"new_values":{"itemnotes":"xxx"}}' ,
+            data => '{"regex_mod":null,"report":{"total_records":1,"modified_fields":1,"modified_itemnumbers":[1]},"new_values":{"itemnotes":"xxx"},"record_ids":["1"],"exclude_from_local_holds_priority":null}',
+        }
+    }
+);
+
+{
+    $t->get_ok("//$superlibrarian_userid:$password@/api/v1/background_jobs")
+      ->status_is(200)->json_is( [ $background_job->to_api ] );
+
+    $t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
+      ->status_is(200)->json_is( [] );
+
+    $t->get_ok("//$patron_userid:$password@/api/v1/background_jobs")
+      ->status_is(403);
+
+    $background_job->borrowernumber( $librarian->borrowernumber )->store;
+
+    $t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
+      ->status_is(200)->json_is( [ $background_job->to_api ] );
+}
+
+{
+    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/background_jobs/"
+          . $background_job->id )->status_is(200)
+      ->json_is( $background_job->to_api );
+
+    $t->get_ok( "//$librarian_userid:$password@/api/v1/background_jobs/"
+          . $background_job->id )->status_is(200)
+      ->json_is( $background_job->to_api );
+
+    $background_job->borrowernumber( $superlibrarian->borrowernumber )->store;
+    $t->get_ok( "//$librarian_userid:$password@/api/v1/background_jobs/"
+          . $background_job->id )->status_is(403);
+}
+
+{
+    $background_job->delete;
+    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/background_jobs/"
+          . $background_job->id )->status_is(404)
+      ->json_is( '/error' => 'Object not found' );
+}
+
+$schema->storage->txn_rollback;