From ffa804f6a3a54e86ca18e3ab1e9eb446808a078d Mon Sep 17 00:00:00 2001 From: Alexandre KARIM Date: Thu, 19 Aug 2021 10:41:18 +0200 Subject: [PATCH] Append unit test (#2325) --- doc/sources/admin/newlocationwarning.rst | 2 +- lemonldap-ng-portal/MANIFEST | 1 + .../NG/Portal/Plugins/NewLocationWarning.pm | 17 +++-- .../t/61-NewLocationWarning-Custom.t | 76 +++++++++++++++++++ 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t diff --git a/doc/sources/admin/newlocationwarning.rst b/doc/sources/admin/newlocationwarning.rst index 238776313..9dd7be0b1 100644 --- a/doc/sources/admin/newlocationwarning.rst +++ b/doc/sources/admin/newlocationwarning.rst @@ -31,7 +31,7 @@ Just enable it in the Manager (section ``General Parameters`` > ``Advanced param Following variables are available in: - \* Warning email body => ``$newLocationIP``, ``$newLocationDate`` + \* Warning email body => ``$newLocationIP``, ``$newLocationDate``, ``$newLocationUrl`` .. |image0| image:: /documentation/beta.png :width: 100px \ No newline at end of file diff --git a/lemonldap-ng-portal/MANIFEST b/lemonldap-ng-portal/MANIFEST index a9b7688d2..3b666c9b5 100644 --- a/lemonldap-ng-portal/MANIFEST +++ b/lemonldap-ng-portal/MANIFEST @@ -672,6 +672,7 @@ t/61-CrowdSec-warn.t t/61-CrowdSec.t t/61-ForceAuthn.t t/61-GrantSession.t +t/61-NewLocationWarning-Custom.t t/61-NewLocationWarning.t t/61-Session-ActivityTimeout.t t/61-Session-Timeout.t diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm index 471ce4cd3..c7838b915 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm @@ -59,6 +59,7 @@ sub _sendMail { my $date = strftime( '%F %X', localtime ); my $ipSource = $req->env->{ipAddr}; my $host = $req->env->{HTTP_HOST}; + my $url = $self->conf->{portal}; my $mail = $req->sessionInfo->{ $self->conf->{newLocationWarningMailAttribute} || 'mail' }; @@ -81,6 +82,7 @@ sub _sendMail { # Replace variables in body $body =~ s/\$newLocationIP/$ipSource/ge; $body =~ s/\$newLocationDate/$date/ge; + $body =~ s/\$newLocationUrl/$url/ge; } else { @@ -97,13 +99,16 @@ sub _sendMail { ); $html = 1; } + if ( $mail && $subject && $body ) { + $self->logger->warn("User $mail is signing in from a new location"); - $self->logger->warn("User $mail is signing in from a new location"); - - # Send mail - $self->logger->debug('Unable to send new location warning mail') - unless ( $self->send_mail( $mail, $subject, $body, $html ) ); - + # Send mail + $self->logger->debug('Unable to send new location warning mail') + unless ( $self->send_mail( $mail, $subject, $body, $html ) ); + } + else{ + $self->logger->error('Unable to send new location warning mail: missing parameter(s)'); + } return PE_OK; } diff --git a/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t b/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t new file mode 100644 index 000000000..b610164c1 --- /dev/null +++ b/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t @@ -0,0 +1,76 @@ +use Test::More; +use strict; +use IO::String; + +BEGIN { + eval { + require 't/test-lib.pm'; + require 't/smtp.pm'; + }; +} + +my $res; +my $maintests = 4; + +SKIP: { + eval 'require Email::Sender::Simple;'; + if ($@) { + skip 'Missing dependencies', $maintests; + } + + my $client = LLNG::Manager::Test->new( { + ini => { + logLevel => 'error', + useSafeJail => 1, + authentication => 'Demo', + userDB => 'Same', + passwordDB => 'Demo', + captcha_mail_enabled => 0, + portalMainLogo => 'common/logos/logo_llng_old.png', + newLocationWarning => 1, + loginHistoryEnabled => 1, + newLocationWarningMailSubject => 'Test new location mail', + newLocationWarningMailBody => + 'Test $newLocationIP $newLocationDate $newLocationUrl', + #newLocationWarningMailAttribute => 'email' + } + } + ); + + ## Simple access + ok( $res = $client->_get( '/', accept => 'text/html' ), 'Get Portal', ); + my ( $host, $url, $query ) = + expectForm( $res, '#', undef, 'user', 'password' ); + + ## Authentication #1 with IP #1 (Test 1) + ok( + $res = $client->_post( + '/', + IO::String->new('user=dwho&password=dwho'), + length => 23, + accept => 'text/html', + ), + 'First auth query' + ); + + ## Authentication #1 with IP #2 (Test 2) + ok( + $res = $client->_post( + '/', + IO::String->new('user=dwho&password=dwho'), + length => 23, + accept => 'text/html', + ip => '127.0.0.2', + ), + 'Second auth query' + ); + like( + mail(), +qr#^Test 127\.0\.0\.2 \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} http://auth.example.com/$#, + ' Mail sent (IP, Date & URL found)' + ); +} + +count($maintests); +clean_sessions(); +done_testing( count() );