42-Register-Demo-with-token.t 2.47 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 = 10;
13 14
my ( $res, $user, $pwd );

15 16 17 18
SKIP: {
    eval 'require Email::Sender::Simple';
    if ($@) {
        skip 'Missing dependencies', $maintests;
19 20
    }

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    my $client = LLNG::Manager::Test->new(
        {
            ini => {
                logLevel                 => 'error',
                useSafeJail              => 1,
                portalDisplayRegister    => 1,
                registerDB               => 'Demo',
                captcha_register_enabled => 0,
                requireToken             => 1,
            }
        }
    );

    # Test normal first access
    # ------------------------
    ok(
        $res = $client->_get( '/register', accept => 'text/html' ),
        'Unauth request',
    );
    my ( $host, $url, $query ) =
      expectForm( $res, '#', undef, 'firstname', 'lastname', 'mail' );
    ok(
        $query =~
44
s/^.*(token=[^&]+).*$/$1&firstname=foo&lastname=bar&mail=foobar%40badwolf.org/,
45 46
        'Token found'
    );
47

48 49 50 51 52 53 54 55 56 57
    ok(
        $res = $client->_post(
            '/register',
            IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Ask to create account'
    );
    expectOK($res);
58

59 60 61 62 63
    ok( mail() =~ m#a href="http://auth.example.com/register\?(.*?)"#,
        'Found register token' )
      or print STDERR Dumper($main::mail);
    $query = $1;
    ok( $query =~ /register_token=/, 'Found register_token' );
64

65 66 67 68 69 70
    ok(
        $res =
          $client->_get( '/register', query => $query, accept => 'text/html' ),
        'Push register_token'
    );
    expectOK($res);
71

72 73 74 75 76 77 78 79
    ok(
        mail() =~
          m#Your login is.+?<b>(\w+)</b>.*?Your password is.+?<b>(.*?)</b>#s,
        'Found user and password'
    );
    $user = $1;
    $pwd  = $2;
    ok( $user eq 'fbar', 'Get good login' );
80

81 82 83
    # Try to authenticate
    #  1. get token
    ok( $res = $client->_get( '/', accept => 'text/html' ), 'Unauth request' );
84

85 86
    ( $host, $url, $query ) = expectForm( $res, '#', undef, 'token' );
    $query =~ s/.*\b(token=[^&]+).*/$1&user=fbar&password=fbar/;
87

88 89 90 91 92 93 94 95 96 97 98
    ok(
        $res = $client->_post(
            '/', IO::String->new($query),
            length => length($query),
            accept => 'text/html'
        ),
        'Try to authenticate'
    );
    expectCookie($res);
}
count($maintests);
99 100 101 102 103

clean_sessions();

done_testing( count() );