Commit 931cdc61 authored by Christophe Maudoux's avatar Christophe Maudoux 🐛

Catch error and Improve parameter tests & Improve unit test (#2049)

parent d4372eae
...@@ -120,9 +120,7 @@ sub getAll { ...@@ -120,9 +120,7 @@ sub getAll {
# keys date, uid, ref and condition. # keys date, uid, ref and condition.
sub getExisting { sub getExisting {
my $self = shift; my $self = shift;
$self->_execute( 'SELECT * FROM ' $self->_execute( 'SELECT * FROM ' . $self->dbiTable . ' ORDER BY date' );
. $self->dbiTable
. ' ORDER BY date' );
my $result; my $result;
while ( my $h = $self->sth->fetchrow_hashref() ) { while ( my $h = $self->sth->fetchrow_hashref() ) {
$result->{"$h->{date}#$h->{uid}#$h->{ref}"} = { $result->{"$h->{date}#$h->{uid}#$h->{ref}"} = {
...@@ -194,7 +192,12 @@ sub purge { ...@@ -194,7 +192,12 @@ sub purge {
# @return true if succeed # @return true if succeed
sub newNotif { sub newNotif {
my ( $self, $date, $uid, $ref, $condition, $xml ) = @_; 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 = my $res =
$condition =~ /.+/ $condition =~ /.+/
? $self->_execute( 'INSERT INTO ' ? $self->_execute( 'INSERT INTO '
...@@ -221,8 +224,14 @@ sub getDone { ...@@ -221,8 +224,14 @@ sub getDone {
while ( my $h = $self->sth->fetchrow_hashref() ) { while ( my $h = $self->sth->fetchrow_hashref() ) {
my @t = split( /\D+/, $h->{date} ); my @t = split( /\D+/, $h->{date} );
$t[1]--; $t[1]--;
my $done = my $done = eval {
timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], $t[0] ); 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}"} = $result->{"$h->{date}#$h->{uid}#$h->{ref}"} =
{ notified => $done, uid => $h->{uid}, ref => $h->{ref}, }; { notified => $done, uid => $h->{uid}, ref => $h->{ref}, };
} }
......
...@@ -7,9 +7,10 @@ package Lemonldap::NG::Common::Notifications::File; ...@@ -7,9 +7,10 @@ package Lemonldap::NG::Common::Notifications::File;
use strict; use strict;
use Mouse; use Mouse;
use Time::Local;
use MIME::Base64; use MIME::Base64;
our $VERSION = '2.0.7'; our $VERSION = '2.0.8';
extends 'Lemonldap::NG::Common::Notifications'; extends 'Lemonldap::NG::Common::Notifications';
...@@ -129,6 +130,13 @@ sub purge { ...@@ -129,6 +130,13 @@ sub purge {
sub newNotif { sub newNotif {
my ( $self, $date, $uid, $ref, $condition, $content ) = @_; my ( $self, $date, $uid, $ref, $condition, $content ) = @_;
my $fns = $self->{fileNameSeparator}; 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; $date =~ s/-//g;
return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ ); return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ );
my $filename = my $filename =
......
...@@ -13,7 +13,7 @@ use MIME::Base64; ...@@ -13,7 +13,7 @@ use MIME::Base64;
use Net::LDAP; use Net::LDAP;
use utf8; use utf8;
our $VERSION = '2.0.7'; our $VERSION = '2.0.8';
extends 'Lemonldap::NG::Common::Notifications'; extends 'Lemonldap::NG::Common::Notifications';
...@@ -187,6 +187,12 @@ sub newNotif { ...@@ -187,6 +187,12 @@ sub newNotif {
my ( $self, $date, $uid, $ref, $condition, $xml ) = @_; my ( $self, $date, $uid, $ref, $condition, $xml ) = @_;
my $fns = $self->conf->{fileNameSeparator}; my $fns = $self->conf->{fileNameSeparator};
$fns ||= '_'; $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; $date =~ s/-//g;
return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ ); return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ );
my $cn = "${date}${fns}${uid}${fns}" . encode_base64( $ref, '' ); my $cn = "${date}${fns}${uid}${fns}" . encode_base64( $ref, '' );
...@@ -230,16 +236,20 @@ sub getDone { ...@@ -230,16 +236,20 @@ sub getDone {
$v = decodeLdapValue($v); $v = decodeLdapValue($v);
$f->{$k} = $v; $f->{$k} = $v;
} }
my @t = split( /\D+/, $f->{done} ); my @t = split( /\D+/, $f->{done} );
my $done = timelocal( $t[5], $t[4], $t[3], $t[2], $t[1], $t[0] ); $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}"} = $result->{"$f->{date}#$f->{uid}#$f->{ref}"} =
{ notified => $done, uid => $f->{uid}, ref => $f->{ref}, }; { notified => $done, uid => $f->{uid}, ref => $f->{ref}, };
} }
# $ldap->unbind() && delete $self->{ldap}; # $ldap->unbind() && delete $self->{ldap};
return $result; return $result;
} }
## @method object private _ldap() ## @method object private _ldap()
......
...@@ -8,7 +8,7 @@ use Test::More; ...@@ -8,7 +8,7 @@ use Test::More;
my $count = 0; my $count = 0;
my $file = 't/notifications.db'; my $file = 't/notifications.db';
my $maintests = 8; my $maintests = 11;
my ( $res, $client ); my ( $res, $client );
eval { unlink $file }; eval { unlink $file };
...@@ -39,7 +39,31 @@ SKIP: { ...@@ -39,7 +39,31 @@ SKIP: {
# Try to create a notification # Try to create a notification
my $notif = 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 = $res =
$client->jsonPostResponse( 'notifications/actives', '', $client->jsonPostResponse( 'notifications/actives', '',
IO::String->new($notif), IO::String->new($notif),
...@@ -70,7 +94,7 @@ SKIP: { ...@@ -70,7 +94,7 @@ SKIP: {
# Delete notification # Delete notification
$res = $res =
$client->_del('notifications/done/dwho_Test_20990503_dwho_VGVzdA==.done'); $client->_del('notifications/done/dwho_Test_20991231_dwho_VGVzdA==.done');
$res = $res =
$client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' ); $client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' );
ok( $res->{result} == 1, 'Result = 1' ); ok( $res->{result} == 1, 'Result = 1' );
......
...@@ -11,11 +11,38 @@ require 't/test-lib.pm'; ...@@ -11,11 +11,38 @@ require 't/test-lib.pm';
# Try to create a notification # Try to create a notification
my $notif = 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 = my $res =
&client->jsonPostResponse( 'notifications/actives', '', &client->jsonPostResponse( 'notifications/actives', '',
IO::String->new($notif), IO::String->new($notif),
'application/json', length($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' ); ok( $res->{result}, 'Result is true' );
count(1); count(1);
...@@ -44,7 +71,7 @@ displayTests('done'); ...@@ -44,7 +71,7 @@ displayTests('done');
# Delete notification # Delete notification
$res = $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)' ); $res = &client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' );
ok( $res->{result} == 1, 'Result = 1' ); ok( $res->{result} == 1, 'Result = 1' );
ok( $res->{count} == 0, 'Count = 0' ); ok( $res->{count} == 0, 'Count = 0' );
......
...@@ -15,6 +15,22 @@ my $json = '{ ...@@ -15,6 +15,22 @@ my $json = '{
"text": "This is a test text" "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 = '{ my $jsonbis = '{
"date": "2016-05-31", "date": "2016-05-31",
"reference": "testref", "reference": "testref",
...@@ -103,6 +119,21 @@ my $client = LLNG::Manager::Test->new( { ...@@ -103,6 +119,21 @@ my $client = LLNG::Manager::Test->new( {
); );
my $res; 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 ) { foreach ( $json, $json2, $jsonall ) {
ok( ok(
$res = $client->_post( $res = $client->_post(
...@@ -155,7 +186,7 @@ ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ); ...@@ -155,7 +186,7 @@ ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' );
ok( scalar @{ $json->{result} } == 3, 'Three notifications found' ) ok( scalar @{ $json->{result} } == 3, 'Three notifications found' )
or print STDERR Dumper($json); or print STDERR Dumper($json);
foreach ( @{$json->{result}} ) { foreach ( @{ $json->{result} } ) {
ok( $_->{reference} =~ /^testref/, "Reference \'$_->{reference}\' found" ) ok( $_->{reference} =~ /^testref/, "Reference \'$_->{reference}\' found" )
or print STDERR Dumper($json); or print STDERR Dumper($json);
ok( $_->{uid} =~ /^(dwho|everyone)$/, "UID \'$_->{uid}\' found" ) ok( $_->{uid} =~ /^(dwho|everyone)$/, "UID \'$_->{uid}\' found" )
...@@ -304,7 +335,8 @@ count(2); ...@@ -304,7 +335,8 @@ count(2);
ok( ok(
$res = $client->_get( $res = $client->_get(
'/notifications/_allExisting_', type => 'application/json', '/notifications/_allExisting_',
type => 'application/json',
), ),
'List all existing notifications' 'List all existing notifications'
); );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment