40-Notifications-XML-DBI.t 3.39 KB
Newer Older
1
use Test::More;
2 3 4 5
use strict;
use IO::String;

my $res;
6
my $file      = 't/notifications.db';
7
my $maintests = 6;
8
eval { unlink $file };
9
require 't/test-lib.pm';
10 11

SKIP: {
12 13 14 15 16 17
    eval {
        require DBI;
        require DBD::SQLite;
        require XML::LibXML;
        require XML::LibXSLT;
    };
18
    if ($@) {
19
        skip 'DBD::SQLite or XML::Lib* not found', $maintests;
20
    }
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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)}
    );

36
    my $client = LLNG::Manager::Test->new(
37
        {
38 39 40
            ini => {
                logLevel                   => 'error',
                useSafeJail                => 1,
Xavier Guimard's avatar
Xavier Guimard committed
41
                notification               => 1,
42 43 44 45 46
                templatesDir               => 'site/templates/',
                notificationStorage        => 'DBI',
                notificationStorageOptions => {
                    dbiChain => "dbi:SQLite:dbname=$file",
                },
47
                oldNotifFormat => 1,
48
            }
49 50 51 52 53 54
        }
    );

    # Try yo authenticate
    # -------------------
    ok(
55
        $res = $client->_post(
56 57 58 59 60 61 62 63 64
            '/',
            IO::String->new(
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
            ),
            accept => 'text/html',
            length => 64,
        ),
        'Auth query'
    );
65 66
    expectOK($res);
    my $id = expectCookie($res);
67
    ok( $res->[2]->[0] =~ /1x1x1/, ' Found ref' );
68
    expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
69 70 71

    # Verify that cookie is ciphered (session unvalid)
    ok(
72
        $res = $client->_get(
73 74 75 76 77 78
            '/',
            query  => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
            cookie => "lemonldap=$id",
        ),
        'Test cookie received'
    );
79
    expectReject($res);
80 81 82 83

    # Try to validate notification without accepting it
    my $str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
    ok(
84
        $res = $client->_post(
85 86 87 88 89 90 91 92
            '/notifback',
            IO::String->new($str),
            cookie => "lemonldap=$id",
            accept => 'text/html',
            length => length($str),
        ),
        "Don't accept notification"
    );
93
    expectOK($res);
94 95

    # Try to validate notification
96
    $str =
97 98
'reference1x1=testref&check1x1x1=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
    ok(
99
        $res = $client->_post(
100 101 102 103 104 105 106 107
            '/notifback',
            IO::String->new($str),
            cookie => "lemonldap=$id",
            accept => 'text/html',
            length => length($str),
        ),
        "Accept notification"
    );
108
    expectRedirection( $res, 'http://test1.example.com/' );
109 110 111 112 113 114 115 116 117 118 119

    # 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();

120
    eval { unlink $file };
121

122
}
123

124
count($maintests);
125 126
done_testing( count() );