02-Password-Demo.t 3.59 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5 6 7 8 9 10 11
use Test::More;
use strict;
use IO::String;
use JSON;
use Lemonldap::NG::Portal::Main::Constants
  qw(PE_BADOLDPASSWORD PE_PASSWORD_MISMATCH PE_PP_MUST_SUPPLY_OLD_PASSWORD);

require 't/test-lib.pm';

my $res;

Yadd's avatar
Yadd committed
12
my $client = LLNG::Manager::Test->new( {
Yadd's avatar
Yadd committed
13 14 15 16
        ini => {
            logLevel                 => 'error',
            passwordDB               => 'Demo',
            portalRequireOldPassword => 1,
17
            showLanguages            => 0,
Christophe Maudoux's avatar
Christophe Maudoux committed
18 19
            translations             => 'site/templates/localeTranslations.txt',
            error_de_85              => 'From lemonlap-ng.ini',
Yadd's avatar
Yadd committed
20
        }
Yadd's avatar
Yadd committed
21 22 23
    }
);

24 25 26
ok( $res = $client->_get( '/', accept => 'text/html' ), 'Get Menu' );
ok( $res->[2]->[0] !~ m%<span id="languages"></span>%,
    ' No language icon found' )
Yadd's avatar
Yadd committed
27
  or print STDERR Dumper( $res->[2]->[0] );
Christophe Maudoux's avatar
Christophe Maudoux committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
ok( $res->[2]->[0] =~ m%"trOver"%,
    ' trOver found' )
  or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%"all":\{\}%,
    ' all found' )
  or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%"en":\{"PE5":"Big brother is watching you, authenticated user"\}%,
    ' en found' )
  or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%"PE0":"Souriez, vous êtes surveillés !"%,
    ' PE0 found' )
  or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%"selectIdP":"Portail de Fédération des Identités"%,
    ' selectIdP found' )
  or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%"fr":\{%,
    ' fr found' )
  or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%"PE85":"From lemonlap-ng.ini"%,
    ' PE85 found' )
  or print STDERR Dumper( $res->[2]->[0] );
count(9);
50

Yadd's avatar
Yadd committed
51 52 53
# Try yo authenticate
# -------------------
ok(
Yadd's avatar
Yadd committed
54
    $res = $client->_post(
Yadd's avatar
Yadd committed
55 56 57 58 59 60
        '/',
        IO::String->new('user=dwho&password=dwho'),
        length => 23
    ),
    'Auth query'
);
Yadd's avatar
Yadd committed
61 62 63
count(1);
expectOK($res);
my $id = expectCookie($res);
Yadd's avatar
Yadd committed
64 65 66

# Test mismatch pwd
ok(
Yadd's avatar
Yadd committed
67
    $res = $client->_post(
Yadd's avatar
Yadd committed
68 69 70 71 72 73 74 75
        '/',
        IO::String->new('oldpassword=dwho&newpassword=test&confirmpassword=t'),
        cookie => "lemonldap=$id",
        accept => 'application/json',
        length => 51
    ),
    'Password mismatch'
);
Yadd's avatar
Yadd committed
76
expectBadRequest($res);
Yadd's avatar
Yadd committed
77 78 79 80 81
my $json;
ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' )
  or print STDERR "$@\n" . Dumper($res);
ok( $json->{error} == PE_PASSWORD_MISMATCH, 'Response is PE_PASSWORD_MISMATCH' )
  or explain( $json, "error => 34" );
Yadd's avatar
Yadd committed
82
count(3);
Yadd's avatar
Yadd committed
83 84 85

# Test missing old pwd
ok(
Yadd's avatar
Yadd committed
86
    $res = $client->_post(
Yadd's avatar
Yadd committed
87 88 89 90 91 92 93 94
        '/',
        IO::String->new('newpassword=test&confirmpassword=test'),
        cookie => "lemonldap=$id",
        accept => 'application/json',
        length => 37
    ),
    'Missing old password'
);
Yadd's avatar
Yadd committed
95
expectBadRequest($res);
Yadd's avatar
Yadd committed
96 97 98 99 100 101
ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' )
  or print STDERR "$@\n" . Dumper($res);
ok(
    $json->{error} == PE_PP_MUST_SUPPLY_OLD_PASSWORD,
    'Response is PE_PP_MUST_SUPPLY_OLD_PASSWORD'
) or explain( $json, "error => 27" );
Yadd's avatar
Yadd committed
102
count(3);
Yadd's avatar
Yadd committed
103 104 105

# Test bad old pwd
ok(
Yadd's avatar
Yadd committed
106
    $res = $client->_post(
Yadd's avatar
Yadd committed
107 108 109 110 111 112 113 114
        '/',
        IO::String->new('oldpassword=dd&newpassword=test&confirmpassword=test'),
        cookie => "lemonldap=$id",
        accept => 'application/json',
        length => 52
    ),
    'Bad old password'
);
Yadd's avatar
Yadd committed
115
expectBadRequest($res);
Yadd's avatar
Yadd committed
116 117 118 119
ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' )
  or print STDERR "$@\n" . Dumper($res);
ok( $json->{error} == PE_BADOLDPASSWORD, 'Response is PE_BADOLDPASSWORD' )
  or explain( $json, "error => 27" );
Yadd's avatar
Yadd committed
120
count(3);
Yadd's avatar
Yadd committed
121

Yadd's avatar
Yadd committed
122 123
# Test $client->logout
$client->logout($id);
Yadd's avatar
Yadd committed
124 125 126 127 128 129

#print STDERR Dumper($res);

clean_sessions();

done_testing( count() );