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 |
shared=> 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 |
shared => 1, |
83 |
} |
84 |
}); |
85 |
|
86 |
my $macros_index = Koha::AdvancedEditorMacros->search({ -or => { shared => 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?shared=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 => 15; |
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 |
shared => 1, |
135 |
} |
136 |
}); |
137 |
my $macro_2 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
138 |
shared => 0, |
139 |
} |
140 |
}); |
141 |
my $macro_3 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
142 |
borrowernumber => $patron->borrowernumber, |
143 |
shared => 0, |
144 |
} |
145 |
}); |
146 |
|
147 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_1->id ) |
148 |
->status_is( 403, 'Cannot get a shared macro via regular endpoint' ) |
149 |
->json_is( '/error' => 'This macro is shared, you must access it via advancededitormacros/shared' ); |
150 |
|
151 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/shared/" . $macro_1->id ) |
152 |
->status_is( 200, 'Can get a shared macro via shared endpoint' ) |
153 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_1->TO_JSON ), 'Macro correctly retrieved' ); |
154 |
|
155 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_2->id ) |
156 |
->status_is( 403, 'Cannot access another users macro' ) |
157 |
->json_is( '/error' => 'You do not have permission to access this macro' ); |
158 |
|
159 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_3->id ) |
160 |
->status_is( 200, 'Can get your own private macro' ) |
161 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'Macro correctly retrieved' ); |
162 |
|
163 |
my $non_existent_code = $macro_1->id; |
164 |
$macro_1->delete; |
165 |
|
166 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $non_existent_code ) |
167 |
->status_is(404) |
168 |
->json_is( '/error' => 'Macro not found' ); |
169 |
|
170 |
}; |
171 |
|
172 |
subtest 'add() tests' => sub { |
173 |
|
174 |
plan tests => 24; |
175 |
|
176 |
my $authorized_patron = $builder->build_object({ |
177 |
class => 'Koha::Patrons', |
178 |
value => { flags => 0 } |
179 |
}); |
180 |
$builder->build({ |
181 |
source => 'UserPermission', |
182 |
value => { |
183 |
borrowernumber => $authorized_patron->borrowernumber, |
184 |
module_bit => 9, |
185 |
code => 'advanced_editor', |
186 |
}, |
187 |
}); |
188 |
|
189 |
my $password = 'thePassword123'; |
190 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
191 |
my $auth_userid = $authorized_patron->userid; |
192 |
|
193 |
my $unauthorized_patron = $builder->build_object({ |
194 |
class => 'Koha::Patrons', |
195 |
value => { flags => 0 } |
196 |
}); |
197 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
198 |
my $unauth_userid = $unauthorized_patron->userid; |
199 |
|
200 |
my $macro = $builder->build_object({ |
201 |
class => 'Koha::AdvancedEditorMacros', |
202 |
value => { shared => 0 } |
203 |
}); |
204 |
my $macro_values = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON ); |
205 |
delete $macro_values->{macro_id}; |
206 |
$macro->delete; |
207 |
|
208 |
# Unauthorized attempt to write |
209 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
210 |
->status_is(403); |
211 |
|
212 |
# Authorized attempt to write invalid data |
213 |
my $macro_with_invalid_field = { %$macro_values }; |
214 |
$macro_with_invalid_field->{'big_mac_ro'} = 'Mac attack'; |
215 |
|
216 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_with_invalid_field ) |
217 |
->status_is(400) |
218 |
->json_is( |
219 |
"/errors" => [ |
220 |
{ |
221 |
message => "Properties not allowed: big_mac_ro.", |
222 |
path => "/body" |
223 |
} |
224 |
] |
225 |
); |
226 |
|
227 |
# Authorized attempt to write |
228 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
229 |
->status_is( 201, 'SWAGGER3.2.1' ) |
230 |
->json_has( '/macro_id', 'We generated a new id' ) |
231 |
->json_is( '/name' => $macro_values->{name}, 'The name matches what we supplied' ) |
232 |
->json_is( '/macro_text' => $macro_values->{macro_text}, 'The text matches what we supplied' ) |
233 |
->json_is( '/patron_id' => $macro_values->{patron_id}, 'The borrower matches the borrower who submitted' ) |
234 |
->json_is( '/shared' => 0, 'The macro is not shared' ) |
235 |
->header_like( Location => qr|^\/api\/v1\/advancededitormacros\/d*|, 'Correct location' ); |
236 |
|
237 |
# save the library_id |
238 |
my $macro_id = 999; |
239 |
|
240 |
# Authorized attempt to create with existing id |
241 |
$macro_values->{macro_id} = $macro_id; |
242 |
|
243 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
244 |
->status_is(400) |
245 |
->json_is( '/errors' => [ |
246 |
{ |
247 |
message => "Read-only.", |
248 |
path => "/body/macro_id" |
249 |
} |
250 |
] |
251 |
); |
252 |
|
253 |
$macro_values->{shared} = 1; |
254 |
delete $macro_values->{macro_id}; |
255 |
|
256 |
# Unauthorized attempt to write a shared macro on private endpoint |
257 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
258 |
->status_is(403); |
259 |
# Unauthorized attempt to write a private macro on shared endpoint |
260 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared" => json => $macro_values ) |
261 |
->status_is(403); |
262 |
|
263 |
$builder->build({ |
264 |
source => 'UserPermission', |
265 |
value => { |
266 |
borrowernumber => $authorized_patron->borrowernumber, |
267 |
module_bit => 9, |
268 |
code => 'create_shared_macros', |
269 |
}, |
270 |
}); |
271 |
|
272 |
# Authorized attempt to write a shared macro on private endpoint |
273 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
274 |
->status_is(403); |
275 |
|
276 |
# Authorized attempt to write a shared macro on shared endpoint |
277 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared" => json => $macro_values ) |
278 |
->status_is(201); |
279 |
|
280 |
}; |
281 |
|
282 |
subtest 'update() tests' => sub { |
283 |
plan tests => 32; |
284 |
|
285 |
my $authorized_patron = $builder->build_object({ |
286 |
class => 'Koha::Patrons', |
287 |
value => { flags => 0 } |
288 |
}); |
289 |
$builder->build({ |
290 |
source => 'UserPermission', |
291 |
value => { |
292 |
borrowernumber => $authorized_patron->borrowernumber, |
293 |
module_bit => 9, |
294 |
code => 'advanced_editor', |
295 |
}, |
296 |
}); |
297 |
|
298 |
my $password = 'thePassword123'; |
299 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
300 |
my $auth_userid = $authorized_patron->userid; |
301 |
|
302 |
my $unauthorized_patron = $builder->build_object({ |
303 |
class => 'Koha::Patrons', |
304 |
value => { flags => 0 } |
305 |
}); |
306 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
307 |
my $unauth_userid = $unauthorized_patron->userid; |
308 |
|
309 |
my $macro = $builder->build_object({ |
310 |
class => 'Koha::AdvancedEditorMacros', |
311 |
value => { borrowernumber => $authorized_patron->borrowernumber, shared => 0 } |
312 |
}); |
313 |
my $macro_2 = $builder->build_object({ |
314 |
class => 'Koha::AdvancedEditorMacros', |
315 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, shared => 0 } |
316 |
}); |
317 |
my $macro_id = $macro->id; |
318 |
my $macro_2_id = $macro_2->id; |
319 |
my $macro_values = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON ); |
320 |
delete $macro_values->{macro_id}; |
321 |
|
322 |
# Unauthorized attempt to update |
323 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/advancededitormacros/$macro_id" |
324 |
=> json => { name => 'New unauthorized name change' } ) |
325 |
->status_is(403); |
326 |
|
327 |
# Attempt partial update on a PUT |
328 |
my $macro_with_missing_field = { |
329 |
name => "Call it macro-roni", |
330 |
}; |
331 |
|
332 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_with_missing_field ) |
333 |
->status_is(400) |
334 |
->json_has( "/errors" => |
335 |
[ { message => "Missing property.", path => "/body/macro_text" } ] |
336 |
); |
337 |
|
338 |
my $macro_update = { |
339 |
name => "Macro-update", |
340 |
macro_text => "delete 100", |
341 |
patron_id => $authorized_patron->borrowernumber, |
342 |
shared => 0, |
343 |
}; |
344 |
|
345 |
my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
346 |
->status_is(200, 'Authorized user can update a macro') |
347 |
->json_is( '/macro_id' => $macro_id, 'We get the id back' ) |
348 |
->json_is( '/name' => $macro_update->{name}, 'We get the name back' ) |
349 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'We get the text back' ) |
350 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'We get the patron_id back' ) |
351 |
->json_is( '/shared' => $macro_update->{shared}, 'It should still not be shared' ); |
352 |
|
353 |
# Now try to make the macro shared |
354 |
$macro_update->{shared} = 1; |
355 |
|
356 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update ) |
357 |
->status_is(403, 'Cannot make your macro shared on private endpoint'); |
358 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update ) |
359 |
->status_is(403, 'Cannot make your macro shared without permission'); |
360 |
|
361 |
$builder->build({ |
362 |
source => 'UserPermission', |
363 |
value => { |
364 |
borrowernumber => $authorized_patron->borrowernumber, |
365 |
module_bit => 9, |
366 |
code => 'create_shared_macros', |
367 |
}, |
368 |
}); |
369 |
|
370 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
371 |
->status_is(403, 'Cannot make your macro shared on the private endpoint'); |
372 |
|
373 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update ) |
374 |
->status_is(200, 'Can update macro to shared with permission') |
375 |
->json_is( '/macro_id' => $macro_id, 'We get back the id' ) |
376 |
->json_is( '/name' => $macro_update->{name}, 'We get back the name' ) |
377 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'We get back the text' ) |
378 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'We get back our patron id' ) |
379 |
->json_is( '/shared' => 1, 'It is shared' ); |
380 |
|
381 |
# Authorized attempt to write invalid data |
382 |
my $macro_with_invalid_field = { %$macro_update }; |
383 |
$macro_with_invalid_field->{'big_mac_ro'} = 'Mac attack'; |
384 |
|
385 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_with_invalid_field ) |
386 |
->status_is(400) |
387 |
->json_is( |
388 |
"/errors" => [ |
389 |
{ |
390 |
message => "Properties not allowed: big_mac_ro.", |
391 |
path => "/body" |
392 |
} |
393 |
] |
394 |
); |
395 |
|
396 |
my $non_existent_macro = $builder->build_object({class => 'Koha::AdvancedEditorMacros'}); |
397 |
my $non_existent_code = $non_existent_macro->id; |
398 |
$non_existent_macro->delete; |
399 |
|
400 |
$t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$non_existent_code" => json => $macro_update) |
401 |
->status_is(404); |
402 |
|
403 |
$t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id" => json => $macro_update) |
404 |
->status_is(403, "Cannot update other borrowers private macro"); |
405 |
}; |
406 |
|
407 |
subtest 'delete() tests' => sub { |
408 |
plan tests => 12; |
409 |
|
410 |
my $authorized_patron = $builder->build_object({ |
411 |
class => 'Koha::Patrons', |
412 |
value => { flags => 0 } |
413 |
}); |
414 |
$builder->build({ |
415 |
source => 'UserPermission', |
416 |
value => { |
417 |
borrowernumber => $authorized_patron->borrowernumber, |
418 |
module_bit => 9, |
419 |
code => 'advanced_editor', |
420 |
}, |
421 |
}); |
422 |
|
423 |
my $password = 'thePassword123'; |
424 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
425 |
my $auth_userid = $authorized_patron->userid; |
426 |
|
427 |
my $unauthorized_patron = $builder->build_object({ |
428 |
class => 'Koha::Patrons', |
429 |
value => { flags => 0 } |
430 |
}); |
431 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
432 |
my $unauth_userid = $unauthorized_patron->userid; |
433 |
|
434 |
my $macro = $builder->build_object({ |
435 |
class => 'Koha::AdvancedEditorMacros', |
436 |
value => { borrowernumber => $authorized_patron->borrowernumber, shared => 0 } |
437 |
}); |
438 |
my $macro_2 = $builder->build_object({ |
439 |
class => 'Koha::AdvancedEditorMacros', |
440 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, shared => 0 } |
441 |
}); |
442 |
my $macro_id = $macro->id; |
443 |
my $macro_2_id = $macro_2->id; |
444 |
|
445 |
# Unauthorized attempt to delete |
446 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
447 |
->status_is(403, "Cannot delete macro without permission"); |
448 |
|
449 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id") |
450 |
->status_is(200, 'Can delete macro with permission'); |
451 |
|
452 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
453 |
->status_is(403, 'Cannot delete other users macro with permission'); |
454 |
|
455 |
$macro_2->shared(1)->store(); |
456 |
|
457 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_2_id") |
458 |
->status_is(403, 'Cannot delete other users shared macro without permission'); |
459 |
|
460 |
$builder->build({ |
461 |
source => 'UserPermission', |
462 |
value => { |
463 |
borrowernumber => $authorized_patron->borrowernumber, |
464 |
module_bit => 9, |
465 |
code => 'delete_shared_macros', |
466 |
}, |
467 |
}); |
468 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
469 |
->status_is(403, 'Cannot delete other users shared macro with permission on private endpoint'); |
470 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_2_id") |
471 |
->status_is(200, 'Can delete other users shared macro with permission'); |
472 |
|
473 |
}; |
474 |
|
475 |
$schema->storage->txn_rollback; |