42-Register-Demo-with-captcha.t 3.01 KB
Newer Older
1 2 3 4 5
use Test::More;
use strict;
use IO::String;

BEGIN {
6 7 8 9
    eval {
        require 't/test-lib.pm';
        require 't/smtp.pm';
    };
10 11
}

12
my $maintests = 14;
13 14
my ( $res, $user, $pwd, $host, $url, $query );
my $mailSend = 0;
15 16

SKIP: {
17
    eval
Xavier Guimard's avatar
Xavier Guimard committed
18
      'require Email::Sender::Simple;use GD::SecurityImage;use Image::Magick;';
19
    if ($@) {
20
        skip 'Missing dependencies', $maintests;
21
    }
22 23

    my $client = LLNG::Manager::Test->new(
Xavier Guimard's avatar
Xavier Guimard committed
24 25
        {
            ini => {
26
                logLevel                 => 'error',
27 28 29 30
                useSafeJail              => 1,
                portalDisplayRegister    => 1,
                registerDB               => 'Demo',
                captcha_register_enabled => 1,
31
                portalMainLogo           => 'common/logos/logo_llng_old.png',
32 33 34 35 36 37
            }
        }
    );

    # Test normal first access
    # ------------------------
Xavier Guimard's avatar
Xavier Guimard committed
38 39
    ok(
        $res = $client->_get( '/register', accept => 'text/html' ),
Xavier Guimard's avatar
Xavier Guimard committed
40
        'Unauth request',
41
    );
Xavier Guimard's avatar
Xavier Guimard committed
42 43 44 45 46
    ( $host, $url, $query ) =
      expectForm( $res, '#', undef, 'firstname', 'lastname', 'mail' );
    ok(
        $query =~
s/^.*token=([^&]+).*$/token=$1&firstname=foo&lastname=bar&mail=foobar%40badwolf.org/,
47 48 49 50 51
        'Token found'
    );
    my $token;
    ok( $token = $1, ' Token value is defined' );
    ok( $res->[2]->[0] =~ m#<img src="data:image/png;base64#,
52
        ' Captcha image inserted' )
Xavier Guimard's avatar
Xavier Guimard committed
53
      or print STDERR Dumper( $res->[2]->[0] );
54 55 56

    # Try to get captcha value
    my ( $ts, $captcha );
57 58 59
    ok( $ts = getCache()->get($token), ' Found token session' );
    $ts = eval { JSON::from_json($ts) };
    ok( $captcha = $ts->{captcha}, ' Found captcha value' );
Xavier Guimard's avatar
Xavier Guimard committed
60 61 62 63
    ok(
        $res->[2]->[0] =~ m%<img src="/static/common/logos/logo_llng_old.png"%,
        'Found custom Main Logo'
    ) or print STDERR Dumper( $res->[2]->[0] );
64 65 66

    $query .= "&captcha=$captcha";

Xavier Guimard's avatar
Xavier Guimard committed
67 68
    ok(
        $res = $client->_post(
69 70 71 72 73 74 75 76 77
            '/register',
            IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Ask to create account'
    );
    expectOK($res);

78 79 80
    ok( mail() =~ m#a href="http://auth.example.com/register\?(.*?)"#,
        'Found register token' );
    $query = $1;
81 82
    ok( $query =~ /register_token=/, 'Found register_token' );

Xavier Guimard's avatar
Xavier Guimard committed
83 84
    ok(
        $res = $client->_get(
85 86 87 88
            '/register',
            query  => $query,
            accept => 'text/html'
        ),
89 90 91 92
        'Push register_token'
    );
    expectOK($res);

Xavier Guimard's avatar
Xavier Guimard committed
93 94 95
    ok(
        mail() =~
          m#Your login is.+?<b>(\w+)</b>.*?Your password is.+?<b>(.*?)</b>#s,
96 97 98 99
        'Found user and password'
    );
    $user = $1;
    $pwd  = $2;
100 101 102 103 104
    ok( $user eq 'fbar', 'Get good login' );

    # Try to authenticate
    $query = '&user=fbar&password=fbar';

Xavier Guimard's avatar
Xavier Guimard committed
105 106
    ok(
        $res = $client->_post(
107 108 109 110 111 112 113 114 115
            '/', IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Try to authenticate'
    );
    expectCookie($res);
}
count($maintests);
116 117 118 119

clean_sessions();

done_testing( count() );