From 3b122daab9947dfbd7b9ff9ca0350a27bb1831e2 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Fri, 6 Nov 2020 17:10:55 +0100 Subject: [PATCH] Unit tests for (#2330) --- lemonldap-ng-portal/MANIFEST | 1 + lemonldap-ng-portal/t/32-OIDC-ClaimTypes.t | 79 +++++++++++++++++++ .../32-OIDC-Password-Grant-with-Bruteforce.t | 26 ++++-- 3 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 lemonldap-ng-portal/t/32-OIDC-ClaimTypes.t diff --git a/lemonldap-ng-portal/MANIFEST b/lemonldap-ng-portal/MANIFEST index 531113348..f8aa3a0fc 100644 --- a/lemonldap-ng-portal/MANIFEST +++ b/lemonldap-ng-portal/MANIFEST @@ -548,6 +548,7 @@ t/32-Auth-and-issuer-OIDC-implicit.t t/32-Auth-and-issuer-OIDC-sorted.t t/32-CAS-10.t t/32-CAS-Macros.t +t/32-OIDC-ClaimTypes.t t/32-OIDC-Code-Flow-with-2F-UpgradeOnly.t t/32-OIDC-Code-Flow-with-2F.t t/32-OIDC-Macro.t diff --git a/lemonldap-ng-portal/t/32-OIDC-ClaimTypes.t b/lemonldap-ng-portal/t/32-OIDC-ClaimTypes.t new file mode 100644 index 000000000..21c6af995 --- /dev/null +++ b/lemonldap-ng-portal/t/32-OIDC-ClaimTypes.t @@ -0,0 +1,79 @@ +use Test::More; +use strict; +use IO::String; +use MIME::Base64; +use JSON qw/to_json/; + +require 't/test-lib.pm'; + +my $res; + +my $client = LLNG::Manager::Test->new( { + ini => { + logLevel => 'error', + useSafeJail => 1, + issuerDBOpenIDConnectActivation => 1, + multiValuesSeparator => ";" + } + } +); + +my $oidc = + $client->p->loadedModules->{'Lemonldap::NG::Portal::Issuer::OpenIDConnect'}; + +my $tests = [ + + # Auto array + [ [ undef, "string", "auto" ], '{"key":null}' ], + [ [ "", "string", "auto" ], '{"key":null}' ], + [ [ "foo", "string", "auto" ], '{"key":"foo"}' ], + [ [ "foo;bar", "string", "auto" ], '{"key":["foo","bar"]}' ], + [ [ undef, "int", "auto" ], '{"key":null}' ], + [ [ "", "int", "auto" ], '{"key":null}' ], + [ [ "0", "int", "auto" ], '{"key":0}' ], + [ [ "0;1;2;3", "int", "auto" ], '{"key":[0,1,2,3]}' ], + [ [ undef, "bool", "auto" ], '{"key":null}' ], + [ [ "", "bool", "auto" ], '{"key":null}' ], + [ [ "0", "bool", "auto" ], '{"key":false}' ], + [ [ "1", "bool", "auto" ], '{"key":true}' ], + [ [ "0;1;;3", "bool", "auto" ], '{"key":[false,true,false,true]}' ], + + # Always array + [ [ undef, "string", "always" ], '{"key":null}' ], + [ [ "", "string", "always" ], '{"key":null}' ], + [ [ "foo", "string", "always" ], '{"key":["foo"]}' ], + [ [ "foo;bar", "string", "always" ], '{"key":["foo","bar"]}' ], + [ [ undef, "int", "always" ], '{"key":null}' ], + [ [ "", "int", "always" ], '{"key":null}' ], + [ [ "0", "int", "always" ], '{"key":[0]}' ], + [ [ "0;1;2;3", "int", "always" ], '{"key":[0,1,2,3]}' ], + [ [ undef, "bool", "always" ], '{"key":null}' ], + [ [ "", "bool", "always" ], '{"key":null}' ], + [ [ "0", "bool", "always" ], '{"key":[false]}' ], + [ [ "1", "bool", "always" ], '{"key":[true]}' ], + [ [ "0;1;;3", "bool", "always" ], '{"key":[false,true,false,true]}' ], + + # Never array + [ [ undef, "string", "never" ], '{"key":null}' ], + [ [ "", "string", "never" ], '{"key":null}' ], + [ [ "foo", "string", "never" ], '{"key":"foo"}' ], + [ [ "foo;bar", "string", "never" ], '{"key":"foo;bar"}' ], + [ [ undef, "int", "never" ], '{"key":null}' ], + [ [ "", "int", "never" ], '{"key":null}' ], + [ [ "0", "int", "never" ], '{"key":0}' ], + [ [ "0;1;2;3", "int", "never" ], '{"key":"0;1;2;3"}' ], + [ [ undef, "bool", "never" ], '{"key":null}' ], + [ [ "", "bool", "never" ], '{"key":null}' ], + [ [ "0", "bool", "never" ], '{"key":false}' ], + [ [ "1", "bool", "never" ], '{"key":true}' ], + [ [ "0;1;;3", "bool", "never" ], '{"key":"0;1;;3"}' ], +]; + +for my $test ( @{$tests} ) { + my @args = @{ $test->[0] }; + my $expect = $test->[1]; + is( to_json( { key => $oidc->_formatValue( @args, "key", "foo" ) } ), + $expect, "_formatvalue(" . join( ', ', map { "'$_'" } @args ) . ")" ); +} + +done_testing(); diff --git a/lemonldap-ng-portal/t/32-OIDC-Password-Grant-with-Bruteforce.t b/lemonldap-ng-portal/t/32-OIDC-Password-Grant-with-Bruteforce.t index aaae1d4d9..26b82f830 100644 --- a/lemonldap-ng-portal/t/32-OIDC-Password-Grant-with-Bruteforce.t +++ b/lemonldap-ng-portal/t/32-OIDC-Password-Grant-with-Bruteforce.t @@ -17,17 +17,24 @@ my $debug = 'error'; # Initialization my $op = LLNG::Manager::Test->new( { ini => { - logLevel => $debug, - domain => 'op.com', - portal => 'http://auth.op.com', - authentication => 'Demo', - userDB => 'Same', + logLevel => $debug, + domain => 'op.com', + portal => 'http://auth.op.com', + authentication => 'Demo', + userDB => 'Same', + macros => { + gender => '"32"', + _whatToTrace => '$uid', + nickname => '"froggie; frenchie"', + }, issuerDBOpenIDConnectActivation => 1, oidcRPMetaDataExportedVars => { rp => { - email => "mail", + email => "mail;string;always", preferred_username => "uid", - name => "cn" + name => "cn", + gender => "gender;int;auto", + nickname => "nickname", } }, oidcRPMetaDataOptions => { @@ -131,6 +138,11 @@ $res = $op->_post( $payload = expectJSON($res); ok( $payload->{'name'} eq "Frédéric Accents", 'Got User Info' ); +like( $res->[2]->[0], qr/"gender":32/, "Attribute released as int in JSON" ); +is( ref( $payload->{email} ), + "ARRAY", "Single valued attribute forced as array" ); +is( ref( $payload->{nickname} ), + "ARRAY", "Multi valued attribute exposed as array" ); clean_sessions(); done_testing();