lemonldap-ng/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t

185 lines
4.8 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
2017-01-22 23:51:22 +01:00
use Test::More;
use strict;
use IO::String;
use LWP::UserAgent;
2018-09-05 22:24:23 +02:00
use LWP::Protocol::PSGI;
2017-01-22 23:51:22 +01:00
BEGIN {
require 't/test-lib.pm';
}
my $debug = 'error';
my ( $issuer, $sp, $res, $spId );
# Redefine LWP methods for tests
LWP::Protocol::PSGI->register(
sub {
my $req = Plack::Request->new(@_);
ok(
$req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#,
' @ REST request (' . $req->method . " $1)"
);
count(1);
my $url = $1;
my $query = $2;
my $res;
if ( $req->method =~ /^(post|put)$/i ) {
my $mth = '_' . lc($1);
my $s = $req->content;
ok(
$res = $issuer->$mth(
$url,
IO::String->new($s),
length => length($s),
type => $req->header('Content-Type'),
),
' Post request'
);
count(1);
expectOK($res);
}
elsif ( $req->method =~ /^(get|delete)$/i ) {
my $mth = '_' . lc($1);
ok(
$res = $issuer->$mth(
$url,
accept => $req->header('Accept'),
cookie => $req->header('Cookie'),
query => $query,
),
' 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);
return $res;
}
);
2020-02-20 23:34:02 +01:00
$issuer = register( 'issuer', \&issuer );
$sp = register( 'sp', \&sp );
2017-01-22 23:51:22 +01:00
# Simple SP access
ok(
$res = $sp->_get(
'/', accept => 'text/html',
),
'Unauth SP request'
);
expectOK($res);
# Try to auth
ok(
$res = $sp->_post(
2017-03-01 18:35:12 +01:00
'/', IO::String->new('user=french&password=french'),
length => 27,
2017-01-22 23:51:22 +01:00
accept => 'text/html'
),
'Post user/password'
);
count(2);
expectRedirection( $res, 'http://auth.sp.com' );
$spId = expectCookie($res);
# Test auth
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId" ), 'Auth test' );
count(1);
expectOK($res);
2017-01-22 23:51:22 +01:00
# Test other REST queries
switch ('issuer');
# Session content
ok( $res = $issuer->_get("/sessions/global/$spId"), 'Session content' );
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
or print STDERR $@;
ok( $res->{_session_id} eq $spId, ' Good ID' )
or explain( $res, "_session_id => $spId" );
ok( $res->{array} =~ /;/, 'Mulivalued attribute found' )
or explain( $res, "Multivalued attribute" );
2020-03-19 23:05:49 +01:00
count(4);
2017-01-22 23:51:22 +01:00
# Session key
2017-03-01 18:35:12 +01:00
ok( $res = $issuer->_get("/sessions/global/$spId/[_session_id,uid,cn]"),
2017-01-22 23:51:22 +01:00
'Some session keys' );
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
or print STDERR $@;
ok( $res->{_session_id} eq $spId, ' Good ID' )
or explain( $res, "_session_id => $spId" );
2017-03-01 18:35:12 +01:00
ok( $res->{uid} eq 'french', ' Uid is french' )
or explain( $res, 'uid => french' );
2017-06-23 11:57:07 +02:00
#ok( $res->{cn} eq 'Frédéric Accents', 'UTF-8 values' )
# or explain( $res->{cn}, 'Frédéric Accents' );
count(4);
2017-01-22 23:51:22 +01:00
# Logout
switch ('sp');
ok(
$res = $sp->_get(
'/',
query => 'logout',
accept => 'text/html',
cookie => "lemonldap=$spId"
),
'Ask for logout'
);
count(1);
expectOK($res);
# Test if user is reject on IdP
ok(
$res = $sp->_get(
'/', cookie => "lemonldap=$spId",
),
'Test if user is reject on IdP'
);
count(1);
expectReject($res);
clean_sessions();
done_testing( count() );
sub issuer {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2017-01-22 23:51:22 +01:00
ini => {
logLevel => $debug,
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
2017-01-22 23:51:22 +01:00
restSessionServer => 1,
}
}
);
}
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2017-01-22 23:51:22 +01:00
ini => {
logLevel => $debug,
domain => 'sp.com',
portal => 'http://auth.sp.com',
authentication => 'Demo',
userDB => 'Same',
2017-01-22 23:51:22 +01:00
globalStorage => 'Lemonldap::NG::Common::Apache::Session::REST',
globalStorageOptions => {
baseUrl => 'http://auth.idp.com/sessions/global/'
},
persistentStorage =>
'Lemonldap::NG::Common::Apache::Session::REST',
2017-02-27 20:30:43 +01:00
persistentStorageOptions => {
baseUrl => 'http://auth.idp.com/sessions/persistent/'
2017-02-27 20:30:43 +01:00
},
2017-01-22 23:51:22 +01:00
},
}
);
}