Add test for Password/Demo (#595)
This commit is contained in:
parent
72d94f0726
commit
bb3a2e37bd
|
@ -125,7 +125,7 @@ sub do {
|
||||||
|
|
||||||
# TODO: updateStatus
|
# TODO: updateStatus
|
||||||
if ( !$self->conf->{noAjaxHook} and $req->wantJSON ) {
|
if ( !$self->conf->{noAjaxHook} and $req->wantJSON ) {
|
||||||
if ( $err > 0 ) {
|
if ( $err > 0 and !%{ $req->sessionInfo } ) {
|
||||||
return [
|
return [
|
||||||
401,
|
401,
|
||||||
[
|
[
|
||||||
|
@ -135,9 +135,21 @@ sub do {
|
||||||
[]
|
[]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
elsif ( $err > 0 ) {
|
||||||
|
return $self->sendJSONresponse(
|
||||||
|
$req,
|
||||||
|
{ result => 0, error => $err },
|
||||||
|
code => 400
|
||||||
|
);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return $self->sendJSONresponse( $req,
|
return $self->sendJSONresponse(
|
||||||
{ result => 1, message => 'Authenticated' } );
|
$req,
|
||||||
|
{
|
||||||
|
result => 1,
|
||||||
|
code => $err
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -5,6 +5,7 @@ use strict;
|
||||||
use Mouse;
|
use Mouse;
|
||||||
use Lemonldap::NG::Portal::Main::Constants qw(
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
||||||
PE_OK
|
PE_OK
|
||||||
|
PE_BADOLDPASSWORD
|
||||||
PE_PASSWORD_OK
|
PE_PASSWORD_OK
|
||||||
PE_PASSWORD_MISMATCH
|
PE_PASSWORD_MISMATCH
|
||||||
PE_PP_MUST_SUPPLY_OLD_PASSWORD
|
PE_PP_MUST_SUPPLY_OLD_PASSWORD
|
||||||
|
@ -28,7 +29,7 @@ sub _modifyPassword {
|
||||||
|
|
||||||
# TODO: verify oldpassword
|
# TODO: verify oldpassword
|
||||||
unless ( $req->datas->{oldpassword} = $req->param('oldpassword') ) {
|
unless ( $req->datas->{oldpassword} = $req->param('oldpassword') ) {
|
||||||
$self->lmLog( "Portal require old password", 'error' );
|
$self->lmLog( "Portal require old password", 'warn' );
|
||||||
return PE_PP_MUST_SUPPLY_OLD_PASSWORD;
|
return PE_PP_MUST_SUPPLY_OLD_PASSWORD;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +39,8 @@ sub _modifyPassword {
|
||||||
$req->datas->{newpassword} eq $req->param('confirmpassword') );
|
$req->datas->{newpassword} eq $req->param('confirmpassword') );
|
||||||
|
|
||||||
# Verify old password
|
# Verify old password
|
||||||
|
return PE_BADOLDPASSWORD
|
||||||
|
unless ( $self->confirm( $req, $req->datas->{newpassword} ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
# Call password package
|
# Call password package
|
||||||
|
|
|
@ -8,15 +8,13 @@ extends 'Lemonldap::NG::Portal::Password::Base';
|
||||||
|
|
||||||
sub init {
|
sub init {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
if ( $self->p->get_module('auth') eq 'Demo' ) {
|
if ( $self->p->getModule(undef,'auth') eq 'Demo' ) {
|
||||||
return PE_OK;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$self->lmLog( "Use PasswordDBDemo only with AuthDemo", 'error' );
|
$self->lmLog( "Use PasswordDBDemo only with AuthDemo", 'error' );
|
||||||
return PE_ERROR;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PE_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub confirm {
|
sub confirm {
|
||||||
|
|
103
lemonldap-ng-portal/t/40-Password-Demo.t
Normal file
103
lemonldap-ng-portal/t/40-Password-Demo.t
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
init(
|
||||||
|
{
|
||||||
|
logLevel => 'error',
|
||||||
|
passwordDB => 'Demo',
|
||||||
|
portalRequireOldPassword => 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
# Try yo authenticate
|
||||||
|
# -------------------
|
||||||
|
ok(
|
||||||
|
$res = &client->_post(
|
||||||
|
'/',
|
||||||
|
IO::String->new('user=dwho&password=dwho'),
|
||||||
|
length => 23
|
||||||
|
),
|
||||||
|
'Auth query'
|
||||||
|
);
|
||||||
|
ok( $res->[0] == 200, 'Response is 200' ) or explain( $res->[0], 200 );
|
||||||
|
my $cookies = getCookies($res);
|
||||||
|
my $id;
|
||||||
|
ok( $id = $cookies->{lemonldap}, 'Get cookie' )
|
||||||
|
or explain( $res, 'Set-Cookie: something' );
|
||||||
|
count(3);
|
||||||
|
|
||||||
|
# Test mismatch pwd
|
||||||
|
ok(
|
||||||
|
$res = &client->_post(
|
||||||
|
'/',
|
||||||
|
IO::String->new('oldpassword=dwho&newpassword=test&confirmpassword=t'),
|
||||||
|
cookie => "lemonldap=$id",
|
||||||
|
accept => 'application/json',
|
||||||
|
length => 51
|
||||||
|
),
|
||||||
|
'Password mismatch'
|
||||||
|
);
|
||||||
|
ok( $res->[0] == 400, 'Response is 400' ) or explain( $res->[0], 400 );
|
||||||
|
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" );
|
||||||
|
count(3);
|
||||||
|
|
||||||
|
# Test missing old pwd
|
||||||
|
ok(
|
||||||
|
$res = &client->_post(
|
||||||
|
'/',
|
||||||
|
IO::String->new('newpassword=test&confirmpassword=test'),
|
||||||
|
cookie => "lemonldap=$id",
|
||||||
|
accept => 'application/json',
|
||||||
|
length => 37
|
||||||
|
),
|
||||||
|
'Missing old password'
|
||||||
|
);
|
||||||
|
ok( $res->[0] == 400, 'Response is 400' ) or explain( $res->[0], 400 );
|
||||||
|
my $json;
|
||||||
|
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" );
|
||||||
|
count(3);
|
||||||
|
|
||||||
|
# Test bad old pwd
|
||||||
|
ok(
|
||||||
|
$res = &client->_post(
|
||||||
|
'/',
|
||||||
|
IO::String->new('oldpassword=dd&newpassword=test&confirmpassword=test'),
|
||||||
|
cookie => "lemonldap=$id",
|
||||||
|
accept => 'application/json',
|
||||||
|
length => 52
|
||||||
|
),
|
||||||
|
'Bad old password'
|
||||||
|
);
|
||||||
|
ok( $res->[0] == 400, 'Response is 400' ) or explain( $res->[0], 400 );
|
||||||
|
my $json;
|
||||||
|
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" );
|
||||||
|
count(3);
|
||||||
|
|
||||||
|
# Test logout
|
||||||
|
logout($id);
|
||||||
|
|
||||||
|
#print STDERR Dumper($res);
|
||||||
|
|
||||||
|
clean_sessions();
|
||||||
|
|
||||||
|
done_testing( count() );
|
Loading…
Reference in New Issue
Block a user