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

215 lines
6.7 KiB
Perl
Raw Normal View History

2016-12-23 11:02:21 +01:00
use Test::More;
2016-06-02 23:20:36 +02:00
use strict;
use IO::String;
use JSON qw(from_json);
2016-06-02 23:20:36 +02:00
my $res;
my $maintests = 16;
2019-08-02 23:09:51 +02:00
2016-06-07 23:04:24 +02:00
require 't/test-lib.pm';
2019-08-29 10:04:06 +02:00
my $file = tempdb();
2016-06-02 23:20:36 +02:00
SKIP: {
2017-03-07 07:30:57 +01:00
eval {
require DBI;
require DBD::SQLite;
require XML::LibXML;
require XML::LibXSLT;
};
2016-06-02 23:20:36 +02:00
if ($@) {
2018-02-08 21:55:21 +01:00
skip 'DBD::SQLite or XML::Lib* not found', $maintests;
2016-06-02 23:20:36 +02:00
}
2016-06-03 06:30:34 +02:00
2016-06-02 23:20:36 +02: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->do(
qq{INSERT INTO notifications VALUES ('dwho','testref','2016-05-30 00:00:00','<?xml version="1.0" encoding="UTF-8"?>
<root><notification uid="dwho" date="2016-05-30" reference="testref">
<title>Test title</title>
<subtitle>Test subtitle</subtitle>
<text>This is a test text</text>
<check>Accept test</check>
<check>I am sure</check>
2019-07-18 13:38:10 +02:00
</notification></root>',null,null)}
);
$dbh->do(
qq{INSERT INTO notifications VALUES ('dwho','testref2','2016-05-30 00:00:00','<?xml version="1.0" encoding="UTF-8"?>
<root><notification uid="dwho" date="2016-05-30" reference="testref2" condition="\$env->{REMOTE_ADDR} =~ /127\.0\.0\.1/">
2019-07-18 13:38:10 +02:00
<title>Test2 title</title>
<subtitle>Test2 subtitle</subtitle>
<text>This is a second test text</text>
<check>Accept test</check>
</notification></root>',null,null)}
);
$dbh->do(
qq{INSERT INTO notifications VALUES ('dwho','testref3','2050-05-30 00:00:00','<?xml version="1.0" encoding="UTF-8"?>
<root><notification uid="dwho" date="2050-05-30" reference="testref3">
<title>Test title</title>
<subtitle>Test subtitle</subtitle>
<text>This is a test text</text>
<check>Accept test</check>
</notification></root>',null,null)}
);
$dbh->do(
qq{INSERT INTO notifications VALUES ('rtyler','testref','2016-05-30 00:00:00','<?xml version="1.0" encoding="UTF-8"?>
<root><notification uid="rtyler" date="2016-05-30" reference="testref" condition="\$env->{REMOTE_ADDR} =~ /127\.1\.1\.1/">
<title>Test title</title>
<subtitle>Test subtitle</subtitle>
<text>This is a test text</text>
<check>Accept test</check>
</notification></root>',null,null)}
);
$dbh->do(
qq{INSERT INTO notifications VALUES ('rtyler','testref2','2050-05-30 00:00:00','<?xml version="1.0" encoding="UTF-8"?>
<root><notification uid="rtyler" date="2050-05-30" reference="testref2">
<title>Test title</title>
<subtitle>Test subtitle</subtitle>
<text>This is a test text</text>
<check>Accept test</check>
2016-06-02 23:20:36 +02:00
</notification></root>',null,null)}
);
2019-02-07 09:27:56 +01:00
my $client = LLNG::Manager::Test->new( {
2016-11-14 13:34:46 +01:00
ini => {
logLevel => 'error',
useSafeJail => 1,
2017-02-02 06:37:58 +01:00
notification => 1,
2016-11-14 13:34:46 +01:00
notificationStorage => 'DBI',
notificationStorageOptions => {
dbiChain => "dbi:SQLite:dbname=$file",
},
2020-02-20 23:34:02 +01:00
oldNotifFormat => 1,
notificationsExplorer => 1
2016-11-14 13:34:46 +01:00
}
2016-06-02 23:20:36 +02:00
}
);
2019-07-17 11:58:22 +02:00
# Try to authenticate
2016-06-02 23:20:36 +02:00
# -------------------
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_post(
2016-06-02 23:20:36 +02:00
'/',
IO::String->new(
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
),
accept => 'text/html',
length => 64,
),
'Auth query'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2019-08-02 23:09:51 +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] );
2017-02-15 07:41:50 +01:00
ok( $res->[2]->[0] =~ /1x1x1/, ' Found ref' );
2017-02-02 13:29:59 +01:00
expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
2016-06-02 23:20:36 +02:00
2020-08-28 21:53:19 +02:00
# Verify that cookie is ciphered (session invalid)
2016-06-02 23:20:36 +02:00
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_get(
2016-06-02 23:20:36 +02:00
'/',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
cookie => "lemonldap=$id",
),
'Test cookie received'
);
2016-12-23 11:02:21 +01:00
expectReject($res);
2016-06-02 23:20:36 +02:00
# Try to validate notification without accepting it
my $str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_post(
2016-06-02 23:20:36 +02:00
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
"Don't accept notification"
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-06-02 23:20:36 +02:00
2019-07-18 13:38:10 +02:00
# Try to validate notifications
2016-06-03 06:30:34 +02:00
$str =
'reference1x1=testref&check1x1x1=accepted&check1x1x2=accepted&reference1x2=testref2&check1x2x1=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
2016-06-02 23:20:36 +02:00
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_post(
2016-06-02 23:20:36 +02:00
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
2019-07-18 13:38:10 +02:00
"Accept notifications"
2016-06-02 23:20:36 +02:00
);
2016-12-23 11:02:21 +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"
);
2020-02-12 23:05:45 +01:00
$id = expectCookie($res);
2016-06-02 23:20:36 +02: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' );
2016-06-02 23:20:36 +02: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);
# 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);
2016-06-02 23:20:36 +02:00
clean_sessions();
eval { unlink $file };
2016-06-03 06:30:34 +02:00
}
2016-06-02 23:20:36 +02:00
2018-02-08 21:55:21 +01:00
count($maintests);
2016-12-23 11:02:21 +01:00
done_testing( count() );