From 088664b9fd75e6a755c46a574c76d137802e62f1 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Tue, 5 Apr 2016 05:23:42 +0000 Subject: [PATCH] Improve tests (#595) --- lemonldap-ng-portal/t/01-AuthDemo.t | 8 ++- lemonldap-ng-portal/t/test-lib.pm | 93 +++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/lemonldap-ng-portal/t/01-AuthDemo.t b/lemonldap-ng-portal/t/01-AuthDemo.t index 6d26d2a0a..cc58f2413 100644 --- a/lemonldap-ng-portal/t/01-AuthDemo.t +++ b/lemonldap-ng-portal/t/01-AuthDemo.t @@ -6,6 +6,8 @@ require 't/test-lib.pm'; my $res; +init( { logLevel => 'debug' } ); + ok( $res = &client->_get('/'), 'Unauth JSON request' ); ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 ); count(2); @@ -18,8 +20,12 @@ ok( ), 'Auth query' ); + +my $cookies = getCookies($res); +my $id; +ok( $id = $cookies->{lemonldap}, 'Get cookie' ); print STDERR Dumper($res); -count(1); +count(2); clean_sessions(); diff --git a/lemonldap-ng-portal/t/test-lib.pm b/lemonldap-ng-portal/t/test-lib.pm index 896449b1f..07633c494 100644 --- a/lemonldap-ng-portal/t/test-lib.pm +++ b/lemonldap-ng-portal/t/test-lib.pm @@ -3,29 +3,27 @@ use strict; use Data::Dumper; use 5.10.0; -use_ok('Lemonldap::NG::Common::PSGI::Cli::Lib'); -use_ok('Lemonldap::NG::Portal::Main'); + +BEGIN { + use_ok('Lemonldap::NG::Portal::Main'); +} our $client; -our $count = 3; +our $count = 1; $Data::Dumper::Deparse = 1; +my $ini; -ok( - $client = Lemonldap::NG::Common::PSGI::Cli::Lib->new( - { - app => Lemonldap::NG::Portal::Main->run( - { - configStorage => { type => 'File', dirName => 't' }, - logLevel => 'debug', - cookieName => 'lemonldap', - securedCookie => 0, - https => 0, - } - ), - } - ), - 'Portal app' -); +sub init { + $ini = shift; + $ini ||= {}; + $ini->{configStorage} ||= { type => 'File', dirName => 't' }; + $ini->{logLevel} ||= 'error'; + $ini->{cookieName} ||= 'lemonldap'; + $ini->{securedCookie} //= 0; + $ini->{https} //= 0; + ok( $client = My::Cli->new(), 'Portal app' ); + count(1); +} sub client { return $client; @@ -50,4 +48,61 @@ sub clean_sessions { } } +sub getCookies { + my $req = shift; + my @hdrs = @{ $req->[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; +} + +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, */*', + 'SCRIPT_NAME' => '', + 'SERVER_NAME' => 'auth.example.com', + 'HTTP_CACHE_CONTROL' => 'max-age=0', + 'HTTP_ACCEPT_LANGUAGE' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', + 'PATH_INFO' => $path, + ( $args{cookie} ? ( 'COOKIE' => $args{cookie} ) : () ), + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => $path + . ( $args{query} ? "?$args{query}" : '' ), + ( $args{query} ? ( 'QUERY_STRING' => $args{query} ) : () ), + 'SERVER_PORT' => '8002', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'HTTP_USER_AGENT' => + 'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox', + 'REMOTE_ADDR' => '127.0.0.1', + 'HTTP_HOST' => 'auth.example.com' + + } + ); +} + 1;