2019-02-06 07:16:21 +01:00
|
|
|
# Test notifications explorer API
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Data::Dumper;
|
|
|
|
use IO::String;
|
|
|
|
use JSON qw(from_json);
|
|
|
|
use Test::More;
|
|
|
|
|
|
|
|
my $count = 0;
|
|
|
|
my $file = 't/notifications.db';
|
2019-12-23 21:18:44 +01:00
|
|
|
my $maintests = 11;
|
2019-02-06 07:16:21 +01:00
|
|
|
my ( $res, $client );
|
|
|
|
eval { unlink $file };
|
|
|
|
|
|
|
|
sub count {
|
|
|
|
my $c = shift;
|
|
|
|
$count += $c if ($c);
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
SKIP: {
|
|
|
|
eval { require DBI; require DBD::SQLite; };
|
|
|
|
if ($@) {
|
|
|
|
skip 'DBD::SQLite not found', $maintests;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$file");
|
|
|
|
$dbh->do(
|
|
|
|
'CREATE TABLE notifications (uid text,ref text,date datetime,xml text,cond text,done datetime)'
|
|
|
|
);
|
|
|
|
|
|
|
|
use_ok('Lemonldap::NG::Manager::Cli::Lib');
|
|
|
|
ok(
|
|
|
|
$client = Lemonldap::NG::Manager::Cli::Lib->new(
|
|
|
|
iniFile => 't/lemonldap-ng-dbi.ini'
|
|
|
|
),
|
|
|
|
'Client object'
|
|
|
|
);
|
|
|
|
|
|
|
|
# Try to create a notification
|
|
|
|
my $notif =
|
2019-12-23 21:18:44 +01:00
|
|
|
'{"date":"2099-02-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}';
|
|
|
|
$res =
|
|
|
|
$client->jsonPostResponse( 'notifications/actives', '',
|
|
|
|
IO::String->new($notif),
|
|
|
|
'application/json', length($notif) );
|
2020-02-20 23:34:02 +01:00
|
|
|
ok( $res->{error} =~ /^Notification not created: Bad date/,
|
|
|
|
'Notification not inserted' );
|
2019-12-23 21:18:44 +01:00
|
|
|
|
|
|
|
$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) );
|
2020-02-20 23:34:02 +01:00
|
|
|
ok( $res->{error} =~ /^Notification not created: Bad date/,
|
|
|
|
'Notification not inserted' );
|
2019-12-23 21:18:44 +01:00
|
|
|
|
|
|
|
$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\"}"}';
|
2019-02-06 07:16:21 +01:00
|
|
|
$res =
|
|
|
|
$client->jsonPostResponse( 'notifications/actives', '',
|
|
|
|
IO::String->new($notif),
|
|
|
|
'application/json', length($notif) );
|
|
|
|
|
|
|
|
ok( $res->{result}, 'Result is true' );
|
|
|
|
|
|
|
|
# Test "actives" notification display
|
|
|
|
displayTests('actives');
|
|
|
|
|
|
|
|
# Mark notification as done
|
|
|
|
$notif = '{"done":1}';
|
|
|
|
$res = $client->jsonPutResponse(
|
|
|
|
'notifications/actives/dwho_Test',
|
|
|
|
'', IO::String->new($notif),
|
|
|
|
'application/json', length($notif)
|
|
|
|
);
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
|
|
|
|
# Test that notification is not active now
|
|
|
|
$res =
|
|
|
|
$client->jsonResponse( 'notifications/actives', 'groupBy=substr(uid,1)' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 0, 'Count = 0' );
|
|
|
|
|
|
|
|
# Test "done" notifications display
|
|
|
|
displayTests('done');
|
|
|
|
|
|
|
|
# Delete notification
|
|
|
|
$res =
|
2019-12-23 21:18:44 +01:00
|
|
|
$client->_del('notifications/done/dwho_Test_20991231_dwho_VGVzdA==.done');
|
2019-02-06 07:16:21 +01:00
|
|
|
$res =
|
|
|
|
$client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
2020-02-20 23:34:02 +01:00
|
|
|
ok( $res->{count} == 0, 'Count = 0' ) or diag Dumper($res);
|
2019-02-06 07:16:21 +01:00
|
|
|
|
|
|
|
#print STDERR Dumper($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
eval { unlink $file };
|
|
|
|
count($maintests);
|
|
|
|
done_testing( count() );
|
|
|
|
|
|
|
|
sub displayTests {
|
|
|
|
my $type = shift;
|
|
|
|
$res =
|
|
|
|
$client->jsonResponse( "notifications/$type", 'groupBy=substr(uid,1)' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 1, 'Count = 1' );
|
|
|
|
ok( $res->{values}->[0]->{value} eq 'd', 'Value is "d"' );
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
$res = $client->jsonResponse( "notifications/$type", 'groupBy=uid' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 1, 'Count = 1' );
|
|
|
|
ok( $res->{values}->[0]->{value} eq 'dwho', 'Value is "dwho"' );
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
$res = $client->jsonResponse( "notifications/$type", 'uid=d*&groupBy=uid' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 1, 'Count = 1' );
|
|
|
|
ok( $res->{values}->[0]->{value} eq 'dwho', 'Value is "dwho"' );
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
$res = $client->jsonResponse( "notifications/$type", 'uid=d*' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 1, 'Count = 1' );
|
|
|
|
ok( $res->{values}->[0]->{uid} eq 'dwho', 'Value is "dwho"' );
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
$res = $client->jsonResponse( "notifications/$type", 'uid=dwho' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 1, 'Count = 1' );
|
|
|
|
ok( $res->{values}->[0]->{uid} eq 'dwho', 'Value is "dwho"' );
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
if ( $type eq 'actives' ) {
|
|
|
|
$res = $client->jsonResponse( "notifications/$type/dwho_Test", '' );
|
|
|
|
ok( $res->{result} == 1, 'Result = 1' );
|
|
|
|
ok( $res->{count} == 1, 'Count = 1' );
|
|
|
|
ok( eval { from_json( $res->{notifications}->[0] ) },
|
|
|
|
'Response is JSON' )
|
|
|
|
or print STDERR "Expect JSON, found:\n$res->{notifications}->[0]\n";
|
|
|
|
count(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove notifications directory
|
|
|
|
`rm -rf t/notifications`;
|