diff --git a/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t b/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t index cb9a96c33..95f55303e 100644 --- a/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t +++ b/lemonldap-ng-portal/t/61-NewLocationWarning-Custom.t @@ -4,7 +4,7 @@ use IO::String; use POSIX qw(locale_h); use locale; -setlocale(LC_TIME, "C"); +setlocale( LC_TIME, "C" ); BEGIN { eval { @@ -34,10 +34,7 @@ SKIP: { newLocationWarning => 1, loginHistoryEnabled => 1, newLocationWarningMailSubject => 'Test new location mail', - newLocationWarningMailBody => -'Test $newLocationIP $newLocationDate $newLocationHost $newLocationUserAgent', - - #newLocationWarningMailAttribute => 'email' + newLocationWarningMailBody => 'Test $location $date $ua', } } ); @@ -72,8 +69,8 @@ SKIP: { like( subject(), qr#Test new location mail#, ' Subject found' ); like( mail(), -qr#^Test 127\.0\.0\.2 \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} auth\.example\.com Mozilla/5\.0 \(VAX-4000; rv:36\.0\) Gecko/20350101 Firefox$#, - ' Mail sent (IP, Date, Host and UA found)' +qr#^Test 127\.0\.0\.2 \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Mozilla/5\.0 \(VAX-4000; rv:36\.0\) Gecko/20350101 Firefox$#, + ' Mail sent (IP, Date and UA found)' ); } diff --git a/lemonldap-ng-portal/t/61-NewLocationWarning.t b/lemonldap-ng-portal/t/61-NewLocationWarning.t index 106767600..7f9fdb0a5 100644 --- a/lemonldap-ng-portal/t/61-NewLocationWarning.t +++ b/lemonldap-ng-portal/t/61-NewLocationWarning.t @@ -4,7 +4,7 @@ use IO::String; use POSIX qw(locale_h); use locale; -setlocale(LC_TIME, "C"); +setlocale( LC_TIME, "C" ); BEGIN { eval { @@ -14,7 +14,7 @@ BEGIN { } my $res; -my $maintests = 16; +my $maintests = 20; SKIP: { eval 'require Email::Sender::Simple;'; @@ -42,7 +42,8 @@ SKIP: { my ( $host, $url, $query ) = expectForm( $res, '#', undef, 'user', 'password' ); - ## Authentication #1 with IP #1 (Test 1) + ## Authentication #1 with IP #1 + clear_mail(); ok( $res = $client->_post( '/', @@ -54,8 +55,10 @@ SKIP: { ); my $id = expectCookie($res); $client->logout($id); + ok( !mail(), "First time seeing a new IP, no mail sent" ); - ## Authentication #2 with IP #1 (Test 2) + ## Authentication #2 with IP #1 + clear_mail(); ok( $res = $client->_post( '/', @@ -68,8 +71,10 @@ SKIP: { $id = expectCookie($res); expectRedirection( $res, 'http://auth.example.com/' ); $client->logout($id); + ok( !mail(), "Second time seeing a new IP, no mail sent" ); - ## Authentication #3 with IP #2 (Test 3) + ## Authentication #3 with IP #2 + clear_mail(); ok( $res = $client->_post( '/', @@ -86,35 +91,39 @@ SKIP: { like( mail(), qr#

Your account was signed in to from a new location\.


-#, ' Mail sent (Good password)' +#, 'First login on a new IP, email sent' ); - ## Authentication #4 with IP #1 wrong password (Test 4) + ## Authentication #1 with IP #3 wrong password + clear_mail(); ok( $res = $client->_post( '/', IO::String->new('user=dwho&password=ohwd'), length => 23, accept => 'text/html', + ip => "127.0.0.3", ), 'Fourth auth query' ); ok( $res->[2]->[0] =~ /<\/span>/, ' Bad credential' ) or print STDERR Dumper( $res->[2]->[0] ); + ok( !mail(), "Failed login with a new IP, no email sent" ); - ## Authentication #5 with IP #2 (Test 5) + ## Authentication #2 with IP #3 + clear_mail(); ok( $res = $client->_post( '/', - IO::String->new('user=dwho&password=ohwd'), + IO::String->new('user=dwho&password=dwho'), length => 23, accept => 'text/html', ip => '127.0.0.3', ), 'Fifth auth query' ); - ok( $res->[2]->[0] =~ /<\/span>/, ' Bad credential' ) - or print STDERR Dumper( $res->[2]->[0] ); + $id = expectCookie($res); + expectRedirection( $res, 'http://auth.example.com/' ); like( subject(), qr#\[LemonLDAP::NG\] Sign-in from a new location#, @@ -127,8 +136,8 @@ qr#

Your account was signed in to from a new location\.


Host
auth.example.com#, - ' Host found in mail body' + qr#Location 127.0.0.3#, + ' Location found in mail body' ); like( mail(), @@ -141,19 +150,36 @@ qr#UA Mozilla/5\.0 \(VAX-4000; rv:36\.0\) Gecko/20350101 Firefox ' UserAgent found in mail body' ); - ## Authentication #5 with IP #2 (Test 5) + ## Authentication #3 with IP #3 + clear_mail(); ok( $res = $client->_post( '/', - IO::String->new('user=dwho&password=ohwd'), + IO::String->new('user=dwho&password=dwho'), length => 23, accept => 'text/html', - ip => '127.0.0.1', + ip => '127.0.0.3', ), 'Fifth auth query' ); - ok( $res->[2]->[0] =~ /<\/span>/, ' Bad credential' ) - or print STDERR Dumper( $res->[2]->[0] ); + $id = expectCookie($res); + expectRedirection( $res, 'http://auth.example.com/' ); + ok( !mail(), "Login on newly learned address, no email" ); + + ## Authentication #3 with IP #1 + clear_mail(); + ok( + $res = $client->_post( + '/', + IO::String->new('user=dwho&password=dwho'), + length => 23, + accept => 'text/html', + ), + 'Fifth auth query' + ); + $id = expectCookie($res); + expectRedirection( $res, 'http://auth.example.com/' ); + ok( !mail(), "Login on previously learned address, no email" ); } count($maintests); diff --git a/lemonldap-ng-portal/t/smtp.pm b/lemonldap-ng-portal/t/smtp.pm index c3448b603..94cc53543 100644 --- a/lemonldap-ng-portal/t/smtp.pm +++ b/lemonldap-ng-portal/t/smtp.pm @@ -1,17 +1,23 @@ package main; -my ($mail, $mail_envelope, $mail_subject); +my ( $mail, $mail_envelope, $mail_subject ); sub mail { return $mail; } +sub clear_mail { + $mail = undef; + $mail_envelope = undef; + $mail_subject = undef; +} + sub envelope { return $mail_envelope; } sub subject { - my $subject = ($mail_subject =~ /=\?utf-8\?B\?(.+?)\?=/)[0]; + my $subject = ( $mail_subject =~ /=\?utf-8\?B\?(.+?)\?=/ )[0]; return decode_base64($subject); }