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

(-)a/t/db_dependent/IllbatchStatuses.t (+183 lines)
Line 0 Link Here
1
#s!/usr/bin/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 File::Basename qw/basename/;
21
use Koha::Database;
22
use Koha::IllbatchStatus;
23
use Koha::IllbatchStatuses;
24
use Koha::Patrons;
25
use Koha::Libraries;
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
use Test::MockObject;
29
use Test::MockModule;
30
31
use Test::More tests => 13;
32
33
my $schema = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
use_ok('Koha::IllbatchStatus');
36
use_ok('Koha::IllbatchStatuses');
37
38
$schema->storage->txn_begin;
39
40
Koha::IllbatchStatuses->search->delete;
41
42
# Keep track of whether our CRUD logging side-effects are happening
43
my $effects = {
44
    batch_status_create => 0,
45
    batch_status_update => 0,
46
    batch_status_delete => 0
47
};
48
49
# Mock a logger so we can check it is called
50
my $logger = Test::MockModule->new('Koha::Illrequest::Logger');
51
$logger->mock('log_something', sub {
52
    my ($self, $to_log ) = @_;
53
    $effects->{$to_log->{actionname}} ++;
54
});
55
56
# Create a batch status
57
my $status = $builder->build({
58
    source => 'IllbatchStatus',
59
    value => {
60
        name      => "Feeling the call to the Dark Side",
61
        code      => "OH_NO",
62
        is_system => 1
63
    }
64
});
65
66
my $status_obj = Koha::IllbatchStatuses->find({ code => $status->{code} });
67
isa_ok( $status_obj, 'Koha::IllbatchStatus' );
68
69
# Try to delete the status, it's a system status, so this should fail
70
$status_obj->delete_and_log;
71
my $status_obj_del = Koha::IllbatchStatuses->find({ code => $status->{code} });
72
isa_ok( $status_obj_del, 'Koha::IllbatchStatus' );
73
74
## Status create
75
76
# Try creating a duplicate status
77
my $status2 = Koha::IllbatchStatus->new({
78
    name => "Obi-wan",
79
    code => $status->{code},
80
    is_system => 0
81
});
82
is_deeply(
83
    $status2->create_and_log,
84
    { error => "Duplicate status found" },
85
    "Creation of statuses with duplicate codes prevented"
86
);
87
88
# Create a non-duplicate status and ensure that the logger is called
89
my $status3 = Koha::IllbatchStatus->new({
90
    name => "Kylo",
91
    code => "DARK_SIDE",
92
    is_system => 0
93
});
94
$status3->create_and_log;
95
is(
96
    $effects->{'batch_status_create'},
97
    1,
98
    "Creation of status calls log_something"
99
);
100
101
# Try creating a system status and ensure it's not created
102
my $cannot_create_system = Koha::IllbatchStatus->new({
103
    name => "Jar Jar Binks",
104
    code => "GUNGAN",
105
    is_system => 1
106
});
107
$cannot_create_system->create_and_log;
108
my $created_but_not_system = Koha::IllbatchStatuses->find({ code => "GUNGAN" });
109
is($created_but_not_system->{is_system}, undef, "is_system statuses cannot be created");
110
111
## Status update
112
113
# Ensure only name can be updated
114
$status3->update_and_log({
115
    name      => "Rey",
116
    code      => "LIGHT_SIDE",
117
    is_system => 1
118
});
119
# Get our updated status, if we can get it by it's code, we know that hasn't changed
120
my $not_updated = Koha::IllbatchStatuses->find({ code => "DARK_SIDE" })->unblessed;
121
is($not_updated->{is_system}, 0, "is_system cannot be changed");
122
is($not_updated->{name}, "Rey", "name can be changed");
123
# Ensure the logger is called
124
is(
125
    $effects->{'batch_status_update'},
126
    1,
127
    "Update of status calls log_something"
128
);
129
130
## Status delete
131
my $cannot_delete = Koha::IllbatchStatus->new({
132
    name => "Palapatine",
133
    code => "SITH",
134
    is_system => 1
135
})->store;
136
my $can_delete = Koha::IllbatchStatus->new({
137
    name => "Windu",
138
    code => "JEDI",
139
    is_system => 0
140
});
141
$cannot_delete->delete_and_log;
142
my $not_deleted = Koha::IllbatchStatuses->find({ code => "SITH" });
143
isa_ok( $not_deleted, 'Koha::IllbatchStatus', "is_system statuses cannot be deleted" );
144
$can_delete->create_and_log;
145
$can_delete->delete_and_log;
146
# Ensure the logger is called following a successful delete
147
is(
148
    $effects->{'batch_status_delete'},
149
    1,
150
    "Delete of status calls log_something"
151
);
152
153
# Create a system "UNKNOWN" status
154
my $status_unknown = Koha::IllbatchStatus->new({
155
    name => "Unknown",
156
    code => "UNKNOWN",
157
    is_system => 1
158
});
159
$status_unknown->create_and_log;
160
# Create a batch and assign it a status
161
my $patron = $builder->build_object({ class => 'Koha::Patrons' });
162
my $library = $builder->build_object({ class => 'Koha::Libraries' });
163
my $status5 = Koha::IllbatchStatus->new({
164
    name => "Plagueis",
165
    code => "DEAD_SITH",
166
    is_system => 0
167
});
168
$status5->create_and_log;
169
my $batch = Koha::Illbatch->new({
170
    name           => "My test batch",
171
    borrowernumber => $patron->borrowernumber,
172
    branchcode     => $library->branchcode,
173
    backend        => "TEST",
174
    statuscode     => $status5->code
175
});
176
$batch->create_and_log;
177
# Delete the batch status and ensure the batch's status has been changed
178
# to UNKNOWN
179
$status5->delete_and_log;
180
my $updated_code = Koha::Illbatches->find({ statuscode => "UNKNOWN" });
181
is($updated_code->statuscode, "UNKNOWN", "batches attached to deleted status have status changed to UNKNOWN");
182
183
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Illbatches.t (+96 lines)
Line 0 Link Here
1
#s!/usr/bin/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 File::Basename qw/basename/;
21
use Koha::Database;
22
use Koha::Illbatch;
23
use Koha::Illbatches;
24
use Koha::Illrequests;
25
use Koha::Patrons;
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
use Test::MockObject;
29
use Test::MockModule;
30
31
use Test::More tests => 8;
32
33
my $schema = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
use_ok('Koha::Illbatch');
36
use_ok('Koha::Illbatches');
37
38
$schema->storage->txn_begin;
39
40
Koha::Illrequests->search->delete;
41
42
# Create a patron
43
my $patron = $builder->build({ source => 'Borrower' });
44
45
# Create a librarian
46
my $librarian = $builder->build({
47
    source => 'Borrower',
48
    value => {
49
        firstname => "Grogu"
50
    }
51
});
52
53
# Create a branch
54
my $branch = $builder->build({
55
    source => 'Branch'
56
});
57
58
# Create a batch
59
my $illbatch = $builder->build({
60
    source => 'Illbatch',
61
    value => {
62
        name  => "My test batch",
63
        backend  => "Mock",
64
        borrowernumber => $librarian->{borrowernumber},
65
        branchcode => $branch->{branchcode}
66
    }
67
});
68
my $batch_obj = Koha::Illbatches->find($illbatch->{id});
69
isa_ok( $batch_obj, 'Koha::Illbatch' );
70
71
# Create an ILL request in the batch
72
my $illrq = $builder->build({
73
    source => 'Illrequest',
74
    value => {
75
        borrowernumber => $patron->{borrowernumber},
76
        batch_id       => $illbatch->{id}
77
    }
78
});
79
my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
80
81
# Check requests_count
82
my $requests_count = $batch_obj->requests_count;
83
is( $requests_count, 1, 'requests_count returns correctly' );
84
85
# Check patron
86
my $batch_patron = $batch_obj->patron;
87
isa_ok( $batch_patron, 'Koha::Patron' );
88
is( $batch_patron->firstname, "Grogu", "patron returns correctly" );
89
90
# Check branch
91
my $batch_branch = $batch_obj->branch;
92
isa_ok( $batch_branch, 'Koha::Library' );
93
is( $batch_branch->branchcode, $branch->{branchcode}, "branch returns correctly" );
94
95
$illrq_obj->delete;
96
$schema->storage->txn_rollback;
(-)a/t/db_dependent/api/v1/ill_requests.t (-1 / +85 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 2;
21
21
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::MockObject;
23
use Test::MockObject;
Lines 235-237 subtest 'list() tests' => sub { Link Here
235
235
236
    $schema->storage->txn_rollback;
236
    $schema->storage->txn_rollback;
237
};
237
};
238
239
subtest 'add() tests' => sub {
240
241
    plan tests => 2;
242
243
    $schema->storage->txn_begin;
244
245
    # create an authorized user
246
    my $patron = $builder->build_object({
247
        class => 'Koha::Patrons',
248
        value => { flags => 2 ** 22 } # 22 => ill
249
    });
250
    my $password = 'thePassword123';
251
    $patron->set_password({ password => $password, skip_validation => 1 });
252
    my $userid = $patron->userid;
253
254
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
255
256
    # Create an ILL request
257
    my $illrequest = $builder->build_object(
258
        {
259
            class => 'Koha::Illrequests',
260
            value => {
261
                backend        => 'Mock',
262
                branchcode     => $library->branchcode,
263
                borrowernumber => $patron->borrowernumber,
264
                status         => 'STATUS1',
265
            }
266
        }
267
    );
268
269
    # Mock ILLBackend (as object)
270
    my $backend = Test::MockObject->new;
271
    $backend->set_isa('Koha::Illbackends::Mock');
272
    $backend->set_always('name', 'Mock');
273
    $backend->set_always('capabilities', sub {
274
        return $illrequest;
275
    } );
276
    $backend->mock(
277
        'metadata',
278
        sub {
279
            my ( $self, $rq ) = @_;
280
            return {
281
                ID => $rq->illrequest_id,
282
                Title => $rq->patron->borrowernumber
283
            }
284
        }
285
    );
286
    $backend->mock(
287
        'status_graph', sub {},
288
    );
289
290
    # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
291
    my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
292
    $illreqmodule->mock( 'load_backend',
293
        sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
294
    );
295
296
    $schema->storage->txn_begin;
297
298
    Koha::Illrequests->search->delete;
299
300
    my $body = {
301
        backend => 'Mock',
302
        borrowernumber => $patron->borrowernumber,
303
        branchcode => $library->branchcode,
304
        metadata => {
305
            article_author => "Jessop, E. G.",
306
            article_title => "Sleep",
307
            issn => "0957-4832",
308
            issue => "2",
309
            pages => "89-90",
310
            publisher => "OXFORD UNIVERSITY PRESS",
311
            title => "Journal of public health medicine.",
312
            year => "2001"
313
        }
314
    };
315
316
    ## Authorized user test
317
    $t->post_ok( "//$userid:$password@/api/v1/illrequests" => json => $body)
318
      ->status_is(201);
319
320
    $schema->storage->txn_rollback;
321
};
(-)a/t/db_dependent/api/v1/illbatches.t (+436 lines)
Line 0 Link Here
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 => 5;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::Illbatch;
27
use Koha::Illbatches;
28
use Koha::Illrequests;
29
use Koha::IllbatchStatuses;
30
use Koha::Database;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
my $t = Test::Mojo->new('Koha::REST::V1');
36
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38
subtest 'list() tests' => sub {
39
40
    plan tests => 19;
41
42
    $schema->storage->txn_begin;
43
44
    Koha::Illbatches->search->delete;
45
46
    my $librarian = $builder->build_object(
47
        {
48
            class => 'Koha::Patrons',
49
            value => {
50
                flags => 2 ** 22 # 22 => ill
51
            }
52
        }
53
    );
54
55
    my $branch = $builder->build_object(
56
        {
57
            class => 'Koha::Libraries'
58
        }
59
    );
60
61
    my $password = 'sheev_is_da_boss!';
62
    $librarian->set_password( { password => $password, skip_validation => 1 } );
63
    my $userid = $librarian->userid;
64
65
    ## Authorized user tests
66
    # No batches, so empty array should be returned
67
    $t->get_ok("//$userid:$password@/api/v1/illbatches")
68
      ->status_is(200)
69
      ->json_is( [] );
70
71
    my $batch = $builder->build_object({
72
        class => 'Koha::Illbatches',
73
        value => {
74
            name           => "PapaPalpatine",
75
            backend        => "Mock",
76
            borrowernumber => $librarian->borrowernumber,
77
            branchcode => $branch->branchcode
78
        }
79
    });
80
81
    my $illrq = $builder->build({
82
        source => 'Illrequest',
83
        value => {
84
            borrowernumber => $librarian->borrowernumber,
85
            batch_id       => $batch->id
86
        }
87
    });
88
89
    # One batch created, should get returned
90
    $t->get_ok("//$userid:$password@/api/v1/illbatches")
91
      ->status_is(200)
92
      ->json_has( '/0/id', 'Batch ID' )
93
      ->json_has( '/0/name', 'Batch name' )
94
      ->json_has( '/0/backend', 'Backend name' )
95
      ->json_has( '/0/borrowernumber', 'Borrowernumber' )
96
      ->json_has( '/0/branchcode', 'Branchcode' )
97
      ->json_has( '/0/patron', 'patron embedded' )
98
      ->json_has( '/0/branch', 'branch embedded' )
99
      ->json_has( '/0/requests_count', 'request count' );
100
101
    # Try to create a second batch with the same name, this should fail
102
    my $another_batch = $builder->build_object({ class => 'Koha::Illbatches', value => {
103
        name => $batch->name
104
    } });
105
    # Create a second batch with a different name
106
    my $batch_with_another_name = $builder->build_object({ class => 'Koha::Illbatches' });
107
108
    # Two batches created, they should both be returned
109
    $t->get_ok("//$userid:$password@/api/v1/illbatches")
110
      ->status_is(200)
111
      ->json_has('/0', 'has first batch')
112
      ->json_has('/1', 'has second batch');
113
114
    my $patron = $builder->build_object(
115
        {
116
            class => 'Koha::Patrons',
117
            value => {
118
                cardnumber => 999,
119
                flags => 0
120
            }
121
        }
122
    );
123
124
    $patron->set_password( { password => $password, skip_validation => 1 } );
125
    my $unauth_userid = $patron->userid;
126
127
    # Unauthorized access
128
    $t->get_ok("//$unauth_userid:$password@/api/v1/illbatches")
129
      ->status_is(403);
130
131
    $schema->storage->txn_rollback;
132
};
133
134
subtest 'get() tests' => sub {
135
136
    plan tests => 15;
137
138
    $schema->storage->txn_begin;
139
140
    my $librarian = $builder->build_object(
141
        {
142
            class => 'Koha::Patrons',
143
            value => { flags => 2**22 }    # 22 => ill
144
        }
145
    );
146
    my $password = 'Rebelz4DaWin';
147
    $librarian->set_password( { password => $password, skip_validation => 1 } );
148
    my $userid = $librarian->userid;
149
150
    my $patron = $builder->build_object(
151
        {
152
            class => 'Koha::Patrons',
153
            value => { flags => 0 }
154
        }
155
    );
156
157
    my $branch = $builder->build_object(
158
        {
159
            class => 'Koha::Libraries'
160
        }
161
    );
162
163
    my $batch = $builder->build_object({
164
        class => 'Koha::Illbatches',
165
        value => {
166
            name           => "LeiaOrgana",
167
            backend        => "Mock",
168
            borrowernumber => $librarian->borrowernumber,
169
            branchcode     => $branch->branchcode
170
        }
171
    });
172
173
174
    $patron->set_password( { password => $password, skip_validation => 1 } );
175
    my $unauth_userid = $patron->userid;
176
177
    $t->get_ok( "//$userid:$password@/api/v1/illbatches/" . $batch->id )
178
      ->status_is(200)
179
      ->json_has( '/id', 'Batch ID' )
180
      ->json_has( '/name', 'Batch name' )
181
      ->json_has( '/backend', 'Backend name' )
182
      ->json_has( '/borrowernumber', 'Borrowernumber' )
183
      ->json_has( '/branchcode', 'Branchcode' )
184
      ->json_has( '/patron', 'patron embedded' )
185
      ->json_has( '/branch', 'branch embedded' )
186
      ->json_has( '/requests_count', 'request count' );
187
188
    $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatches/" . $batch->id )
189
      ->status_is(403);
190
191
    my $batch_to_delete = $builder->build_object({ class => 'Koha::Illbatches' });
192
    my $non_existent_id = $batch_to_delete->id;
193
    $batch_to_delete->delete;
194
195
    $t->get_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" )
196
      ->status_is(404)
197
      ->json_is( '/error' => 'ILL batch not found' );
198
199
    $schema->storage->txn_rollback;
200
};
201
202
subtest 'add() tests' => sub {
203
204
    plan tests =>19;
205
206
    $schema->storage->txn_begin;
207
208
    my $librarian = $builder->build_object(
209
        {
210
            class => 'Koha::Patrons',
211
            value => { flags => 2**22 }    # 22 => ill
212
        }
213
    );
214
    my $password = 'v4d3rRox';
215
    $librarian->set_password( { password => $password, skip_validation => 1 } );
216
    my $userid = $librarian->userid;
217
218
    my $patron = $builder->build_object(
219
        {
220
            class => 'Koha::Patrons',
221
            value => { flags => 0 }
222
        }
223
    );
224
225
    $patron->set_password( { password => $password, skip_validation => 1 } );
226
    my $unauth_userid = $patron->userid;
227
228
    my $branch = $builder->build_object(
229
        {
230
            class => 'Koha::Libraries'
231
        }
232
    );
233
234
    my $batch_status = $builder->build_object(
235
        {
236
            class => 'Koha::IllbatchStatuses'
237
        }
238
    );
239
240
    my $batch_metadata = {
241
        name           => "Anakin's requests",
242
        backend        => "Mock",
243
        cardnumber     => $librarian->cardnumber,
244
        branchcode     => $branch->branchcode,
245
        statuscode     => $batch_status->code
246
    };
247
248
    # Unauthorized attempt to write
249
    $t->post_ok("//$unauth_userid:$password@/api/v1/illbatches" => json => $batch_metadata)
250
      ->status_is(403);
251
252
    # Authorized attempt to write invalid data
253
    my $batch_with_invalid_field = {
254
        %{$batch_metadata},
255
        doh => 1
256
    };
257
258
    $t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_with_invalid_field )
