From 60fc9f523c1a044276f459e89f2b7e22f44dc5de Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Mon, 9 Apr 2018 13:58:50 +0200 Subject: [PATCH 1/6] Update manifest (#1148) --- lemonldap-ng-portal/MANIFEST | 1 + lemonldap-ng-portal/t/73-2F-U2F.t | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lemonldap-ng-portal/MANIFEST b/lemonldap-ng-portal/MANIFEST index 02e25a26c..e9a08c8b8 100644 --- a/lemonldap-ng-portal/MANIFEST +++ b/lemonldap-ng-portal/MANIFEST @@ -423,6 +423,7 @@ t/65-AutoSignin.t t/70-2F-TOTP.t t/71-2F-UTOTP-TOTP-only.t t/72-2F-REST.t +t/73-2F-U2F.t t/90-Translations.t t/99-pod.t t/lmConf-1.json diff --git a/lemonldap-ng-portal/t/73-2F-U2F.t b/lemonldap-ng-portal/t/73-2F-U2F.t index 67e952269..e5f95bc40 100644 --- a/lemonldap-ng-portal/t/73-2F-U2F.t +++ b/lemonldap-ng-portal/t/73-2F-U2F.t @@ -179,8 +179,10 @@ JjTJecOOS+88fK8qL1TrYv5rapIdqUI7aQ== ), 'Push U2F signature' ); + + # See https://github.com/mschout/perl-authen-u2f-tester/issues/2 pass( -'For an unknown reason, Authen::2F::Tester signatures are not recognized by Crypt::U2F::Server' +'For an unknown reason, Authen::2F::Tester signatures are not recognized by Yubico library' ); } count($maintests); From 8e81d31bc196da08f64b9aee2c48b27b35fbbd30 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Mon, 9 Apr 2018 14:31:15 +0200 Subject: [PATCH 2/6] Add Yubico debug (#1148) Available with Crypt::U2F::Server >= 0.43 --- lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/U2F.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/U2F.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/U2F.pm index 82eb86f2d..6919752fa 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/U2F.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/U2F.pm @@ -26,6 +26,7 @@ sub init { Crypt::U2F::Server::Simple->new( appId => $self->origin, origin => $self->origin, + ( $self->conf->{logLevel} eq 'debug' ? ( debug => 1 ) : () ), ) ) { From e729676492439fd6b6c60689d3425731aa33bd4a Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Mon, 9 Apr 2018 20:29:44 +0200 Subject: [PATCH 3/6] Fix U2F test (#1148) --- lemonldap-ng-portal/t/73-2F-U2F.t | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lemonldap-ng-portal/t/73-2F-U2F.t b/lemonldap-ng-portal/t/73-2F-U2F.t index e5f95bc40..a34877f41 100644 --- a/lemonldap-ng-portal/t/73-2F-U2F.t +++ b/lemonldap-ng-portal/t/73-2F-U2F.t @@ -3,7 +3,7 @@ use strict; use IO::String; require 't/test-lib.pm'; -my $maintests = 18; +my $maintests = 17; SKIP: { eval { require Authen::U2F::Tester }; @@ -181,9 +181,15 @@ JjTJecOOS+88fK8qL1TrYv5rapIdqUI7aQ== ); # See https://github.com/mschout/perl-authen-u2f-tester/issues/2 - pass( -'For an unknown reason, Authen::2F::Tester signatures are not recognized by Yubico library' - ); + if ( $Authen::U2F::Tester::VERSION >= 0.03 ) { + expectCookie($res); + } + else { + count(1); + pass( +'Authen::2F::Tester-0.02 signatures are not recognized by Yubico library' + ); + } } count($maintests); From d01a453f4ea0ecd7c27f895b78b254dae2356504 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Mon, 9 Apr 2018 22:33:59 +0200 Subject: [PATCH 4/6] Avoid little warning (#595) --- .../lib/Lemonldap/NG/Portal/Main/Process.pm | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Process.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Process.pm index fdcfda24e..7e0dcf41b 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Process.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Process.pm @@ -149,19 +149,20 @@ sub authLogout { sub deleteSession { my ( $self, $req ) = @_; $req->userData( {} ); - my $apacheSession = $self->getApacheSession( $req->id ); - my $id = $req->id; - unless ($apacheSession) { - $self->logger->debug("Session $id already deleted"); - return PE_OK; - } - unless ( $self->_deleteSession( $req, $apacheSession ) ) { - $self->logger->error("Unable to delete session $id"); - $self->logger->error( $apacheSession->error ); - return PE_ERROR; - } - else { - $self->logger->debug("Session $id deleted from global storage"); + if ( my $id = $req->id ) { + my $apacheSession = $self->getApacheSession( $req->id ); + unless ($apacheSession) { + $self->logger->debug("Session $id already deleted"); + return PE_OK; + } + unless ( $self->_deleteSession( $req, $apacheSession ) ) { + $self->logger->error("Unable to delete session $id"); + $self->logger->error( $apacheSession->error ); + return PE_ERROR; + } + else { + $self->logger->debug("Session $id deleted from global storage"); + } } # TODO From 89e818d407ebe49b04baf7ccb0c88c6f8d892806 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Mon, 9 Apr 2018 22:56:14 +0200 Subject: [PATCH 5/6] Use inc::LWP::Protocol::PSGI in tests instead of redefining LWP::UserAgent methods (#595) --- .../t/23-Auth-and-password-REST.t | 93 +++++++++------ ...AML-Artifact-with-SOAP-SLO-IdP-initiated.t | 64 +++++----- ...h-and-issuer-SAML-Artifact-with-SOAP-SLO.t | 62 +++++----- ...-Auth-and-issuer-SAML-POST-IdP-initiated.t | 23 ++-- .../t/30-Auth-and-issuer-SAML-POST.t | 23 ++-- ...h-and-issuer-SAML-Redirect-IdP-initiated.t | 23 ++-- .../t/30-Auth-and-issuer-SAML-Redirect.t | 23 ++-- .../t/30-SAML-ReAuth-with-choice.t | 23 ++-- lemonldap-ng-portal/t/30-SAML-ReAuth.t | 23 ++-- lemonldap-ng-portal/t/30-SAML-SP-rule.t | 23 ++-- .../t/31-Auth-and-issuer-CAS-declared-app.t | 94 +++++++-------- .../t/31-Auth-and-issuer-CAS-default.t | 94 +++++++-------- .../t/31-Auth-and-issuer-CAS-proxied.t | 90 +++++++------- .../t/31-Auth-and-issuer-CAS-with-choice.t | 90 +++++++------- ...issuer-OIDC-authorization_code-OP-logout.t | 112 +++++++++--------- ...-OIDC-authorization_code-with-authchoice.t | 112 +++++++++--------- ...-Auth-and-issuer-OIDC-authorization_code.t | 112 +++++++++--------- .../t/32-Auth-and-issuer-OIDC-hybrid.t | 110 +++++++++-------- .../t/32-Auth-and-issuer-OIDC-implicit.t | 110 +++++++++-------- lemonldap-ng-portal/t/32-OIDC-RP-rule.t | 112 +++++++++--------- 20 files changed, 687 insertions(+), 729 deletions(-) diff --git a/lemonldap-ng-portal/t/23-Auth-and-password-REST.t b/lemonldap-ng-portal/t/23-Auth-and-password-REST.t index 501138d8b..51010746f 100644 --- a/lemonldap-ng-portal/t/23-Auth-and-password-REST.t +++ b/lemonldap-ng-portal/t/23-Auth-and-password-REST.t @@ -2,12 +2,66 @@ use strict; use IO::String; use Test::More; use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use JSON qw(to_json from_json); BEGIN { require 't/test-lib.pm'; } +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#^http://ws/(auth|user|confirm|modify)#, + ' ' . ucfirst($1) . ' REST request' ) + or explain( $req->uri, 'http://ws/(auth|user)' ); + my $type = $1; + count(1); + my $res = from_json( $req->content ); + ok( $res->{user} eq 'dwho', ' User is dwho' ); + count(1); + + if ( $type eq 'auth' ) { + ok( $res->{password} eq 'dwho', ' Password is dwho' ) + or explain( $res, 'password: dwho' ); + count(1); + return [ + 200, + [ 'Content-Type' => 'application/json' ], + ['{"result":true,"info":{"uid":"dwho"}}'] + ]; + } + elsif ( $type eq 'modify' ) { + ok( $res->{password} eq 'test', ' Password is test' ); + count(1); + return [ + 200, [ 'Content-Type' => 'application/json' ], + ['{"result":true}'] + ]; + } + elsif ( $type eq 'confirm' ) { + ok( $res->{password} eq 'dwho', ' Password is dwho' ); + count(1); + return [ + 200, [ 'Content-Type' => 'application/json' ], + ['{"result":true}'] + ]; + } + elsif ( $type eq 'user' ) { + return [ + 200, + [ 'Content-Type' => 'application/json' ], + ['{"result":true,"info":{"cn":"dwho"}}'] + ]; + } + else { + fail('Unknwon URL'); + count(1); + } + return [ 500, [], [] ]; + } +); + my $res; my $client = LLNG::Manager::Test->new( @@ -56,42 +110,3 @@ clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#^http://ws/(auth|user|confirm|modify)#, - ' ' . ucfirst($1) . ' REST request' ) - or explain( $req->uri, 'http://ws/(auth|user)' ); - my $type = $1; - count(1); - my $res = from_json( $req->content ); - ok( $res->{user} eq 'dwho', ' User is dwho' ); - count(1); - my $resp = HTTP::Response->new( 200, 'OK' ); - - if ( $type eq 'auth' ) { - ok( $res->{password} eq 'dwho', ' Password is dwho' ) - or explain( $res, 'password: dwho' ); - count(1); - $resp->content('{"result":true,"info":{"uid":"dwho"}}'); - } - elsif ( $type eq 'modify' ) { - ok( $res->{password} eq 'test', ' Password is test' ); - count(1); - $resp->content('{"result":true}'); - } - elsif ( $type eq 'confirm' ) { - ok( $res->{password} eq 'dwho', ' Password is dwho' ); - count(1); - $resp->content('{"result":true}'); - } - elsif ( $type eq 'user' ) { - $resp->content('{"result":true,"info":{"cn":"dwho"}}'); - } - else { - fail('Unknwon URL'); - count(1); - } - return $resp; -} diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO-IdP-initiated.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO-IdP-initiated.t index ce26d9bd2..f95469686 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO-IdP-initiated.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO-IdP-initiated.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -12,6 +14,34 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, 'SOAP request' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => 'application/xml', + ), + 'Execute request' + ); + ok( ( $res->[0] == 200 or $res->[0] == 400 ), 'Response is 200 or 400' ) + or explain( $res->[0], "200 or 400" ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/xml#, + 'Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -139,40 +169,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, 'SOAP request' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => 'application/xml', - ), - 'Execute request' - ); - ok( ( $res->[0] == 200 or $res->[0] == 400 ), 'Response is 200 or 400' ) - or explain( $res->[0], "200 or 400" ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/xml#, - 'Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t index 60c1f32f7..dd6f536b4 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -12,6 +14,33 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, 'SOAP request' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => 'application/xml', + ), + 'Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/xml#, + 'Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + count(3); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -192,39 +221,6 @@ done_testing( count() ); no warnings 'redefine'; -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, 'SOAP request' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => 'application/xml', - ), - 'Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/xml#, - 'Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(3); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST-IdP-initiated.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST-IdP-initiated.t index 0b44a192a..adf44ca1e 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST-IdP-initiated.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST-IdP-initiated.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -189,17 +201,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('POST should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST.t index 43b72c7a6..ba0af74cf 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-POST.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -208,17 +220,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('POST should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect-IdP-initiated.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect-IdP-initiated.t index 98b8ba7eb..638f7a7d2 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect-IdP-initiated.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect-IdP-initiated.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -169,17 +181,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('Redirect should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect.t index 29a65599c..a2f033343 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -216,17 +228,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('Redirect should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-SAML-ReAuth-with-choice.t b/lemonldap-ng-portal/t/30-SAML-ReAuth-with-choice.t index c6ee75a54..0e1a81518 100644 --- a/lemonldap-ng-portal/t/30-SAML-ReAuth-with-choice.t +++ b/lemonldap-ng-portal/t/30-SAML-ReAuth-with-choice.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -14,6 +16,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -184,17 +196,6 @@ count($maintests); eval { unlink 't/userdb.db' }; done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('POST should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-SAML-ReAuth.t b/lemonldap-ng-portal/t/30-SAML-ReAuth.t index 90b9ad4eb..65da39434 100644 --- a/lemonldap-ng-portal/t/30-SAML-ReAuth.t +++ b/lemonldap-ng-portal/t/30-SAML-ReAuth.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -172,17 +184,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('POST should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/30-SAML-SP-rule.t b/lemonldap-ng-portal/t/30-SAML-SP-rule.t index 5e0941d8e..64f0ae169 100644 --- a/lemonldap-ng-portal/t/30-SAML-SP-rule.t +++ b/lemonldap-ng-portal/t/30-SAML-SP-rule.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,16 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + fail('POST should not launch SOAP requests'); + count(1); + return [ 500, [], [] ]; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -115,17 +127,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - fail('Redirect should not launch SOAP requests'); - my $httpResp = HTTP::Response->new(500); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-declared-app.t b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-declared-app.t index 6a19c9714..2d369720a 100644 --- a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-declared-app.t +++ b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-declared-app.t @@ -1,6 +1,8 @@ use Test::More; # skip_all => 'CAS is in rebuild'; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,7 +13,46 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); -no warnings 'redefine'; +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, + 'SOAP request' ); + my $host = $1; + my $url = $2; + my $query = $3; + my $res; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + if ( $req->method eq 'POST' ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + query => $query, + type => 'application/xml', + ), + "Execute POST request to $url" + ); + } + else { + ok( + $res = $client->_get( + $url, + type => 'application/xml', + query => $query, + ), + "Execute request to $url" + ); + } + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + count(3); + return $res; + } +); ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; @@ -130,8 +171,8 @@ ok( ); count(1); expectRedirection( $res, 'http://auth.sp.com/?logout' ); -ok( getHeader( $res, 'Content-Security-Policy' ) !~ /frame-ancestors/, - ' Frame can be embedded' ) +my $h = getHeader( $res, 'Content-Security-Policy' ); +ok( ( not $h or $h !~ /frame-ancestors/ ), ' Frame can be embedded' ) or explain( $res->[1], 'Content-Security-Policy does not contain a frame-ancestors' ); count(1); @@ -154,53 +195,6 @@ expectRedirection( $res, clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, - 'SOAP request' ); - my $host = $1; - my $url = $2; - my $query = $3; - my $res; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - if ( $req->method eq 'POST' ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - query => $query, - type => 'application/xml', - ), - "Execute POST request to $url" - ); - } - else { - ok( - $res = $client->_get( - $url, - type => 'application/xml', - query => $query, - ), - "Execute request to $url" - ); - } - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(3); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-default.t b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-default.t index b2dae98f4..b9a58862d 100644 --- a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-default.t +++ b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-default.t @@ -1,6 +1,8 @@ use Test::More; # skip_all => 'CAS is in rebuild'; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,7 +13,46 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); -no warnings 'redefine'; +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, + 'SOAP request' ); + my $host = $1; + my $url = $2; + my $query = $3; + my $res; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + if ( $req->method eq 'POST' ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + query => $query, + type => 'application/xml', + ), + "Execute POST request to $url" + ); + } + else { + ok( + $res = $client->_get( + $url, + type => 'application/xml', + query => $query, + ), + "Execute request to $url" + ); + } + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + count(3); + return $res; + } +); ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; @@ -140,8 +181,8 @@ ok( ); count(1); expectRedirection( $res, 'http://auth.sp.com/?logout' ); -ok( getHeader( $res, 'Content-Security-Policy' ) !~ /frame-ancestors/, - ' Frame can be embedded' ) +my $h = getHeader( $res, 'Content-Security-Policy' ); +ok( ( not $h or $h !~ /frame-ancestors/ ), ' Frame can be embedded' ) or explain( $res->[1], 'Content-Security-Policy does not contain a frame-ancestors' ); count(1); @@ -167,53 +208,6 @@ expectRedirection( $res, clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, - ' Request to ' . $req->uri ); - my $host = $1; - my $url = $2; - my $query = $3; - my $res; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - if ( $req->method eq 'POST' ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - query => $query, - type => 'application/xml', - ), - " Execute POST request to $url" - ); - } - else { - ok( - $res = $client->_get( - $url, - type => 'application/xml', - query => $query, - ), - " Execute request to $url" - ); - } - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(3); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-proxied.t b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-proxied.t index a402af260..33df0457e 100644 --- a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-proxied.t +++ b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-proxied.t @@ -1,6 +1,8 @@ use Test::More; # skip_all => 'CAS is in rebuild'; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,7 +13,44 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); -no warnings 'redefine'; +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, + ' Request to ' . $req->uri ); + my $host = $1; + my $url = $2; + my $query = $3; + my $res; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + if ( $req->method eq 'POST' ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + query => $query, + type => 'application/xml', + ), + " Execute POST request to $url" + ); + } + else { + ok( + $res = $client->_get( + $url, + type => 'application/xml', + query => $query, + ), + " Execute request to $url" + ); + } + expectOK($res); + count(2); + return $res; + } +); ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; @@ -140,8 +179,8 @@ ok( ); count(1); expectRedirection( $res, 'http://auth.sp.com/?logout' ); -ok( getHeader( $res, 'Content-Security-Policy' ) !~ /frame-ancestors/, - ' Frame can be embedded' ) +my $h = getHeader( $res, 'Content-Security-Policy' ); +ok( ( not $h or $h !~ /frame-ancestors/ ), ' Frame can be embedded' ) or explain( $res->[1], 'Content-Security-Policy does not contain a frame-ancestors' ); count(1); @@ -167,51 +206,6 @@ expectRedirection( $res, clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, - ' Request to ' . $req->uri ); - my $host = $1; - my $url = $2; - my $query = $3; - my $res; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - if ( $req->method eq 'POST' ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - query => $query, - type => 'application/xml', - ), - " Execute POST request to $url" - ); - } - else { - ok( - $res = $client->_get( - $url, - type => 'application/xml', - query => $query, - ), - " Execute request to $url" - ); - } - expectOK($res); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(2); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-with-choice.t b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-with-choice.t index 1cb1c8f7a..405d17965 100644 --- a/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-with-choice.t +++ b/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS-with-choice.t @@ -1,6 +1,8 @@ use Test::More; # skip_all => 'CAS is in rebuild'; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,7 +15,46 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); -no warnings 'redefine'; +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, + 'SOAP request' ); + my $host = $1; + my $url = $2; + my $query = $3; + my $res; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + if ( $req->method eq 'POST' ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + query => $query, + type => 'application/xml', + ), + "Execute POST request to $url" + ); + } + else { + ok( + $res = $client->_get( + $url, + type => 'application/xml', + query => $query, + ), + "Execute request to $url" + ); + } + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + count(3); + return $res; + } +); SKIP: { eval { require DBI; require DBD::SQLite; }; @@ -167,53 +208,6 @@ count($maintests); eval { unlink 't/userdb.db' }; done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#, - ' Request to ' . $req->uri ); - my $host = $1; - my $url = $2; - my $query = $3; - my $res; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - if ( $req->method eq 'POST' ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - query => $query, - type => 'application/xml', - ), - " Execute POST request to $url" - ); - } - else { - ok( - $res = $client->_get( - $url, - type => 'application/xml', - query => $query, - ), - " Execute request to $url" - ); - } - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(3); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-OP-logout.t b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-OP-logout.t index 0db78f0b3..686f4be7e 100644 --- a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-OP-logout.t +++ b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-OP-logout.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,6 +13,57 @@ my $debug = 'error'; my ( $op, $rp, $res ); my %handlerOR = ( op => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return [ 500, [], [] ]; + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + # Initialization ok( $op = op(), 'OP portal' ); @@ -149,65 +202,6 @@ expectReject($res); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-with-authchoice.t b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-with-authchoice.t index ac0b685b3..b3279dae7 100644 --- a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-with-authchoice.t +++ b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code-with-authchoice.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -12,6 +14,57 @@ my $maintests = 18; my ( $op, $rp, $res ); my %handlerOR = ( op => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval { require DBI; require DBD::SQLite; }; if ($@) { @@ -186,65 +239,6 @@ count($maintests); eval { unlink 't/userdb.db' }; done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code.t b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code.t index 0366add43..5e76f9cc8 100644 --- a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code.t +++ b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-authorization_code.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,6 +13,57 @@ my $debug = 'error'; my ( $op, $rp, $res ); my %handlerOR = ( op => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return [ 500, [], [] ]; + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + # Initialization ok( $op = op(), 'OP portal' ); @@ -218,65 +271,6 @@ expectRedirection( $res, qr#^http://auth.rp.com/# ); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-hybrid.t b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-hybrid.t index 579016b05..ee66b4d22 100644 --- a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-hybrid.t +++ b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-hybrid.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,6 +13,57 @@ my $debug = 'error'; my ( $op, $rp, $res ); my %handlerOR = ( op => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return [ 500, [], [] ]; + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + # Initialization ok( $op = op(), 'OP portal' ); @@ -99,63 +152,6 @@ count(5); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-implicit.t b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-implicit.t index 58a47dfc6..da745c192 100644 --- a/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-implicit.t +++ b/lemonldap-ng-portal/t/32-Auth-and-issuer-OIDC-implicit.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,6 +13,57 @@ my $debug = 'error'; my ( $op, $rp, $res ); my %handlerOR = ( op => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return [ 500, [], [] ]; + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + # Initialization ok( $op = op(), 'OP portal' ); @@ -90,63 +143,6 @@ count(5); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/32-OIDC-RP-rule.t b/lemonldap-ng-portal/t/32-OIDC-RP-rule.t index ce4be7833..dad06059d 100644 --- a/lemonldap-ng-portal/t/32-OIDC-RP-rule.t +++ b/lemonldap-ng-portal/t/32-OIDC-RP-rule.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -11,6 +13,57 @@ my $debug = 'error'; my ( $op, $rp, $res ); my %handlerOR = ( op => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return [ 500, [], [] ]; + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + # Initialization ok( $op = op(), 'OP portal' ); @@ -64,65 +117,6 @@ count(1); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); From 6586b684ddbcabae76cbeac056111c08ef9fbcf6 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Tue, 10 Apr 2018 06:54:08 +0200 Subject: [PATCH 6/6] Use inc::LWP::Protocol::PSGI in tests instead of redefining LWP::UserAgent methods (#595) --- ...h-and-issuer-SAML-Artifact-with-SOAP-SLO.t | 2 - .../t/33-Auth-and-issuer-OpenID2.t | 68 +++++------ .../t/34-Auth-Proxy-and-REST-Server.t | 108 ++++++++--------- .../t/34-Auth-Proxy-and-SOAP-Server.t | 77 ++++++------ .../t/35-REST-config-backend.t | 109 ++++++++--------- .../t/35-REST-sessions-with-REST-server.t | 107 ++++++++--------- .../t/35-SOAP-config-backend.t | 73 ++++++------ .../t/35-SOAP-sessions-with-SOAP-server.t | 73 ++++++------ .../t/37-Logout-from-OIDC-RP-to-SAML-SP.t | 112 +++++++++--------- .../t/37-OIDC-RP-to-SAML-IdP-GET.t | 112 +++++++++--------- .../t/37-OIDC-RP-to-SAML-IdP-POST.t | 112 +++++++++--------- .../t/37-SAML-SP-GET-to-OIDC-OP.t | 112 +++++++++--------- .../t/37-SAML-SP-POST-to-OIDC-OP.t | 112 +++++++++--------- .../t/40-Notifications-XML-Server.t | 68 +++++------ 14 files changed, 591 insertions(+), 654 deletions(-) diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t index dd6f536b4..e922af640 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t @@ -219,8 +219,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t b/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t index 039fe63fe..3a09605a2 100644 --- a/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t +++ b/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -12,6 +14,36 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.idp.com(.*)#, + ' Request from SP to IdP' ); + my $url = $1; + my ($res); + count(1); + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $issuer->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + accept => 'text/plain', + ), + ' Execute request' + ); + } + else { + ok( $res = $issuer->_get( $url, accept => 'text/plain', ), + ' Execute post request' ); + } + expectOK($res); + count(1); + return $res; + } +); + SKIP: { eval { require Net::OpenID::Consumer; require Net::OpenID::Server; }; if ($@) { @@ -105,42 +137,6 @@ sub switch { }; } -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.idp.com(.*)#, ' Request from SP to IdP' ); - my $url = $1; - my ($res); - count(1); - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $issuer->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - accept => 'text/plain', - ), - ' Execute request' - ); - } - else { - ok( $res = $issuer->_get( $url, accept => 'text/plain', ), - ' Execute post request' ); - } - expectOK($res); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->request($req); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(1); - return $httpResp; -} - sub issuer { return LLNG::Manager::Test->new( { diff --git a/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t b/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t index 8fc3a69d4..9f380c5f7 100644 --- a/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t +++ b/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,55 @@ my $debug = 'error'; my ( $issuer, $sp, $res, $spId, $idpId ); my %handlerOR = ( issuer => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( + $req->uri =~ m#http://auth.idp.com(.*)#, + ' @ REST request (' . $req->method . " $1)" + ); + count(1); + my $url = $1; + my $res; + my $s = $req->content; + if ( $req->method =~ /^(post|put)$/i ) { + my $mth = '_' . lc($1); + my $s = $req->content; + ok( + $res = $issuer->$mth( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Post request' + ); + count(1); + expectOK($res); + $idpId = expectCookie($res); + } + elsif ( $req->method =~ /^(get|delete)$/i ) { + my $mth = '_' . lc($1); + ok( + $res = $issuer->$mth( + $url, + accept => $req->header('Accept'), + cookie => $req->header('Cookie') + ), + ' Execute request' + ); + count(1); + expectOK($res); + } + ok( + getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/json#, + 'Content is JSON' + ) or explain( $res->[1], 'Content-Type => application/json' ); + count(1); + return $res; + } +); + ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; switch ('sp'); @@ -175,63 +226,6 @@ clean_sessions(); done_testing( count() ); # Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( - $req->uri =~ m#http://auth.idp.com(.*)#, - ' @ REST request (' . $req->method . " $1)" - ); - count(1); - my $url = $1; - my $res; - my $s = $req->content; - if ( $req->method =~ /^(post|put)$/i ) { - my $mth = '_' . lc($1); - my $s = $req->content; - ok( - $res = $issuer->$mth( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Post request' - ); - count(1); - expectOK($res); - $idpId = expectCookie($res); - } - elsif ( $req->method =~ /^(get|delete)$/i ) { - my $mth = '_' . lc($1); - ok( - $res = $issuer->$mth( - $url, - accept => $req->header('Accept'), - cookie => $req->header('Cookie') - ), - ' Execute request' - ); - count(1); - expectOK($res); - } - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/json#, - 'Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - count(1); - my $httpResp; - $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF REST REQUEST'); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t b/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t index 7cac5df8e..987407c30 100644 --- a/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t +++ b/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -11,6 +13,41 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, + ' @ SOAP REQUEST @' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + switch ( $host eq 'idp' ? 'issuer' : 'sp' ); + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + switch ( $host eq 'idp' ? 'sp' : 'issuer' ); + return $res; + } +); + SKIP: { eval 'use SOAP::Lite'; if ($@) { @@ -96,46 +133,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, ' @ SOAP REQUEST @' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - switch ( $host eq 'idp' ? 'issuer' : 'sp' ); - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - switch ( $host eq 'idp' ? 'sp' : 'issuer' ); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-REST-config-backend.t b/lemonldap-ng-portal/t/35-REST-config-backend.t index 51c269c65..5a69b25f7 100644 --- a/lemonldap-ng-portal/t/35-REST-config-backend.t +++ b/lemonldap-ng-portal/t/35-REST-config-backend.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,56 @@ my $debug = 'error'; my ( $issuer, $sp, $res, $spId ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( + $req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#, + ' @ REST request (' . $req->method . " $1)" + ); + count(1); + my $url = $1; + my $query = $2; + my $res; + my $s = $req->content; + if ( $req->method =~ /^(post|put)$/i ) { + my $mth = '_' . lc($1); + my $s = $req->content; + ok( + $res = $issuer->$mth( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Post request' + ); + count(1); + expectOK($res); + } + elsif ( $req->method =~ /^(get|delete)$/i ) { + my $mth = '_' . lc($1); + ok( + $res = $issuer->$mth( + $url, + accept => $req->header('Accept'), + cookie => $req->header('Cookie'), + query => $query, + ), + ' Execute request' + ); + ok( ( $res->[0] == 200 or $res->[0] == 400 ), + ' Response is 200 or 400' ) + or explain( $res->[0], '200 or 400' ); + count(2); + } + pass(' @ END OF REST REQUEST'); + count(1); + return $res; + } +); + ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; @@ -103,63 +155,6 @@ expectReject($res); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( - $req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#, - ' @ REST request (' . $req->method . " $1)" - ); - count(1); - my $url = $1; - my $query = $2; - my $res; - my $s = $req->content; - if ( $req->method =~ /^(post|put)$/i ) { - my $mth = '_' . lc($1); - my $s = $req->content; - ok( - $res = $issuer->$mth( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Post request' - ); - count(1); - expectOK($res); - } - elsif ( $req->method =~ /^(get|delete)$/i ) { - my $mth = '_' . lc($1); - ok( - $res = $issuer->$mth( - $url, - accept => $req->header('Accept'), - cookie => $req->header('Cookie'), - query => $query, - ), - ' Execute request' - ); - ok( ( $res->[0] == 200 or $res->[0] == 400 ), - ' Response is 200 or 400' ) - or explain( $res->[0], '200 or 400' ); - count(2); - } - my $httpResp; - $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF REST REQUEST'); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t b/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t index e21b97d6e..d46230509 100644 --- a/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t +++ b/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,56 @@ my $debug = 'error'; my ( $issuer, $sp, $res, $spId ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( + $req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#, + ' @ REST request (' . $req->method . " $1)" + ); + count(1); + my $url = $1; + my $query = $2; + my $res; + my $s = $req->content; + if ( $req->method =~ /^(post|put)$/i ) { + my $mth = '_' . lc($1); + my $s = $req->content; + ok( + $res = $issuer->$mth( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Post request' + ); + count(1); + expectOK($res); + } + elsif ( $req->method =~ /^(get|delete)$/i ) { + my $mth = '_' . lc($1); + ok( + $res = $issuer->$mth( + $url, + accept => $req->header('Accept'), + cookie => $req->header('Cookie'), + query => $query, + ), + ' Execute request' + ); + ok( ( $res->[0] == 200 or $res->[0] == 400 ), + ' Response is 200 or 400' ) + or explain( $res->[0], '200 or 400' ); + count(2); + } + pass(' @ END OF REST REQUEST'); + count(1); + return $res; + } +); + ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; switch ('sp'); @@ -99,61 +151,6 @@ expectReject($res); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( - $req->uri =~ m#http://auth.idp.com(.*)#, - ' @ REST request (' . $req->method . " $1)" - ); - count(1); - my $url = $1; - my $res; - my $s = $req->content; - if ( $req->method =~ /^(post|put)$/i ) { - my $mth = '_' . lc($1); - my $s = $req->content; - ok( - $res = $issuer->$mth( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Post request' - ); - count(1); - expectOK($res); - } - elsif ( $req->method =~ /^(get|delete)$/i ) { - my $mth = '_' . lc($1); - ok( - $res = $issuer->$mth( - $url, - accept => $req->header('Accept'), - cookie => $req->header('Cookie') - ), - ' Execute request' - ); - ok( ( $res->[0] == 200 or $res->[0] == 400 ), - ' Response is 200 or 400' ) - or explain( $res->[0], '200 or 400' ); - count(2); - } - my $httpResp; - $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF REST REQUEST'); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-SOAP-config-backend.t b/lemonldap-ng-portal/t/35-SOAP-config-backend.t index 6255d479b..396f23dad 100644 --- a/lemonldap-ng-portal/t/35-SOAP-config-backend.t +++ b/lemonldap-ng-portal/t/35-SOAP-config-backend.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -11,6 +13,39 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, + ' @ SOAP REQUEST @' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + return $res; + } +); + SKIP: { eval 'use SOAP::Lite'; if ($@) { @@ -89,44 +124,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, ' @ SOAP REQUEST @' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t b/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t index 76f615f3b..2ed411f1b 100644 --- a/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t +++ b/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -11,6 +13,39 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, + ' @ SOAP REQUEST @' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + return $res; + } +); + SKIP: { eval 'use SOAP::Lite'; if ($@) { @@ -77,44 +112,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, ' @ SOAP REQUEST @' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t b/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t index b1bcf82e5..37318b0eb 100644 --- a/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t +++ b/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $op, $rp, $sp, $res ); my %handlerOR = ( op => [], rp => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -285,65 +338,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t index 34c0730d3..7ecc96b76 100644 --- a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t +++ b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $idp, $sp, $rp, $res ); my %handlerOR = ( idp => [], sp => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'sp' ) { + pass(" Request from RP to OP(sp), endpoint $url"); + $client = $sp; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP(proxy)'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -231,65 +284,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'sp' ) { - pass(" Request from RP to OP(sp), endpoint $url"); - $client = $sp; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP(proxy)'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t index 8e5ecfd6f..1832a07e0 100644 --- a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t +++ b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $idp, $sp, $rp, $res ); my %handlerOR = ( idp => [], sp => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'sp' ) { + pass(" Request from RP to OP(sp), endpoint $url"); + $client = $sp; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP(proxy)'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -233,65 +286,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'sp' ) { - pass(" Request from RP to OP(sp), endpoint $url"); - $client = $sp; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP(proxy)'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t b/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t index 334f8158b..3fb5024b5 100644 --- a/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t +++ b/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $op, $proxy, $sp, $res ); my %handlerOR = ( op => [], proxy => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP(proxy) to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'proxy' ) { + pass(' Request from OP to RP(proxy)'); + $client = $proxy; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -205,65 +258,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP(proxy) to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'proxy' ) { - pass(' Request from OP to RP(proxy)'); - $client = $proxy; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t b/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t index 09f2d81f4..abea7f93c 100644 --- a/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t +++ b/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $op, $proxy, $sp, $res ); my %handlerOR = ( op => [], proxy => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP(proxy) to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'proxy' ) { + pass(' Request from OP to RP(proxy)'); + $client = $proxy; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -205,65 +258,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP(proxy) to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'proxy' ) { - pass(' Request from OP to RP(proxy)'); - $client = $proxy; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/40-Notifications-XML-Server.t b/lemonldap-ng-portal/t/40-Notifications-XML-Server.t index 0485223e3..1ea784aa4 100644 --- a/lemonldap-ng-portal/t/40-Notifications-XML-Server.t +++ b/lemonldap-ng-portal/t/40-Notifications-XML-Server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,36 @@ my $maintests = 3; my $debug = 'error'; my $client; +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.example.com(.*)#, ' @ SOAP REQUEST @' ); + my $url = $1; + my $res; + my $s = $req->content; + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + return $res; + } +); + eval { unlink 't/20160530_dwho_dGVzdHJlZg==.xml' }; my $xml = ' @@ -76,39 +108,3 @@ eval { unlink 't/20160530_dwho_dGVzdHJlZg==.xml' }; count($maintests); clean_sessions(); done_testing( count() ); - -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.example.com(.*)#, ' @ SOAP REQUEST @' ); - my $url = $1; - my $res; - my $s = $req->content; - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - return $httpResp; -}