lemonldap-ng/lemonldap-ng-portal/t/40-Notifications-JSON-DBI.t

152 lines
4.1 KiB
Perl
Raw Normal View History

2017-01-31 23:31:25 +01:00
use Test::More;
use strict;
use IO::String;
my $res;
2019-08-07 22:29:12 +02:00
my $maintests = 9;
2017-01-31 23:31:25 +01:00
require 't/test-lib.pm';
2019-08-29 10:04:06 +02: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",
"check": ["Accept test"]
}
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",
"title": "Test2 title",
"subtitle": "Test2 subtitle",
"text": "This is a second 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",
},
oldNotifFormat => 0,
}
}
);
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] =~
/<input type="hidden" name="reference[\dx]+" value="(\w+?)">/gs );
ok( @refs == 2, 'Two notification references found' )
or print STDERR Dumper( $res->[2]->[0] );
ok( @refs[0] eq 'testref2', '1st reference found is "testref2"' )
or print STDERR Dumper( $res->[2]->[0] );
ok( @refs[1] eq 'testref', '2nd reference found is "testref"' )
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
# Verify that cookie is ciphered (session unvalid)
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-07-18 13:38:10 +02:00
'reference1x1=testref&check1x1x1=accepted&reference1x2=testref2&check1x2x1=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);
ok(
2019-08-07 22:29:12 +02:00
!defined( $cookies->{lemonldappdata} ),
" Make sure no pdata is returned"
);
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
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() );