28-AuthChoice-with-rules.t 3.75 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4 5 6 7
use Test::More;
use strict;
use IO::String;

require 't/test-lib.pm';

my $res;
8
my $maintests = 13;
Xavier Guimard's avatar
Xavier Guimard committed
9 10 11 12 13 14 15 16 17 18 19 20 21

eval { unlink 't/userdb.db' };

SKIP: {
    eval { require DBI; require DBD::SQLite; };
    if ($@) {
        skip 'DBD::SQLite not found', $maintests;
    }
    my $dbh = DBI->connect("dbi:SQLite:dbname=t/userdb.db");
    $dbh->do('CREATE TABLE users (user text,password text,name text)');
    $dbh->do("INSERT INTO users VALUES ('dwho','dwho','Doctor who')");

    my $client = LLNG::Manager::Test->new(
Xavier Guimard's avatar
Xavier Guimard committed
22 23
        {
            ini => {
Xavier Guimard's avatar
Xavier Guimard committed
24 25
                logLevel       => 'error',
                useSafeJail    => 1,
26
                portalMainLogo => 'common/logos/logo_llng_old.png',
Xavier Guimard's avatar
Xavier Guimard committed
27 28 29 30 31
                authentication => 'Choice',
                userDB         => 'Same',

                authChoiceParam   => 'test',
                authChoiceModules => {
32 33 34
                    '1_demo' => 'Demo;Demo;Null;;0',
                    '2_sql'  => 'DBI;DBI;DBI;;1',
                    '3_demo' =>
Xavier Guimard's avatar
Xavier Guimard committed
35
'Demo;Demo;Null;https://test.example.com;$env->{ipAddr} =~ /127.0.0.1/',
36
                    '4_demo' =>
Xavier Guimard's avatar
Xavier Guimard committed
37
'Demo;Demo;Null;https://test.example.com;$env->{ipAddr} =~ /1.2.3.4/',
Xavier Guimard's avatar
Xavier Guimard committed
38
                    '5_ssl'        => 'SSL;Demo;Demo',
Xavier Guimard's avatar
Xavier Guimard committed
39
                    '6_FakeCustom' => 'Custom;Demo;Demo',
Xavier Guimard's avatar
Xavier Guimard committed
40 41 42 43 44 45 46 47 48
                },

                dbiAuthChain        => 'dbi:SQLite:dbname=t/userdb.db',
                dbiAuthUser         => '',
                dbiAuthPassword     => '',
                dbiAuthTable        => 'users',
                dbiAuthLoginCol     => 'user',
                dbiAuthPasswordCol  => 'password',
                dbiAuthPasswordHash => '',
49
                customAuth          => '::Auth::Apache',
Xavier Guimard's avatar
Xavier Guimard committed
50
                customAddParams     => {},
51 52
                sslByAjax           => 1,
                sslHost             => 'https://authssl.example.com:19876'
Xavier Guimard's avatar
Xavier Guimard committed
53 54 55 56 57
            }
        }
    );

    ok( $res = $client->_get( '/', accept => 'text/html' ), 'Get Menu' );
Xavier Guimard's avatar
Xavier Guimard committed
58 59 60
    ok( $res->[2]->[0] !~ /1_demo/,       '1_demo not displayed' );
    ok( $res->[2]->[0] =~ /3_demo/,       '3_demo displayed' );
    ok( $res->[2]->[0] =~ /5_ssl/,        '5_ssl displayed' );
61 62 63
    ok( $res->[2]->[0] =~ /6_FakeCustom/, '6_FakeCustom displayed' );
    ok( $res->[2]->[0] =~ qr%img src="/static/common/modules/Apache.png"%,
        'Found 6_FakeCustom Logo' )
Xavier Guimard's avatar
Xavier Guimard committed
64
      or print STDERR Dumper( $res->[2]->[0] );
65 66
    ok( $res->[2]->[0] =~ qr%<img src="/static/common/modules/SSL.png"%,
        'Found 5_ssl Logo' )
Xavier Guimard's avatar
Xavier Guimard committed
67
      or print STDERR Dumper( $res->[2]->[0] );
68
    ok( $res->[2]->[0] =~ m%<form id="lformDemo" action="https://test.example.com"%,
69
        ' Redirect URL found' )
Xavier Guimard's avatar
Xavier Guimard committed
70
      or print STDERR Dumper( $res->[2]->[0] );
71 72 73
    ok( $res->[2]->[0] =~ m%<script type="application/init">\{"sslHost":"https://authssl.example.com:19876"\}</script>%,
        ' SSL AJAX URL found' )
      or print STDERR Dumper( $res->[2]->[0] );
Xavier Guimard's avatar
Xavier Guimard committed
74
    my $header = getHeader( $res, 'Content-Security-Policy' );
75
    ok( $header =~ m%;form-action \'self\'  https://test.example.com;%,
76
        ' CSP URL found' )
Xavier Guimard's avatar
Xavier Guimard committed
77
      or print STDERR Dumper( $res->[1] );
78
    ok( $res->[2]->[0] !~ /4_demo/, '4_Demo not displayed' );
Xavier Guimard's avatar
Xavier Guimard committed
79 80 81 82
    ok(
        $res->[2]->[0] =~ qr%<img src="/static/common/logos/logo_llng_old.png"%,
        'Found custom Main Logo'
    ) or print STDERR Dumper( $res->[2]->[0] );
Xavier Guimard's avatar
Xavier Guimard committed
83 84

    # Test SQL
85
    my $postString = 'user=dwho&password=dwho&test=2_sql';
Xavier Guimard's avatar
Xavier Guimard committed
86 87 88

    # Try yo authenticate
    # -------------------
Xavier Guimard's avatar
Xavier Guimard committed
89 90
    ok(
        $res = $client->_post(
Xavier Guimard's avatar
Xavier Guimard committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104
            '/', IO::String->new($postString),
            length => length($postString)
        ),
        'Auth query'
    );
    expectOK($res);
    my $id = expectCookie($res);
    $client->logout($id);
    clean_sessions();
}
count($maintests);
eval { unlink 't/userdb.db' };
clean_sessions();
done_testing( count() );