test-psgi-lib.pm 3.49 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';
6 7 8
use_ok('Lemonldap::NG::Common::PSGI::Cli::Lib');

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

Xavier Guimard's avatar
Xavier Guimard committed
11 12
no warnings 'redefine';

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

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

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

sub client {
    return $client;
}

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

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

78 79 80 81 82 83
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
84 85 86 87
sub clean {
    unlink $file;
}

88 89 90 91 92 93
package Lemonldap::NG::Handler::PSGI::Cli::Lib;

use Mouse;

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

94 95
has ini => ( is => 'rw' );

96 97 98 99
has app => (
    is      => 'ro',
    isa     => 'CodeRef',
    builder => sub {
100
        return $module->run( $_[0]->{ini} );
101 102 103
    }
);

104
sub _get {
105
    my ( $self, $path, $query, $host, $cookie, %custom ) = @_;
106 107 108 109 110 111 112 113 114 115 116 117
    $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',
118 119 120
            'REQUEST_URI'          => '/lmauth',
            'X_ORIGINAL_URI'       => $path . ( $query ? "?$query" : '' ),
            'SERVER_PORT'          => '80',
121 122 123 124
            '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',
125
            'HTTP_HOST'   => $host,
126 127
            ( $cookie ? ( HTTP_COOKIE => $cookie ) : ( HTTP_COOKIE => '' ) ),
            %custom,
128 129 130 131
        }
    );
}

132
1;