2017-01-31 23:31:25 +01:00
|
|
|
use Test::More;
|
|
|
|
use strict;
|
|
|
|
use IO::String;
|
2020-02-12 23:05:45 +01:00
|
|
|
use JSON qw(from_json);
|
2017-01-31 23:31:25 +01:00
|
|
|
|
|
|
|
my $res;
|
2020-02-12 23:05:45 +01:00
|
|
|
my $maintests = 17;
|
2017-01-31 23:31:25 +01:00
|
|
|
require 't/test-lib.pm';
|
2019-11-17 22:25:06 +01:00
|
|
|
my $file = tempdb();
|
2017-01-31 23:31:25 +01:00
|
|
|
|
|
|
|
SKIP: {
|
|
|
|
eval { require DBI; require DBD::SQLite; };
|
|
|
|
if ($@) {
|
2018-02-08 21:55:21 +01:00
|
|
|
skip 'DBD::SQLite not found', $maintests;
|
2017-01-31 23:31:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)'
|
|
|
|
);
|
|
|
|
$dbh->prepare(
|
|
|
|
q{INSERT INTO notifications VALUES ('dwho','testref','2016-05-30 00:00:00',?,null,null)}
|
2018-11-26 14:40:21 +01:00
|
|
|
)->execute(
|
2017-01-31 23:31:25 +01:00
|
|
|
'[
|
|
|
|
{
|
|
|
|
"uid": "dwho",
|
|
|
|
"date": "2016-05-30",
|
|
|
|
"reference": "testref",
|
|
|
|
"title": "Test title",
|
|
|
|
"subtitle": "Test subtitle",
|
|
|
|
"text": "This is a test text",
|
2019-11-17 22:25:06 +01:00
|
|
|
"check": "Accept test"
|
2017-01-31 23:31:25 +01:00
|
|
|
}
|
2019-07-18 13:38:10 +02:00
|
|
|
]'
|
|
|
|
);
|
2019-07-26 17:49:19 +02:00
|
|
|
$dbh->prepare(
|
2019-07-18 13:38:10 +02:00
|
|
|
q{INSERT INTO notifications VALUES ('dwho','testref2','2016-05-30 00:00:00',?,null,null)}
|
|
|
|
)->execute(
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"uid": "dwho",
|
2019-07-26 17:49:19 +02:00
|
|
|
"date": "2016-05-29",
|
2019-07-18 13:38:10 +02:00
|
|
|
"reference": "testref2",
|
2019-11-24 23:05:15 +01:00
|
|
|
"condition": "$env->{REMOTE_ADDR} =~ /127.0.0.1/",
|
2019-07-18 13:38:10 +02:00
|
|
|
"title": "Test2 title",
|
|
|
|
"subtitle": "Test2 subtitle",
|
|
|
|
"text": "This is a second test text",
|
2019-11-24 23:05:15 +01:00
|
|
|
"check": ["Accept test","I am sure"]
|
|
|
|
}
|
|
|
|
]'
|
|
|
|
);
|
|
|
|
$dbh->prepare(
|
|
|
|
q{INSERT INTO notifications VALUES ('dwho','testref3','2050-05-30 00:00:00',?,null,null)}
|
|
|
|
)->execute(
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"uid": "dwho",
|
|
|
|
"date": "2050-05-30",
|
|
|
|
"reference": "testref3",
|
|
|
|
"condition": "\'0\'",
|
|
|
|
"title": "Test title",
|
|
|
|
"subtitle": "Test subtitle",
|
|
|
|
"text": "This is a test text",
|
2019-07-18 13:38:10 +02:00
|
|
|
"check": ["Accept test"]
|
|
|
|
}
|
2019-11-17 22:25:06 +01:00
|
|
|
]'
|
|
|
|
);
|
|
|
|
$dbh->prepare(
|
|
|
|
q{INSERT INTO notifications VALUES ('rtyler','testref','2016-05-30 00:00:00',?,null,null)}
|
|
|
|
)->execute(
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"uid": "rtyler",
|
|
|
|
"date": "2016-05-29",
|
|
|
|
"reference": "testref",
|
2019-11-24 23:05:15 +01:00
|
|
|
"condition": "$env->{REMOTE_ADDR} =~ /127.1.1.1/",
|
|
|
|
"title": "Test title",
|
|
|
|
"subtitle": "Test subtitle",
|
|
|
|
"text": "This is a test text",
|
|
|
|
"check": ["Accept test"]
|
|
|
|
}
|
|
|
|
]'
|
|
|
|
);
|
|
|
|
$dbh->prepare(
|
|
|
|
q{INSERT INTO notifications VALUES ('rtyler','testref2','2050-05-30 00:00:00',?,null,null)}
|
|
|
|
)->execute(
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"uid": "rtyler",
|
|
|
|
"date": "2050-05-16",
|
|
|
|
"reference": "testref2",
|
2019-11-17 22:25:06 +01:00
|
|
|
"title": "Test title",
|
|
|
|
"subtitle": "Test subtitle",
|
|
|
|
"text": "This is a test text",
|
|
|
|
"check": ["Accept test"]
|
|
|
|
}
|
2017-01-31 23:31:25 +01:00
|
|
|
]'
|
2018-11-26 14:40:21 +01:00
|
|
|
);
|
2017-01-31 23:31:25 +01:00
|
|
|
|
2019-02-07 09:27:56 +01:00
|
|
|
my $client = LLNG::Manager::Test->new( {
|
2017-01-31 23:31:25 +01:00
|
|
|
ini => {
|
|
|
|
logLevel => 'error',
|
|
|
|
useSafeJail => 1,
|
2017-02-02 06:37:58 +01:00
|
|
|
notification => 1,
|
2017-01-31 23:31:25 +01:00
|
|
|
notificationStorage => 'DBI',
|
|
|
|
notificationStorageOptions => {
|
|
|
|
dbiChain => "dbi:SQLite:dbname=$file",
|
|
|
|
},
|
2020-02-12 23:05:45 +01:00
|
|
|
oldNotifFormat => 0,
|
2020-02-12 23:33:46 +01:00
|
|
|
notificationsExplorer => 1
|
2017-01-31 23:31:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-07-17 11:58:22 +02:00
|
|
|
# Try to authenticate
|
2017-01-31 23:31:25 +01:00
|
|
|
# -------------------
|
|
|
|
ok(
|
|
|
|
$res = $client->_post(
|
|
|
|
'/',
|
|
|
|
IO::String->new(
|
|
|
|
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
|
|
|
|
),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => 64,
|
|
|
|
),
|
|
|
|
'Auth query'
|
|
|
|
);
|
|
|
|
expectOK($res);
|
2019-07-26 17:49:19 +02:00
|
|
|
my $id = expectCookie($res);
|
|
|
|
my @refs = ( $res->[2]->[0] =~
|
2020-08-25 00:24:54 +02:00
|
|
|
/<input type="hidden" name="reference[\dx]+" value="(\w+?)"\/>/gs );
|
2019-07-26 17:49:19 +02:00
|
|
|
ok( @refs == 2, 'Two notification references found' )
|
|
|
|
or print STDERR Dumper( $res->[2]->[0] );
|
2020-02-16 14:35:14 +01:00
|
|
|
ok( $refs[0] eq 'testref2', '1st reference found is "testref2"' )
|
2019-07-26 17:49:19 +02:00
|
|
|
or print STDERR Dumper( $res->[2]->[0] );
|
2020-02-16 14:35:14 +01:00
|
|
|
ok( $refs[1] eq 'testref', '2nd reference found is "testref"' )
|
2019-07-26 17:49:19 +02:00
|
|
|
or print STDERR Dumper( $res->[2]->[0] );
|
2017-02-02 13:29:59 +01:00
|
|
|
expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
|
2017-01-31 23:31:25 +01:00
|
|
|
|
2020-08-28 21:53:19 +02:00
|
|
|
# Verify that cookie is ciphered (session invalid)
|
2017-01-31 23:31:25 +01:00
|
|
|
ok(
|
|
|
|
$res = $client->_get(
|
|
|
|
'/',
|
|
|
|
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
|
|
|
|
cookie => "lemonldap=$id",
|
|
|
|
),
|
|
|
|
'Test cookie received'
|
|
|
|
);
|
|
|
|
expectReject($res);
|
|
|
|
|
|
|
|
# Try to validate notification without accepting it
|
|
|
|
my $str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
|
|
|
|
ok(
|
|
|
|
$res = $client->_post(
|
|
|
|
'/notifback',
|
|
|
|
IO::String->new($str),
|
|
|
|
cookie => "lemonldap=$id",
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($str),
|
|
|
|
),
|
|
|
|
"Don't accept notification"
|
|
|
|
);
|
|
|
|
expectOK($res);
|
|
|
|
|
2019-07-18 13:38:10 +02:00
|
|
|
# Try to validate notifications
|
2017-01-31 23:31:25 +01:00
|
|
|
$str =
|
2019-11-24 23:05:15 +01:00
|
|
|
'reference1x1=testref&check1x1x1=accepted&reference1x2=testref2&check1x2x1=accepted&check1x2x2=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
|
2017-01-31 23:31:25 +01:00
|
|
|
ok(
|
|
|
|
$res = $client->_post(
|
|
|
|
'/notifback',
|
|
|
|
IO::String->new($str),
|
|
|
|
cookie => "lemonldap=$id",
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($str),
|
|
|
|
),
|
2019-07-18 13:38:10 +02:00
|
|
|
"Accept notifications"
|
2017-01-31 23:31:25 +01:00
|
|
|
);
|
|
|
|
expectRedirection( $res, 'http://test1.example.com/' );
|
2019-08-07 22:29:12 +02:00
|
|
|
my $cookies = getCookies($res);
|
2019-08-06 22:25:02 +02:00
|
|
|
ok(
|
2019-08-07 22:29:12 +02:00
|
|
|
!defined( $cookies->{lemonldappdata} ),
|
|
|
|
" Make sure no pdata is returned"
|
|
|
|
);
|
2020-02-12 23:05:45 +01:00
|
|
|
$id = expectCookie($res);
|
2019-08-06 22:25:02 +02:00
|
|
|
|
2017-01-31 23:31:25 +01:00
|
|
|
# Verify that notification was tagged as 'done'
|
|
|
|
my $sth =
|
|
|
|
$dbh->prepare('SELECT * FROM notifications WHERE done IS NOT NULL');
|
|
|
|
$sth->execute;
|
|
|
|
my $i = 0;
|
|
|
|
while ( $sth->fetchrow_hashref ) { $i++ }
|
2019-07-18 13:38:10 +02:00
|
|
|
ok( $i == 2, 'Notification was deleted' );
|
2017-01-31 23:31:25 +01:00
|
|
|
|
2020-02-12 23:05:45 +01:00
|
|
|
# GET notifications explorer
|
|
|
|
ok(
|
|
|
|
$res = $client->_get(
|
|
|
|
'/mynotifications', cookie => "lemonldap=$id",
|
|
|
|
),
|
|
|
|
'Notifications explorer query'
|
|
|
|
);
|
|
|
|
|
|
|
|
my $json;
|
|
|
|
ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' )
|
|
|
|
or print STDERR "$@\n" . Dumper($res);
|
|
|
|
ok( $json->{result} == 2, ' Result is 2' )
|
|
|
|
or explain( $json, "result => 2" );
|
|
|
|
ok( $json->{MSG} == 'myNotifications', ' MSG is myNotifications' )
|
|
|
|
or explain( $json, "result => 2" );
|
|
|
|
ok( $json->{NOTIFICATIONS}->[0]->{reference} =~ /testref2?/,
|
|
|
|
' Notification 1 found' )
|
|
|
|
or explain( $json, "Notification 1" );
|
|
|
|
ok( $json->{NOTIFICATIONS}->[0]->{reference} =~ /testref2?/,
|
|
|
|
' Notification 2 found' )
|
|
|
|
or explain( $json, "Notification 2" );
|
|
|
|
ok( $json->{NOTIFICATIONS}->[0]->{epoch} =~ /\d{10}/, ' epoch found' )
|
|
|
|
or explain( $json, "Epoch found" );
|
|
|
|
|
|
|
|
$client->logout($id);
|
|
|
|
|
2019-11-17 22:25:06 +01:00
|
|
|
# Try to authenticate
|
|
|
|
# -------------------
|
|
|
|
ok(
|
|
|
|
$res = $client->_post(
|
|
|
|
'/',
|
|
|
|
IO::String->new(
|
|
|
|
'user=rtyler&password=rtyler&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
|
|
|
|
),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => 68,
|
|
|
|
),
|
|
|
|
'Auth query'
|
|
|
|
);
|
|
|
|
expectRedirection( $res, 'http://test1.example.com/' );
|
|
|
|
$id = expectCookie($res);
|
|
|
|
$client->logout($id);
|
|
|
|
|
2017-01-31 23:31:25 +01:00
|
|
|
clean_sessions();
|
|
|
|
eval { unlink $file };
|
|
|
|
}
|
|
|
|
|
2018-02-08 21:55:21 +01:00
|
|
|
count($maintests);
|
2017-01-31 23:31:25 +01:00
|
|
|
done_testing( count() );
|
|
|
|
|