test-lib.pm 5.29 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

Yadd's avatar
Yadd committed
7
use_ok('Lemonldap::NG::Portal::Main');
Yadd's avatar
Yadd committed
8
9

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

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

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
45
46
47
48
49
50
51
52
53
54
55
56
57
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 );
58
59
60
61
    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
62
    ok( $res = &client->_get( '/', cookie => "lemonldap=$id" ),
63
64
        'Disconnect request' )
      or explain( $res, '[<code>,<hdrs>,<content>]' );
Yadd's avatar
Yadd committed
65
    ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
66
    count(5);
Yadd's avatar
Yadd committed
67
68
69

}

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

Yadd's avatar
Yadd committed
81
sub getCookies {
82
83
    my $resp = shift;
    my @hdrs = @{ $resp->[1] };
Yadd's avatar
Yadd committed
84
85
86
87
    my $res  = {};
    while ( my $name = shift @hdrs ) {
        my $v = shift @hdrs;
        if ( $name eq 'Set-Cookie' ) {
88
            if ( $v =~ /^(\w+)=([^;]*)/ ) {
Yadd's avatar
Yadd committed
89
90
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
                $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
118
            'HTTP_CACHE_CONTROL'   => 'max-age=0',
Yadd's avatar
Yadd committed
119
            ( $args{cookie} ? ( HTTP_COOKIE => $args{cookie} ) : () ),
Yadd's avatar
Yadd committed
120
121
122
            'HTTP_HOST' => 'auth.example.com',
            'HTTP_USER_AGENT' =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
Yadd's avatar
Yadd committed
123
124
            'PATH_INFO' => $path,
            ( $args{referer} ? ( REFERER => $args{referer} ) : () ),
Yadd's avatar
Yadd committed
125
126
127
128
129
130
            'REMOTE_ADDR' => '127.0.0.1',
            (
                $args{remote_user}
                ? ( 'REMOTE_USER' => $args{remote_user} )
                : ()
            ),
Yadd's avatar
Yadd committed
131
            'REQUEST_METHOD' => 'GET',
Yadd's avatar
Yadd committed
132
            'REQUEST_URI'    => $path . ( $args{query} ? "?$args{query}" : '' ),
Yadd's avatar
Yadd committed
133
            ( $args{query} ? ( QUERY_STRING => $args{query} ) : () ),
Yadd's avatar
Yadd committed
134
135
            'SCRIPT_NAME'     => '',
            'SERVER_NAME'     => 'auth.example.com',
Yadd's avatar
Yadd committed
136
137
            'SERVER_PORT'     => '8002',
            'SERVER_PROTOCOL' => 'HTTP/1.1',
Yadd's avatar
Yadd committed
138
            ( $args{custom} ? %{ $args{custom} } : () ),
Yadd's avatar
Yadd committed
139
140
141
142
        }
    );
}

Yadd's avatar
Yadd committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
sub _post {
    my ( $self, $path, $body, %args ) = @_;
    die "$body must be a IO::Handle"
      unless ( ref($body) and $body->can('read') );
    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',
            'HTTP_CACHE_CONTROL'   => 'max-age=0',
            ( $args{cookie} ? ( HTTP_COOKIE => $args{cookie} ) : () ),
            'HTTP_HOST' => 'auth.example.com',
            'HTTP_USER_AGENT' =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
            'PATH_INFO' => $path,
            ( $args{query} ? ( QUERY_STRING => $args{query} ) : () ),
            ( $args{referer} ? ( REFERER => $args{referer} ) : () ),
            'REMOTE_ADDR' => '127.0.0.1',
            (
                $args{remote_user}
                ? ( 'REMOTE_USER' => $args{remote_user} )
                : ()
            ),
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI'    => $path . ( $args{query} ? "?$args{query}" : '' ),
            'SCRIPT_NAME'     => '',
            'SERVER_NAME'     => 'auth.example.com',
            'SERVER_PORT'     => '8002',
            'SERVER_PROTOCOL' => 'HTTP/1.1',
            ( $args{custom} ? %{ $args{custom} } : () ),
            'psgix.input.buffered' => 1,
            'psgi.input'           => $body,
            'CONTENT_LENGTH' => $args{length} // scalar( ( stat $body )[7] ),
            'CONTENT_TYPE'   => $args{type}
              || 'application/x-www-form-urlencoded',
        }
    );
}

Yadd's avatar
Yadd committed
182
1;