259
      ->status_is(400)
260
      ->json_is(
261
        "/errors" => [
262
            {
263
                message => "Properties not allowed: doh.",
264
                path    => "/body"
265
            }
266
        ]
267
      );
268
269
    # Authorized attempt to write
270
    my $batch_id =
271
      $t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_metadata )
272
        ->status_is( 201 )
273
        ->json_is( '/name'           => $batch_metadata->{name} )
274
        ->json_is( '/backend'        => $batch_metadata->{backend} )
275
        ->json_is( '/borrowernumber' => $librarian->borrowernumber )
276
        ->json_is( '/branchcode'     => $batch_metadata->{branchcode} )
277
        ->json_is( '/statuscode'     => $batch_status->code )
278
        ->json_has( '/patron' )
279
        ->json_has( '/status' )
280
        ->json_has( '/requests_count' )
281
        ->json_has( '/branch' );
282
283
    # Authorized attempt to create with null id
284
    $batch_metadata->{id} = undef;
285
    $t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_metadata )
286
      ->status_is(400)
287
      ->json_has('/errors');
288
289
    $schema->storage->txn_rollback;
290
};
291
292
subtest 'update() tests' => sub {
293
294
    plan tests => 15;
295
296
    $schema->storage->txn_begin;
297
298
    my $librarian = $builder->build_object(
299
        {
300
            class => 'Koha::Patrons',
301
            value => { flags => 2**22 }    # 22 => ill
302
        }
303
    );
304
    my $password = 'aw3s0m3y0d41z';
305
    $librarian->set_password( { password => $password, skip_validation => 1 } );
306
    my $userid = $librarian->userid;
307
308
    my $patron = $builder->build_object(
309
        {
310
            class => 'Koha::Patrons',
311
            value => { flags => 0 }
312
        }
313
    );
314
315
    $patron->set_password( { password => $password, skip_validation => 1 } );
316
    my $unauth_userid = $patron->userid;
317
318
    my $branch = $builder->build_object(
319
        {
320
            class => 'Koha::Libraries'
321
        }
322
    );
323
324
    my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' } )->id;
325
326
    # Unauthorized attempt to update
327
    $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" => json => { name => 'These are not the droids you are looking for' } )
328
      ->status_is(403);
329
330
    my $batch_status = $builder->build_object(
331
        {
332
            class => 'Koha::IllbatchStatuses'
333
        }
334
    );
335
336
    # Attempt partial update on a PUT
337
    my $batch_with_missing_field = {
338
        backend => "Mock",
339
        borrowernumber => $librarian->borrowernumber,
340
        branchcode => $branch->branchcode,
341
        statuscode => $batch_status->code
342
    };
343
344
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_missing_field )
345
      ->status_is(400)
