Debian Codegraph @

lemonldap-ng-manager/t/04-menu-api.t

raw
1 # Test Providers API
2
3 use warnings;
4 use Test::More;
5 use strict;
6 use JSON;
7 use IO::String;
8 require 't/test-lib.pm';
9
10 our $_json = JSON->new->allow_nonref;
11
12 sub check201 {
13 my ( $test, $res ) = splice @_;
14
15 #diag Dumper($res);
16 is( $res->[0], "201", "$test: Result code is 201" )
17 or diag explain $res->[2];
18 count(1);
19 checkJson( $test, $res );
20 }
21
22 sub check204 {
23 my ( $test, $res ) = splice @_;
24
25 #diag Dumper($res);
26 is( $res->[0], "204", "$test: Result code is 204" )
27 or diag explain $res->[2];
28 count(1);
29 is( $res->[2]->[0], undef, "204 code returns no content" );
30 }
31
32 sub check200 {
33 my ( $test, $res ) = splice @_;
34
35 #diag Dumper($res);
36 is( $res->[0], "200", "$test: Result code is 200" )
37 or diag explain $res->[2];
38 count(1);
39 checkJson( $test, $res );
40
41 }
42
43 sub check409 {
44 my ( $test, $res ) = splice @_;
45
46 #diag Dumper($res);
47 is( $res->[0], "409", "$test: Result code is 409" )
48 or diag explain $res->[2];
49 count(1);
50 checkJson( $test, $res );
51 }
52
53 sub check404 {
54 my ( $test, $res ) = splice @_;
55
56 #diag Dumper($res);
57 is( $res->[0], "404", "$test: Result code is 404" )
58 or diag explain $res->[2];
59 count(1);
60 checkJson( $test, $res );
61 }
62
63 sub check400 {
64 my ( $test, $res ) = splice @_;
65 is( $res->[0], "400", "$test: Result code is 400" )
66 or diag explain $res->[2];
67 count(1);
68 count(1);
69 checkJson( $test, $res );
70 }
71
72 sub checkJson {
73 my ( $test, $res ) = splice @_;
74 my $key;
75
76 #diag Dumper($res->[2]->[0]);
77 ok( $key = from_json( $res->[2]->[0] ), "$test: Response is JSON" );
78 count(1);
79 }
80
81 sub add {
82 my ( $test, $type, $obj ) = splice @_;
83 my $j = $_json->encode($obj);
84 my $res;
85 $type ||= '';
86
87 #diag Dumper($j);
88 ok(
89 $res = &client->_post(
90 "/api/v1/menu/$type", '',
91 IO::String->new($j), 'application/json',
92 length($j)
93 ),
94 "$test: Request succeed"
95 );
96 count(1);
97 return $res;
98 }
99
100 sub checkAdd {
101 my ( $test, $type, $add ) = splice @_;
102 check201( $test, add( $test, $type, $add ) );
103 }
104
105 sub checkAddNotFound {
106 my ( $test, $type, $add ) = splice @_;
107 check404( $test, add( $test, $type, $add ) );
108 }
109
110 sub checkAddFailsIfExists {
111 my ( $test, $type, $add ) = splice @_;
112 check409( $test, add( $test, $type, $add ) );
113 }
114
115 sub checkAddFailsOnInvalidConfkey {
116 my ( $test, $type, $add ) = splice @_;
117 check400( $test, add( $test, $type, $add ) );
118 }
119
120 sub get {
121 my ( $test, $type, $confKey ) = splice @_;
122 my $res;
123 ok( $res = &client->_get( "/api/v1/menu/$type/$confKey", '' ),
124 "$test: Request succeed" );
125 count(1);
126 return $res;
127 }
128
129 sub checkGet {
130 my ( $test, $type, $confKey, $attrPath, $expectedValue ) = splice @_;
131 my $res = get( $test, $type, $confKey );
132 check200( $test, $res );
133 my @path = split '/', $attrPath;
134 my $key = from_json( $res->[2]->[0] );
135 for (@path) {
136 if ( ref($key) eq 'ARRAY' ) {
137 $key = $key->[$_];
138 }
139 else {
140 $key = $key->{$_};
141 }
142 }
143 ok(
144 $key eq $expectedValue,
145 "$test: check if $attrPath value \"$key\" matches expected value \"$expectedValue\""
146 );
147 count(1);
148 }
149
150 sub checkGetNotFound {
151 my ( $test, $type, $confKey ) = splice @_;
152 check404( $test, get( $test, $type, $confKey ) );
153 }
154
155 sub checkGetList {
156 my ( $test, $type, $confKey, $expectedHits ) = splice @_;
157 my $res = get( $test, $type, $confKey );
158 check200( $test, $res );
159 my $hits = from_json( $res->[2]->[0] );
160 my $counter = @{$hits};
161 ok(
162 $counter eq $expectedHits,
163 "$test: check if nb of hits returned ($counter) matches expectation ($expectedHits)"
164 );
165 count(1);
166 }
167
168 sub update {
169 my ( $test, $type, $confKey, $obj ) = splice @_;
170 my $j = $_json->encode($obj);
171
172 #diag Dumper($j);
173 my $res;
174 ok(
175 $res = &client->_patch(
176 "/api/v1/menu/$type/$confKey", '',
177 IO::String->new($j), 'application/json',
178 length($j)
179 ),
180 "$test: Request succeed"
181 );
182 count(1);
183 return $res;
184 }
185
186 sub checkUpdate {
187 my ( $test, $type, $confKey, $update ) = splice @_;
188 check204( $test, update( $test, $type, $confKey, $update ) );
189 }
190
191 sub checkUpdateNotFound {
192 my ( $test, $type, $confKey, $update ) = splice @_;
193 check404( $test, update( $test, $type, $confKey, $update ) );
194 }
195
196 sub checkUpdateFailsIfExists {
197 my ( $test, $type, $confKey, $update ) = splice @_;
198 check409( $test, update( $test, $type, $confKey, $update ) );
199 }
200
201 sub checkUpdateWithUnknownAttributes {
202 my ( $test, $type, $confKey, $update ) = splice @_;
203 check400( $test, update( $test, $type, $confKey, $update ) );
204 }
205
206 sub replace {
207 my ( $test, $type, $confKey, $obj ) = splice @_;
208 my $j = $_json->encode($obj);
209 my $res;
210 ok(
211 $res = &client->_put(
212 "/api/v1/menu/$type/$confKey", '',
213 IO::String->new($j), 'application/json',
214 length($j)
215 ),
216 "$test: Request succeed"
217 );
218 count(1);
219 return $res;
220 }
221
222 sub checkReplace {
223 my ( $test, $type, $confKey, $replace ) = splice @_;
224 check204( $test, replace( $test, $type, $confKey, $replace ) );
225 }
226
227 sub checkReplaceAlreadyThere {
228 my ( $test, $type, $confKey, $replace ) = splice @_;
229 check400( $test, replace( $test, $type, $confKey, $replace ) );
230 }
231
232 sub checkReplaceNotFound {
233 my ( $test, $type, $confKey, $update ) = splice @_;
234 check404( $test, replace( $test, $type, $confKey, $update ) );
235 }
236
237 sub checkReplaceWithInvalidAttribute {
238 my ( $test, $type, $confKey, $replace ) = splice @_;
239 check400( $test, replace( $test, $type, $confKey, $replace ) );
240 }
241
242 sub findByConfKey {
243 my ( $test, $type, $confKey ) = splice @_;
244 my $res;
245 ok(
246 $res = &client->_get(
247 "/api/v1/menu/$type/findByConfKey",
248 "pattern=$confKey"
249 ),
250 "$test: Request succeed"
251 );
252 count(1);
253 return $res;
254 }
255
256 sub checkFindByConfKeyError {
257 my ( $test, $type, $pattern ) = splice @_;
258 my $res = findByConfKey( $test, $type, $pattern );
259 check400( $test, $res );
260 }
261
262 sub checkFindByConfKey {
263 my ( $test, $type, $confKey, $expectedHits ) = splice @_;
264 my $res = findByConfKey( $test, $type, $confKey );
265 check200( $test, $res );
266 my $hits = from_json( $res->[2]->[0] );
267 my $counter = @{$hits};
268 ok(
269 $counter eq $expectedHits,
270 "$test: check if nb of hits returned ($counter) matches expectation ($expectedHits)"
271 );
272 count(1);
273 }
274
275 sub deleteMenu {
276 my ( $test, $type, $confKey ) = splice @_;
277 my $res;
278 ok(
279 $res = &client->_del(
280 "/api/v1/menu/$type/$confKey", '', '', 'application/json', 0
281 ),
282 "$test: Request succeed"
283 );
284 count(1);
285 return $res;
286 }
287
288 sub checkDelete {
289 my ( $test, $type, $confKey ) = splice @_;
290 check204( $test, deleteMenu( $test, $type, $confKey ) );
291 }
292
293 sub checkDeleteNotFound {
294 my ( $test, $type, $confKey ) = splice @_;
295 check404( $test, deleteMenu( $test, $type, $confKey ) );
296 }
297
298 my $test;
299
300 my $cat1 = {
301 confKey => 'mycat1',
302 catname => 'My Cat 1',
303 order => 1
304 };
305 my $cat2 = {
306 confKey => 'mycat2',
307 catname => 'My Cat 2',
308 order => 2
309 };
310 my $cat3 = {
311 confKey => 'mycat/mycat3',
312 catname => 'My Cat 3',
313 order => 2
314 };
315 $test = "Cat - Get mycat1 cat should err on not found";
316 checkGetNotFound( $test, 'cat', 'mycat1' );
317
318 $test = "Cat - Add should succeed";
319 checkAdd( $test, 'cat', $cat1 );
320 checkGet( $test, 'cat', 'mycat1', 'catname', 'My Cat 1' );
321 checkGet( $test, 'cat', 'mycat1', 'order', 1 );
322
323 $test = "Cat - Add should fail on duplicate confKey";
324 checkAddFailsIfExists( $test, 'cat', $cat1 );
325
326 $test = "Cat - Add should fail on invalid confKey";
327 checkAddFailsOnInvalidConfkey( $test, 'cat', $cat3 );
328
329 checkAddFailsOnInvalidConfkey
330
331 $test = "Cat - Update should succeed and keep existing values";
332 $cat1->{order} = 3;
333 delete $cat1->{catname};
334 checkUpdate( $test, 'cat', 'mycat1', $cat1 );
335 checkGet( $test, 'cat', 'mycat1', 'catname', 'My Cat 1' );
336 checkGet( $test, 'cat', 'mycat1', 'order', 3 );
337
338 $test = "Cat - Update should fail if confKey not found";
339 $cat1->{confKey} = 'mycat3';
340 checkUpdateNotFound( $test, 'cat', 'mycat3', $cat1 );
341
342 $test = "Cat - 2nd add should succeed";
343 checkAdd( $test, 'cat', $cat2 );
344
345 $test = "Cat - Replace should succeed";
346 delete $cat2->{order};
347 checkReplace( $test, 'cat', 'mycat2', $cat2 );
348
349 $test = "Cat - Replace should fail if confKey not found";
350 $cat2->{confKey} = 'mycat3';
351 checkReplaceNotFound( $test, 'cat', 'mycat3', $cat2 );
352
353 $test = "Cat - FindByConfKey should find 2 hits";
354 checkFindByConfKey( $test, 'cat', 'mycat', 2 );
355
356 $test = "Cat - FindByConfKey should find 1 hits";
357 checkFindByConfKey( $test, 'cat', 'mycat1', 1 );
358
359 $test = "Cat - FindByConfKey should find 1 hits";
360 checkFindByConfKey( $test, 'cat', 'mycat2', 1 );
361
362 $test = "Cat - FindByConfKey should find 0 hits";
363 checkFindByConfKey( $test, 'cat', 'mycat3', 0 );
364
365 $test = "Cat - FindByConfKey should err on invalid patterns";
366 checkFindByConfKeyError( $test, 'cat', '' );
367 checkFindByConfKeyError( $test, 'cat', '$' );
368
369 my $app1 = {
370 confKey => 'myapp1',
371 options => {
372 name => 'My App 1',
373 description => 'My app 1 description',
374 tooltip => 'My app 1 tooltip',
375 uri => 'http://app1.example.com/'
376 },
377 order => 1
378 };
379 my $app2 = {
380 confKey => 'myapp2',
381 options => {
382 name => 'My App 2',
383 description => 'My app 2 description',
384 display => 'enabled',
385 logo => 'demo.png',
386 tooltip => 'My app 2 tooltip',
387 uri => 'http://app2.example.com/'
388 },
389 order => 2
390 };
391 my $app3 = {
392 confKey => 'myapp3',
393 options => {
394 name => 'My App 3',
395 description => 'My app 3 description',
396 display => "\$uid eq 'dwho'",
397 logo => 'attach.png',
398 tooltip => 'My app 3 tooltip',
399 uri => 'http://app3.example.com/'
400 },
401 order => 1
402 };
403 my $app4 = {
404 confKey => 'myapp1/myapp4',
405 options => {
406 name => 'My App 4',
407 description => 'My app 4 description',
408 tooltip => 'My app 4 tooltip',
409 uri => 'http://app4.example.com/'
410 },
411 order => 1
412 };
413
414 $test = "App - Get mycat3 apps should err on not found";
415 checkGetNotFound( $test, 'app', 'mycat3' );
416
417 $test = "App - Get app myapp1 from existing mycat2 should err on not found";
418 checkGetNotFound( $test, 'app/mycat2', 'myapp1' );
419
420 $test = "App - Get app myapp1 from mycat3 should err on not found";
421 checkGetNotFound( $test, 'app/mycat3', 'myapp1' );
422
423 $test = "App - Add app myapp1 to mycat3 should err on not found";
424 checkAddNotFound( $test, 'app/mycat3', $app1 );
425
426 $test = "App - Add app1 to cat1 should succeed";
427 checkAdd( $test, 'app/mycat1', $app1 );
428 checkGet( $test, 'app/mycat1', 'myapp1', 'order', '1' );
429 checkGet( $test, 'app/mycat1', 'myapp1', 'options/name', 'My App 1' );
430 checkGet( $test, 'app/mycat1', 'myapp1', 'options/description',
431 'My app 1 description' );
432 checkGet( $test, 'app/mycat1', 'myapp1', 'options/tooltip',
433 'My app 1 tooltip' );
434 checkGet( $test, 'app/mycat1', 'myapp1', 'options/uri',
435 'http://app1.example.com/' );
436
437 $test = "App - Add app2 to cat1 should succeed";
438 checkAdd( $test, 'app/mycat1', $app2 );
439 checkGet( $test, 'app/mycat1', 'myapp2', 'order', '2' );
440 checkGet( $test, 'app/mycat1', 'myapp2', 'options/name', 'My App 2' );
441 checkGet( $test, 'app/mycat1', 'myapp2', 'options/logo', 'demo.png' );
442
443 $test = "App - Add app3 to cat2 should succeed";
444 checkAdd( $test, 'app/mycat2', $app3 );
445 checkGet( $test, 'app/mycat2', 'myapp3', 'order', '1' );
446 checkGet( $test, 'app/mycat2', 'myapp3', 'options/display', "\$uid eq 'dwho'" );
447
448 $test = "App - Add should fail on duplicate confKey";
449 checkAddFailsIfExists( $test, 'app/mycat1', $app1 );
450
451 $test = "App - Add should fail on invalid confKey";
452 checkAddFailsOnInvalidConfkey( $test, 'app/mycat1', $app4 );
453
454 $test = "App - Check default value were set";
455 checkGet( $test, 'app/mycat1', 'myapp1', 'options/logo', 'network.png' );
456 checkGet( $test, 'app/mycat1', 'myapp1', 'options/display', 'auto' );
457
458 $test = "App - Category 1 should return 2 apps";
459 checkGetList( $test, 'app', 'mycat1', 2 );
460
461 $test = "App - Category 2 should return 1 app";
462 checkGetList( $test, 'app', 'mycat2', 1 );
463
464 $test = "App - FindByConfKey should find 2 hits";
465 checkFindByConfKey( $test, 'app/mycat1', '*', 2 );
466
467 $test = "App - FindByConfKey should find 1 hit";
468 checkFindByConfKey( $test, 'app/mycat1', 'app1', 1 );
469
470 $test = "App - FindByConfKey should err on invalid patterns";
471 checkFindByConfKeyError( $test, 'app/mycat1', '' );
472 checkFindByConfKeyError( $test, 'app/mycat1', '$' );
473
474 $test = "App - Update should succeed and keep existing values";
475 $app1->{options}->{name} = 'My App 1 updated';
476 delete $app1->{options}->{tooltip};
477 delete $app1->{order};
478 checkUpdate( $test, 'app/mycat1', 'myapp1', $app1 );
479 checkGet( $test, 'app/mycat1', 'myapp1', 'options/name', 'My App 1 updated' );
480 checkGet( $test, 'app/mycat1', 'myapp1', 'options/tooltip',
481 'My app 1 tooltip' );
482 checkGet( $test, 'app/mycat1', 'myapp1', 'order', 1 );
483
484 $test = "App - Update should fail if confKey not found";
485 checkUpdateNotFound( $test, 'app/mycat4', 'myapp1', $app1 );
486 $app1->{confKey} = 'myapp4';
487 checkUpdateNotFound( $test, 'app/mycat1', 'myapp4', $app1 );
488
489 $test = "App - Replace should succeed";
490 $app3->{options}->{name} = 'My App 3 updated';
491 checkReplace( $test, 'app/mycat2', 'myapp3', $app3 );
492 checkGet( $test, 'app/mycat2', 'myapp3', 'options/name', 'My App 3 updated' );
493
494 $test = "App - Replace should fail if confKey not found";
495 checkReplaceNotFound( $test, 'app/mycat4', 'myapp3', $app3 );
496 $app3->{confKey} = 'myapp4';
497 checkReplaceNotFound( $test, 'app/mycat2', 'myapp4', $app3 );
498
499 $test = "App - Delete should succeed";
500 checkDelete( $test, 'app/mycat1', 'myapp2' );
501
502 $test = "App - Entity should not be found after deletion";
503 checkDeleteNotFound( $test, 'app/mycat1', 'myapp2' );
504
505 $test = "App - Category 1 should return 1 app";
506 checkGetList( $test, 'app', 'mycat1', 1 );
507
508 $test = "Cat - Clean up";
509 checkDelete( $test, 'cat', 'mycat1' );
510 checkDelete( $test, 'cat', 'mycat2' );
511 $test = "cat - Entity should not be found after clean up";
512 checkDeleteNotFound( $test, 'cat', 'mycat1' );
513
514 # Clean up generated conf files, except for "lmConf-1.json"
515 unlink grep { $_ ne "$main::tmpdir/conf/lmConf-1.json" }
516 glob "$main::tmpdir/conf/lmConf-*.json";
517
518 done_testing();