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

125 lines
3.3 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;
my $res;
2016-12-23 11:02:21 +01:00
my $file = 't/notifications.db';
2018-02-08 21:55:21 +01:00
my $maintests = 6;
2016-06-02 23:20:36 +02:00
eval { unlink $file };
2016-06-07 23:04:24 +02:00
require 't/test-lib.pm';
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>
</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",
},
oldNotifFormat => 1,
2016-11-14 13:34:46 +01:00
}
2016-06-02 23:20:36 +02:00
}
);
# Try yo authenticate
# -------------------
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);
my $id = expectCookie($res);
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
# Verify that cookie is ciphered (session unvalid)
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
# Try to validate notification
2016-06-03 06:30:34 +02:00
$str =
2016-06-02 23:20:36 +02:00
'reference1x1=testref&check1x1x1=accepted&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),
),
"Accept notification"
);
2016-12-23 11:02:21 +01:00
expectRedirection( $res, 'http://test1.example.com/' );
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++ }
ok( $i == 1, 'Notification was deleted' );
clean_sessions();
2016-06-03 06:30:34 +02:00
eval { unlink $file };
2016-06-02 23:20:36 +02:00
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() );