346
      ->json_is( "/errors" =>
347
          [ { message => "Missing property.", path => "/body/name" } ]
348
      );
349
350
    # Full object update on PUT
351
    my $batch_with_updated_field = {
352
        name           => "Master Ploo Koon",
353
        backend        => "Mock",
354
        borrowernumber => $librarian->borrowernumber,
355
        branchcode => $branch->branchcode,
356
        statuscode => $batch_status->code
357
    };
358
359
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_updated_field )
360
      ->status_is(200)
361
      ->json_is( '/name' => 'Master Ploo Koon' );
362
363
    # Authorized attempt to write invalid data
364
    my $batch_with_invalid_field = {
365
        doh  => 1,
366
        name => "Master Mace Windu",
367
        backend => "Mock"
368
    };
369
370
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_invalid_field )
371
      ->status_is(400)
372
      ->json_is(
373
        "/errors" => [
374
            {
375
                message => "Properties not allowed: doh.",
376
                path    => "/body"
377
            }
378
        ]
379
    );
380
381
    my $batch_to_delete = $builder->build_object({ class => 'Koha::Cities' });
382
    my $non_existent_id = $batch_to_delete->id;
383
    $batch_to_delete->delete;
384
385
    $t->put_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" => json => $batch_with_updated_field )
386
      ->status_is(404);
