From b54c95ccd2ca3380217d9b1b9e1696aed8ced35e Mon Sep 17 00:00:00 2001 From: Christophe Maudoux Date: Wed, 21 Apr 2021 22:14:47 +0200 Subject: [PATCH] Improve unit tests & Fix regex (#2509) --- .../lib/Lemonldap/NG/Portal/Password/Base.pm | 2 +- .../t/02-Password-Demo-Local-Ppolicy.t | 4 +- .../02-Password-Demo-Local-SpeChars-Ppolicy.t | 96 +++++++++++++++++++ .../t/02-Password-Demo-Local-noPpolicy.t | 8 +- 4 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 lemonldap-ng-portal/t/02-Password-Demo-Local-SpeChars-Ppolicy.t diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Password/Base.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Password/Base.pm index 928329c67..e7b7f752e 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Password/Base.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Password/Base.pm @@ -176,7 +176,7 @@ sub checkPasswordQuality { ## Min special characters # Just number of special characters must be checked if ( $self->conf->{passwordPolicyMinSpeChar} && $speChars eq '__ALL__' ) { - my $spe = $password =~ s/\w//g; + my $spe = $password =~ s/\W//g; if ( $spe < $self->conf->{passwordPolicyMinSpeChar} ) { $self->logger->error("Password has not enough special characters"); return PE_PP_INSUFFICIENT_PASSWORD_QUALITY; diff --git a/lemonldap-ng-portal/t/02-Password-Demo-Local-Ppolicy.t b/lemonldap-ng-portal/t/02-Password-Demo-Local-Ppolicy.t index b67990bed..d0e4961ab 100644 --- a/lemonldap-ng-portal/t/02-Password-Demo-Local-Ppolicy.t +++ b/lemonldap-ng-portal/t/02-Password-Demo-Local-Ppolicy.t @@ -9,7 +9,7 @@ use Lemonldap::NG::Portal::Main::Constants qw( require 't/test-lib.pm'; -my $res; +my ($res, $json); my $client = LLNG::Manager::Test->new( { ini => { @@ -56,7 +56,7 @@ ok( 'Password min size not respected' ); expectBadRequest($res); -my $json; + ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) or print STDERR "$@\n" . Dumper($res); ok( diff --git a/lemonldap-ng-portal/t/02-Password-Demo-Local-SpeChars-Ppolicy.t b/lemonldap-ng-portal/t/02-Password-Demo-Local-SpeChars-Ppolicy.t new file mode 100644 index 000000000..f43f4b070 --- /dev/null +++ b/lemonldap-ng-portal/t/02-Password-Demo-Local-SpeChars-Ppolicy.t @@ -0,0 +1,96 @@ +use Test::More; +use strict; +use IO::String; +use JSON; +use Lemonldap::NG::Portal::Main::Constants + 'PE_PP_INSUFFICIENT_PASSWORD_QUALITY'; + +require 't/test-lib.pm'; + +my ( $res, $json ); + +my $client = LLNG::Manager::Test->new( { + ini => { + logLevel => 'error', + passwordDB => 'Demo', + portalRequireOldPassword => 1, + passwordPolicyMinSize => 0, + passwordPolicyMinLower => 0, + passwordPolicyMinUpper => 0, + passwordPolicyMinDigit => 0, + passwordPolicyMinSpeChar => 2, + passwordPolicySpecialChar => '__ALL__', + portalDisplayPasswordPolicy => 1 + } + } +); + +# Try to authenticate +# ------------------- +ok( + $res = $client->_post( + '/', + IO::String->new('user=dwho&password=dwho'), + length => 23 + ), + 'Auth query' +); +count(1); +expectOK($res); +my $id = expectCookie($res); + +ok( + $res = + $client->_get( '/', cookie => "lemonldap=$id", accept => 'text/html' ), + 'Get Menu' +); +ok( $res->[2]->[0] =~ m%[2]->[0] ); +ok( + $res->[2]->[0] =~ +m%Minimal special characters: 2%, + ' passwordPolicyMinSpeChar' +) or print STDERR Dumper( $res->[2]->[0] ); +count(3); + +my $query = 'oldpassword=dwho&newpassword=@test&confirmpassword=@test'; +ok( + $res = $client->_post( + '/', + IO::String->new($query), + cookie => "lemonldap=$id", + accept => 'application/json', + length => length($query) + ), + 'Password min special char policy not respected' +); +expectBadRequest($res); +ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); +ok( + $json->{error} == PE_PP_INSUFFICIENT_PASSWORD_QUALITY, + 'Response is PE_PP_INSUFFICIENT_PASSWORD_QUALITY' +) or explain( $json, "error => 28" ); +count(3); + +$query = 'oldpassword=dwho&newpassword=@%&confirmpassword=@%'; +ok( + $res = $client->_post( + '/', + IO::String->new($query), + cookie => "lemonldap=$id", + accept => 'application/json', + length => length($query) + ), + 'Password min special char respected' +); +expectOK($res); +count(1); + +# Test $client->logout +$client->logout($id); + +clean_sessions(); + +done_testing( count() ); diff --git a/lemonldap-ng-portal/t/02-Password-Demo-Local-noPpolicy.t b/lemonldap-ng-portal/t/02-Password-Demo-Local-noPpolicy.t index b8a04281e..b6e82eb44 100644 --- a/lemonldap-ng-portal/t/02-Password-Demo-Local-noPpolicy.t +++ b/lemonldap-ng-portal/t/02-Password-Demo-Local-noPpolicy.t @@ -19,8 +19,8 @@ my $client = LLNG::Manager::Test->new( { passwordPolicyMinLower => 0, passwordPolicyMinUpper => 0, passwordPolicyMinDigit => 0, - passwordPolicyMinSpeChar => 2, - passwordPolicySpecialChar => '', + passwordPolicyMinSpeChar => 0, + passwordPolicySpecialChar => '__ALL__', portalDisplayPasswordPolicy => 1 } } @@ -48,8 +48,8 @@ ok( ok( $res->[2]->[0] =~ m%[2]->[0] ); -ok( $res->[2]->[0] =~ m%Minimal special characters: 2%, - ' passwordPolicyMinSpeChar' ) +ok( $res->[2]->[0] =~ m%%, + ' passwordPolicyNone' ) or print STDERR Dumper( $res->[2]->[0] ); count(3);