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

184 lines
5.3 KiB
Perl
Raw Normal View History

2016-04-03 18:27:13 +02:00
# Base library for portal tests
use strict;
use Data::Dumper;
use 5.10.0;
2016-04-05 07:23:42 +02:00
2016-05-25 21:30:43 +02:00
use_ok('Lemonldap::NG::Portal::Main');
2016-04-03 18:27:13 +02:00
our $client;
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
2016-04-05 07:23:42 +02:00
sub init {
$ini = shift;
$ini ||= {};
$ini->{configStorage} ||= { type => 'File', dirName => 't' };
$ini->{localSessionStorage} ||= '';
$ini->{logLevel} ||= 'error';
$ini->{cookieName} ||= 'lemonldap';
2016-05-31 22:41:35 +02:00
$ini->{domain} ||= 'example.com';
$ini->{templateDir} ||= 'site/templates';
$ini->{staticPrefix} ||= '/index.fcgi';
2016-04-05 07:23:42 +02:00
$ini->{securedCookie} //= 0;
$ini->{https} //= 0;
ok( $client = My::Cli->new(), 'Portal app' );
count(1);
}
2016-04-03 18:27:13 +02:00
sub client {
return $client;
}
sub count {
my $c = shift;
$count += $c if ($c);
return $count;
}
sub explain {
my ( $get, $ref ) = @_;
$get = Dumper($get) if ( ref $get );
print STDERR "Expect $ref, get $get\n";
}
2016-05-22 19:06:55 +02:00
sub logout {
my ($id) = @_;
my $res;
ok(
$res = &client->_get(
'/',
query => 'logout',
cookie => "lemonldap=$id",
accept => 'text/html'
),
'Logout'
);
ok( $res->[0] == 200, 'Response is 200' ) or explain( $res->[0], 200 );
my $c;
ok( ( defined( $c = getCookies($res)->{lemonldap} ) and not $c ),
'Cookie is deleted' )
or explain( $res->[1], "Set-Cookie => 'lemonldap='" );
2016-05-22 19:06:55 +02:00
ok( $res = &client->_get( '/', cookie => "lemonldap=$id" ),
'Disconnect request' )
or explain( $res, '[<code>,<hdrs>,<content>]' );
2016-05-22 19:06:55 +02:00
ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
count(5);
2016-05-22 19:06:55 +02:00
}
2016-04-04 22:39:22 +02:00
sub clean_sessions {
opendir D, 't/sessions' or die $!;
foreach ( grep { /^[^\.]/ } readdir(D) ) {
unlink "t/sessions/$_", "t/sessions/lock/Apache-Session-$_.lock";
}
2016-05-22 19:06:55 +02:00
opendir D, 't/sessions/lock' or die $!;
foreach ( grep { /^[^\.]/ } readdir(D) ) {
unlink "t/sessions/lock/$_";
}
2016-04-04 22:39:22 +02:00
}
2016-04-05 07:23:42 +02:00
sub getCookies {
my $resp = shift;
my @hdrs = @{ $resp->[1] };
2016-04-05 07:23:42 +02:00
my $res = {};
while ( my $name = shift @hdrs ) {
my $v = shift @hdrs;
if ( $name eq 'Set-Cookie' ) {
if ( $v =~ /^(\w+)=([^;]*)/ ) {
2016-04-05 07:23:42 +02:00
$res->{$1} = $2;
}
}
}
return $res;
}
package My::Cli;
use strict;
use Mouse;
extends 'Lemonldap::NG::Common::PSGI::Cli::Lib';
has app => (
is => 'ro',
isa => 'CodeRef',
builder => sub {
return Lemonldap::NG::Portal::Main->run($ini);
}
);
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-04-05 07:23:42 +02:00
'SERVER_PORT' => '8002',
'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} ) : () ),
'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',
'SERVER_PORT' => '8002',
'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;