Bugzilla – Attachment 140491 Details for
Bug 30982
Use the REST API for background job list view
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 30982: Add tests and implement GET /background_jobs/$id
Bug-30982-Add-tests-and-implement-GET-backgroundjo.patch (text/plain), 6.68 KB, created by
Tomás Cohen Arazi (tcohen)
on 2022-09-12 18:08:45 UTC
(
hide
)
Description:
Bug 30982: Add tests and implement GET /background_jobs/$id
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2022-09-12 18:08:45 UTC
Size:
6.68 KB
patch
obsolete
>From 1a20c242f1e1c7719cf9cc0e1a0a59b26f20aedd Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Wed, 29 Jun 2022 11:28:31 +0200 >Subject: [PATCH] Bug 30982: Add tests and implement GET /background_jobs/$id > >--- > 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 <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; >-- >2.34.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 30982
:
136245
|
136246
|
136670
|
136671
|
136672
|
136707
|
136708
|
136715
|
138114
|
139636
|
139637
|
139638
|
139639
|
139640
|
139641
|
140488
|
140489
|
140490
|
140491
|
140492
|
140493
|
140494
|
140495
|
140496
|
140497
|
140498
|
140589
|
140590
|
140591
|
140592
|
140593
|
140594
|
140595
|
140596
|
140597
|
140598
|
140599
|
140789
|
140884
|
140885
|
140886
|
140887
|
140888
|
140889
|
140890
|
140891
|
140892
|
140893
|
140894
|
140895
|
140896
|
140897
|
142859