Commit ce00317d authored by Xavier Guimard's avatar Xavier Guimard

Add AuthBasic test (#1700)

parent d4593fda
......@@ -455,6 +455,7 @@ t/34-Auth-Proxy-and-REST-Server.t
t/34-Auth-Proxy-and-SOAP-Server.t
t/35-My-session.t
t/35-REST-config-backend.t
t/35-REST-sessions-with-AuthBasic-handler.t
t/35-REST-sessions-with-REST-server.t
t/35-SOAP-config-backend.t
t/35-SOAP-sessions-with-SOAP-server.t
......@@ -529,12 +530,14 @@ t/76-2F-Ext-with-History.t
t/77-2F-Mail.t
t/90-Translations.t
t/99-pod.t
t/auth-basic.pm
t/gpghome/key.asc
t/gpghome/openpgp-revocs.d/9482CEFB055809CBAFE6D71AAB2D5542891D1677.rev
t/gpghome/private-keys-v1.d/A076B0E7DB141A919271EE8B581CDFA8DA42F333.key
t/gpghome/private-keys-v1.d/B7219440BCCD85200121CFB89F94C8D98C0397B3.key
t/gpghome/pubring.kbx
t/gpghome/trustdb.gpg
t/lib/Lemonldap/NG/Handler/Test.pm
t/lib/Lemonldap/NG/Portal/Auth/LDAPPolicy.pm
t/lmConf-1.json
t/pdata.pm
......
use lib 'inc';
use strict;
use IO::String;
use JSON;
use Test::More;
our $debug = 'debug';
my ( $p, $res, $spId );
$| = 1;
require 't/auth-basic.pm';
require "t/test-lib.pm";
ok( $p = issuer(), 'Issuer portal' );
count(1);
# BEGIN TESTS
ok( $res = handler( req => [ GET => 'http://test2.example.com/' ] ),
'Simple request to handler' );
ok( getHeader( $res, 'WWW-Authenticate' ) eq 'Basic realm="LemonLDAP::NG"',
'Get WWW-Authenticate header' );
count(2);
ok(
$res = handler(
req => [
GET => 'http://test2.example.com/',
[ 'Authorization' => 'Basic ZHdobzpkd2hv' ]
],
sub => sub {
my ($res) = @_;
subtest 'Portal request' => sub {
plan tests => 3;
ok( $res->[0] eq 'POST', 'Get POST request' );
my ( $url, $query ) = split /\?/, $res->[1];
ok(
$res = $p->_post(
$url, IO::String->new( $res->[3] ),
length => length( $res->[3] ),
query => $query,
),
'Push request to portal'
);
ok( $res->[0] == 200, 'Response is 200' );
return $res;
};
count(1);
return $res;
},
),
'AuthBasic request'
);
count(1);
ok(
$res = handler(
req => [
GET => 'http://test2.example.com/',
[ 'Authorization' => 'Basic ZHdobzpkd2hv' ]
],
sub => sub {
fail "Cache didn't work";
return [ 500, [], [] ];
},
),
'New AuthBasic request'
);
count(1);
expectOK($res);
end_handler();
clean_sessions();
done_testing( count() );
sub issuer {
return LLNG::Manager::Test->new( {
ini => {
logLevel => $debug,
templatesDir => 'site/htdocs/static',
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
restSessionServer => 1,
}
}
);
}
use IO::Pipe;
use IO::Select;
our ( $in, $out ) = ( IO::Pipe->new, IO::Pipe->new );
our ( $rin, $rout ) = ( IO::Pipe->new, IO::Pipe->new );
my $pid = fork;
unless ( defined $pid ) {
plan skip_all => 'Unable to fork';
exit;
}
unless ($pid) {
{
no warnings;
}
$in->reader;
$out->writer;
$rin->writer;
$rout->reader;
$out->autoflush(1);
$rin->autoflush(1);
use lib 't/lib/';
require Lemonldap::NG::Handler::Test;
Lemonldap::NG::Handler::Test::init();
Lemonldap::NG::Handler::Test::run();
exit;
}
$in->writer;
$out->reader;
$rin->reader;
$rout->writer;
$in->autoflush(1);
$rout->autoflush(1);
my $s = IO::Select->new();
$s->add($out);
$s->add($rin);
sub handler {
my (%args) = @_;
print $in JSON::to_json( $args{req} ) . "\n";
while(my @ready = $s->can_read) {
foreach $fh (@ready) {
if($fh == $out) {
my $res = <$out>;
return JSON::from_json($res);
}
else {
my $res = <$rin>;
$res = $args{sub}->(JSON::from_json($res));
print $rout JSON::to_json($res)."\n";
}
}
}
}
sub end_handler {
print $in "END\n";
}
1;
package Lemonldap::NG::Handler::Test;
use File::Temp;
use HTTP::Request::Common;
use Lemonldap::NG::Handler::Server;
use LWP::UserAgent;
use LWP::Protocol::PSGI;
use Plack::Test;
our ( $in, $out, $rin, $rout, $server );
*in = *main::in;
*out = *main::out;
*rin = *main::rin;
*rout = *main::rout;
sub init {
my $tdir = File::Temp::tempdir( CLEANUP => 1 );
my $h = Lemonldap::NG::Handler::Server->new( {} );
$h->init( {
configStorage => {
type => 'File',
dirName => 't',
},
cookieName => 'lemonldap',
securedCookie => 0,
https => 0,
logger => 'Lemonldap::NG::Common::Logger::Std',
domain => 'idp.com',
logLevel => $main::debug,
portal => 'http://auth.idp.com',
configStorage => {
type => 'File',
dirName => 't',
},
localSessionStorage => 'Cache::FileCache',
localSessionStorageOptions => {
namespace => 'lemonldap-ng-session',
cache_root => $tdir,
cache_depth => 0,
},
}
);
$server = Plack::Test->create( $h->run );
}
# Redefine LWP methods for tests
LWP::Protocol::PSGI->register(
sub {
my ($env) = @_;
my $req = Plack::Request->new($env);
my $method = $req->method;
my $url = $req->request_uri;
my $content = $req->content;
print $rin JSON::to_json( [ $method => $url, [], $content ] ) . "\n";
my $res;
$res = <$rout>, 'Get portal response';
return JSON::from_json($res);
}
);
sub run {
while (<$in>) {
chomp;
if (/^END/) {
return;
}
next unless $_;
my ( $req, $res );
$req = HTTP::Request->new( @{ JSON::from_json($_) } );
$res = $server->request($req);
print $out JSON::to_json(
[ $res->code, [ $res->flatten ], [ $res->content ] ] )
. "\n";
}
}
1;
......@@ -86,5 +86,10 @@
"reloadUrls": {},
"requireToken": 0,
"userDB": "Same",
"vhostOptions": {
"test2.example.com": {
"vhostType": "AuthBasic"
}
},
"whatToTrace": "_whatToTrace"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment