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

94 lines
2.5 KiB
Perl
Raw 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;
2016-04-03 18:27:13 +02:00
require 't/test-lib.pm';
2016-04-04 07:08:26 +02:00
my $res;
2016-11-14 13:34:46 +01:00
my $client = LLNG::Manager::Test->new(
{ ini => { logLevel => 'error', useSafeJail => 1 } } );
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-04-04 07:08:26 +02:00
ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
count(2);
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'
);
ok( $res->[0] == 401, 'Response is 401' ) or explain( $res, 401 );
count(2);
2016-12-14 09:49:30 +01:00
# Try to authenticate
2016-04-08 06:40:41 +02:00
# -------------------
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'),
2016-05-30 22:20:50 +02:00
length => 23
2016-04-04 20:05:22 +02:00
),
2016-04-04 22:39:22 +02:00
'Auth query'
);
ok( $res->[0] == 200, 'Response is 200' ) or explain( $res->[0], 200 );
2016-11-14 13:34:46 +01:00
my $cookies = $client->getCookies($res);
2016-04-05 07:23:42 +02:00
my $id;
ok( $id = $cookies->{lemonldap}, 'Get cookie' )
or explain( $res, 'Set-Cookie: something' );
2016-04-14 20:42:59 +02:00
count(3);
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'
);
ok( $res->[0] == 302, 'Get redirection' )
or explain( [ $res->[0], $res->[1] ], 302 );
2016-04-05 22:46:11 +02:00
my %hdrs = @{ $res->[1] };
ok(
$hdrs{Location} eq 'http://test1.example.com/',
'Location is http://test1.example.com/'
) or explain( \%hdrs, 'Location => "http://test1.example.com/"' );
ok( $hdrs{'Lm-Remote-User'} eq 'dwho', 'User is set' )
or explain( \%hdrs, 'Lm-Remote-User => "dwho"' );
count(4);
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'
);
ok( $res->[0] == 200, 'HTTP code is 200' ) or explain( $res, 200 );
2016-05-25 21:30:43 +02:00
%hdrs = @{ $res->[1] };
2016-04-06 22:10:03 +02:00
ok( $hdrs{'Lm-Remote-User'} eq 'dwho', 'User is set' )
or explain( \%hdrs, 'Lm-Remote-User => "dwho"' );
ok( $hdrs{'Content-Type'} eq 'text/html', 'Reponse is HTML' )
or explain( \%hdrs, 'Content-Type => "text/html"' );
count(4);
2016-04-06 07:16:47 +02:00
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() );