lemonldap-ng/lemonldap-ng-portal/t/test-lib.pm

277 lines
7.6 KiB
Perl
Raw Normal View History

2016-04-03 18:27:13 +02:00
# Base library for portal tests
2016-11-14 13:34:46 +01:00
package main;
2016-04-03 18:27:13 +02:00
use strict;
use Data::Dumper;
2016-11-29 22:44:33 +01:00
use LWP::UserAgent;
2016-04-03 18:27:13 +02:00
use 5.10.0;
2016-04-05 07:23:42 +02:00
2016-12-20 12:53:31 +01:00
no warnings 'redefine';
2016-11-29 22:44:33 +01:00
BEGIN {
use_ok('Lemonldap::NG::Portal::Main');
}
2016-04-05 07:23:42 +02:00
our $count = 1;
2016-04-03 18:27:13 +02:00
$Data::Dumper::Deparse = 1;
2016-04-05 07:23:42 +02:00
my $ini;
2016-04-03 18:27:13 +02:00
sub count {
my $c = shift;
$count += $c if ($c);
return $count;
}
2016-11-29 22:44:33 +01:00
sub main::explain {
2016-04-03 18:27:13 +02:00
my ( $get, $ref ) = @_;
$get = Dumper($get) if ( ref $get );
print STDERR "Expect $ref, get $get\n";
}
2016-11-14 13:34:46 +01:00
sub clean_sessions {
opendir D, 't/sessions' or die $!;
foreach ( grep { /^[^\.]/ } readdir(D) ) {
unlink "t/sessions/$_", "t/sessions/lock/Apache-Session-$_.lock";
}
2016-11-22 21:55:10 +01:00
foreach my $dir (qw(t/sessions/lock t/sessions/saml/lock t/sessions/saml)) {
if ( -d $dir ) {
opendir D, $dir or die $!;
foreach ( grep { /^[^\.]/ } readdir(D) ) {
unlink "$dir/$_";
}
}
2016-11-14 13:34:46 +01:00
}
}
2016-12-23 07:41:03 +01:00
sub expectRedirection {
my ( $res, $location ) = @_;
ok( $res->[0] == 302, 'Get redirection' )
or explain( $res->[0], 302 );
count(2);
if ( ref $location ) {
my @match;
@match = ( getRedirection($res) =~ $location );
ok( @match, 'Location header found' )
or explain( $res->[1], "Location match: " . Dumper($location) );
return @match;
}
else {
ok( getRedirection($res) eq $location, "Location is $location" )
or explain( $res->[1], "Location => $location" );
}
}
sub expectAuthenticatedAs {
my ( $res, $user ) = @_;
ok( getHeader( $res, 'Lm-Remote-User' ) eq $user, "Authenticated as $user" )
or explain( $res->[1], "Lm-Remote-User => $user" );
count(1);
}
sub expectOK {
my ($res) = @_;
ok( $res->[0] == 200, 'HTTP code is 200' ) or explain( $res, 200 );
count(1);
}
sub expectBadRequest {
my ($res) = @_;
ok( $res->[0] == 400, 'HTTP code is 400' ) or explain( $res->[0], 400 );
count(1);
}
sub expectReject {
my ($res) = @_;
ok( $res->[0] == 401, 'Response is 401' ) or explain( $res->[0], 401 );
count(1);
}
sub expectCookie {
my ( $res, $cookieName ) = @_;
$cookieName ||= 'lemonldap';
my $cookies = getCookies($res);
my $id;
2016-12-26 10:23:35 +01:00
ok( defined( $id = $cookies->{$cookieName} ), "Get cookie $cookieName" )
2016-12-23 07:41:03 +01:00
or explain( $res->[1], "Set-Cookie: $cookieName=something" );
count(1);
return $id;
}
sub getCookies {
my ($resp) = @_;
my @hdrs = @{ $resp->[1] };
my $res = {};
while ( my $name = shift @hdrs ) {
my $v = shift @hdrs;
if ( $name eq 'Set-Cookie' ) {
if ( $v =~ /^(\w+)=([^;]*)/ ) {
$res->{$1} = $2;
}
}
}
return $res;
}
sub getHeader {
my ( $resp, $hname ) = @_;
my @hdrs = @{ $resp->[1] };
my $res = {};
while ( my $name = shift @hdrs ) {
my $v = shift @hdrs;
if ( $name eq $hname ) {
return $v;
}
}
return undef;
}
sub getRedirection {
my ($resp) = @_;
return getHeader( $resp, 'Location' );
}
sub getUser {
my ($resp) = @_;
return getHeader( $resp, 'Lm-Remote-User' );
}
2016-11-14 13:34:46 +01:00
package LLNG::Manager::Test;
use strict;
use Mouse;
extends 'Lemonldap::NG::Common::PSGI::Cli::Lib';
our $defaultIni = {
configStorage => { type => 'File', dirName => 't' },
localSessionStorage => '',
logLevel => 'error',
cookieName => 'lemonldap',
domain => 'example.com',
templateDir => 'site/templates',
staticPrefix => '/index.fcgi',
securedCookie => 0,
https => 0,
};
has app => (
is => 'rw',
isa => 'CodeRef',
2016-11-14 13:34:46 +01:00
);
has ini => (
is => 'rw',
default => sub { $defaultIni; },
trigger => sub {
my ( $self, $ini ) = @_;
foreach my $k ( keys %$defaultIni ) {
$ini->{$k} //= $defaultIni->{$k};
}
$self->{ini} = $ini;
main::ok( $self->{app} = Lemonldap::NG::Portal::Main->run($ini),
'Portal app' );
main::count(1);
$self;
2016-11-14 13:34:46 +01:00
}
);
2016-05-22 19:06:55 +02:00
sub logout {
my ( $self, $id ) = @_;
2016-05-22 19:06:55 +02:00
my $res;
2016-11-14 13:34:46 +01:00
main::ok(
$res = $self->_get(
2016-05-22 19:06:55 +02:00
'/',
query => 'logout',
cookie => "lemonldap=$id",
accept => 'text/html'
),
2016-12-23 07:41:03 +01:00
'Logout request'
2016-05-22 19:06:55 +02:00
);
main::ok( $res->[0] == 200, 'Response is 200' )
or explain( $res->[0], 200 );
my $c;
main::ok(
2016-12-23 07:41:03 +01:00
( defined( $c = main::getCookies($res)->{lemonldap} ) and not $c ),
'Cookie is deleted' )
or explain( $res->[1], "Set-Cookie => 'lemonldap='" );
2016-11-14 13:34:46 +01:00
main::ok( $res = $self->_get( '/', cookie => "lemonldap=$id" ),
'Disconnect request' )
or explain( $res, '[<code>,<hdrs>,<content>]' );
2016-11-14 13:34:46 +01:00
main::ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
main::count(5);
2016-05-22 19:06:55 +02:00
}
2016-04-05 07:23:42 +02:00
sub _get {
my ( $self, $path, %args ) = @_;
return $self->app->(
{
'HTTP_ACCEPT' => $args{accept}
|| 'application/json, text/plain, */*',
'HTTP_ACCEPT_LANGUAGE' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
2016-05-22 13:27:37 +02:00
'HTTP_CACHE_CONTROL' => 'max-age=0',
2016-05-23 18:55:23 +02:00
( $args{cookie} ? ( HTTP_COOKIE => $args{cookie} ) : () ),
2016-05-22 13:27:37 +02:00
'HTTP_HOST' => 'auth.example.com',
'HTTP_USER_AGENT' =>
'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
2016-05-23 18:55:23 +02:00
'PATH_INFO' => $path,
( $args{referer} ? ( REFERER => $args{referer} ) : () ),
2016-05-22 13:27:37 +02:00
'REMOTE_ADDR' => '127.0.0.1',
(
$args{remote_user}
? ( 'REMOTE_USER' => $args{remote_user} )
: ()
),
2016-04-05 07:23:42 +02:00
'REQUEST_METHOD' => 'GET',
2016-05-22 13:27:37 +02:00
'REQUEST_URI' => $path . ( $args{query} ? "?$args{query}" : '' ),
2016-05-23 18:55:23 +02:00
( $args{query} ? ( QUERY_STRING => $args{query} ) : () ),
2016-05-22 13:27:37 +02:00
'SCRIPT_NAME' => '',
'SERVER_NAME' => 'auth.example.com',
2016-12-07 23:30:00 +01:00
'SERVER_PORT' => '80',
2016-04-05 07:23:42 +02:00
'SERVER_PROTOCOL' => 'HTTP/1.1',
2016-05-25 21:30:43 +02:00
( $args{custom} ? %{ $args{custom} } : () ),
2016-04-05 07:23:42 +02:00
}
);
}
2016-05-30 22:20:50 +02:00
sub _post {
my ( $self, $path, $body, %args ) = @_;
die "$body must be a IO::Handle"
unless ( ref($body) and $body->can('read') );
return $self->app->(
{
'HTTP_ACCEPT' => $args{accept}
|| 'application/json, text/plain, */*',
'HTTP_ACCEPT_LANGUAGE' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'HTTP_CACHE_CONTROL' => 'max-age=0',
( $args{cookie} ? ( HTTP_COOKIE => $args{cookie} ) : () ),
'HTTP_HOST' => 'auth.example.com',
'HTTP_USER_AGENT' =>
'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
'PATH_INFO' => $path,
( $args{query} ? ( QUERY_STRING => $args{query} ) : () ),
( $args{referer} ? ( REFERER => $args{referer} ) : () ),
2016-05-30 22:20:50 +02:00
'REMOTE_ADDR' => '127.0.0.1',
(
$args{remote_user}
? ( 'REMOTE_USER' => $args{remote_user} )
: ()
),
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => $path . ( $args{query} ? "?$args{query}" : '' ),
'SCRIPT_NAME' => '',
'SERVER_NAME' => 'auth.example.com',
2016-12-07 23:30:00 +01:00
'SERVER_PORT' => '80',
2016-05-30 22:20:50 +02:00
'SERVER_PROTOCOL' => 'HTTP/1.1',
( $args{custom} ? %{ $args{custom} } : () ),
'psgix.input.buffered' => 1,
'psgi.input' => $body,
'CONTENT_LENGTH' => $args{length} // scalar( ( stat $body )[7] ),
'CONTENT_TYPE' => $args{type}
|| 'application/x-www-form-urlencoded',
}
);
}
2016-04-03 18:27:13 +02:00
1;