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

(-)a/t/db_dependent/api/v1/stockrotation_rotas.t (-1 / +350 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 Mojo::JSON qw(encode_json);
24
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
27
28
use Koha::StockRotationRotas;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'list() tests' => sub {
38
39
    plan tests => 20;
40
41
    $schema->storage->txn_begin;
42
43
    Koha::StockRotationRotas->search->delete;
44
45
    my $librarian = $builder->build_object(
46
        {
47
            class => 'Koha::Patrons',
48
            value => { flags => 2**2 }    # catalogue flag = 2
49
        }
50
    );
51
    my $password = 'thePassword123';
52
    $librarian->set_password( { password => $password, skip_validation => 1 } );
53
    my $userid = $librarian->userid;
54
55
    my $patron = $builder->build_object(
56
        {
57
            class => 'Koha::Patrons',
58
            value => { flags => 0 }
59
        }
60
    );
61
62
    $patron->set_password( { password => $password, skip_validation => 1 } );
63
    my $unauth_userid = $patron->userid;
64
65
    ## Authorized user tests
66
    # No rotas, so empty array should be returned
67
    $t->get_ok("//$userid:$password@/api/v1/rotas")->status_is(200)->json_is( [] );
68
69
    my $active_rota = $builder->build_object( { class => 'Koha::StockRotationRotas', value => { active => 1 } } );
70
71
    # One rota created, should get returned
72
    $t->get_ok("//$userid:$password@/api/v1/rotas")->status_is(200)->json_is( [ $active_rota->to_api ] );
73
74
    my $another_active_rota =
75
        $builder->build_object( { class => 'Koha::StockRotationRotas', value => { active => 1 } } );
76
    my $inactive_rota = $builder->build_object( { class => 'Koha::StockRotationRotas' } );
77
78
    # Three rotas created, all should both be returned
79
    $t->get_ok("//$userid:$password@/api/v1/rotas")->status_is(200)->json_is(
80
        [
81
            $active_rota->to_api, $another_active_rota->to_api,
82
            $inactive_rota->to_api
83
        ]
84
    );
85
86
    # Filtering works, two rotas are active
87
    my $api_filter = encode_json( { 'me.active' => Mojo::JSON->true } );
88
    $t->get_ok("//$userid:$password@/api/v1/rotas?q=$api_filter")->status_is(200)
89
        ->json_is( [ $active_rota->to_api, $another_active_rota->to_api ] );
90
91
    $api_filter = encode_json( { 'me.title' => $active_rota->title } );
92
    $t->get_ok("//$userid:$password@/api/v1/rotas?q=$api_filter")->status_is(200)->json_is( [ $active_rota->to_api ] );
93
94
    # Warn on unsupported query parameter
95
    $t->get_ok( "//$userid:$password@/api/v1/rotas?title=" . $active_rota->title )->status_is(400)
96
        ->json_is( [ { path => '/query/title', message => 'Malformed query string' } ] );
97
98
    # Unauthorized access
99
    $t->get_ok("//$unauth_userid:$password@/api/v1/rotas")->status_is(403);
100
101
    $schema->storage->txn_rollback;
102
};
103
104
subtest 'get() tests' => sub {
105
106
    plan tests => 8;
107
108
    $schema->storage->txn_begin;
109
110
    my $rota      = $builder->build_object( { class => 'Koha::StockRotationRotas' } );
111
    my $librarian = $builder->build_object(
112
        {
113
            class => 'Koha::Patrons',
114
            value => { flags => 2**2 }    # catalogue flag = 2
115
        }
116
    );
117
    my $password = 'thePassword123';
118
    $librarian->set_password( { password => $password, skip_validation => 1 } );
119
    my $userid = $librarian->userid;
120
121
    my $patron = $builder->build_object(
122
        {
123
            class => 'Koha::Patrons',
124
            value => { flags => 0 }
125
        }
126
    );
127
128
    $patron->set_password( { password => $password, skip_validation => 1 } );
129
    my $unauth_userid = $patron->userid;
130
131
    $t->get_ok( "//$userid:$password@/api/v1/rotas/" . $rota->rota_id )->status_is(200)->json_is( $rota->to_api );
132
133
    $t->get_ok( "//$unauth_userid:$password@/api/v1/rotas/" . $rota->rota_id )->status_is(403);
134
135
    my $rota_to_delete  = $builder->build_object( { class => 'Koha::StockRotationRotas' } );
136
    my $non_existent_id = $rota_to_delete->id;
137
    $rota_to_delete->delete;
138
139
    $t->get_ok("//$userid:$password@/api/v1/rotas/$non_existent_id")->status_is(404)
140
        ->json_is( '/error' => 'Rota not found' );
141
142
    $schema->storage->txn_rollback;
143
};
144
145
subtest 'add() tests' => sub {
146
147
    plan tests => 18;
148
149
    $schema->storage->txn_begin;
150
151
    my $librarian = $builder->build_object(
152
        {
153
            class => 'Koha::Patrons',
154
            value => { flags => 2**24 }    # stockrotation flag = 24
155
        }
156
    );
157
    my $password = 'thePassword123';
158
    $librarian->set_password( { password => $password, skip_validation => 1 } );
159
    my $userid = $librarian->userid;
160
161
    my $patron = $builder->build_object(
162
        {
163
            class => 'Koha::Patrons',
164
            value => { flags => 0 }
165
        }
166
    );
167
168
    $patron->set_password( { password => $password, skip_validation => 1 } );
169
    my $unauth_userid = $patron->userid;
170
171
    my $rota = {
172
        title       => "Rota Title",
173
        description => "Rota Description",
174
        active      => Mojo::JSON->true,
175
        cyclical    => Mojo::JSON->true
176
    };
177
178
    # Unauthorized attempt to write
179
    $t->post_ok( "//$unauth_userid:$password@/api/v1/rotas" => json => $rota )->status_is(403);
180
181
    # Authorized attempt to write invalid data
182
    my $rota_with_invalid_field = {
183
        blah        => "Rota Blah",
184
        description => "Rota Description",
185
        active      => Mojo::JSON->true,
186
        cyclical    => Mojo::JSON->true
187
    };
188
189
    $t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota_with_invalid_field )->status_is(400)->json_is(
190
        "/errors" => [
191
            {
192
                message => "Properties not allowed: blah.",
193
                path    => "/body"
194
            }
195
        ]
196
    );
197
198
    # Authorized attempt to write
199
    my $rota_id =
200
        $t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota )->status_is( 201, 'SWAGGER3.2.1' )
