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

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

our $client;
10
our $count = 1;
11

12 13 14 15
BEGIN {
    require 't/Time-Fake.pm';
}

Xavier Guimard's avatar
Xavier Guimard committed
16 17
no warnings 'redefine';

18
my $module;
19 20
our $sessionId =
  'f5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545';
Xavier Guimard's avatar
Xavier Guimard committed
21
our $file = "t/sessions/$sessionId";
22

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

52
    print F '{"_updateTime":"'
Xavier Guimard's avatar
Xavier Guimard committed
53
      . $ts
54
      . '","_timezone":"1","_session_kind":"SSO","_passwordDB":"Demo","_startTime":"'
Xavier Guimard's avatar
Xavier Guimard committed
55 56 57 58 59
      . $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
60
      . ',"_loginHistory":{"successLogin":[{"ipAddr":"127.0.0.1","_utime":'
Xavier Guimard's avatar
Xavier Guimard committed
61 62 63
      . $now
      . '}]},"cn":"Doctor Who","mail":"dwho@badwolf.org"}';
    close F;
64
}
65 66 67 68 69

sub client {
    return $client;
}

70
sub module {
Xavier Guimard's avatar
Xavier Guimard committed
71
    if ( my $arg = shift ) {
72 73 74 75 76
        $module = $arg;
    }
    return $module;
}

77 78 79 80 81 82
sub count {
    my $c = shift;
    $count += $c if ($c);
    return $count;
}

83 84 85 86 87 88
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
89 90 91 92
sub clean {
    unlink $file;
}

93 94 95 96 97 98
package Lemonldap::NG::Handler::PSGI::Cli::Lib;

use Mouse;

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

99 100
has ini => ( is => 'rw' );

101 102 103 104
has app => (
    is      => 'ro',
    isa     => 'CodeRef',
    builder => sub {
105
        return $module->run( $_[0]->{ini} );
106 107 108
    }
);

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

136
1;