Bug 30982: Add Koha::BackgroundJobs->filter_by_current
[koha.git] / t / db_dependent / api / v1 / background_jobs.t
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;