42-Register-Demo-with-captcha.t 5.16 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 = 28;
13 14
my ( $res, $user, $pwd, $host, $url, $query );
my $mailSend = 0;
15 16

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

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

35 36
    # Try to register with an alredy existing mail address
    # ------------------------
Xavier Guimard's avatar
Xavier Guimard committed
37 38
    ok(
        $res = $client->_get( '/register', accept => 'text/html' ),
39 40
        'Unauth request',
    );
Xavier Guimard's avatar
Xavier Guimard committed
41 42 43 44 45
    ( $host, $url, $query ) =
      expectForm( $res, '#', undef, 'firstname', 'lastname', 'mail' );
    ok(
        $query =~
s/^.*token=([^&]+).*$/token=$1&firstname=who&lastname=doctor&mail=dwho%40badwolf.org/,
46 47 48 49
        'Token found'
    );
    my $token;
    ok( $token = $1, ' Token value is defined' );
50
    ok( $res->[2]->[0] =~ m#<img id="captcha" src="data:image/png;base64#,
51
        ' Captcha image inserted' )
Xavier Guimard's avatar
Xavier Guimard committed
52
      or print STDERR Dumper( $res->[2]->[0] );
53 54 55 56 57 58

    # Try to get captcha value
    my ( $ts, $captcha );
    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
59 60
    ok(
        $res->[2]->[0] =~ m%<img src="/static/common/logos/logo_llng_old.png"%,
61 62
        'Found custom Main Logo'
    ) or print STDERR Dumper( $res->[2]->[0] );
63 64 65 66 67 68
    ok(
        $res->[2]->[0] =~
m#<img class="renewcaptchaclick" src="/static/common/icons/arrow_refresh.png" alt="Renew Captcha" title="Renew Captcha" class="img-thumbnail mb-3" />#,
        ' Renew Captcha button found'
    ) or explain( $res->[2]->[0], 'Renew captcha button not found' );
    ok( $res->[2]->[0] =~ /captcha\.(?:min\.)?js/, 'Get captcha javascript' );
69 70 71

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

Xavier Guimard's avatar
Xavier Guimard committed
72 73
    ok(
        $res = $client->_post(
74 75 76 77 78 79 80 81 82 83
            '/register',
            IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Ask to create account'
    );

    ok( $res->[2]->[0] =~ q%<span trmsg="80"></span>%,
        'Rejected -> Mail already exists' );
Xavier Guimard's avatar
Xavier Guimard committed
84 85 86
    ok(
        $res->[2]->[0] !~
          m%<form action="#" method="post" class="login" role="form">%,
87 88 89
        'No form found'
    );

90 91
    # Test normal first access
    # ------------------------
Xavier Guimard's avatar
Xavier Guimard committed
92 93 94 95
    ok( $res = $client->_get( '/', accept => 'text/html' ), 'Get Menu', );
    ok(
        $res->[2]->[0] =~
m%<a class="btn btn-secondary" href="http://auth.example.com/register\?skin=bootstrap">%,
96 97
        'Found Register link & submit button'
    ) or print STDERR Dumper( $res->[2]->[0] );
Xavier Guimard's avatar
Xavier Guimard committed
98 99
    ok(
        $res = $client->_get( '/register', accept => 'text/html' ),
Xavier Guimard's avatar
Xavier Guimard committed
100
        'Unauth request',
101
    );
Xavier Guimard's avatar
Xavier Guimard committed
102 103 104 105 106
    ( $host, $url, $query ) =
      expectForm( $res, '#', undef, 'firstname', 'lastname', 'mail' );
    ok(
        $query =~
s/^.*token=([^&]+).*$/token=$1&firstname=foo&lastname=bar&mail=foobar%40badwolf.org/,
107 108 109
        'Token found'
    );
    ok( $token = $1, ' Token value is defined' );
110
    ok( $res->[2]->[0] =~ m#<img id="captcha" src="data:image/png;base64#,
111
        ' Captcha image inserted' )
Xavier Guimard's avatar
Xavier Guimard committed
112
      or print STDERR Dumper( $res->[2]->[0] );
113 114

    # Try to get captcha value
115 116 117
    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
118 119
    ok(
        $res->[2]->[0] =~ m%<img src="/static/common/logos/logo_llng_old.png"%,
Xavier Guimard's avatar
Xavier Guimard committed
120 121
        'Found custom Main Logo'
    ) or print STDERR Dumper( $res->[2]->[0] );
122 123
    $query .= "&captcha=$captcha";

Xavier Guimard's avatar
Xavier Guimard committed
124 125
    ok(
        $res = $client->_post(
126 127 128 129 130 131 132 133 134
            '/register',
            IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Ask to create account'
    );
    expectOK($res);

135 136 137
    ok( mail() =~ m#a href="http://auth.example.com/register\?(.*?)"#,
        'Found register token' );
    $query = $1;
138 139
    ok( $query =~ /register_token=/, 'Found register_token' );

Xavier Guimard's avatar
Xavier Guimard committed
140 141
    ok(
        $res = $client->_get(
142 143 144 145
            '/register',
            query  => $query,
            accept => 'text/html'
        ),
146 147 148 149
        'Push register_token'
    );
    expectOK($res);

Xavier Guimard's avatar
Xavier Guimard committed
150 151 152
    ok(
        mail() =~
          m#Your login is.+?<b>(\w+)</b>.*?Your password is.+?<b>(.*?)</b>#s,
153 154 155 156
        'Found user and password'
    );
    $user = $1;
    $pwd  = $2;
157 158 159 160 161
    ok( $user eq 'fbar', 'Get good login' );

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

Xavier Guimard's avatar
Xavier Guimard committed
162 163
    ok(
        $res = $client->_post(
164 165 166 167 168 169 170 171 172
            '/', IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Try to authenticate'
    );
    expectCookie($res);
}
count($maintests);
173 174 175 176

clean_sessions();

done_testing( count() );