201
        ->header_like(
202
        Location => qr|^\/api\/v1\/rotas/\d*|,
203
        'SWAGGER3.4.1'
204
    )->json_is( '/title' => $rota->{title} )->json_is( '/description' => $rota->{description} )
205
        ->json_is( '/active' => $rota->{active} )->json_is( '/cyclical' => $rota->{cyclical} )
206
        ->tx->res->json->{rota_id};
207
208
    # Authorized attempt to create with null id
209
    $rota->{rota_id} = undef;
210
    $t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota )->status_is(400)->json_has('/errors');
211
212
    # Authorized attempt to create with existing id
213
    $rota->{rota_id} = $rota_id;
214
    $t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota )->status_is(400)->json_is(
215
        "/errors" => [
216
            {
217
                message => "Read-only.",
218
                path    => "/body/rota_id"
219
            }
220
        ]
221
    );
222
223
    $schema->storage->txn_rollback;
224
};
225
226
subtest 'update() tests' => sub {
227
228
    plan tests => 15;
229
230
    $schema->storage->txn_begin;
231
232
    my $librarian = $builder->build_object(
233
        {
234
            class => 'Koha::Patrons',
235
            value => { flags => 2**24 }    # stockrotation flag = 24
236
        }
237
    );
238
    my $password = 'thePassword123';
239
    $librarian->set_password( { password => $password, skip_validation => 1 } );
240
    my $userid = $librarian->userid;
241
242
    my $patron = $builder->build_object(
243
        {
244
            class => 'Koha::Patrons',
245
            value => { flags => 0 }
246
        }
247
    );
248
249
    $patron->set_password( { password => $password, skip_validation => 1 } );
250
    my $unauth_userid = $patron->userid;
251
252
    my $rota_id = $builder->build_object( { class => 'Koha::StockRotationRotas' } )->id;
253
254
    # Unauthorized attempt to update
255
    $t->put_ok(
256
        "//$unauth_userid:$password@/api/v1/rotas/$rota_id" => json => { title => 'New unauthorized title change' } )
257
        ->status_is(403);
258
259
    # Attempt partial update on a PUT
260
    my $rota_with_missing_field = {
261
        title       => 'New title',
262
        description => 'New description',
263
        cyclical    => Mojo::JSON->false
264
    };
265
266
    $t->put_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => $rota_with_missing_field )->status_is(400)
