61-Lemonldap-NG-Handler-PSGI-Server.t 3.28 KB
Newer Older
1 2 3 4 5 6
use Test::More;
use JSON;
use MIME::Base64;

require 't/test-psgi-lib.pm';

7
init('Lemonldap::NG::Handler::Server');
8 9 10 11

my $res;

# Unauthentified query
12
# --------------------
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
ok( $res = $client->_get('/'), 'Unauthentified query' );
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);

# Authentified queries
# --------------------
Christophe Maudoux's avatar
Typo  
Christophe Maudoux committed
31
# Authorized query
Xavier Guimard's avatar
Xavier Guimard committed
32 33
ok( $res = $client->_get( '/', undef, undef, "lemonldap=$sessionId" ),
    'Authentified query' );
34 35 36 37 38
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res->[0], 200 );
count(2);

# Check headers
%h = @{ $res->[1] };
Xavier Guimard's avatar
Xavier Guimard committed
39
ok( $h{'Auth-User'} eq 'dwho', 'Header Auth-User is set to "dwho"' )
Xavier Guimard's avatar
Xavier Guimard committed
40
  or explain( \%h, 'Auth-User => "dwho"' );
41 42 43
count(1);

# Denied query
Xavier Guimard's avatar
Xavier Guimard committed
44 45
ok( $res = $client->_get( '/deny', undef, undef, "lemonldap=$sessionId" ),
    'Denied query' );
46
ok( $res->[0] == 403, 'Code is 403' ) or explain( $res->[0], 403 );
47
count(2);
48

49 50 51 52
# Required AuthnLevel = 1
ok( $res = $client->_get( '/AuthWeak', undef, undef, "lemonldap=$sessionId" ),
    'Weak Authentified query' );
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res, 200 );
53 54
count(2);

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
# Required AuthnLevel = 5
ok(
    $res = $client->_get( '/AuthStrong', undef, undef, "lemonldap=$sessionId" ),
    'Strong Authentified query'
);
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);

74 75 76 77 78 79 80 81 82 83 84
# 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 );
Xavier Guimard's avatar
Xavier Guimard committed
85 86 87
unlink(
't/sessions/lock/Apache-Session-e5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545.lock'
);
88
count(2);
89

90 91 92 93 94 95 96 97
# Required AuthnLevel = 1
ok(
    $res = $client->_get(
        '/AuthWeak', undef, 'test2.example.com', "lemonldap=$sessionId"
    ),
    'Weak Authentified query'
);
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res, 200 );
98 99
count(2);

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
# Required AuthnLevel = 5
ok(
    $res =
      $client->_get( '/', undef, 'test2.example.com', "lemonldap=$sessionId" ),
    'Default Authentified query'
);
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);

120 121
done_testing( count() );

Xavier Guimard's avatar
Xavier Guimard committed
122
clean();