387
388
    # Wrong method (POST)
389
    $batch_with_updated_field->{id} = 2;
390
391
    $t->post_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_updated_field )
392
      ->status_is(404);
393
394
    $schema->storage->txn_rollback;
395
};
396
397
subtest 'delete() tests' => sub {
398
399
    plan tests => 6;
400
401
    $schema->storage->txn_begin;
402
403
    my $librarian = $builder->build_object(
404
        {
405
            class => 'Koha::Patrons',
406
            value => { flags => 2**22 }    # 22 => ill
407
        }
408
    );
409
    my $password = 's1th43v3r!';
410
    $librarian->set_password( { password => $password, skip_validation => 1 } );
411
    my $userid = $librarian->userid;
412
413
    my $patron = $builder->build_object(
414
        {
415
            class => 'Koha::Patrons',
416
            value => { flags => 0 }
417
        }
418
    );
419
420
    $patron->set_password( { password => $password, skip_validation => 1 } );
421
    my $unauth_userid = $patron->userid;
422
423
    my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' })->id;
424
425
    # Unauthorized attempt to delete
426
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" )
427
      ->status_is(403);
428
429
    $t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id")
430
      ->status_is(204);
431
432
    $t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id")
433
      ->status_is(404);
434
435
    $schema->storage->txn_rollback;
436
};
(-)a/t/db_dependent/api/v1/illbatchstatuses.t (-1 / +352 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 => 5;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::IllbatchStatus;
27
use Koha::IllbatchStatuses;
28
use Koha::Database;
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
my $t = Test::Mojo->new('Koha::REST::V1');
34
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36
subtest 'list() tests' => sub {
37
38
    plan tests => 9;
39
40
    $schema->storage->txn_begin;
41
42
    Koha::IllbatchStatuses->search->delete;
43
44
    # Create an admin user
45
    my $librarian = $builder->build_object(
46
        {
47
            class => 'Koha::Patrons',
48
            value => {
49
                flags => 2 ** 22 # 22 => ill
50
            }
51
        }
52
    );
53
    my $password = 'yoda4ever!';
54
    $librarian->set_password( { password => $password, skip_validation => 1 } );
55
    my $userid = $librarian->userid;
56
57
    ## Authorized user tests
58
    # No statuses, so empty array should be returned
59
    $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")
60
      ->status_is(200)
61
      ->json_is( [] );
62
63
    my $status = $builder->build_object({
64
        class => 'Koha::IllbatchStatuses',
65
        value => {
66
            name           => "Han Solo",
67
            code           => "SOLO",
68
            is_system      => 0
69
        }
70
    });
71
72
    # One batch created, should get returned
73
    $t->get_ok("//$userid:$password@/api/v1/illbatchstatuses")
74
      ->status_is(200)
75
      ->json_has( '/0/id', 'ID' )
76
      ->json_has( '/0/name', 'Name' )
77
      ->json_has( '/0/code', 'Code' )
78
      ->json_has( '/0/is_system', 'is_system' );
79
80
    $schema->storage->txn_rollback;
81
};
82
83
subtest 'get() tests' => sub {
84
85
    plan tests => 11;
86
87
    $schema->storage->txn_begin;
88
89
    my $librarian = $builder->build_object(
90
        {
91
            class => 'Koha::Patrons',
92
            value => { flags => 2**22 }    # 22 => ill
93
        }
94
    );
95
    my $password = 'Rebelz4DaWin';
96
    $librarian->set_password( { password => $password, skip_validation => 1 } );
97
    my $userid = $librarian->userid;
98
99
    my $status = $builder->build_object({
100
        class => 'Koha::IllbatchStatuses',
101
        value => {
102
            name           => "Han Solo",
103
            code           => "SOLO",
104
            is_system      => 0
105
        }
106
    });
107
108
    # Unauthorised user
109
    my $patron = $builder->build_object(
110
        {
111
            class => 'Koha::Patrons',
112
            value => { flags => 0 }
113
        }
114
    );
115
    $patron->set_password( { password => $password, skip_validation => 1 } );
116
    my $unauth_userid = $patron->userid;
117
118
    $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $status->code )
119
      ->status_is(200)
120
      ->json_has( '/id', 'ID' )
121
      ->json_has( '/name', 'Name' )
122
      ->json_has( '/code', 'Code' )
123
      ->json_has( '/is_system', 'is_system' );
124
125
    $t->get_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $status->id )