267
        ->json_is( "/errors" => [ { message => "Missing property.", path => "/body/active" } ] );
268
269
    # Full object update on PUT
270
    my $rota_with_updated_field = {
271
        title       => "London",
272
        description => "Rota Description",
273
        active      => Mojo::JSON->true,
274
        cyclical    => Mojo::JSON->false
275
    };
276
277
    $t->put_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => $rota_with_updated_field )->status_is(200)
278
        ->json_is( '/title' => 'London' );
279
280
    # Authorized attempt to write invalid data
281
    my $rota_with_invalid_field = {
282
        blah        => "Rota Blah",
283
        description => "Rota Description",
284
        active      => Mojo::JSON->true,
285
        cyclical    => Mojo::JSON->false
286
    };
287
288
    $t->put_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => $rota_with_invalid_field )->status_is(400)
289
        ->json_is(
290
        "/errors" => [
291
            {
292
                message => "Properties not allowed: blah.",
293
                path    => "/body"
294
            }
295
        ]
296
        );
297
298
    my $rota_to_delete  = $builder->build_object( { class => 'Koha::StockRotationRotas' } );
299
    my $non_existent_id = $rota_to_delete->id;
300
    $rota_to_delete->delete;
301
302
    $t->put_ok( "//$userid:$password@/api/v1/rotas/$non_existent_id" => json => $rota_with_updated_field )
303
        ->status_is(404);
304
305
    # Wrong method (POST)
306
    $rota_with_updated_field->{rota_id} = 2;
307
308
    $t->post_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => $rota_with_updated_field )->status_is(404);
309
310
    $schema->storage->txn_rollback;
311
};
312
313
subtest 'delete() tests' => sub {
314
315
    plan tests => 7;
316
317
    $schema->storage->txn_begin;
318
319
    my $librarian = $builder->build_object(
320
        {
321
            class => 'Koha::Patrons',
322
            value => { flags => 2**24 }    # stockrotation flag = 24
323
        }
324
    );
325
    my $password = 'thePassword123';
326
    $librarian->set_password( { password => $password, skip_validation => 1 } );
327
    my $userid = $librarian->userid;
328
329
    my $patron = $builder->build_object(
330
        {
331
            class => 'Koha::Patrons',
332
            value => { flags => 0 }
333
        }
334
    );
335
336
    $patron->set_password( { password => $password, skip_validation => 1 } );
337
    my $unauth_userid = $patron->userid;
338
339
    my $rota_id = $builder->build_object( { class => 'Koha::StockRotationRotas' } )->id;
340
341
    # Unauthorized attempt to delete
342
    $t->delete_ok("//$unauth_userid:$password@/api/v1/rotas/$rota_id")->status_is(403);
343
344
    $t->delete_ok("//$userid:$password@/api/v1/rotas/$rota_id")->status_is( 204, 'SWAGGER3.2.4' )
345
        ->content_is( '', 'SWAGGER3.3.4' );
346
347
    $t->delete_ok("//$userid:$password@/api/v1/rotas/$rota_id")->status_is(404);
348
349
    $schema->storage->txn_rollback;
350
};

Return to bug 35430