lemonldap-ng/lemonldap-ng-portal/t/62-SingleSession.t

214 lines
5.6 KiB
Perl
Raw Normal View History

2017-02-17 08:40:18 +01:00
use Test::More;
use strict;
use IO::String;
2020-04-22 11:24:05 +02:00
use MIME::Base64;
2017-02-17 08:40:18 +01:00
BEGIN {
require 't/test-lib.pm';
}
my $level = 'error';
2017-02-17 08:40:18 +01:00
my $res;
my $client1 = LLNG::Manager::Test->new( {
2017-02-17 08:40:18 +01:00
ini => {
logLevel => $level,
2017-02-17 08:40:18 +01:00
authentication => 'Demo',
userDB => 'Same',
singleSession => 1,
}
}
);
my $client2 = LLNG::Manager::Test->new( {
ini => {
logLevel => $level,
authentication => 'Demo',
userDB => 'Same',
singleIP => 1,
}
}
);
my $client3 = LLNG::Manager::Test->new( {
ini => {
logLevel => $level,
authentication => 'Demo',
userDB => 'Same',
singleUserByIP => 1,
}
}
2017-02-17 08:40:18 +01:00
);
2020-04-25 13:29:18 +02:00
my $client4 = LLNG::Manager::Test->new( {
ini => {
logLevel => $level,
authentication => 'Demo',
userDB => 'Same',
notifyOther => 1,
notifyDeleted => 1,
singleIP => 1,
}
}
);
my $client5 = LLNG::Manager::Test->new( {
ini => {
logLevel => $level,
authentication => 'Demo',
userDB => 'Same',
}
}
);
sub loginUser {
2020-04-22 11:24:05 +02:00
my ( $client, $user, $ip, %args ) = @_;
my $query = "user=$user&password=$user";
ok(
my $res = $client->_post(
'/',
IO::String->new($query),
length => length($query),
2020-04-22 11:24:05 +02:00
accept => 'text/html',
ip => $ip,
2020-04-22 11:24:05 +02:00
%args
),
'Auth query'
);
count(1);
return $res;
}
sub testReq {
my ( $client, $id, $msg ) = @_;
my $res;
ok( $res = $client->_get( '/', cookie => "lemonldap=$id" ), $msg );
count(1);
return $res;
}
# Issue #2081
sub testGetParam {
my ( $res, $expected ) = @_;
if ($expected) {
ok(
$res->[2]->[0] =~
m%<input type="hidden" name="skin" value="bootstrap" />%,
'"skin=bootstrap" input found'
) or explain( $res->[2]->[0], '"skin=bootstrap" not found' );
}
else {
ok(
$res->[2]->[0] =
!m%<input type="hidden" name="skin" value="bootstrap" />%,
'"skin=bootstrap" input not found'
) or explain( $res->[2]->[0], '"skin=bootstrap" found' );
}
count(1);
}
####################
# Test singleSession
switch ($client1);
2020-04-22 11:24:05 +02:00
# Test login
$res = loginUser( $client1, "dwho", "127.0.0.1" );
2017-02-17 08:40:18 +01:00
my $id1 = expectCookie($res);
2020-04-22 11:24:05 +02:00
testGetParam( $res, 0 );
2017-02-17 08:40:18 +01:00
$res = loginUser( $client1, "dwho", "127.0.0.1" );
2017-02-17 08:40:18 +01:00
my $id2 = expectCookie($res);
2020-04-22 11:24:05 +02:00
testGetParam( $res, 1 );
# Check that skin=bootstrap isn't appended when going to external URL (#2081)
$res = loginUser( $client1, "dwho", "127.0.0.1",
2020-04-25 13:29:18 +02:00
query => 'url=' . encode_base64( "http://test1.example.com/", '' ) );
2020-04-22 11:24:05 +02:00
my $id3 = expectCookie($res);
testGetParam( $res, 0 );
2020-04-22 11:24:05 +02:00
expectOK( testReq( $client1, $id3, 'Attempt login with latest session' ) );
expectReject( testReq( $client1, $id2, 'Attempt login with removed session' ) );
expectReject( testReq( $client1, $id1, 'Attempt login with removed session' ) );
clean_sessions();
####################
# Test singleIP
switch ($client2);
$res = loginUser( $client2, "dwho", "127.0.0.1" );
2020-04-25 13:29:18 +02:00
$id1 = expectCookie($res);
$res = loginUser( $client2, "dwho", "127.0.0.1" );
2020-04-25 13:29:18 +02:00
$id2 = expectCookie($res);
$res = loginUser( $client2, "dwho", "127.0.0.2" );
2020-04-25 13:29:18 +02:00
$id3 = expectCookie($res);
$res = loginUser( $client2, "dwho", "127.0.0.2" );
my $id4 = expectCookie($res);
expectOK( testReq( $client2, $id3, 'First session on latest IP' ) );
expectOK( testReq( $client2, $id4, 'Latest session on latest IP' ) );
expectReject( testReq( $client2, $id1, 'session on old IP' ) );
expectReject( testReq( $client2, $id2, 'session on old IP' ) );
clean_sessions();
####################
# Test singleUserByIP
switch ($client3);
$res = loginUser( $client3, "rtyler", "127.0.0.1" );
2020-04-25 13:29:18 +02:00
$id1 = expectCookie($res);
$res = loginUser( $client3, "rtyler", "127.0.0.2" );
2020-04-25 13:29:18 +02:00
$id2 = expectCookie($res);
$res = loginUser( $client3, "dwho", "127.0.0.2" );
2020-04-25 13:29:18 +02:00
$id3 = expectCookie($res);
2017-02-17 08:40:18 +01:00
$res = loginUser( $client3, "dwho", "127.0.0.2" );
2020-04-25 13:29:18 +02:00
$id4 = expectCookie($res);
2017-02-17 08:40:18 +01:00
expectOK( testReq( $client3, $id1, 'Other user, but other IP' ) );
expectReject( testReq( $client3, $id2, 'Other user, same IP' ) );
expectOK( testReq( $client3, $id3, 'Same user, same IP' ) );
expectOK( testReq( $client3, $id4, 'Same user, same IP' ) );
2017-02-17 08:40:18 +01:00
clean_sessions();
2020-04-25 13:29:18 +02:00
####################
# Test DisplayDeleted & DisplayOther
switch ($client5);
$res = loginUser( $client5, "dwho", "127.0.0.1" );
$id1 = expectCookie($res);
$res = loginUser( $client5, "dwho", "127.0.0.1" );
$id2 = expectCookie($res);
$res = loginUser( $client5, "dwho", "127.0.0.2" );
$id3 = expectCookie($res);
switch ($client4);
2020-04-25 14:28:25 +02:00
$res = loginUser( $client4, "dwho", "127.0.0.2",
query => 'url=' . encode_base64( "http://test1.example.com/", '' ) );
2020-04-25 13:29:18 +02:00
$id4 = expectCookie($res);
2020-04-25 14:28:25 +02:00
ok( $res->[2]->[0] =~ m%<h3 trspan="sessionsDeleted"></h3>%,
'sessionsDeleted found' )
or explain( $res->[2]->[0], 'sessionsDeleted found' );
ok( $res->[2]->[0] =~ m%<h3 trspan="otherSessions"></h3>%,
'otherSessions found' )
or explain( $res->[2]->[0], 'otherSessions found' );
2020-04-25 13:29:18 +02:00
ok(
$res->[2]->[0] =~
2020-08-16 12:11:07 +02:00
m%<a href="http://auth.example.com/removeOther\?token=\w+?" onclick="_go=0" trspan="removeOtherSessions"></a>%,
2020-04-25 14:28:25 +02:00
'Link found'
2020-04-25 13:29:18 +02:00
) or explain( $res->[2]->[0], 'Link found' );
2020-05-24 00:04:33 +02:00
ok( $res->[2]->[0] =~ m%action="http://test1.example.com/"%, 'action found' )
or explain( $res->[2]->[0], 'action found' );
2020-04-25 14:28:25 +02:00
count(4);
2020-04-25 13:29:18 +02:00
clean_sessions();
2017-02-17 08:40:18 +01:00
done_testing( count() );