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

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
47
48
49
50
51
52
53
54
55
56
57
58
59
sub logout {
    my ($id) = @_;
    my $res;
    ok(
        $res = &client->_get(
            '/',
            query  => 'logout',
            cookie => "lemonldap=$id",
            accept => 'text/html'
        ),
        'Logout'
    );
    ok( $res->[0] == 200, 'Response is 200' ) or explain( $res->[0], 200 );
60
61
62
63
    my $c;
    ok( ( defined( $c = getCookies($res)->{lemonldap} ) and not $c ),
        'Cookie is deleted' )
      or explain( $res->[1], "Set-Cookie => 'lemonldap='" );
Yadd's avatar
Yadd committed
64
    ok( $res = &client->_get( '/', cookie => "lemonldap=$id" ),
65
66
        'Disconnect request' )
      or explain( $res, '[<code>,<hdrs>,<content>]' );
Yadd's avatar
Yadd committed
67
    ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
68
    count(5);
Yadd's avatar
Yadd committed
69
70
71

}

Yadd's avatar
Yadd committed
72
73
74
75
76
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
77
78
79
80
    opendir D, 't/sessions/lock' or die $!;
    foreach ( grep { /^[^\.]/ } readdir(D) ) {
        unlink "t/sessions/lock/$_";
    }
Yadd's avatar
Yadd committed
81
82
}

Yadd's avatar
Yadd committed
83
sub getCookies {
84
85
    my $resp = shift;
    my @hdrs = @{ $resp->[1] };
Yadd's avatar
Yadd committed
86
87
88
89
    my $res  = {};
    while ( my $name = shift @hdrs ) {
        my $v = shift @hdrs;
        if ( $name eq 'Set-Cookie' ) {
90
            if ( $v =~ /^(\w+)=([^;]*)/ ) {
Yadd's avatar
Yadd committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
                $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, */*',
            'HTTP_ACCEPT_LANGUAGE' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
Yadd's avatar
Yadd committed
120
            'HTTP_CACHE_CONTROL'   => 'max-age=0',
Yadd's avatar
Yadd committed
121
            ( $args{cookie} ? ( HTTP_COOKIE => $args{cookie} ) : () ),
Yadd's avatar
Yadd committed
122
123
124
            'HTTP_HOST' => 'auth.example.com',
            'HTTP_USER_AGENT' =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
Yadd's avatar
Yadd committed
125
126
            'PATH_INFO' => $path,
            ( $args{referer} ? ( REFERER => $args{referer} ) : () ),
Yadd's avatar
Yadd committed
127
128
129
130
131
132
            'REMOTE_ADDR' => '127.0.0.1',
            (
                $args{remote_user}
                ? ( 'REMOTE_USER' => $args{remote_user} )
                : ()
            ),
Yadd's avatar
Yadd committed
133
            'REQUEST_METHOD' => 'GET',
Yadd's avatar
Yadd committed
134
            'REQUEST_URI'    => $path . ( $args{query} ? "?$args{query}" : '' ),
Yadd's avatar
Yadd committed
135
            ( $args{query} ? ( QUERY_STRING => $args{query} ) : () ),
Yadd's avatar
Yadd committed
136
137
            'SCRIPT_NAME'     => '',
            'SERVER_NAME'     => 'auth.example.com',
Yadd's avatar
Yadd committed
138
139
140
141
142
143
            'SERVER_PORT'     => '8002',
            'SERVER_PROTOCOL' => 'HTTP/1.1',
        }
    );
}

Yadd's avatar
Yadd committed
144
1;