Debian Codegraph @

lemonldap-ng-manager/t/04-providers-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
86 #diag Dumper($j);
87 ok(
88 $res = &client->_post(
89 "/api/v1/providers/$type", '',
90 IO::String->new($j), 'application/json',
91 length($j)
92 ),
93 "$test: Request succeed"
94 );
95 count(1);
96 return $res;
97 }
98
99 sub checkAdd {
100 my ( $test, $type, $add ) = splice @_;
101 check201( $test, add( $test, $type, $add ) );
102 }
103
104 sub checkAddFailsIfExists {
105 my ( $test, $type, $add ) = splice @_;
106 check409( $test, add( $test, $type, $add ) );
107 }
108
109 sub checkAddWithUnknownAttributes {
110 my ( $test, $type, $add ) = splice @_;
111 check400( $test, add( $test, $type, $add ) );
112 }
113
114 sub get {
115 my ( $test, $type, $confKey ) = splice @_;
116 my $res;
117 ok( $res = &client->_get( "/api/v1/providers/$type/$confKey", '' ),
118 "$test: Request succeed" );
119 count(1);
120 return $res;
121 }
122
123 sub checkGet {
124 local $Test::Builder::Level = $Test::Builder::Level + 1;
125 my ( $test, $type, $confKey, $attrPath, $expectedValue ) = splice @_;
126 my $res = get( $test, $type, $confKey );
127 check200( $test, $res );
128 my @path = split '/', $attrPath;
129 my $key = from_json( $res->[2]->[0] );
130 for (@path) {
131 if ( ref($key) eq 'ARRAY' ) {
132 if ( exists $key->[$_] ) {
133 if ( defined $key->[$_] ) {
134 $key = $key->[$_];
135 }
136 else {
137 $key = "_undef";
138 }
139 }
140 else {
141 $key = "_missing";
142 }
143 }
144 else {
145 if ( exists $key->{$_} ) {
146 if ( defined $key->{$_} ) {
147 $key = $key->{$_};
148 }
149 else {
150 $key = "_undef";
151 }
152 }
153 else {
154 $key = "_missing";
155 }
156 }
157 }
158 is( $key, $expectedValue,
159 "$test: check if $attrPath value \"$key\" matches expected value \"$expectedValue\""
160 );
161 count(1);
162 }
163
164 sub checkGetNotFound {
165 my ( $test, $type, $confKey ) = splice @_;
166 check404( $test, get( $test, $type, $confKey ) );
167 }
168
169 sub update {
170 my ( $test, $type, $confKey, $obj ) = splice @_;
171 my $j = $_json->encode($obj);
172
173 #diag Dumper($j);
174 my $res;
175 ok(
176 $res = &client->_patch(
177 "/api/v1/providers/$type/$confKey", '',
178 IO::String->new($j), 'application/json',
179 length($j)
180 ),
181 "$test: Request succeed"
182 );
183 count(1);
184 return $res;
185 }
186
187 sub checkUpdate {
188 my ( $test, $type, $confKey, $update ) = splice @_;
189 check204( $test, update( $test, $type, $confKey, $update ) );
190 }
191
192 sub checkUpdateNotFound {
193 my ( $test, $type, $confKey, $update ) = splice @_;
194 check404( $test, update( $test, $type, $confKey, $update ) );
195 }
196
197 sub checkUpdateFailsIfExists {
198 my ( $test, $type, $confKey, $update ) = splice @_;
199 check409( $test, update( $test, $type, $confKey, $update ) );
200 }
201
202 sub checkUpdateWithUnknownAttributes {
203 my ( $test, $type, $confKey, $update ) = splice @_;
204 check400( $test, update( $test, $type, $confKey, $update ) );
205 }
206
207 sub replace {
208 my ( $test, $type, $confKey, $obj ) = splice @_;
209 my $j = $_json->encode($obj);
210 my $res;
211 ok(
212 $res = &client->_put(
213 "/api/v1/providers/$type/$confKey", '',
214 IO::String->new($j), 'application/json',
215 length($j)
216 ),
217 "$test: Request succeed"
218 );
219 count(1);
220 return $res;
221 }
222
223 sub checkReplace {
224 my ( $test, $type, $confKey, $replace ) = splice @_;
225 check204( $test, replace( $test, $type, $confKey, $replace ) );
226 }
227
228 sub checkReplaceAlreadyThere {
229 my ( $test, $type, $confKey, $replace ) = splice @_;
230 check400( $test, replace( $test, $type, $confKey, $replace ) );
231 }
232
233 sub checkReplaceNotFound {
234 my ( $test, $type, $confKey, $update ) = splice @_;
235 check404( $test, replace( $test, $type, $confKey, $update ) );
236 }
237
238 sub checkReplaceWithInvalidAttribute {
239 my ( $test, $type, $confKey, $replace ) = splice @_;
240 check400( $test, replace( $test, $type, $confKey, $replace ) );
241 }
242
243 sub findByConfKey {
244 my ( $test, $type, $confKey ) = splice @_;
245 my $res;
246 ok(
247 $res = &client->_get(
248 "/api/v1/providers/$type/findByConfKey",
249 "pattern=$confKey"
250 ),
251 "$test: Request succeed"
252 );
253 count(1);
254 return $res;
255 }
256
257 sub checkFindByConfKeyError {
258 my ( $test, $type, $pattern ) = splice @_;
259 my $res = findByConfKey( $test, $type, $pattern );
260 check400( $test, $res );
261 }
262
263 sub checkFindByConfKey {
264 my ( $test, $type, $confKey, $expectedHits ) = splice @_;
265 my $res = findByConfKey( $test, $type, $confKey );
266 check200( $test, $res );
267 my $hits = from_json( $res->[2]->[0] );
268 my $counter = @{$hits};
269 ok(
270 $counter eq $expectedHits,
271 "$test: check if nb of hits returned ($counter) matches expectation ($expectedHits)"
272 );
273 count(1);
274 }
275
276 sub findByProviderId {
277 my ( $test, $type, $providerIdName, $providerId ) = splice @_;
278 my $res;
279 ok(
280 $res = &client->_get(
281 "/api/v1/providers/$type/findBy" . ucfirst $providerIdName,
282 "$providerIdName=$providerId"
283 ),
284 "$test: Request succeed"
285 );
286 count(1);
287 return $res;
288 }
289
290 sub checkFindByProviderId {
291 my ( $test, $type, $providerIdName, $providerId ) = splice @_;
292 my $res = findByProviderId( $test, $type, $providerIdName, $providerId );
293 check200( $test, $res );
294 my $result = from_json( $res->[2]->[0] );
295 my $gotProviderId;
296 if ( $providerIdName eq 'entityId' ) {
297 if ( $result->{metadata} ) {
298 ($gotProviderId) =
299 $result->{metadata} =~ m/entityID=['"](.+?)['"]/i;
300 }
301 $gotProviderId ||= $result->{options}->{federationEntityID};
302
303 }
304 elsif ( $providerIdName eq 'serviceUrl' ) {
305 $gotProviderId = shift @{ $result->{options}->{service} };
306 }
307 else {
308 $gotProviderId = $result->{$providerIdName};
309 }
310 ok(
311 $gotProviderId eq $providerId,
312 "$test: Check $providerIdName value returned \"$gotProviderId\" matched expected value \"$providerId\""
313 );
314 count(1);
315 }
316
317 sub checkFindByProviderIdNotFound {
318 my ( $test, $type, $providerIdName, $providerId ) = splice @_;
319 check404( $test,
320 findByProviderId( $test, $type, $providerIdName, $providerId ) );
321 }
322
323 sub deleteProvider {
324 my ( $test, $type, $confKey ) = splice @_;
325 my $res;
326 ok(
327 $res = &client->_del(
328 "/api/v1/providers/$type/$confKey",
329 '', '', 'application/json', 0
330 ),
331 "$test: Request succeed"
332 );
333 count(1);
334 return $res;
335 }
336
337 sub checkDelete {
338 my ( $test, $type, $confKey ) = splice @_;
339 check204( $test, deleteProvider( $test, $type, $confKey ) );
340 }
341
342 sub checkDeleteNotFound {
343 my ( $test, $type, $confKey ) = splice @_;
344 check404( $test, deleteProvider( $test, $type, $confKey ) );
345 }
346
347 my $test;
348
349 my $oidcRp = {
350 confKey => 'myOidcRp1',
351 clientId => 'myOidcClient0',
352 exportedVars => {
353 'sub' => "uid",
354 family_name => "sn",
355 given_name => "givenName"
356 },
357 macros => {
358 given_name => '$cn',
359 },
360 redirectUris => [ "http://url/1", "http://url/2", ],
361 extraClaims => {
362 phone => 'telephoneNumber',
363 email => 'mail',
364 },
365 options => {
366 clientSecret => 'secret',
367 icon => 'web.png',
368 postLogoutRedirectUris =>
369 [ "http://url/logout1", "http://url/logout2" ],
370 },
371 scopeRules => {
372 "myscope" => 1,
373 },
374 };
375
376 $test = "OidcRp - Add should succeed";
377 checkAdd( $test, 'oidc/rp', $oidcRp );
378 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/icon', 'web.png' );
379 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/clientId', 'myOidcClient0' );
380 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/redirectUris/0',
381 'http://url/1' );
382 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'scopeRules/myscope', '1' );
383 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/clientSecret', 'secret' );
384
385 $test = "OidcRp - Check attribute default value was set after add";
386 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/IDTokenSignAlg', 'RS256' );
387
388 $test = "OidcRp - Add should fail on duplicate confKey";
389 checkAddFailsIfExists( $test, 'oidc/rp', $oidcRp );
390
391 $test =
392 "OidcRp - Update should succeed, keep existing values, drop undefined ones";
393 $oidcRp->{options}->{clientId} = 'myOidcClient1';
394 $oidcRp->{options}->{clientSecret} = 'secret2';
395 $oidcRp->{options}->{IDTokenSignAlg} = 'RS512';
396 delete $oidcRp->{options}->{icon};
397 delete $oidcRp->{extraClaims};
398 delete $oidcRp->{exportedVars};
399 delete $oidcRp->{clientId};
400 $oidcRp->{macros}->{given_name} = undef;
401 $oidcRp->{macros}->{cn} = '$cn';
402 $oidcRp->{exportedVars}->{cn} = 'cn';
403 checkUpdate( $test, 'oidc/rp', 'myOidcRp1', $oidcRp );
404 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/clientSecret', 'secret2' );
405 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/clientId', 'myOidcClient1' );
406 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/IDTokenSignAlg', 'RS512' );
407 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/icon', 'web.png' );
408 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'exportedVars/cn', 'cn' );
409 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'exportedVars/family_name', 'sn' );
410 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'macros/cn', '$cn' );
411 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'macros/given_name', "_missing" );
412 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'extraClaims/phone',
413 'telephoneNumber' );
414 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/redirectUris/1',
415 'http://url/2' );
416 checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/postLogoutRedirectUris/1',
417 'http://url/logout2' );
418
419 $test = "OidcRp - Update should fail on non existing options";
420 $oidcRp->{options}->{playingPossum} = 'elephant';
421 checkUpdateWithUnknownAttributes( $test, 'oidc/rp', 'myOidcRp1', $oidcRp );
422 delete $oidcRp->{options}->{playingPossum};
423
424 $test = "OidcRp - Add should fail on duplicate clientId";
425 $oidcRp->{clientId} = "myOidcClient1";
426 $oidcRp->{confKey} = 'myOidcRp2';
427 checkAddFailsIfExists( $test, 'oidc/rp', $oidcRp );
428
429 $test = "OidcRp - Add should fail on non existing options";
430 $oidcRp->{confKey} = 'myOidcRp2';
431 $oidcRp->{clientId} = 'myOidcClient2';
432 $oidcRp->{options}->{playingPossum} = 'ElephantInTheRoom';
433 checkAddWithUnknownAttributes( $test, 'oidc/rp', $oidcRp );
434 delete $oidcRp->{options}->{playingPossum};
435
436 $test = "OidcRp - 2nd add should succeed";
437 checkAdd( $test, 'oidc/rp', $oidcRp );
438
439 $test = "OidcRp - Update should fail if client id exists";
440 $oidcRp->{clientId} = 'myOidcClient1';
441 checkUpdateFailsIfExists( $test, 'oidc/rp', 'myOidcRp2', $oidcRp );
442
443 $test = "OidcRp - Update should fail if confKey not found";
444 $oidcRp->{confKey} = 'myOidcRp3';
445 checkUpdateNotFound( $test, 'oidc/rp', 'myOidcRp3', $oidcRp );
446
447 $test = "OidcRp - Replace should succeed";
448 $oidcRp->{confKey} = 'myOidcRp2';
449 $oidcRp->{clientId} = 'myOidcClient2';
450 $oidcRp->{redirectUris} = ["http://url/3"];
451 $oidcRp->{options}->{postLogoutRedirectUris} = [];
452 delete $oidcRp->{options}->{icon};
453 delete $oidcRp->{options}->{IDTokenSignAlg};
454 checkReplace( $test, 'oidc/rp', 'myOidcRp2', $oidcRp );
455
456 $test = "OidcRp - Check attribute default value was set after replace";
457 checkGet( $test, 'oidc/rp', 'myOidcRp2', 'options/IDTokenSignAlg', 'RS256' );
458 checkGet( $test, 'oidc/rp', 'myOidcRp2', 'options/redirectUris/0',
459 'http://url/3' );
460 checkGet( $test, 'oidc/rp', 'myOidcRp2', 'options/postLogoutRedirectUris/0',
461 '_missing' );
462
463 $test = "OidcRp - Replace should fail on non existing or invalid options";
464 $oidcRp->{options}->{playingPossum} = 'elephant';
465 checkReplaceWithInvalidAttribute( $test, 'oidc/rp', 'myOidcRp2', $oidcRp );
466 delete $oidcRp->{options}->{playingPossum};
467 $oidcRp->{options}->{IDTokenExpiration} = "XXX";
468 checkReplaceWithInvalidAttribute( $test, 'oidc/rp', 'myOidcRp2', $oidcRp );
469
470 $test = "OidcRp - Replace should fail if confKey not found";
471 $oidcRp->{confKey} = 'myOidcRp3';
472 checkReplaceNotFound( $test, 'oidc/rp', 'myOidcRp3', $oidcRp );
473
474 $test = "OidcRp - FindByConfKey should find 2 hits";
475 checkFindByConfKey( $test, 'oidc/rp', '*', 2 );
476
477 $test = "OidcRp - FindByConfKey should find 2 hits";
478 checkFindByConfKey( $test, 'oidc/rp', 'myOidcRp*', 2 );
479
480 $test = "OidcRp - FindByConfKey should find 1 hit";
481 checkFindByConfKey( $test, 'oidc/rp', 'myOidcRp1', 1 );
482
483 $test = "OidcRp - FindByConfKey should find 0 hits";
484 checkFindByConfKey( $test, 'oidc/rp', 'myOidcRp3', 0 );
485
486 $test = "OidcRp - FindByConfKey should err on invalid patterns";
487 checkFindByConfKeyError( $test, 'oidc/rp', '' );
488 checkFindByConfKeyError( $test, 'oidc/rp', '$' );
489
490 $test = "OidcRp - FindByClientId should find one entry";
491 checkFindByProviderId( $test, 'oidc/rp', 'clientId', 'myOidcClient1' );
492
493 $test = "OidcRp - FindByClientId should find nothing";
494 checkFindByProviderIdNotFound( $test, 'oidc/rp', 'clientId', 'myOidcClient3' );
495
496 $test = "OidcRp - Clean up";
497 checkDelete( $test, 'oidc/rp', 'myOidcRp1' );
498 checkDelete( $test, 'oidc/rp', 'myOidcRp2' );
499 $test = "OidcRp - Entity should not be found after clean up";
500 checkDeleteNotFound( $test, 'oidc/rp', 'myOidcRp1' );
501
502 my $metadata1 =
503 "<?xml version=\"1.0\"?><md:EntityDescriptor xmlns:md=\"urn:oasis:names:tc:SAML:2.0:metadata\" validUntil=\"2019-09-25T16:44:38Z\" cacheDuration=\"PT604800S\" entityID=\"https://myapp.domain.com/saml/metadata\"><md:SPSSODescriptor AuthnRequestsSigned=\"false\" WantAssertionsSigned=\"false\" protocolSupportEnumeration=\"urn:oasis:names:tc:SAML:2.0:protocol\"><md:SingleLogoutService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" Location=\"https://myapp.domain.com/saml/sls\" /><md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat><md:AssertionConsumerService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" Location=\"https://myapp.domain.com/saml/acs\" index=\"1\" /></md:SPSSODescriptor></md:EntityDescriptor>";
504
505 my $metadata2 =
506 "<?xml version=\"1.0\"?><md:EntityDescriptor xmlns:md=\"urn:oasis:names:tc:SAML:2.0:metadata\" validUntil=\"2019-09-25T16:44:38Z\" cacheDuration=\"PT604800S\" entityID=\"https://myapp2.domain.com/saml/metadata\"><md:SPSSODescriptor AuthnRequestsSigned=\"false\" WantAssertionsSigned=\"false\" protocolSupportEnumeration=\"urn:oasis:names:tc:SAML:2.0:protocol\"><md:SingleLogoutService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" Location=\"https://myapp2.domain.com/saml/sls\" /><md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat><md:AssertionConsumerService Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" Location=\"https://myapp2.domain.com/saml/acs\" index=\"1\" /></md:SPSSODescriptor></md:EntityDescriptor>";
507
508 my $samlSp = {
509 confKey => 'mySamlSp1',
510 metadata => $metadata1,
511 exportedAttributes => {
512 family_name => {
513 format => "urn:oasis:names:tc:SAML:2.0:attrname-format:basic",
514 friendlyName => "surname",
515 mandatory => "false",
516 name => "sn"
517 },
518 cn => {
519 friendlyName => "commonname",
520 mandatory => "true",
521 name => "uid"
522 },
523 uid => {
524 mandatory => "true",
525 name => "uid"
526 },
527 phone => {
528 mandatory => "false",
529 format => "urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified",
530 name => "telephoneNumber"
531 },
532 function => {
533 name => "title",
534 mandatory => "false",
535 format => "urn:oasis:names:tc:SAML:2.0:attrname-format:uri"
536 },
537 given_name => {
538 mandatory => "false",
539 name => "givenName"
540 }
541 },
542 macros => {
543 given_name => '$givenName',
544 },
545 options => {
546 checkSLOMessageSignature => 0,
547 encryptionMode => "assertion",
548 sessionNotOnOrAfterTimeout => 36000
549 }
550 };
551
552 my $fedSamlSp = {
553 confKey => 'myFedSamlSp1',
554 exportedAttributes => {
555 given_name => {
556 mandatory => "false",
557 name => "givenName"
558 }
559 },
560 macros => {
561 given_name => '$givenName',
562 },
563 options => {
564 checkSLOMessageSignature => 0,
565 encryptionMode => "assertion",
566 federationEntityID => "https://myapp.domain.com/saml/metadata",
567 }
568 };
569
570 $test = "SamlSp - Add should succeed";
571 checkAdd( $test, 'saml/sp', $samlSp );
572 checkGet( $test, 'saml/sp', 'mySamlSp1',
573 'options/encryptionMode', 'assertion' );
574 checkGet( $test, 'saml/sp', 'mySamlSp1',
575 'options/sessionNotOnOrAfterTimeout', 36000 );
576
577 $test = "SamlSp - Check attribute default value was set after add";
578 checkGet( $test, 'saml/sp', 'mySamlSp1', 'options/notOnOrAfterTimeout', 72000 );
579
580 $test = "SamlSp - Add should fail on duplicate confKey";
581 checkAddFailsIfExists( $test, 'saml/sp', $samlSp );
582
583 $test =
584 "SamlSp - Update should succeed, keep existing values, drop undefined ones";
585 $samlSp->{options}->{checkSLOMessageSignature} = 1;
586 $samlSp->{options}->{encryptionMode} = 'nameid';
587 delete $samlSp->{options}->{sessionNotOnOrAfterTimeout};
588 delete $samlSp->{exportedAttributes};
589 $samlSp->{macros}->{family_name} = '$sn';
590 $samlSp->{exportedAttributes}->{cn}->{name} = "cn";
591 $samlSp->{exportedAttributes}->{cn}->{friendlyName} = "common_name";
592 $samlSp->{exportedAttributes}->{cn}->{mandatory} = "false";
593 $samlSp->{exportedAttributes}->{family_name} = undef;
594
595 checkUpdate( $test, 'saml/sp', 'mySamlSp1', $samlSp );
596 checkGet( $test, 'saml/sp', 'mySamlSp1',
597 'options/checkSLOMessageSignature', 1 );
598 checkGet( $test, 'saml/sp', 'mySamlSp1',
599 'options/sessionNotOnOrAfterTimeout', 36000 );
600 checkGet( $test, 'saml/sp', 'mySamlSp1', 'exportedAttributes/cn/friendlyName',
601 'common_name' );
602 checkGet( $test, 'saml/sp', 'mySamlSp1', 'exportedAttributes/cn/mandatory',
603 'false' );
604 checkGet( $test, 'saml/sp', 'mySamlSp1', 'exportedAttributes/cn/mandatory',
605 'false' );
606 checkGet( $test, 'saml/sp', 'mySamlSp1', 'exportedAttributes/cn/name', 'uid' );
607 checkGet( $test, 'saml/sp', 'mySamlSp1', 'exportedAttributes/family_name',
608 '_missing' );
609 checkGet( $test, 'saml/sp', 'mySamlSp1', 'exportedAttributes/given_name/name',
610 'givenName' );
611 checkGet( $test, 'saml/sp', 'mySamlSp1', 'macros/family_name', '$sn' );
612 checkGet( $test, 'saml/sp', 'mySamlSp1', 'macros/given_name', '$givenName' );
613
614 $test = "SamlSp - Update should fail on non existing options";
615 $samlSp->{options}->{playingPossum} = 'elephant';
616 checkUpdateWithUnknownAttributes( $test, 'saml/sp', 'mySamlSp1', $samlSp );
617 delete $samlSp->{options}->{playingPossum};
618
619 $test = "SamlSp - Add should fail on duplicate entityId";
620 $samlSp->{confKey} = 'mySamlSp2';
621 checkAddFailsIfExists( $test, 'saml/sp', $samlSp );
622
623 $test = "SamlSp - Add should fail on non existing options";
624 $samlSp->{confKey} = 'mySamlSp2';
625 $samlSp->{metadata} = $metadata2;
626 $samlSp->{options}->{playingPossum} = 'ElephantInTheRoom';
627 checkAddWithUnknownAttributes( $test, 'saml/sp', $samlSp );
628 delete $samlSp->{options}->{playingPossum};
629
630 $test = "SamlSp - 2nd add should succeed";
631 checkAdd( $test, 'saml/sp', $samlSp );
632
633 $test = "SamlSp - Update should fail if client id exists";
634 $samlSp->{metadata} = $metadata1;
635 checkUpdateFailsIfExists( $test, 'saml/sp', 'mySamlSp2', $samlSp );
636
637 $test = "SamlSp - Update should fail if confKey not found";
638 $samlSp->{confKey} = 'mySamlSp3';
639 checkUpdateNotFound( $test, 'saml/sp', 'mySamlSp3', $samlSp );
640
641 $test = "SamlSp - Replace should succeed";
642 $samlSp->{confKey} = 'mySamlSp2';
643 $samlSp->{metadata} = $metadata2;
644 delete $samlSp->{options}->{encryptionMode};
645 checkReplace( $test, 'saml/sp', 'mySamlSp2', $samlSp );
646
647 $test = "SamlSp - Check attribute default value was set after replace";
648 checkGet( $test, 'saml/sp', 'mySamlSp2', 'options/encryptionMode', 'none' );
649
650 $test = "SamlSp - Replace should fail on non existing options";
651 $samlSp->{options}->{playingPossum} = 'elephant';
652 checkReplaceWithInvalidAttribute( $test, 'saml/sp', 'mySamlSp2', $samlSp );
653 delete $samlSp->{options}->{playingPossum};
654 $samlSp->{options}->{notOnOrAfterTimeout} = "XXX";
655 checkReplaceWithInvalidAttribute( $test, 'saml/sp', 'mySamlSp2', $samlSp );
656
657 $test = "SamlSp - Replace should fail if confKey not found";
658 $samlSp->{confKey} = 'mySamlSp3';
659 checkReplaceNotFound( $test, 'saml/sp', 'mySamlSp3', $samlSp );
660
661 $test = "SamlSp - FindByConfKey should find 2 hits";
662 checkFindByConfKey( $test, 'saml/sp', '*', 2 );
663
664 $test = "SamlSp - FindByConfKey should find 2 hits";
665 checkFindByConfKey( $test, 'saml/sp', 'mySamlSp*', 2 );
666
667 $test = "SamlSp - FindByConfKey should find 1 hit";
668 checkFindByConfKey( $test, 'saml/sp', 'mySamlSp1', 1 );
669
670 $test = "SamlSp - FindByConfKey should find 1 hit";
671 checkFindByConfKey( $test, 'saml/sp', '*Sp1', 1 );
672
673 $test = "SamlSp - FindByConfKey should find 0 hits";
674 checkFindByConfKey( $test, 'saml/sp', 'mySamlSp3', 0 );
675
676 $test = "SamlSp - FindByConfKey should err on invalid patterns";
677 checkFindByConfKeyError( $test, 'saml/sp', '' );
678 checkFindByConfKeyError( $test, 'saml/sp', '$' );
679
680 $test = "SamlSp - FindByEntityId should find one entry";
681 checkFindByProviderId( $test, 'saml/sp', 'entityId',
682 'https://myapp.domain.com/saml/metadata' );
683
684 $test = "SamlSp - FindByEntityId should find nothing";
685 checkFindByProviderIdNotFound( $test, 'saml/sp', 'entityId',
686 'https://myapp3.domain.com/saml/metadata' );
687
688 $test = "FederatedSalmSp - Add";
689 checkAddFailsIfExists( $test, 'saml/sp', $fedSamlSp );
690 $fedSamlSp->{options}->{federationEntityID} = "http://fed.example.com/";
691 checkAdd( $test, 'saml/sp', $fedSamlSp );
692 checkGet( $test, 'saml/sp', 'myFedSamlSp1',
693 'options/encryptionMode', 'assertion' );
694 checkGet( $test, 'saml/sp', 'myFedSamlSp1',
695 'options/federationEntityID', 'http://fed.example.com/' );
696 checkFindByProviderId( $test, 'saml/sp', 'entityId',
697 'http://fed.example.com/' );
698
699 $test = "SamlSp - Clean up";
700 checkDelete( $test, 'saml/sp', 'mySamlSp1' );
701 checkDelete( $test, 'saml/sp', 'mySamlSp2' );
702 $test = "SamlSp - Entity should not be found after clean up";
703 checkDeleteNotFound( $test, 'saml/sp', 'mySamlSp1' );
704
705 my $casApp = {
706 confKey => 'myCasApp1',
707 exportedVars => {
708 "cn" => "cn",
709 "uid" => "uid",
710 "mail" => "mail"
711 },
712 macros => {
713 given_name => '$firstName',
714 },
715 options => {
716 service => [
717 'http://mycasapp.example.com', 'http://mycasapp2.example.com/test'
718 ],
719 rule => '$uid eq \'dwho\'',
720 userAttribute => 'uid'
721 }
722 };
723
724 $test = "CasApp - Add should succeed";
725 checkAdd( $test, 'cas/app', $casApp );
726 checkGet( $test, 'cas/app', 'myCasApp1', 'options/service/0',
727 'http://mycasapp.example.com' );
728 checkGet( $test, 'cas/app', 'myCasApp1', 'options/userAttribute', 'uid' );
729 checkGet( $test, 'cas/app', 'myCasApp1', 'options/rule', '$uid eq \'dwho\'' );
730
731 $test = "CasApp - Add should fail on duplicate confKey";
732 checkAddFailsIfExists( $test, 'cas/app', $casApp );
733
734 $test =
735 "CasApp - Update should succeed, keep existing values, drop undefined ones";
736 $casApp->{options}->{service} = ['http://mycasapp.acme.com'];
737 $casApp->{options}->{userAttribute} = 'cn';
738 delete $casApp->{options}->{rule};
739 delete $casApp->{macros};
740 delete $casApp->{exportedVars};
741 $casApp->{macros}->{given_name} = undef;
742 $casApp->{macros}->{cn} = '$cn';
743 $casApp->{exportedVars}->{cn} = 'uid';
744 checkUpdate( $test, 'cas/app', 'myCasApp1', $casApp );
745 checkGet( $test, 'cas/app', 'myCasApp1', 'options/service/0',
746 'http://mycasapp.acme.com' );
747 checkGet( $test, 'cas/app', 'myCasApp1', 'options/userAttribute', 'cn' );
748 checkGet( $test, 'cas/app', 'myCasApp1', 'options/rule', '$uid eq \'dwho\'' );
749 checkGet( $test, 'cas/app', 'myCasApp1', 'exportedVars/cn', 'uid' );
750 checkGet( $test, 'cas/app', 'myCasApp1', 'exportedVars/uid', 'uid' );
751 checkGet( $test, 'cas/app', 'myCasApp1', 'macros/cn', '$cn' );
752 checkGet( $test, 'cas/app', 'myCasApp1', 'macros/given_name', "_missing" );
753
754 $test = "CasApp - Update should fail on non existing options";
755 $casApp->{options}->{playingPossum} = 'elephant';
756 checkUpdateWithUnknownAttributes( $test, 'cas/app', 'myCasApp1', $casApp );
757 delete $casApp->{options}->{playingPossum};
758
759 $test = "CasApp - Add should fail on non existing options";
760 $casApp->{confKey} = 'myCasApp2';
761 $casApp->{options}->{service} = ['http://mycasapp.skynet.com'];
762 $casApp->{options}->{playingPossum} = 'ElephantInTheRoom';
763 checkAddWithUnknownAttributes( $test, 'cas/app', $casApp );
764 delete $casApp->{options}->{playingPossum};
765
766 $test = "CasApp - Add should fail because service host already exists";
767 $casApp->{options}->{service} = ['http://mycasapp.acme.com/ignoredbyissuer'];
768 checkAddFailsIfExists( $test, 'cas/app', $casApp );
769
770 $test = "CasApp - 2nd add should succeed";
771 $casApp->{options}->{service} = ['http://mycasapp.skynet.com'];
772 checkAdd( $test, 'cas/app', $casApp );
773
774 $test = "CasApp - Update should fail if confKey not found";
775 $casApp->{confKey} = 'myCasApp3';
776 checkUpdateNotFound( $test, 'cas/app', 'myCasApp3', $casApp );
777
778 $test = "CasApp - Replace should succeed";
779 $casApp->{confKey} = 'myCasApp2';
780 checkGet( $test, 'cas/app', 'myCasApp2', 'options/userAttribute', 'cn' );
781 $casApp->{options}->{userAttribute} = 'uid';
782 checkReplace( $test, 'cas/app', 'myCasApp2', $casApp );
783 checkGet( $test, 'cas/app', 'myCasApp2', 'options/userAttribute', 'uid' );
784
785 $test = "CasApp - Replace should fail on non existing or invalid options";
786 $casApp->{options}->{playingPossum} = 'elephant';
787 checkReplaceWithInvalidAttribute( $test, 'cas/app', 'myCasApp2', $casApp );
788 delete $casApp->{options}->{playingPossum};
789 $casApp->{options}->{service} = ["XXX"];
790 checkReplaceWithInvalidAttribute( $test, 'cas/app', 'myCasApp2', $casApp );
791
792 $test = "CasApp - Replace should fail if service is not an array";
793 $casApp->{options}->{service} = "http://cas.url.string";
794 check409( $test, update( $test, 'cas/app', 'myCasApp2', $casApp ) );
795
796 $test = "CasApp - Replace should fail if confKey not found";
797 $casApp->{confKey} = 'myCasApp3';
798 checkReplaceNotFound( $test, 'cas/app', 'myCasApp3', $casApp );
799
800 $test = "CasApp - FindByConfKey should find 2 hits";
801 checkFindByConfKey( $test, 'cas/app', '*', 2 );
802
803 $test = "CasApp - FindByConfKey should find 2 hits";
804 checkFindByConfKey( $test, 'cas/app', 'myCasApp*', 2 );
805
806 $test = "CasApp - FindByConfKey should find 1 hit";
807 checkFindByConfKey( $test, 'cas/app', 'myCasApp1', 1 );
808
809 $test = "CasApp - FindByConfKey should find 0 hits";
810 checkFindByConfKey( $test, 'cas/app', 'myCasApp3', 0 );
811
812 $test = "CasApp - FindByConfKey should err on invalid patterns";
813 checkFindByConfKeyError( $test, 'cas/app', '' );
814 checkFindByConfKeyError( $test, 'cas/app', '$' );
815
816 $test = "CasApp - FindByServiceUrl should find one entry";
817 checkFindByProviderId( $test, 'cas/app', 'serviceUrl',
818 'http://mycasapp.acme.com' );
819
820 $test = "CasApp - FindByServiceUrl should find nothing";
821 checkFindByProviderIdNotFound( $test, 'cas/app', 'serviceUrl',
822 'http://mycasapp.corporation.com' );
823
824 $test = "CasApp - Clean up";
825 checkDelete( $test, 'cas/app', 'myCasApp1' );
826 checkDelete( $test, 'cas/app', 'myCasApp2' );
827 $test = "CasApp - Entity should not be found after clean up";
828 checkDeleteNotFound( $test, 'cas/app', 'myCasApp1' );
829
830 # Clean up generated conf files, except for "lmConf-1.json"
831 unlink grep { $_ ne "$main::tmpdir/conf/lmConf-1.json" }
832 glob "$main::tmpdir/conf/lmConf-*.json";
833
834 done_testing();