43-MailPasswordReset-LDAP.t 2.85 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4 5 6 7 8 9 10 11 12
use Test::More;
use strict;
use IO::String;

BEGIN {
    eval {
        require 't/test-lib.pm';
        require 't/smtp.pm';
    };
}

my ( $res, $user, $pwd );
Christophe Maudoux's avatar
Christophe Maudoux committed
13
my $maintests = 8;
Xavier Guimard's avatar
Xavier Guimard committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
my $mailSend  = 0;

my $mail2 = 0;

SKIP: {
    eval
      'require Email::Sender::Simple;use GD::SecurityImage;use Image::Magick;';
    if ($@) {
        skip 'Missing dependencies', $maintests;
    }

    skip 'LLNGTESTLDAP is not set', $maintests unless ( $ENV{LLNGTESTLDAP} );
    require 't/test-ldap.pm';

    my $client = LLNG::Manager::Test->new(
        {
            ini => {
31
                logLevel                   => 'error',
Xavier Guimard's avatar
Xavier Guimard committed
32 33 34 35 36
                useSafeJail                => 1,
                portalDisplayRegister      => 1,
                authentication             => 'LDAP',
                userDB                     => 'Same',
                passwordDB                 => 'LDAP',
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
37 38 39 40
                ldapServer                 => 'ldap://127.0.0.1:19389/',
                ldapBase                   => 'ou=users,dc=example,dc=com',
                managerDn                  => 'cn=admin,dc=example,dc=com',
                managerPassword            => 'admin',
Xavier Guimard's avatar
Xavier Guimard committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                captcha_mail_enabled       => 0,
                portalDisplayResetPassword => 1,
            }
        }
    );

    # Test form
    # ------------------------
    ok( $res = $client->_get( '/resetpwd', accept => 'text/html' ),
        'Reset form', );
    my ( $host, $url, $query ) = expectForm( $res, '#', undef, 'mail' );

    $query = 'mail=dwho%40badwolf.org';

    # Post email
    ok(
        $res = $client->_post(
            '/resetpwd', IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Post mail'
    );

    ok( mail() =~ m#a href="http://auth.example.com/resetpwd\?(.*?)"#,
        'Found link in mail' );
    $query = $1;

    ok(
        $res =
          $client->_get( '/resetpwd', query => $query, accept => 'text/html' ),
        'Post mail token received by mail'
    );
    ( $host, $url, $query ) = expectForm( $res, '#', undef, 'token' );
    ok( $res->[2]->[0] =~ /newpassword/s, ' Ask for a new password' );

    $query .= '&newpassword=zz&confirmpassword=zz';

    # Post new password
    ok(
        $res = $client->_post(
            '/resetpwd', IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Post new password'
    );

    ok( mail() =~ /Your password was changed/, 'Password was changed' );

Christophe Maudoux's avatar
Christophe Maudoux committed
91 92 93
    ok(
        $res = $client->_post(
            '/',
Christophe Maudoux's avatar
Typo  
Christophe Maudoux committed
94 95
            IO::String->new('user=dwho&password=zz'),
            length => 21
Christophe Maudoux's avatar
Christophe Maudoux committed
96 97 98 99 100 101 102 103
        ),
        'Auth query'
    );
    expectOK($res);
    my $id = expectCookie($res);

    $client->logout($id);

Xavier Guimard's avatar
Xavier Guimard committed
104 105 106 107 108
    #print STDERR Dumper($query);
}
count($maintests);
stopLdapServer() if $ENV{LLNGTESTLDAP};
done_testing( count() );