126
      ->status_is(403);
127
128
    my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' });
129
    my $non_existent_code = $status_to_delete->code;
130
    $status_to_delete->delete;
131
132
    $t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" )
133
      ->status_is(404)
134
      ->json_is( '/error' => 'ILL batch status not found' );
135
136
    $schema->storage->txn_rollback;
137
};
138
139
subtest 'add() tests' => sub {
140
141
    plan tests =>14;
142
143
    $schema->storage->txn_begin;
144
145
    my $librarian = $builder->build_object(
146
        {
147
            class => 'Koha::Patrons',
148
            value => { flags => 2**22 }    # 22 => ill
149
        }
150
    );
151
    my $password = '3poRox';
152
    $librarian->set_password( { password => $password, skip_validation => 1 } );
153
    my $userid = $librarian->userid;
154
155
    my $patron = $builder->build_object(
156
        {
157
            class => 'Koha::Patrons',
158
            value => { flags => 0 }
159
        }
160
    );
161
    $patron->set_password( { password => $password, skip_validation => 1 } );
162
    my $unauth_userid = $patron->userid;
163
164
    my $status_metadata = {
165
        name           => "In a bacta tank",
166
        code           => "BACTA",
167
        is_system      => 0
168
    };
169
170
    # Unauthorized attempt to write
171
    $t->post_ok("//$unauth_userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata)
172
      ->status_is(403);
173
174
    # Authorized attempt to write invalid data
175
    my $status_with_invalid_field = {
176
        %{$status_metadata},
177
        doh => 1
178
    };
179
180
    $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_with_invalid_field )
181
      ->status_is(400)
182
      ->json_is(
183
        "/errors" => [
184
            {
185
                message => "Properties not allowed: doh.",
186
                path    => "/body"
187
            }
188
        ]
189
      );
190
191
    # Authorized attempt to write
192
    my $status_id =
193
      $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )
