lemonldap-ng/lemonldap-ng-portal/t/01-AuthDemo.t

189 lines
5.0 KiB
Perl
Raw Permalink Normal View History

2016-04-03 18:27:13 +02:00
use Test::More;
use strict;
2016-04-04 07:08:26 +02:00
use IO::String;
2020-02-23 12:46:03 +01:00
use MIME::Base64;
2021-09-15 14:09:04 +02:00
use URI;
use URI::QueryParam;
2016-04-03 18:27:13 +02:00
require 't/test-lib.pm';
2016-04-04 07:08:26 +02:00
my $res;
2022-05-16 22:32:11 +02:00
my $client = LLNG::Manager::Test->new( {
ini => {
logLevel => 'error',
portal => 'https://auth.example.com/',
useSafeJail => 1,
strictTransportSecurityMax_Age => '1977',
portalFavicon => 'common/llng.ico'
2022-05-16 22:32:11 +02:00
}
}
);
2016-04-05 07:23:42 +02:00
2016-04-08 06:40:41 +02:00
# Test normal first access
# ------------------------
2016-11-14 13:34:46 +01:00
ok( $res = $client->_get('/'), 'Unauth JSON request' );
2016-12-23 07:41:03 +01:00
count(1);
expectReject($res);
2016-04-04 07:08:26 +02:00
2020-02-23 12:46:03 +01:00
# Test "first access" with an unprotected url
ok(
$res = $client->_get(
'/',
query => 'url=' . encode_base64( "http://test.example.fr/", '' ),
accept => 'text/html'
),
'Get Menu'
);
ok( getHeader( $res, 'Strict-Transport-Security' ) =~ /^max-age=1977$/,
'Strict-Transport-Security is set' )
or explain( $res->[1], 'Content-Type => application/xml' );
2020-05-24 00:04:33 +02:00
ok( $res->[2]->[0] =~ /<span trmsg="37">/, 'Rejected with PE_BADURL' )
2020-02-23 12:46:03 +01:00
or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%<span id="languages"></span>%, ' Language icons found' )
or print STDERR Dumper( $res->[2]->[0] );
2022-06-23 12:12:25 +02:00
ok( $res->[2]->[0] =~ m%link href="/static/common/llng.ico%,
' Custom favicon found' )
2022-05-16 22:32:11 +02:00
or print STDERR Dumper( $res->[2]->[0] );
count(5);
2020-02-23 12:46:03 +01:00
# Test "first access" with a wildcard-protected url
ok(
$res = $client->_get(
'/',
query => 'url=' . encode_base64( "http://test.example.llng/", '' ),
accept => 'text/html'
),
'Get Menu'
);
2020-05-24 00:04:33 +02:00
ok( $res->[2]->[0] =~ /<span trmsg="9">/, 'Rejected with PE_FIRSTACCESS' )
2020-02-23 12:46:03 +01:00
or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%<span id="languages"></span>%, ' Language icons found' )
or print STDERR Dumper( $res->[2]->[0] );
count(3);
2016-04-08 06:40:41 +02:00
# Test "first access" with good url
2016-05-30 22:20:50 +02:00
ok(
$res =
2016-11-14 13:34:46 +01:00
$client->_get( '/', query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==' ),
2016-05-30 22:20:50 +02:00
'Unauth ajax request with good url'
);
2016-12-23 07:41:03 +01:00
count(1);
expectReject($res);
2018-11-07 22:27:58 +01:00
ok( $res = $client->_get( '/', accept => 'text/html' ), 'Get Menu' );
2018-11-26 14:40:21 +01:00
ok( $res->[2]->[0] =~ m%<span id="languages"></span>%, ' Language icons found' )
or print STDERR Dumper( $res->[2]->[0] );
2018-11-07 22:27:58 +01:00
count(2);
# Try to authenticate with unknown user
# -------------------------------------
ok(
$res = $client->_post(
'/',
IO::String->new('user=jdoe&password=jdoe'),
accept => 'text/html',
length => 23
),
'Auth query'
);
2020-05-24 00:04:33 +02:00
ok(
$res->[2]->[0] =~ /<span trmsg="5">/,
'jdoe rejected with PE_BADCREDENTIALS'
) or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%<span trspan="connect">Connect</span>%,
'Found connect button' )
or print STDERR Dumper( $res->[2]->[0] );
2020-02-23 12:46:03 +01:00
count(3);
2021-09-15 14:09:04 +02:00
my ( $host, $uri, $query ) =
expectForm( $res, undef, undef, 'user', 'password' );
my $uri = URI->new;
$uri->query($query);
is( $uri->query_param("user"), 'jdoe',
"Login is pre-filled on second attemps" );
count(1);
# Try to authenticate with bad password
# -------------------------------------
ok(
$res = $client->_post(
'/',
IO::String->new('user=dwho&password=jdoe'),
accept => 'text/html',
length => 23
),
'Auth query'
);
count(1);
2020-05-24 00:04:33 +02:00
ok(
$res->[2]->[0] =~ /<span trmsg="5">/,
'dwho rejected with PE_BADCREDENTIALS'
) or print STDERR Dumper( $res->[2]->[0] );
count(1);
ok( $res->[2]->[0] =~ m%<span trspan="connect">Connect</span>%,
'Found connect button' )
or print STDERR Dumper( $res->[2]->[0] );
count(1);
# Try to authenticate with good password
# --------------------------------------
2016-04-04 20:05:22 +02:00
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_post(
2016-05-30 22:20:50 +02:00
'/',
2016-04-04 20:05:22 +02:00
IO::String->new('user=dwho&password=dwho'),
2018-11-07 22:27:58 +01:00
length => 23,
2016-04-04 20:05:22 +02:00
),
2016-04-04 22:39:22 +02:00
'Auth query'
);
2016-12-23 07:41:03 +01:00
count(1);
expectOK($res);
my $id = expectCookie($res);
2016-04-03 18:27:13 +02:00
2016-04-08 06:40:41 +02:00
# Try to get a redirection for an auth user with a valid url
# ----------------------------------------------------------
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_get(
'/',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
cookie => "lemonldap=$id",
accept => 'text/html'
),
'Auth ajax request with good url'
);
2016-12-23 07:41:03 +01:00
count(1);
expectRedirection( $res, 'http://test1.example.com/' );
expectAuthenticatedAs( $res, 'dwho' );
2016-04-05 22:46:11 +02:00
2016-04-08 06:40:41 +02:00
# Try to get a redirection for an auth user with a bad url (host undeclared
# in manager)
# -------------------------------------------------------------------------
2016-04-06 22:10:03 +02:00
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_get(
2016-04-06 22:10:03 +02:00
'/',
query => 'url=aHR0cHM6Ly90LmV4YW1wbGUuY29tLw==',
cookie => "lemonldap=$id",
accept => 'text/html'
),
'Auth request with bad url'
);
2016-12-23 07:41:03 +01:00
count(1);
expectOK($res);
expectAuthenticatedAs( $res, 'dwho' );
2016-04-06 07:16:47 +02:00
2017-03-04 09:07:41 +01:00
require 't/test-psgi.pm';
ok( $res = mirror( cookie => "lemonldap=$id" ), 'PSGI test' );
count(1);
expectOK($res);
expectAuthenticatedAs( $res, 'dwho' );
2016-05-22 19:06:55 +02:00
# Test logout
2016-11-14 13:34:46 +01:00
$client->logout($id);
2016-05-22 19:06:55 +02:00
2016-04-05 22:46:11 +02:00
#print STDERR Dumper($res);
2016-04-05 22:46:11 +02:00
clean_sessions();
2016-04-04 22:39:22 +02:00
2016-04-03 18:27:13 +02:00
done_testing( count() );