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 {
# 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}, };
}
......
......@@ -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 =
......
......@@ -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()
......
......@@ -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' );
......
......@@ -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' );
......
......@@ -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'
);
......
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