test-psgi-lib.pm 3.53 KB
Newer Older
1 2 3 4 5
# Base library for tests

use strict;
use Data::Dumper;
use 5.10.0;
Xavier Guimard's avatar
Xavier Guimard committed
6
use POSIX 'strftime';
7 8 9
use_ok('Lemonldap::NG::Common::PSGI::Cli::Lib');

our $client;
10
our $count = 1;
Xavier Guimard's avatar
Xavier Guimard committed
11 12
$Data::Dumper::Deparse = 1;

Xavier Guimard's avatar
Xavier Guimard committed
13 14
no warnings 'redefine';

Xavier Guimard's avatar
Xavier Guimard committed
15
my $module;
Xavier Guimard's avatar
Xavier Guimard committed
16 17
our $sessionId =
  'f5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545';
Xavier Guimard's avatar
Xavier Guimard committed
18
our $file = "t/sessions/$sessionId";
19

20
sub init {
Xavier Guimard's avatar
Xavier Guimard committed
21
    my ( $arg, $prms ) = @_;
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
22
    if ($arg) {
23 24 25
        $module = $arg;
        use_ok($module);
    }
Xavier Guimard's avatar
Xavier Guimard committed
26 27
    $prms ||= {};
    %$prms = (
Xavier Guimard's avatar
Xavier Guimard committed
28 29 30 31 32 33
        configStorage       => { type => 'File', dirName => 't' },
        localSessionStorage => '',
        logLevel            => 'error',
        cookieName          => 'lemonldap',
        securedCookie       => 0,
        https               => 0,
Xavier Guimard's avatar
Xavier Guimard committed
34 35 36 37 38 39 40 41
        logger => 'Lemonldap::NG::Common::Logger::Std',
        %$prms
    );
    ok(
        $client =
          Lemonldap::NG::Handler::PSGI::Cli::Lib->new( { ini => $prms } ),
        'Client object'
    );
Xavier Guimard's avatar
Xavier Guimard committed
42 43
    ok( $client->app, 'App object' ) or explain( $client, '->app...' );
    count(3);
Xavier Guimard's avatar
Xavier Guimard committed
44 45 46 47 48
    open F, ">$file"
      or die $!;
    my $now = time;
    my $ts = strftime "%Y%m%d%H%M%S", localtime;

49
    print F '{"_updateTime":"'
Xavier Guimard's avatar
Xavier Guimard committed
50
      . $ts
51
      . '","_timezone":"1","_session_kind":"SSO","_passwordDB":"Demo","_startTime":"'
Xavier Guimard's avatar
Xavier Guimard committed
52 53 54 55 56
      . $ts
      . '","ipAddr":"127.0.0.1","UA":"Mozilla/5.0 (X11; VAX4000; rv:43.0) Gecko/20100101 Firefox/143.0 Iceweasel/143.0.1","_user":"dwho","_userDB":"Demo","_lastAuthnUTime":'
      . $now
      . ',"uid":"dwho","_issuerDB":"Null","_session_id":"f5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545","authenticationLevel":1,"_whatToTrace":"dwho","_auth":"Demo","_utime":'
      . $now
57
      . ',"_loginHistory":{"successLogin":[{"ipAddr":"127.0.0.1","_utime":'
Xavier Guimard's avatar
Xavier Guimard committed
58 59 60
      . $now
      . '}]},"cn":"Doctor Who","mail":"dwho@badwolf.org"}';
    close F;
61
}
62 63 64 65 66

sub client {
    return $client;
}

67
sub module {
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
68
    if ( my $arg = shift ) {
69 70 71 72 73
        $module = $arg;
    }
    return $module;
}

74 75 76 77 78 79
sub count {
    my $c = shift;
    $count += $c if ($c);
    return $count;
}

Xavier Guimard's avatar
Xavier Guimard committed
80 81 82 83 84 85
sub explain {
    my ( $get, $ref ) = @_;
    $get = Dumper($get) if ( ref $get );
    print STDERR "Expect $ref, get $get\n";
}

Xavier Guimard's avatar
Xavier Guimard committed
86 87 88 89
sub clean {
    unlink $file;
}

90 91 92 93 94 95
package Lemonldap::NG::Handler::PSGI::Cli::Lib;

use Mouse;

extends 'Lemonldap::NG::Common::PSGI::Cli::Lib';

Xavier Guimard's avatar
Xavier Guimard committed
96 97
has ini => ( is => 'rw' );

Xavier Guimard's avatar
Xavier Guimard committed
98 99 100 101
has app => (
    is      => 'ro',
    isa     => 'CodeRef',
    builder => sub {
Xavier Guimard's avatar
Xavier Guimard committed
102
        return $module->run( $_[0]->{ini} );
Xavier Guimard's avatar
Xavier Guimard committed
103 104 105
    }
);

106
sub _get {
Xavier Guimard's avatar
Xavier Guimard committed
107
    my ( $self, $path, $query, $host, $cookie, %custom ) = @_;
108 109 110 111 112 113 114 115 116 117 118 119
    $query //= '';
    $host ||= 'test1.example.com';
    return $self->app->(
        {
            'HTTP_ACCEPT'          => 'text/html',
            'SCRIPT_NAME'          => 'lmAuth',
            'SERVER_NAME'          => '127.0.0.1',
            'QUERY_STRING'         => $query,
            '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,
            'REQUEST_METHOD'       => 'GET',
Xavier Guimard's avatar
Xavier Guimard committed
120 121 122
            'REQUEST_URI'          => '/lmauth',
            'X_ORIGINAL_URI'       => $path . ( $query ? "?$query" : '' ),
            'SERVER_PORT'          => '80',
123 124 125 126
            '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',
Xavier Guimard's avatar
Xavier Guimard committed
127
            'HTTP_HOST'   => $host,
Xavier Guimard's avatar
Xavier Guimard committed
128 129
            ( $cookie ? ( HTTP_COOKIE => $cookie ) : ( HTTP_COOKIE => '' ) ),
            %custom,
130 131 132 133
        }
    );
}

134
1;