From 931cdc611da6d512b62b9795fcf3ffde0d3ea94f Mon Sep 17 00:00:00 2001 From: Christophe Maudoux Date: Mon, 23 Dec 2019 21:18:44 +0100 Subject: [PATCH] Catch error and Improve parameter tests & Improve unit test (#2049) --- .../Lemonldap/NG/Common/Notifications/DBI.pm | 21 +++++++---- .../Lemonldap/NG/Common/Notifications/File.pm | 10 +++++- .../Lemonldap/NG/Common/Notifications/LDAP.pm | 20 ++++++++--- lemonldap-ng-manager/t/50-notifications-DBI.t | 30 ++++++++++++++-- lemonldap-ng-manager/t/50-notifications.t | 31 ++++++++++++++-- .../t/40-Notifications-JSON-Server.t | 36 +++++++++++++++++-- 6 files changed, 129 insertions(+), 19 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/DBI.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/DBI.pm index 1e8da623c..22d7c0346 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/DBI.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/DBI.pm @@ -120,9 +120,7 @@ sub getAll { # keys date, uid, ref and condition. sub getExisting { my $self = shift; - $self->_execute( 'SELECT * FROM ' - . $self->dbiTable - . ' ORDER BY date' ); + $self->_execute( 'SELECT * FROM ' . $self->dbiTable . ' ORDER BY date' ); my $result; while ( my $h = $self->sth->fetchrow_hashref() ) { $result->{"$h->{date}#$h->{uid}#$h->{ref}"} = { @@ -194,7 +192,12 @@ sub purge { # @return true if succeed sub newNotif { my ( $self, $date, $uid, $ref, $condition, $xml ) = @_; - + my @t = split( /\D+/, $date ); + $t[1]--; + eval { + timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], $t[0] ); + }; + return ( 0, "Bad date" ) if ($@); my $res = $condition =~ /.+/ ? $self->_execute( 'INSERT INTO ' @@ -221,8 +224,14 @@ sub getDone { while ( my $h = $self->sth->fetchrow_hashref() ) { my @t = split( /\D+/, $h->{date} ); $t[1]--; - my $done = - timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], $t[0] ); + my $done = eval { + timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], + $t[0] ); + }; + if ($@) { + $self->logger->warn("Bad date: $h->{date}"); + return {}; + } $result->{"$h->{date}#$h->{uid}#$h->{ref}"} = { notified => $done, uid => $h->{uid}, ref => $h->{ref}, }; } diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm index 4a5ab1398..259a34b39 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm @@ -7,9 +7,10 @@ package Lemonldap::NG::Common::Notifications::File; use strict; use Mouse; +use Time::Local; use MIME::Base64; -our $VERSION = '2.0.7'; +our $VERSION = '2.0.8'; extends 'Lemonldap::NG::Common::Notifications'; @@ -129,6 +130,13 @@ sub purge { sub newNotif { my ( $self, $date, $uid, $ref, $condition, $content ) = @_; my $fns = $self->{fileNameSeparator}; + $fns ||= '_'; + my @t = split( /\D+/, $date ); + $t[1]--; + eval { + timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], $t[0] ); + }; + return ( 0, "Bad date" ) if ($@); $date =~ s/-//g; return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ ); my $filename = diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/LDAP.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/LDAP.pm index 59efabfff..e25d933eb 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/LDAP.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/LDAP.pm @@ -13,7 +13,7 @@ use MIME::Base64; use Net::LDAP; use utf8; -our $VERSION = '2.0.7'; +our $VERSION = '2.0.8'; extends 'Lemonldap::NG::Common::Notifications'; @@ -187,6 +187,12 @@ sub newNotif { my ( $self, $date, $uid, $ref, $condition, $xml ) = @_; my $fns = $self->conf->{fileNameSeparator}; $fns ||= '_'; + my @t = split( /\D+/, $date ); + $t[1]--; + eval { + timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], $t[0] ); + }; + return ( 0, "Bad date" ) if ($@); $date =~ s/-//g; return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ ); my $cn = "${date}${fns}${uid}${fns}" . encode_base64( $ref, '' ); @@ -230,16 +236,20 @@ sub getDone { $v = decodeLdapValue($v); $f->{$k} = $v; } - my @t = split( /\D+/, $f->{done} ); - my $done = timelocal( $t[5], $t[4], $t[3], $t[2], $t[1], $t[0] ); + my @t = split( /\D+/, $f->{done} ); + $t[1]--; + my $done = + eval { timelocal( $t[5], $t[4], $t[3], $t[2], $t[1], $t[0] ) }; + if ($@) { + $self->logger->warn("Bad date: $f->{done}"); + return {}; + } $result->{"$f->{date}#$f->{uid}#$f->{ref}"} = { notified => $done, uid => $f->{uid}, ref => $f->{ref}, }; - } # $ldap->unbind() && delete $self->{ldap}; return $result; - } ## @method object private _ldap() diff --git a/lemonldap-ng-manager/t/50-notifications-DBI.t b/lemonldap-ng-manager/t/50-notifications-DBI.t index 79192e8a4..69c98698b 100644 --- a/lemonldap-ng-manager/t/50-notifications-DBI.t +++ b/lemonldap-ng-manager/t/50-notifications-DBI.t @@ -8,7 +8,7 @@ use Test::More; my $count = 0; my $file = 't/notifications.db'; -my $maintests = 8; +my $maintests = 11; my ( $res, $client ); eval { unlink $file }; @@ -39,7 +39,31 @@ SKIP: { # Try to create a notification my $notif = -'{"date":"2099-05-03","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; +'{"date":"2099-02-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; + $res = + $client->jsonPostResponse( 'notifications/actives', '', + IO::String->new($notif), + 'application/json', length($notif) ); + ok( $res->{error} =~ /^Notification not created: Bad date/, 'Notification not inserted' ); + + $notif = +'{"date":"2099-13-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; + $res = + $client->jsonPostResponse( 'notifications/actives', '', + IO::String->new($notif), + 'application/json', length($notif) ); + ok( $res->{error} =~ /^Notification not created: Bad date/, 'Notification not inserted' ); + + $notif = +'{"date":"2099-05_12","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; + $res = + $client->jsonPostResponse( 'notifications/actives', '', + IO::String->new($notif), + 'application/json', length($notif) ); + ok( $res->{error} =~ /^Malformed date$/, 'Notification not inserted' ); + + $notif = +'{"date":"2099-12-31","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; $res = $client->jsonPostResponse( 'notifications/actives', '', IO::String->new($notif), @@ -70,7 +94,7 @@ SKIP: { # Delete notification $res = - $client->_del('notifications/done/dwho_Test_20990503_dwho_VGVzdA==.done'); + $client->_del('notifications/done/dwho_Test_20991231_dwho_VGVzdA==.done'); $res = $client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' ); ok( $res->{result} == 1, 'Result = 1' ); diff --git a/lemonldap-ng-manager/t/50-notifications.t b/lemonldap-ng-manager/t/50-notifications.t index 89e896ac6..d482b40ef 100644 --- a/lemonldap-ng-manager/t/50-notifications.t +++ b/lemonldap-ng-manager/t/50-notifications.t @@ -11,11 +11,38 @@ require 't/test-lib.pm'; # Try to create a notification my $notif = -'{"date":"2099-05-03","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; +'{"date":"2099-02-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; my $res = &client->jsonPostResponse( 'notifications/actives', '', IO::String->new($notif), 'application/json', length($notif) ); +ok( $res->{error} =~ /^Notification not created: Bad date/, 'Notification not inserted' ); +count(1); + +$notif = +'{"date":"2099-13-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; +my $res = + &client->jsonPostResponse( 'notifications/actives', '', + IO::String->new($notif), + 'application/json', length($notif) ); +ok( $res->{error} =~ /^Notification not created: Bad date/, 'Notification not inserted' ); +count(1); + +$notif = +'{"date":"2099-05_12","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; +my $res = + &client->jsonPostResponse( 'notifications/actives', '', + IO::String->new($notif), + 'application/json', length($notif) ); +ok( $res->{error} =~ /^Malformed date$/, 'Notification not inserted' ); +count(1); + +$notif = +'{"date":"2099-12-31","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; +$res = + &client->jsonPostResponse( 'notifications/actives', '', + IO::String->new($notif), + 'application/json', length($notif) ); ok( $res->{result}, 'Result is true' ); count(1); @@ -44,7 +71,7 @@ displayTests('done'); # Delete notification $res = - &client->_del('notifications/done/dwho_Test_20990503_dwho_VGVzdA==.done'); + &client->_del('notifications/done/dwho_Test_20991231_dwho_VGVzdA==.done'); $res = &client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' ); ok( $res->{result} == 1, 'Result = 1' ); ok( $res->{count} == 0, 'Count = 0' ); diff --git a/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t b/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t index 6a08646c1..6496ee444 100644 --- a/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t +++ b/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t @@ -15,6 +15,22 @@ my $json = '{ "text": "This is a test text" }'; +my $bad_json = '{ +"date": "2016-13-30 15:35:10", +"reference": "testref", +"uid": "dwho", +"title": "Test title", +"text": "This is a test text" +}'; + +my $bad_json2 = '{ +"date": "2016-13_30 15:35:10", +"reference": "testref", +"uid": "dwho", +"title": "Test title", +"text": "This is a test text" +}'; + my $jsonbis = '{ "date": "2016-05-31", "reference": "testref", @@ -103,6 +119,21 @@ my $client = LLNG::Manager::Test->new( { ); my $res; +foreach ( $bad_json, $bad_json2 ) { + ok( + $res = $client->_post( + '/notifications', IO::String->new($_), + type => 'application/json', + length => length($_) + ), + "POST notification $_" + ); + ok( $res->[2]->[0] =~ /"error"\s*:\s*"Bad date/, + 'Notification not inserted' ) + or print STDERR Dumper( $res->[2]->[0] ); + count(2); +} + foreach ( $json, $json2, $jsonall ) { ok( $res = $client->_post( @@ -155,7 +186,7 @@ ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ); ok( scalar @{ $json->{result} } == 3, 'Three notifications found' ) or print STDERR Dumper($json); -foreach ( @{$json->{result}} ) { +foreach ( @{ $json->{result} } ) { ok( $_->{reference} =~ /^testref/, "Reference \'$_->{reference}\' found" ) or print STDERR Dumper($json); ok( $_->{uid} =~ /^(dwho|everyone)$/, "UID \'$_->{uid}\' found" ) @@ -304,7 +335,8 @@ count(2); ok( $res = $client->_get( - '/notifications/_allExisting_', type => 'application/json', + '/notifications/_allExisting_', + type => 'application/json', ), 'List all existing notifications' ); -- GitLab