lemonldap-ng/lemonldap-ng-handler/t/60-Lemonldap-NG-Handler-PSGI.t

150 lines
4.4 KiB
Perl
Raw Normal View History

2016-02-01 12:11:31 +01:00
use Test::More;
use JSON;
2016-02-01 21:10:28 +01:00
use MIME::Base64;
2019-02-05 11:23:09 +01:00
use Data::Dumper;
2016-02-01 12:11:31 +01:00
2016-02-01 21:10:28 +01:00
require 't/test-psgi-lib.pm';
2016-02-01 12:11:31 +01:00
2016-02-01 13:15:27 +01:00
init('Lemonldap::NG::Handler::PSGI');
2016-02-01 20:05:14 +01:00
my $res;
2016-02-01 21:10:28 +01:00
# Unauthentified query
2019-10-29 22:14:34 +01:00
# --------------------
2016-02-01 21:10:28 +01:00
ok( $res = $client->_get('/'), 'Unauthentified query' );
2016-02-01 20:05:14 +01:00
ok( ref($res) eq 'ARRAY', 'Response is an array' ) or explain( $res, 'array' );
ok( $res->[0] == 302, 'Code is 302' ) or explain( $res->[0], 302 );
my %h = @{ $res->[1] };
ok(
$h{Location} eq 'http://auth.example.com/?url='
. encode_base64( 'http://test1.example.com/', '' ),
'Redirection points to portal'
)
or explain(
\%h,
'Location => http://auth.example.com/?url='
. encode_base64( 'http://test1.example.com/', '' )
);
count(4);
2016-02-01 21:10:28 +01:00
# Authentified queries
# --------------------
2019-08-26 21:57:13 +02:00
# Authorized query
2016-06-09 13:45:10 +02:00
ok( $res = $client->_get( '/', undef, undef, "lemonldap=$sessionId" ),
'Authentified query' );
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res, 200 );
2016-02-01 21:10:28 +01:00
count(2);
2019-11-05 17:16:07 +01:00
# Request an URI protected by custom function -> allowed
ok( $res = $client->_get( '/test-uri1/dwho', undef, undef, "lemonldap=$sessionId" ),
'Authentified query' );
ok( $res->[0] == 200, '/test-uri1 -> Code is 200' ) or explain( $res, 200 );
count(2);
# Request an URI protected by custom function -> allowed
ok( $res = $client->_get( '/test-uri2/dwho/dummy', undef, undef, "lemonldap=$sessionId" ),
'Authentified query' );
ok( $res->[0] == 200, '/test-uri2 -> Code is 200' ) or explain( $res, 200 );
count(2);
# Request an URI protected by custom function -> denied
ok( $res = $client->_get( '/test-uri1/dwho/', undef, undef, "lemonldap=$sessionId" ),
'Denied query' );
ok( $res->[0] == 403, '/test-uri1 -> Code is 403' ) or explain( $res->[0], 403 );
count(2);
# Request an URI protected by custom function -> denied
ok( $res = $client->_get( '/test-uri1/dwh', undef, undef, "lemonldap=$sessionId" ),
'Denied query' );
ok( $res->[0] == 403, '/test-uri1 -> Code is 403' ) or explain( $res->[0], 403 );
count(2);
2016-02-01 21:10:28 +01:00
# Denied query
2016-06-09 13:45:10 +02:00
ok( $res = $client->_get( '/deny', undef, undef, "lemonldap=$sessionId" ),
'Denied query' );
2016-02-01 21:10:28 +01:00
ok( $res->[0] == 403, 'Code is 403' ) or explain( $res->[0], 403 );
2019-10-29 22:14:34 +01:00
count(2);
2016-02-01 21:10:28 +01:00
2019-10-29 22:14:34 +01:00
# Required AuthnLevel = 1
ok( $res = $client->_get( '/AuthWeak', undef, undef, "lemonldap=$sessionId" ),
2019-10-29 22:35:21 +01:00
'Weak Authentified query' );
2019-10-29 22:14:34 +01:00
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res, 200 );
2016-02-01 21:10:28 +01:00
count(2);
2019-10-29 22:14:34 +01:00
# Required AuthnLevel = 5
ok(
$res = $client->_get( '/AuthStrong', undef, undef, "lemonldap=$sessionId" ),
2019-10-29 22:35:21 +01:00
'Strong Authentified query'
2019-10-29 22:14:34 +01:00
);
ok( $res->[0] == 302, 'Code is 302' ) or explain( $res, 302 );
%h = @{ $res->[1] };
ok(
$h{Location} eq 'http://auth.example.com//upgradesession?url='
. encode_base64( 'http://test1.example.com/AuthStrong', '' ),
'Redirection points to http://test1.example.com/AuthStrong'
)
or explain(
\%h,
'http://auth.example.com//upgradesession?url='
. encode_base64( 'http://test1.example.com/AuthStrong', '' )
);
count(3);
2016-02-01 21:10:28 +01:00
# Bad cookie
ok(
$res = $client->_get(
'/deny',
undef,
'manager.example.com',
'lemonldap=e5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545'
),
'Bad cookie'
);
ok( $res->[0] == 302, 'Code is 302' ) or explain( $res->[0], 302 );
2016-02-17 11:12:19 +01:00
unlink(
't/sessions/lock/Apache-Session-e5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545.lock'
);
2019-10-29 22:14:34 +01:00
count(2);
2016-02-01 21:10:28 +01:00
2019-10-29 22:14:34 +01:00
# Required AuthnLevel = 1
ok(
$res = $client->_get(
'/AuthWeak', undef, 'test2.example.com', "lemonldap=$sessionId"
),
2019-10-29 22:35:21 +01:00
'Weak Authentified query'
2019-10-29 22:14:34 +01:00
);
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res, 200 );
2016-02-01 21:10:28 +01:00
count(2);
2019-10-29 22:14:34 +01:00
# Required AuthnLevel = 5
ok(
$res =
$client->_get( '/', undef, 'test2.example.com', "lemonldap=$sessionId" ),
2019-10-29 22:35:21 +01:00
'Default Authentified query'
2019-10-29 22:14:34 +01:00
);
ok( $res->[0] == 302, 'Code is 302' ) or explain( $res, 302 );
%h = @{ $res->[1] };
ok(
$h{Location} eq 'http://auth.example.com//upgradesession?url='
. encode_base64( 'http://test2.example.com/', '' ),
'Redirection points to http://test2.example.com/'
)
or explain(
\%h,
'http://auth.example.com//upgradesession?url='
. encode_base64( 'http://test2.example.com/', '' )
);
count(3);
2016-02-01 12:11:31 +01:00
done_testing( count() );
2016-02-01 21:10:28 +01:00
2016-06-09 13:45:10 +02:00
clean();
sub Lemonldap::NG::Handler::PSGI::handler {
2016-02-01 21:10:28 +01:00
my ( $self, $req ) = @_;
ok( $req->env->{HTTP_AUTH_USER} eq 'dwho', 'Header is given to app' )
or explain( $req->env->{HTTP_AUTH_USER}, 'dwho' );
2016-02-01 21:10:28 +01:00
count(1);
return [ 200, [ 'Content-Type', 'text/plain' ], ['Hello'] ];
}