194
        ->status_is( 201 )
195
        ->json_has( '/id', 'ID' )
196
        ->json_has( '/name', 'Name' )
197
        ->json_has( '/code', 'Code' )
198
        ->json_has( '/is_system', 'is_system' );
199
200
    # Authorized attempt to create with null id
201
    $status_metadata->{id} = undef;
202
    $t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata )
203
      ->status_is(400)
204
      ->json_has('/errors');
205
206
    $schema->storage->txn_rollback;
207
};
208
209
subtest 'update() tests' => sub {
210
211
    plan tests => 13;
212
213
    $schema->storage->txn_begin;
214
215
    my $librarian = $builder->build_object(
216
        {
217
            class => 'Koha::Patrons',
218
            value => { flags => 2**22 }    # 22 => ill
219
        }
220
    );
221
    my $password = 'aw3s0m3y0d41z';
222
    $librarian->set_password( { password => $password, skip_validation => 1 } );
223
    my $userid = $librarian->userid;
224
225
    my $patron = $builder->build_object(
226
        {
227
            class => 'Koha::Patrons',
228
            value => { flags => 0 }
229
        }
230
    );
231
    $patron->set_password( { password => $password, skip_validation => 1 } );
232
    my $unauth_userid = $patron->userid;
233
234
    my $status_code = $builder->build_object({ class => 'Koha::IllbatchStatuses' } )->code;
235
236
    # Unauthorized attempt to update
237
    $t->put_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/$status_code" => json => { name => 'These are not the droids you are looking for' } )
238
      ->status_is(403);
239
240
    # Attempt partial update on a PUT
241
    my $status_with_missing_field = {
242
        code      => $status_code,
243
        is_system => 0
244
    };
245
246
    $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_missing_field )
