Lines 64-70
subtest 'list() tests' => sub {
Link Here
|
64 |
name => 'Test2', |
64 |
name => 'Test2', |
65 |
macro => 'delete 100', |
65 |
macro => 'delete 100', |
66 |
borrowernumber => $patron_1->borrowernumber, |
66 |
borrowernumber => $patron_1->borrowernumber, |
67 |
public => 1, |
67 |
shared=> 1, |
68 |
} |
68 |
} |
69 |
}); |
69 |
}); |
70 |
my $macro_3 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value => |
70 |
my $macro_3 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value => |
Lines 79-89
subtest 'list() tests' => sub {
Link Here
|
79 |
name => 'Test4', |
79 |
name => 'Test4', |
80 |
macro => 'delete 100', |
80 |
macro => 'delete 100', |
81 |
borrowernumber => $patron_2->borrowernumber, |
81 |
borrowernumber => $patron_2->borrowernumber, |
82 |
public => 1, |
82 |
shared => 1, |
83 |
} |
83 |
} |
84 |
}); |
84 |
}); |
85 |
|
85 |
|
86 |
my $macros_index = Koha::AdvancedEditorMacros->search({ -or => { public => 1, borrowernumber => $patron_1->borrowernumber } })->count-1; |
86 |
my $macros_index = Koha::AdvancedEditorMacros->search({ -or => { shared => 1, borrowernumber => $patron_1->borrowernumber } })->count-1; |
87 |
## Authorized user tests |
87 |
## Authorized user tests |
88 |
# Make sure we are returned with the correct amount of macros |
88 |
# Make sure we are returned with the correct amount of macros |
89 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros" ) |
89 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros" ) |
Lines 106-112
subtest 'list() tests' => sub {
Link Here
|
106 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?patron_id=" . $patron_1->borrowernumber) |
106 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?patron_id=" . $patron_1->borrowernumber) |
107 |
->status_is(200) |
107 |
->status_is(200) |
108 |
->json_has( [ $macro_1, $macro_2 ] ); |
108 |
->json_has( [ $macro_1, $macro_2 ] ); |
109 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?public=1") |
109 |
$t->get_ok("//$userid:$password@/api/v1/advancededitormacros?shared=1") |
110 |
->status_is(200) |
110 |
->status_is(200) |
111 |
->json_has( [ $macro_2, $macro_4 ] ); |
111 |
->json_has( [ $macro_2, $macro_4 ] ); |
112 |
}; |
112 |
}; |
Lines 120-126
subtest 'list() tests' => sub {
Link Here
|
120 |
|
120 |
|
121 |
subtest 'get() tests' => sub { |
121 |
subtest 'get() tests' => sub { |
122 |
|
122 |
|
123 |
plan tests => 12; |
123 |
plan tests => 15; |
124 |
|
124 |
|
125 |
my $patron = $builder->build_object({ |
125 |
my $patron = $builder->build_object({ |
126 |
class => 'Koha::Patrons', |
126 |
class => 'Koha::Patrons', |
Lines 131-159
subtest 'get() tests' => sub {
Link Here
|
131 |
my $userid = $patron->userid; |
131 |
my $userid = $patron->userid; |
132 |
|
132 |
|
133 |
my $macro_1 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
133 |
my $macro_1 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
134 |
public => 1, |
134 |
shared => 1, |
135 |
} |
135 |
} |
136 |
}); |
136 |
}); |
137 |
my $macro_2 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
137 |
my $macro_2 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
138 |
public => 0, |
138 |
shared => 0, |
139 |
} |
139 |
} |
140 |
}); |
140 |
}); |
141 |
my $macro_3 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
141 |
my $macro_3 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => { |
142 |
borrowernumber => $patron->borrowernumber, |
142 |
borrowernumber => $patron->borrowernumber, |
|
|
143 |
shared => 0, |
143 |
} |
144 |
} |
144 |
}); |
145 |
}); |
145 |
|
146 |
|
146 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_1->id ) |
147 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_1->id ) |
147 |
->status_is( 200, 'SWAGGER3.2.2' ) |
148 |
->status_is( 403, 'Cannot get a shared macro via regular endpoint' ) |
148 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_1->TO_JSON ), 'SWAGGER3.3.2' ); |
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' ); |
149 |
|
154 |
|
150 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_2->id ) |
155 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_2->id ) |
151 |
->status_is( 403, 'SWAGGER3.2.2' ) |
156 |
->status_is( 403, 'Cannot access another users macro' ) |
152 |
->json_is( '/error' => 'You do not have permission to access this macro' ); |
157 |
->json_is( '/error' => 'You do not have permission to access this macro' ); |
153 |
|
158 |
|
154 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_3->id ) |
159 |
$t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_3->id ) |
155 |
->status_is( 200, 'SWAGGER3.2.2' ) |
160 |
->status_is( 200, 'Can get your own private macro' ) |
156 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'SWAGGER3.3.2' ); |
161 |
->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'Macro correctly retrieved' ); |
157 |
|
162 |
|
158 |
my $non_existent_code = $macro_1->id; |
163 |
my $non_existent_code = $macro_1->id; |
159 |
$macro_1->delete; |
164 |
$macro_1->delete; |
Lines 166-172
subtest 'get() tests' => sub {
Link Here
|
166 |
|
171 |
|
167 |
subtest 'add() tests' => sub { |
172 |
subtest 'add() tests' => sub { |
168 |
|
173 |
|
169 |
plan tests => 20; |
174 |
plan tests => 24; |
170 |
|
175 |
|
171 |
my $authorized_patron = $builder->build_object({ |
176 |
my $authorized_patron = $builder->build_object({ |
172 |
class => 'Koha::Patrons', |
177 |
class => 'Koha::Patrons', |
Lines 194-200
subtest 'add() tests' => sub {
Link Here
|
194 |
|
199 |
|
195 |
my $macro = $builder->build_object({ |
200 |
my $macro = $builder->build_object({ |
196 |
class => 'Koha::AdvancedEditorMacros', |
201 |
class => 'Koha::AdvancedEditorMacros', |
197 |
value => { public => 0 } |
202 |
value => { shared => 0 } |
198 |
}); |
203 |
}); |
199 |
my $macro_values = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON ); |
204 |
my $macro_values = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON ); |
200 |
delete $macro_values->{macro_id}; |
205 |
delete $macro_values->{macro_id}; |
Lines 222-233
subtest 'add() tests' => sub {
Link Here
|
222 |
# Authorized attempt to write |
227 |
# Authorized attempt to write |
223 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
228 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
224 |
->status_is( 201, 'SWAGGER3.2.1' ) |
229 |
->status_is( 201, 'SWAGGER3.2.1' ) |
225 |
->json_has( '/macro_id', 'SWAGGER3.3.1' ) |
230 |
->json_has( '/macro_id', 'We generated a new id' ) |
226 |
->json_is( '/name' => $macro_values->{name}, 'SWAGGER3.3.1' ) |
231 |
->json_is( '/name' => $macro_values->{name}, 'The name matches what we supplied' ) |
227 |
->json_is( '/macro_text' => $macro_values->{macro_text}, 'SWAGGER3.3.1' ) |
232 |
->json_is( '/macro_text' => $macro_values->{macro_text}, 'The text matches what we supplied' ) |
228 |
->json_is( '/patron_id' => $macro_values->{patron_id}, 'SWAGGER3.3.1' ) |
233 |
->json_is( '/patron_id' => $macro_values->{patron_id}, 'The borrower matches the borrower who submitted' ) |
229 |
->json_is( '/public' => $macro_values->{public}, 'SWAGGER3.3.1' ) |
234 |
->json_is( '/shared' => 0, 'The macro is not shared' ) |
230 |
->header_like( Location => qr|^\/api\/v1\/advancededitormacros\/d*|, 'SWAGGER3.4.1' ); |
235 |
->header_like( Location => qr|^\/api\/v1\/advancededitormacros\/d*|, 'Correct location' ); |
231 |
|
236 |
|
232 |
# save the library_id |
237 |
# save the library_id |
233 |
my $macro_id = 999; |
238 |
my $macro_id = 999; |
Lines 245-274
subtest 'add() tests' => sub {
Link Here
|
245 |
] |
250 |
] |
246 |
); |
251 |
); |
247 |
|
252 |
|
248 |
$macro_values->{public} = 1; |
253 |
$macro_values->{shared} = 1; |
249 |
delete $macro_values->{macro_id}; |
254 |
delete $macro_values->{macro_id}; |
250 |
|
255 |
|
251 |
# Unauthorized attempt to write a public macro |
256 |
# Unauthorized attempt to write a shared macro on private endpoint |
252 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
257 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
253 |
->status_is(403); |
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); |
254 |
|
262 |
|
255 |
$builder->build({ |
263 |
$builder->build({ |
256 |
source => 'UserPermission', |
264 |
source => 'UserPermission', |
257 |
value => { |
265 |
value => { |
258 |
borrowernumber => $authorized_patron->borrowernumber, |
266 |
borrowernumber => $authorized_patron->borrowernumber, |
259 |
module_bit => 9, |
267 |
module_bit => 9, |
260 |
code => 'create_public_macros', |
268 |
code => 'create_shared_macros', |
261 |
}, |
269 |
}, |
262 |
}); |
270 |
}); |
263 |
|
271 |
|
264 |
# Authorized attempt to write a public macro |
272 |
# Authorized attempt to write a shared macro on private endpoint |
265 |
$t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values ) |
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 ) |
266 |
->status_is(201); |
278 |
->status_is(201); |
267 |
|
279 |
|
268 |
}; |
280 |
}; |
269 |
|
281 |
|
270 |
subtest 'update() tests' => sub { |
282 |
subtest 'update() tests' => sub { |
271 |
plan tests => 28; |
283 |
plan tests => 32; |
272 |
|
284 |
|
273 |
my $authorized_patron = $builder->build_object({ |
285 |
my $authorized_patron = $builder->build_object({ |
274 |
class => 'Koha::Patrons', |
286 |
class => 'Koha::Patrons', |
Lines 296-306
subtest 'update() tests' => sub {
Link Here
|
296 |
|
308 |
|
297 |
my $macro = $builder->build_object({ |
309 |
my $macro = $builder->build_object({ |
298 |
class => 'Koha::AdvancedEditorMacros', |
310 |
class => 'Koha::AdvancedEditorMacros', |
299 |
value => { borrowernumber => $authorized_patron->borrowernumber, public => 0 } |
311 |
value => { borrowernumber => $authorized_patron->borrowernumber, shared => 0 } |
300 |
}); |
312 |
}); |
301 |
my $macro_2 = $builder->build_object({ |
313 |
my $macro_2 = $builder->build_object({ |
302 |
class => 'Koha::AdvancedEditorMacros', |
314 |
class => 'Koha::AdvancedEditorMacros', |
303 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, public => 0 } |
315 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, shared => 0 } |
304 |
}); |
316 |
}); |
305 |
my $macro_id = $macro->id; |
317 |
my $macro_id = $macro->id; |
306 |
my $macro_2_id = $macro_2->id; |
318 |
my $macro_2_id = $macro_2->id; |
Lines 327-365
subtest 'update() tests' => sub {
Link Here
|
327 |
name => "Macro-update", |
339 |
name => "Macro-update", |
328 |
macro_text => "delete 100", |
340 |
macro_text => "delete 100", |
329 |
patron_id => $authorized_patron->borrowernumber, |
341 |
patron_id => $authorized_patron->borrowernumber, |
330 |
public => 0, |
342 |
shared => 0, |
331 |
}; |
343 |
}; |
332 |
|
344 |
|
333 |
my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
345 |
my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
334 |
->status_is(200, 'SWAGGER3.2.1') |
346 |
->status_is(200, 'Authorized user can update a macro') |
335 |
->json_has( '/macro_id', 'SWAGGER3.3.1' ) |
347 |
->json_is( '/macro_id' => $macro_id, 'We get the id back' ) |
336 |
->json_is( '/name' => $macro_update->{name}, 'SWAGGER3.3.1' ) |
348 |
->json_is( '/name' => $macro_update->{name}, 'We get the name back' ) |
337 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'SWAGGER3.3.1' ) |
349 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'We get the text back' ) |
338 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'SWAGGER3.3.1' ) |
350 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'We get the patron_id back' ) |
339 |
->json_is( '/public' => $macro_update->{public}, 'SWAGGER3.3.1' ); |
351 |
->json_is( '/shared' => $macro_update->{shared}, 'It should still not be shared' ); |
340 |
|
352 |
|
341 |
# Now try to make the macro public |
353 |
# Now try to make the macro shared |
342 |
$macro_update->{public} = 1; |
354 |
$macro_update->{shared} = 1; |
343 |
|
355 |
|
344 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
356 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update ) |
345 |
->status_is(403, 'Cannot make your macro public without permission'); |
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'); |
346 |
|
360 |
|
347 |
$builder->build({ |
361 |
$builder->build({ |
348 |
source => 'UserPermission', |
362 |
source => 'UserPermission', |
349 |
value => { |
363 |
value => { |
350 |
borrowernumber => $authorized_patron->borrowernumber, |
364 |
borrowernumber => $authorized_patron->borrowernumber, |
351 |
module_bit => 9, |
365 |
module_bit => 9, |
352 |
code => 'create_public_macros', |
366 |
code => 'create_shared_macros', |
353 |
}, |
367 |
}, |
354 |
}); |
368 |
}); |
355 |
|
369 |
|
356 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update ) |
370 |
$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') |
371 |
->status_is(403, 'Cannot make your macro shared on the private endpoint'); |
358 |
->json_has( '/macro_id', 'SWAGGER3.3.1' ) |
372 |
|
359 |
->json_is( '/name' => $macro_update->{name}, 'SWAGGER3.3.1' ) |
373 |
$t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update ) |
360 |
->json_is( '/macro_text' => $macro_update->{macro_text}, 'SWAGGER3.3.1' ) |
374 |
->status_is(200, 'Can update macro to shared with permission') |
361 |
->json_is( '/patron_id' => $macro_update->{patron_id}, 'SWAGGER3.3.1' ) |
375 |
->json_is( '/macro_id' => $macro_id, 'We get back the id' ) |
362 |
->json_is( '/public' => $macro_update->{public}, 'SWAGGER3.3.1' ); |
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' ); |
363 |
|
380 |
|
364 |
# Authorized attempt to write invalid data |
381 |
# Authorized attempt to write invalid data |
365 |
my $macro_with_invalid_field = { %$macro_update }; |
382 |
my $macro_with_invalid_field = { %$macro_update }; |
Lines 384-394
subtest 'update() tests' => sub {
Link Here
|
384 |
->status_is(404); |
401 |
->status_is(404); |
385 |
|
402 |
|
386 |
$t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id" => json => $macro_update) |
403 |
$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"); |
404 |
->status_is(403, "Cannot update other borrowers private macro"); |
388 |
}; |
405 |
}; |
389 |
|
406 |
|
390 |
subtest 'delete() tests' => sub { |
407 |
subtest 'delete() tests' => sub { |
391 |
plan tests => 10; |
408 |
plan tests => 12; |
392 |
|
409 |
|
393 |
my $authorized_patron = $builder->build_object({ |
410 |
my $authorized_patron = $builder->build_object({ |
394 |
class => 'Koha::Patrons', |
411 |
class => 'Koha::Patrons', |
Lines 416-426
subtest 'delete() tests' => sub {
Link Here
|
416 |
|
433 |
|
417 |
my $macro = $builder->build_object({ |
434 |
my $macro = $builder->build_object({ |
418 |
class => 'Koha::AdvancedEditorMacros', |
435 |
class => 'Koha::AdvancedEditorMacros', |
419 |
value => { borrowernumber => $authorized_patron->borrowernumber, public => 0 } |
436 |
value => { borrowernumber => $authorized_patron->borrowernumber, shared => 0 } |
420 |
}); |
437 |
}); |
421 |
my $macro_2 = $builder->build_object({ |
438 |
my $macro_2 = $builder->build_object({ |
422 |
class => 'Koha::AdvancedEditorMacros', |
439 |
class => 'Koha::AdvancedEditorMacros', |
423 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, public => 0 } |
440 |
value => { borrowernumber => $unauthorized_patron->borrowernumber, shared => 0 } |
424 |
}); |
441 |
}); |
425 |
my $macro_id = $macro->id; |
442 |
my $macro_id = $macro->id; |
426 |
my $macro_2_id = $macro_2->id; |
443 |
my $macro_2_id = $macro_2->id; |
Lines 435-455
subtest 'delete() tests' => sub {
Link Here
|
435 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
452 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
436 |
->status_is(403, 'Cannot delete other users macro with permission'); |
453 |
->status_is(403, 'Cannot delete other users macro with permission'); |
437 |
|
454 |
|
438 |
$macro_2->public(1)->store(); |
455 |
$macro_2->shared(1)->store(); |
439 |
|
456 |
|
440 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
457 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_2_id") |
441 |
->status_is(403, 'Cannot delete other users public macro without permission'); |
458 |
->status_is(403, 'Cannot delete other users shared macro without permission'); |
442 |
|
459 |
|
443 |
$builder->build({ |
460 |
$builder->build({ |
444 |
source => 'UserPermission', |
461 |
source => 'UserPermission', |
445 |
value => { |
462 |
value => { |
446 |
borrowernumber => $authorized_patron->borrowernumber, |
463 |
borrowernumber => $authorized_patron->borrowernumber, |
447 |
module_bit => 9, |
464 |
module_bit => 9, |
448 |
code => 'delete_public_macros', |
465 |
code => 'delete_shared_macros', |
449 |
}, |
466 |
}, |
450 |
}); |
467 |
}); |
451 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
468 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id") |
452 |
->status_is(200, 'Can delete other users public macro with permission'); |
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'); |
453 |
|
472 |
|
454 |
}; |
473 |
}; |
455 |
|
474 |
|
456 |
- |
|
|