From 182767f2168e99883b91fd81a7603ee7f4f7c0b5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 29 Jun 2022 11:28:31 +0200 Subject: [PATCH] Bug 30982: Add tests and implement GET /background_jobs/$id Signed-off-by: Martin Renvoize --- Koha/REST/V1/BackgroundJobs.pm | 36 +++++++ api/v1/swagger/paths/background_jobs.yaml | 4 + t/db_dependent/api/v1/background_jobs.t | 125 ++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100755 t/db_dependent/api/v1/background_jobs.t diff --git a/Koha/REST/V1/BackgroundJobs.pm b/Koha/REST/V1/BackgroundJobs.pm index e7deaa353c..55f121b403 100644 --- a/Koha/REST/V1/BackgroundJobs.pm +++ b/Koha/REST/V1/BackgroundJobs.pm @@ -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; diff --git a/api/v1/swagger/paths/background_jobs.yaml b/api/v1/swagger/paths/background_jobs.yaml index 320ef811ed..c386dfc5c5 100644 --- a/api/v1/swagger/paths/background_jobs.yaml +++ b/api/v1/swagger/paths/background_jobs.yaml @@ -103,6 +103,10 @@ 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 index 0000000000..11181b13a8 --- /dev/null +++ b/t/db_dependent/api/v1/background_jobs.t @@ -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 . + +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; -- 2.20.1