test-lib.pm 2.59 KB
Newer Older
Yadd's avatar
Yadd committed
1
2
3
4
5
# Base library for portal tests

use strict;
use Data::Dumper;
use 5.10.0;
Yadd's avatar
Yadd committed
6
7
8
9

BEGIN {
    use_ok('Lemonldap::NG::Portal::Main');
}
Yadd's avatar
Yadd committed
10
11

our $client;
Yadd's avatar
Yadd committed
12
our $count = 1;
Yadd's avatar
Yadd committed
13
$Data::Dumper::Deparse = 1;
Yadd's avatar
Yadd committed
14
my $ini;
Yadd's avatar
Yadd committed
15

Yadd's avatar
Yadd committed
16
17
18
19
20
21
sub init {
    $ini = shift;
    $ini ||= {};
    $ini->{configStorage} ||= { type => 'File', dirName => 't' };
    $ini->{logLevel}      ||= 'error';
    $ini->{cookieName}    ||= 'lemonldap';
Yadd's avatar
Yadd committed
22
    $ini->{templateDir}   ||= 'site/templates';
Yadd's avatar
Yadd committed
23
    $ini->{staticPrefix}  ||= '/index.fcgi';
Yadd's avatar
Yadd committed
24
25
26
27
28
    $ini->{securedCookie} //= 0;
    $ini->{https}         //= 0;
    ok( $client = My::Cli->new(), 'Portal app' );
    count(1);
}
Yadd's avatar
Yadd committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

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";
}

Yadd's avatar
Yadd committed
46
47
48
49
50
51
52
sub clean_sessions {
    opendir D, 't/sessions' or die $!;
    foreach ( grep { /^[^\.]/ } readdir(D) ) {
        unlink "t/sessions/$_", "t/sessions/lock/Apache-Session-$_.lock";
    }
}

Yadd's avatar
Yadd committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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,
Yadd's avatar
Yadd committed
94
            ( $args{cookie} ? ( 'HTTP_COOKIE' => $args{cookie} ) : () ),
Yadd's avatar
Yadd committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
            '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'

        }
    );
}

Yadd's avatar
Yadd committed
110
1;