test-lib.pm 5.33 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
    $ini->{localSessionStorage} ||= '';
    $ini->{logLevel}            ||= 'error';
    $ini->{cookieName}          ||= 'lemonldap';
Yadd's avatar
Yadd committed
21
    $ini->{domain}              ||= 'example.com';
Yadd's avatar
Yadd committed
22
23
    $ini->{templateDir}         ||= 'site/templates';
    $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
53
54
55
56
57
58
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 );
59
60
61
62
    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
63
    ok( $res = &client->_get( '/', cookie => "lemonldap=$id" ),
64
65
        'Disconnect request' )
      or explain( $res, '[<code>,<hdrs>,<content>]' );
Yadd's avatar
Yadd committed
66
    ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
67
    count(5);
Yadd's avatar
Yadd committed
68
69
70

}

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

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

Yadd's avatar
Yadd committed
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
182
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
183
1;