60_ldap.t 3.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use warnings;
use strict;

use Data::Dumper;
use Hash::Util qw(lock_hash);
use Test::More;
use YAML qw( LoadFile );

use lib 't/lib';
use Test::Ravada;

use_ok('Ravada::Front');
use_ok('Ravada::Auth');
use_ok('Ravada::Auth::LDAP');

Francesc Guasch's avatar
Francesc Guasch committed
16
my $CONFIG_FILE = 't/etc/ravada_ldap.conf';
17

Francesc Guasch's avatar
Francesc Guasch committed
18
init( $CONFIG_FILE);
19
20
rvd_back();

21
my $RVD_FRONT;
Francesc Guasch's avatar
Francesc Guasch committed
22
my $USER_DATA = { name => 'jimmy', password => 'jameson' };
23
24
25
26
27
28

#########################################################################

sub test_ldap {
    $RVD_FRONT = Ravada::Front->new(
        config => $CONFIG_FILE
Francesc Guasch's avatar
Francesc Guasch committed
29
        ,connector => connector()
30
    );
Francesc Guasch's avatar
Francesc Guasch committed
31
    delete $Ravada::CONFIG->{ldap}->{ravada_posix_group};
Francesc Guasch's avatar
Francesc Guasch committed
32
    create_ldap_user($USER_DATA->{name}, $USER_DATA->{password});
33
34
35
36
37
38
39
40
41
42
43
    my $login_ok;
    eval { $login_ok = Ravada::Auth::login($USER_DATA->{name}, $USER_DATA->{password}) };
    is($@, '');
    ok($login_ok,"Expecting login with $USER_DATA->{name}");

    $login_ok = 0;
    eval { $login_ok = Ravada::Auth::SQL->new( %$USER_DATA ); };
    like($@, qr(Login failed));
    ok(!$login_ok,"Expecting no login with SQL");

    delete $Ravada::CONFIG->{ldap}->{auth};
44
    $login_ok = undef;
45
46
47
48
49
    eval { $login_ok = Ravada::Auth::LDAP->new( %$USER_DATA ); };
    is($@, '');
    ok($login_ok,"Expecting login with $USER_DATA->{name}");

    $Ravada::CONFIG->{ldap}->{auth} = 'bind';
50
    $login_ok = undef;
51
52
53
54
55
56
57
    eval { $login_ok = Ravada::Auth::LDAP->new( %$USER_DATA ); };
    is($@, '');
    ok($login_ok,"Expecting login with $USER_DATA->{name}");
    is($login_ok->{_auth},'bind',"Expecting bind login with $USER_DATA->{name}")
        if $login_ok;

    $Ravada::CONFIG->{ldap}->{auth} = 'match';
58
    $login_ok = undef;
59
60
61
62
63
64
65
66
67
68
69
70
    eval { $login_ok = Ravada::Auth::LDAP->new( %$USER_DATA ); };
    is($@, '');
    ok($login_ok,"Expecting login with $USER_DATA->{name}");
    is($login_ok->{_auth},'match',"Expecting match login with $USER_DATA->{name}")
        if $login_ok;

    ok(Ravada::Auth::LDAP::_init_ldap_admin(),"Expecting LDAP admin connected");
}

#########################################################################

SKIP: {
Francesc Guasch's avatar
Francesc Guasch committed
71
<<<<<<< HEAD
Francesc Guasch's avatar
Francesc Guasch committed
72
    my $ravada = Ravada->new(config => $CONFIG_FILE
73
                        , connector => connector());
Francesc Guasch's avatar
Francesc Guasch committed
74
75
76
77
78
79
80
81
    $ravada->_install();
    my $ldap;


    eval { $ldap = Ravada::Auth::LDAP::_init_ldap_admin() };

    if ($@ =~ /Bad credentials/) {
        diag("$@\nFix admin credentials in $CONFIG_FILE");
Francesc Guasch's avatar
Francesc Guasch committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
||||||| parent of 6811de74... Feature #894 master (#897)
    my $ok = 1;
    $USER_DATA = LoadFile($file_test_data)  if -e $file_test_data;
    if (!-e $file_test_data || !$USER_DATA->{name} || !$USER_DATA->{password}) {
        my $config = {
            name => 'ldap.cn', password => '****'
        };
        warn "SKIPPED: To test Front LDAP create the file $file_test_data with\n"
            .YAML::Dump($config);
        $ok = 0;
=======
    my $ravada = Ravada->new(config => $CONFIG_FILE
                        , connector => connector);
    $ravada->_install();
    my $ldap;


    eval { $ldap = Ravada::Auth::LDAP::_init_ldap_admin() };

    if ($@ =~ /Bad credentials/) {
        diag("$@\nFix admin credentials in $CONFIG_FILE");
>>>>>>> 6811de74... Feature #894 master (#897)
104
    } else {
Francesc Guasch's avatar
Francesc Guasch committed
105
        diag("Skipped LDAP tests ".($@ or '')) if !$ldap;
106
107
    }

Francesc Guasch's avatar
Francesc Guasch committed
108
109
110
111
112
113
114
    skip( ($@ or "No LDAP server found"),6) if !$ldap && $@ !~ /Bad credentials/;

    ok($ldap) and do {

        test_ldap();

    };
115
116
117
}

done_testing();