View | Details | Raw Unified | Return to bug 30982
Collapse All | Expand All

(-)a/Koha/REST/V1/BackgroundJobs.pm (+36 lines)
Lines 55-58 sub list { Link Here
55
55
56
}
56
}
57
57
58
sub get {
59
    my $c = shift->openapi->valid_input or return;
60
61
    return try {
62
63
        my $background_job_id = $c->validation->param('background_job_id');
64
        my $patron            = $c->stash('koha.user');
65
66
        my $can_manage_background_jobs =
67
          $patron->has_permission( { parameters => 'manage_background_jobs' } );
68
69
        my $background_job = Koha::BackgroundJobs->find($background_job_id);
70
71
        return $c->render(
72
            status  => 404,
73
            openapi => { error => "Object not found" }
74
        ) unless $background_job;
75
76
        return $c->render(
77
            status  => 403,
78
            openapi => { error => "Cannot see background job info" }
79
          )
80
          if !$can_manage_background_jobs
81
          && $background_job->borrowernumber != $patron->borrowernumber;
82
83
        return $c->render(
84
            status  => 200,
85
            openapi => $background_job->to_api
86
        );
87
    }
88
    catch {
89
        $c->unhandled_exception($_);
90
    };
91
}
92
93
58
1;
94
1;
(-)a/api/v1/swagger/paths/background_jobs.yaml (+4 lines)
Lines 103-108 Link Here
103
        description: A background job
103
        description: A background job
104
        schema:
104
        schema:
105
          $ref: "../swagger.yaml#/definitions/background_job"
105
          $ref: "../swagger.yaml#/definitions/background_job"
106
      "403":
107
        description: Access forbidden
108
        schema:
109
          $ref: "../swagger.yaml#/definitions/error"
106
      "404":
110
      "404":
107
        description: Background job not found
111
        description: Background job not found
108
        schema:
112
        schema:
(-)a/t/db_dependent/api/v1/background_jobs.t (-1 / +125 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 25;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::BackgroundJobs;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
#use t::lib::Mojo;
34
#my $t = t::lib::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
$schema->storage->txn_begin;
38
39
Koha::BackgroundJobs->delete;
40
my $superlibrarian = $builder->build_object(
41
    {
42
        class => 'Koha::Patrons',
43
        value => { flags => 1 },
44
    }
45
);
46
my $password = 'thePassword123';
47
$superlibrarian->set_password( { password => $password, skip_validation => 1 } );
48
my $superlibrarian_userid = $superlibrarian->userid;
49
50
my $librarian = $builder->build_object(
51
    {
52
        class => 'Koha::Patrons',
53
        value => { flags => 2 ** 2 }, # catalogue flag = 2
54
    }
55
);
56
$librarian->set_password( { password => $password, skip_validation => 1 } );
57
my $librarian_userid = $librarian->userid;
58
59
my $patron = $builder->build_object(
60
    {
61
        class => 'Koha::Patrons',
62
        value => { flags => 0 },
63
    }
64
);
65
$patron->set_password( { password => $password, skip_validation => 1 } );
66
my $patron_userid = $patron->userid;
67
68
$t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
69
  ->status_is(200)
70
  ->json_is( [] );
71
72
my $background_job = $builder->build_object(
73
    {
74
        class => 'Koha::BackgroundJobs',
75
        value => {
76
            status         => 'finished',
77
            progress       => 100,
78
            size           => 100,
79
            borrowernumber => $patron->borrowernumber,
80
            type           => 'batch_item_record_modification',
81
            queue => 'default',
82
            #data => '{"record_ids":["1"],"regex_mod":null,"exclude_from_local_holds_priority":null,"new_values":{"itemnotes":"xxx"}}' ,
83
            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}',
84
        }
85
    }
86
);
87
88
{
89
    $t->get_ok("//$superlibrarian_userid:$password@/api/v1/background_jobs")
90
      ->status_is(200)->json_is( [ $background_job->to_api ] );
91
92
    $t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
93
      ->status_is(200)->json_is( [] );
94
95
    $t->get_ok("//$patron_userid:$password@/api/v1/background_jobs")
96
      ->status_is(403);
97
98
    $background_job->borrowernumber( $librarian->borrowernumber )->store;
99
100
    $t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
101
      ->status_is(200)->json_is( [ $background_job->to_api ] );
102
}
103
104
{
105
    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/background_jobs/"
106
          . $background_job->id )->status_is(200)
107
      ->json_is( $background_job->to_api );
108
109
    $t->get_ok( "//$librarian_userid:$password@/api/v1/background_jobs/"
110
          . $background_job->id )->status_is(200)
111
      ->json_is( $background_job->to_api );
112
113
    $background_job->borrowernumber( $superlibrarian->borrowernumber )->store;
114
    $t->get_ok( "//$librarian_userid:$password@/api/v1/background_jobs/"
115
          . $background_job->id )->status_is(403);
116
}
117
118
{
119
    $background_job->delete;
120
    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/background_jobs/"
121
          . $background_job->id )->status_is(404)
122
      ->json_is( '/error' => 'Object not found' );
123
}
124
125
$schema->storage->txn_rollback;

Return to bug 30982