66-CDA-with-REST.t 5.35 KB
Newer Older
1
use lib 'inc';
2 3 4 5
use Test::More;
use strict;
use IO::String;
use LWP::UserAgent;
6
use LWP::Protocol::PSGI;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

use Lemonldap::NG::Portal::Main::Constants qw(
  PE_FIRSTACCESS
);

require 't/test-lib.pm';

my $res;
my $maintests = 8;
my $debug     = 'error';
my %handlerOR = ( portal => [], app => [] );
my $client;

# Redefine LWP methods for tests
LWP::Protocol::PSGI->register(
    sub {
        my $req = Plack::Request->new(@_);
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
24 25
        ok( $req->uri =~ m#http://auth.example.com(.*?)(?:\?(.*))?$#,
            " @ REST REQUEST @" );
26
        count(1);
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
27
        my $url   = $1;
28 29 30 31 32 33 34 35
        my $query = $2;
        my $res;
        switch ('portal');
        if ( $req->method =~ /^(post|put)$/i ) {
            my $mth = '_' . lc($1);
            my $s   = $req->content;
            ok(
                $res = $client->$mth(
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
36 37 38 39
                    $url,
                    IO::String->new($s),
                    length => length($s),
                    type   => $req->header('Content-Type'),
40 41 42 43 44 45 46 47 48 49
                ),
                ' Post request'
            );
            count(1);
            expectOK($res);
        }
        elsif ( $req->method =~ /^(get|delete)$/i ) {
            my $mth = '_' . lc($1);
            ok(
                $res = $client->$mth(
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
50 51 52 53
                    $url,
                    accept => $req->header('Accept'),
                    cookie => $req->header('Cookie'),
                    query  => $query,
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                ),
                ' Execute request'
            );
            ok( ( $res->[0] == 200 or $res->[0] == 400 ),
                ' Response is 200 or 400' )
              or explain( $res->[0], '200 or 400' );
            count(2);
        }
        pass(' @ END OF REST REQUEST @');
        count(1);
        switch ('app');
        return $res;
    }
);

Xavier Guimard's avatar
Xavier Guimard committed
69
$client = LLNG::Manager::Test->new( {
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
70 71 72 73 74 75
        ini => {
            logLevel          => $debug,
            useSafeJail       => 1,
            cda               => 1,
            restSessionServer => 1,
            logger            => 'Lemonldap::NG::Common::Logger::Std',
76
        }
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
77 78 79
    }
);
$handlerOR{portal} = \@Lemonldap::NG::Handler::Main::_onReload;
80

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
81 82 83 84 85 86 87 88 89 90 91
# CDA with unauthentified user
ok(
    $res = $client->_get(
        '/',
        query  => 'url=aHR0cDovL3Rlc3QuZXhhbXBsZS5vcmcv',
        accept => 'text/html',
    ),
    'Unauth CDA request'
);
my ( $host, $url, $query ) = expectForm( $res, undef, undef, 'url' );
ok( $query =~ /\burl=aHR0cDovL3Rlc3QuZXhhbXBsZS5vcmcv\b/, ' check url value' );
92

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
93 94 95 96 97 98 99 100 101 102
# Authentification
$query .= '&user=dwho&password=dwho';
ok(
    $res = $client->_post(
        '/'    => IO::String->new($query),
        length => length($query),
        accept => 'text/html',
    ),
    'Post credentials'
);
103

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
104 105
($query) =
  expectRedirection( $res, qr#^http://test.example.org/\?(lemonldapcda=.*)$# );
106

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
107 108 109
# Handler part
use_ok('Lemonldap::NG::Handler::Server');
use_ok('Lemonldap::NG::Common::PSGI::Cli::Lib');
110

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
111 112 113 114
my ( $cli, $app );
&Lemonldap::NG::Handler::Main::cfgNum( 0, 0 );
switch ('app');
ok(
Xavier Guimard's avatar
Xavier Guimard committed
115
    $app = Lemonldap::NG::Handler::Server->run( {
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
116 117 118 119 120 121 122 123 124
            %{ $client->ini },
            globalStorage => 'Lemonldap::NG::Common::Apache::Session::REST',
            globalStorageOptions =>
              { baseUrl => 'http://auth.example.com/sessions/global/' },
            localSessionStorage => undef,
        }
    ),
    'App'
);
125

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
126
ok(
Xavier Guimard's avatar
Xavier Guimard committed
127
    $res = $app->( {
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            'HTTP_ACCEPT'          => 'text/html',
            'SCRIPT_NAME'          => '/',
            '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'            => '/',
            'REQUEST_METHOD'       => 'GET',
            'REQUEST_URI'          => "/?$query",
            'X_ORIGINAL_URI'       => "/?$query",
            'SERVER_PORT'          => '80',
            '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'   => 'test.example.org',
            'VHOSTTYPE'   => 'CDA',
        }
    ),
    'Push cda cookie'
);
expectRedirection( $res, 'http://test.example.org/' );
my $cid = expectCookie($res);
151

Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
152
ok(
Xavier Guimard's avatar
Xavier Guimard committed
153
    $res = $app->( {
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            'HTTP_ACCEPT'          => 'text/html',
            'SCRIPT_NAME'          => '/',
            'SERVER_NAME'          => '127.0.0.1',
            'HTTP_COOKIE'          => "lemonldap=$cid",
            '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'            => '/',
            'REQUEST_METHOD'       => 'GET',
            'REQUEST_URI'          => "/",
            'X_ORIGINAL_URI'       => "/",
            'SERVER_PORT'          => '80',
            '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'   => 'test.example.org',
            'VHOSTTYPE'   => 'CDA',
        }
    ),
    'Authenticated query'
);
expectOK($res);
expectAuthenticatedAs( $res, 'dwho' );
177 178 179 180 181 182 183 184 185 186 187

clean_sessions();

done_testing( count($maintests) );

sub switch {
    my $type = shift;
    @Lemonldap::NG::Handler::Main::_onReload = @{
        $handlerOR{$type};
    };
}