247
      ->status_is(400)
248
      ->json_is( "/errors" =>
249
          [ { message => "Missing property.", path => "/body/name" } ]
250
      );
251
252
    # Full object update on PUT
253
    my $status_with_updated_field = {
254
        name           => "Master Ploo Koon",
255
        code           => $status_code,
256
        is_system      => 0
257
    };
258
259
    $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_updated_field )
260
      ->status_is(200)
261
      ->json_is( '/name' => 'Master Ploo Koon' );
262
263
    # Authorized attempt to write invalid data
264
    my $status_with_invalid_field = {
265
        doh  => 1,
266
        name => "Master Mace Windu",
267
        code => $status_code
268
    };
269
270
    $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_invalid_field )
271
      ->status_is(400)
272
      ->json_is(
273
        "/errors" => [
274
            {
275
                message => "Properties not allowed: doh.",
276
                path    => "/body"
277
            }
278
        ]
279
    );
280
281
    my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' });
282
    my $non_existent_code = $status_to_delete->code;
283
    $status_to_delete->delete;
284
285
    $t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" => json => $status_with_updated_field )
286
      ->status_is(404);
287
288
    $schema->storage->txn_rollback;
289
};
290
291
subtest 'delete() tests' => sub {
292
293
    plan tests => 9;
294
295
    $schema->storage->txn_begin;
296
297
    my $librarian = $builder->build_object(
298
        {
299
            class => 'Koha::Patrons',
300
            value => { flags => 2**22 }    # 22 => ill
301
        }
302
    );
303
    my $password = 's1th43v3r!';
304
    $librarian->set_password( { password => $password, skip_validation => 1 } );
305
    my $userid = $librarian->userid;
306
307
    my $patron = $builder->build_object(
308
        {
309
            class => 'Koha::Patrons',
310
            value => { flags => 0 }
311
        }
312
    );
313
314
    $patron->set_password( { password => $password, skip_validation => 1 } );
315
    my $unauth_userid = $patron->userid;
316
317
    my $non_system_status = $builder->build_object({
318
        class => 'Koha::IllbatchStatuses',
319
        value => {
320
            is_system => 0
321
        }
322
    });
323
324
    my $system_status = $builder->build_object({
325
        class => 'Koha::IllbatchStatuses',
326
        value => {
327
            is_system => 1
328
        }
329
    });
330
331
    # Unauthorized attempt to delete
332
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )
333
      ->status_is(403);
334
335
    $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )
336
      ->status_is(204);
337
338
    $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code )
339
      ->status_is(404);
340
341
    $t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $system_status->code )
342
      ->status_is(400)
343
      ->json_is(
344
        "/errors" => [
345
            {
346
                message => "ILL batch status cannot be deleted"
347
            }
348
        ]
349
      );
350
351
    $schema->storage->txn_rollback;
352
};

Return to bug 30719