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 under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 5; |
21 |
use Test::Mojo; |
22 |
use Test::Warn; |
23 |
|
24 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
26 |
|
27 |
use Koha::AdvancedEditorMacros; |
28 |
use Koha::Database; |
29 |
|
30 |
my $schema = Koha::Database->new->schema; |
31 |
my $builder = t::lib::TestBuilder->new; |
32 |
|
33 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
34 |
|
35 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
36 |
|
37 |
$schema->storage->txn_begin; |
38 |
|
39 |
subtest 'list() tests' => sub { |
40 |
plan tests => 8; |
41 |
|
42 |
|
43 |
my $patron_1 = $builder->build_object({ |
44 |
class => 'Koha::Patrons', |
45 |
value => { flags => 9 } |
46 |
}); |
47 |
my $patron_2 = $builder->build_object({ |
48 |
class => 'Koha::Patrons', |
49 |
}); |
50 |
my $password = 'thePassword123'; |
51 |
$patron_1->set_password({ password => $password, skip_validation => 1 }); |
52 |
my $userid = $patron_1->userid; |
53 |
|
54 |
# Create test context |
55 |
my $macro_1 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value => |
56 |
{ |
57 |
name => 'Test1', |
58 |
macro => 'delete 100', |
59 |
borrowernumber => $patron_1->borrowernumber, |
60 |
} |
61 |
}); |
62 |
my $macro_2 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value => |
63 |
{ |
64 |
name => 'Test2', |
65 |
macro => 'delete 100', |
66 |
borrowernumber => $patron_1->borrowernumber, |
67 |
public => 1, |
68 |
} |
69 |
}); |
70 |
my $macro_3 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value => |
71 |
{ |
72 |
name => 'Test3', |
73 |
macro => 'delete 100', |
74 |
borrowernumber => $patron_2->borrowernumber, |
75 |
} |
76 |
}); |
77 |
my $macro_4 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value => |
78 |
{ |
79 |
name => 'Test4', |
80 |
macro => 'delete 100', |
81 |
borrowernumber => $patron_2->borrowernumber, |
82 |
public => 1, |
83 |
} |
84 |
}); |
85 |
|
86 |
my $macros_index = Koha::AdvancedEditorMacros->search({ -or => { public => 1, borrowernumber => $patron_1->borrowernumber } })->count-1; |
87 |
## Authorized user tests |
88 |
# Make sure we are returned with the correct amount of macros |
89 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros" ) |
90 |
->status_is( 200, 'SWAGGER3.2.2' ) |
91 |
->json_has('/' . $macros_index . '/macro_id') |
92 |
->json_hasnt('/' . ($macros_index + 1) . '/macro_id'); |
93 |
|
94 |
subtest 'query parameters' => sub { |
95 |
|
96 |
plan tests => 15; |
97 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?name=" . $macro_2->name) |
98 |
->status_is(200) |
99 |
->json_has( [ $macro_2 ] ); |
100 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?name=" . $macro_3->name) |
101 |
->status_is(200) |
102 |
->json_has( [ ] ); |
103 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?macro_text=delete 100") |
104 |
->status_is(200) |
105 |
->json_has( [ $macro_1, $macro_2, $macro_4 ] ); |
106 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?patron_id=" . $patron_1->borrowernumber) |
107 |
->status_is(200) |
108 |
->json_has( [ $macro_1, $macro_2 ] ); |
109 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?public=1") |
110 |
->status_is(200) |
111 |
->json_has( [ $macro_2, $macro_4 ] ); |
112 |
}; |
113 |
|
114 |
# Warn on unsupported query parameter |
115 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros?macro_blah=blah" ) |
116 |
->status_is(400) |
117 |
->json_is( [{ path => '/query/macro_blah', message => 'Malformed query string'}] ); |
118 |
|
119 |
}; |
120 |
|
121 |
subtest 'get() tests' => sub { |
122 |
|
123 |
plan tests => 12; |
124 |
|
125 |
my $patron = $builder->build_object({ |
126 |
class => 'Koha::Patrons', |
127 |
value => { flags => 1 } |
128 |
}); |
129 |
my $password = 'thePassword123'; |
130 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
131 |
my $userid = $patron->userid; |
132 |
|
133 |
my $macro_1 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
134 |
public => 1, |
135 |
} |
136 |
}); |
137 |
my $macro_2 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
138 |
public => 0, |
139 |
} |
140 |
}); |
141 |
my $macro_3 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
142 |
borrowernumber => $patron->borrowernumber, |
143 |
} |
144 |
}); |
145 |
|
146 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_1->id ) |
147 |
->status_is( 200, 'SWAGGER3.2.2' ) |
148 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_1->TO_JSON ), 'SWAGGER3.3.2' ); |
149 |
|
150 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_2->id ) |
151 |
->status_is( 403, 'SWAGGER3.2.2' ) |
152 |
->json_is( '/error' => 'You do not have permission to access this macro' ); |
153 |
|
154 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_3->id ) |
155 |
->status_is( 200, 'SWAGGER3.2.2' ) |
156 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'SWAGGER3.3.2' ); |
157 |
|
158 |
my $non_existent_code = $macro_1->id; |
159 |
$macro_1->delete; |
160 |
|
161 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $non_existent_code ) |
162 |
->status_is(404) |
163 |
->json_is( '/error' => 'Macro not found' ); |
164 |
|
165 |
}; |
166 |
|
167 |
subtest 'add() tests' => sub { |
168 |
|
169 |
plan tests => 20; |
170 |
|
171 |
my $authorized_patron = $builder->build_object({ |
172 |
class => 'Koha::Patrons', |
173 |
value => { flags => 0 } |
174 |
}); |
175 |
$builder->build({ |
176 |
source => 'UserPermission', |
177 |
value => { |
178 |
borrowernumber => $authorized_patron->borrowernumber, |
179 |
module_bit => 9, |
180 |
code => 'advanced_editor', |
181 |
}, |
182 |
}); |
183 |
|
184 |
my $password = 'thePassword123'; |
185 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
186 |
my $auth_userid = $authorized_patron->userid; |
187 |
|
188 |
my $unauthorized_patron = $builder->build_object({ |
189 |
class => 'Koha::Patrons', |
190 |
value => { flags => 0 } |
191 |
}); |
192 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
193 |
my $unauth_userid = $unauthorized_patron->userid; |
194 |
|
195 |
my $macro = $builder->build_object({ |
196 |
class => 'Koha::AdvancedEditorMacros', |
197 |
value => { public => 0 } |
198 |
}); |
199 |
my $macro_values = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON ); |
200 |
delete $macro_values->{macro_id}; |
201 |
$macro->delete; |
202 |
|
203 |
# Unauthorized attempt to write |
204 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
205 |
->status_is(403); |
206 |
|
207 |
# Authorized attempt to write invalid data |
208 |
my $macro_with_invalid_field = { %$macro_values }; |
209 |
$macro_with_invalid_field->{'big_mac_ro'} = 'Mac attack'; |
210 |
|
211 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_with_invalid_field ) |
212 |
->status_is(400) |
213 |
->json_is( |
214 |
"/errors" => [ |
215 |
{ |
216 |
message => "Properties not allowed: big_mac_ro.", |
217 |
path => "/body" |
218 |
} |
219 |
] |
220 |
); |
221 |
|
222 |
# Authorized attempt to write |
223 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
224 |
->status_is( 201, 'SWAGGER3.2.1' ) |
225 |
->json_has( '/macro_id', 'SWAGGER3.3.1' ) |
226 |
->json_is( '/name' => $macro_values->{name}, 'SWAGGER3.3.1' ) |
227 |
->json_is( '/macro_text' => $macro_values->{macro_text}, 'SWAGGER3.3.1' ) |
228 |
->json_is( '/patron_id' => $macro_values->{patron_id}, 'SWAGGER3.3.1' ) |
229 |
->json_is( '/public' => $macro_values->{public}, 'SWAGGER3.3.1' ) |
230 |
->header_like( Location => qr|^\/api\/v1\/advancededitormacros\/d*|, 'SWAGGER3.4.1' ); |
231 |
|
232 |
# save the library_id |
233 |
my $macro_id = 999; |
234 |
|
235 |
# Authorized attempt to create with existing id |
236 |
$macro_values->{macro_id} = $macro_id; |
237 |
|
238 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
239 |
->status_is(400) |
240 |
->json_is( '/errors' => [ |
241 |
{ |
242 |
message => "Read-only.", |
243 |
path => "/body/macro_id" |
244 |
} |
245 |
] |
246 |
); |
247 |
|
248 |
$macro_values->{public} = 1; |
249 |
delete $macro_values->{macro_id}; |
250 |
|
251 |
# Unauthorized attempt to write a public macro |
252 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
253 |
->status_is(403); |
254 |
|
255 |
$builder->build({ |
256 |
source => 'UserPermission', |
257 |
value => { |
258 |
borrowernumber => $authorized_patron->borrowernumber, |
259 |
module_bit => 9, |
260 |
code => 'create_public_macros', |
261 |
}, |
262 |
}); |
263 |
|
264 |
# Authorized attempt to write a public macro |
265 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
266 |
->status_is(201); |
267 |
|
268 |
}; |
269 |
|
270 |
subtest 'update() tests' => sub { |
271 |
plan tests => 28; |
272 |
|
273 |
my $authorized_patron = $builder->build_object({ |
274 |
class => 'Koha::Patrons', |
275 |
value => { flags => 0 } |
276 |
}); |
277 |
$builder->build({ |
278 |
source => 'UserPermission', |
279 |
value => { |
280 |
borrowernumber => $authorized_patron->borrowernumber, |
281 |
module_bit => 9, |
282 |
code => 'advanced_editor', |
283 |
}, |
284 |
}); |
285 |
|
286 |
my $password = 'thePassword123'; |
287 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
288 |
my $auth_userid = $authorized_patron->userid; |
289 |
|
290 |
my $unauthorized_patron = $builder->build_object({ |
291 |
class => 'Koha::Patrons', |
292 |
value => { flags => 0 } |
293 |
}); |
294 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
295 |
my $unauth_userid = $unauthorized_patron->userid; |
296 |
|
297 |
my $macro = $builder->build_object({ |
298 |
class => 'Koha::AdvancedEditorMacros', |
299 |
value => { borrowernumber => $authorized_patron->borrowernumber, public => 0 } |
300 |
}); |
301 |
my $macro_2 = $builder->build_object({ |
302 |
class => 'Koha::AdvancedEditorMacros', |
303 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, public => 0 } |
304 |
}); |
305 |
my $macro_id = $macro->id; |
306 |
my $macro_2_id = $macro_2->id; |
307 |
my $macro_values = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON ); |
308 |
delete $macro_values->{macro_id}; |
309 |
|
310 |
# Unauthorized attempt to update |
311 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/advancededitormacros/$macro_id" |
312 |
=> json => { name => 'New unauthorized name change' } ) |
313 |
->status_is(403); |
314 |
|
315 |
# Attempt partial update on a PUT |
316 |
my $macro_with_missing_field = { |
317 |
name => "Call it macro-roni", |
318 |
}; |
319 |
|
320 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_with_missing_field ) |
321 |
->status_is(400) |
322 |
->json_has( "/errors" => |
323 |
[ { message => "Missing property.", path => "/body/macro_text" } ] |
324 |
); |
325 |
|
326 |
my $macro_update = { |
327 |
name => "Macro-update", |
328 |
macro_text => "delete 100", |
329 |
patron_id => $authorized_patron->borrowernumber, |
330 |
public => 0, |
331 |
}; |
332 |
|
333 |
my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
334 |
->status_is(200, 'SWAGGER3.2.1') |
335 |
->json_has( '/macro_id', 'SWAGGER3.3.1' ) |
336 |
->json_is( '/name' => $macro_update->{name}, 'SWAGGER3.3.1' ) |
337 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'SWAGGER3.3.1' ) |
338 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'SWAGGER3.3.1' ) |
339 |
->json_is( '/public' => $macro_update->{public}, 'SWAGGER3.3.1' ); |
340 |
|
341 |
# Now try to make the macro public |
342 |
$macro_update->{public} = 1; |
343 |
|
344 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
345 |
->status_is(403, 'Cannot make your macro public without permission'); |
346 |
|
347 |
$builder->build({ |
348 |
source => 'UserPermission', |
349 |
value => { |
350 |
borrowernumber => $authorized_patron->borrowernumber, |
351 |
module_bit => 9, |
352 |
code => 'create_public_macros', |
353 |
}, |
354 |
}); |
355 |
|
356 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
357 |
->status_is(200, 'Can update macro to public with permission') |
358 |
->json_has( '/macro_id', 'SWAGGER3.3.1' ) |
359 |
->json_is( '/name' => $macro_update->{name}, 'SWAGGER3.3.1' ) |
360 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'SWAGGER3.3.1' ) |
361 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'SWAGGER3.3.1' ) |
362 |
->json_is( '/public' => $macro_update->{public}, 'SWAGGER3.3.1' ); |
363 |
|
364 |
# Authorized attempt to write invalid data |
365 |
my $macro_with_invalid_field = { %$macro_update }; |
366 |
$macro_with_invalid_field->{'big_mac_ro'} = 'Mac attack'; |
367 |
|
368 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_with_invalid_field ) |
369 |
->status_is(400) |
370 |
->json_is( |
371 |
"/errors" => [ |
372 |
{ |
373 |
message => "Properties not allowed: big_mac_ro.", |
374 |
path => "/body" |
375 |
} |
376 |
] |
377 |
); |
378 |
|
379 |
my $non_existent_macro = $builder->build_object({class => 'Koha::AdvancedEditorMacros'}); |
380 |
my $non_existent_code = $non_existent_macro->id; |
381 |
$non_existent_macro->delete; |
382 |
|
383 |
$t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$non_existent_code" => json => $macro_update) |
384 |
->status_is(404); |
385 |
|
386 |
$t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id" => json => $macro_update) |
387 |
->status_is(403, "Cannot update other borrowers non public macro"); |
388 |
}; |
389 |
|
390 |
subtest 'delete() tests' => sub { |
391 |
plan tests => 10; |
392 |
|
393 |
my $authorized_patron = $builder->build_object({ |
394 |
class => 'Koha::Patrons', |
395 |
value => { flags => 0 } |
396 |
}); |
397 |
$builder->build({ |
398 |
source => 'UserPermission', |
399 |
value => { |
400 |
borrowernumber => $authorized_patron->borrowernumber, |
401 |
module_bit => 9, |
402 |
code => 'advanced_editor', |
403 |
}, |
404 |
}); |
405 |
|
406 |
my $password = 'thePassword123'; |
407 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
408 |
my $auth_userid = $authorized_patron->userid; |
409 |
|
410 |
my $unauthorized_patron = $builder->build_object({ |
411 |
class => 'Koha::Patrons', |
412 |
value => { flags => 0 } |
413 |
}); |
414 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
415 |
my $unauth_userid = $unauthorized_patron->userid; |
416 |
|
417 |
my $macro = $builder->build_object({ |
418 |
class => 'Koha::AdvancedEditorMacros', |
419 |
value => { borrowernumber => $authorized_patron->borrowernumber, public => 0 } |
420 |
}); |
421 |
my $macro_2 = $builder->build_object({ |
422 |
class => 'Koha::AdvancedEditorMacros', |
423 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, public => 0 } |
424 |
}); |
425 |
my $macro_id = $macro->id; |
426 |
my $macro_2_id = $macro_2->id; |
427 |
|
428 |
# Unauthorized attempt to delete |
429 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
430 |
->status_is(403, "Cannot delete macro without permission"); |
431 |
|
432 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id") |
433 |
->status_is(200, 'Can delete macro with permission'); |
434 |
|
435 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
436 |
->status_is(403, 'Cannot delete other users macro with permission'); |
437 |
|
438 |
$macro_2->public(1)->store(); |
439 |
|
440 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
441 |
->status_is(403, 'Cannot delete other users public macro without permission'); |
442 |
|
443 |
$builder->build({ |
444 |
source => 'UserPermission', |
445 |
value => { |
446 |
borrowernumber => $authorized_patron->borrowernumber, |
447 |
module_bit => 9, |
448 |
code => 'delete_public_macros', |
449 |
}, |
450 |
}); |
451 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
452 |
->status_is(200, 'Can delete other users public macro with permission'); |
453 |
|
454 |
}; |
455 |
|
456 |
$schema->storage->txn_rollback; |