40-Notifications-JSON-DBI.t 3.27 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4 5 6
use Test::More;
use strict;
use IO::String;

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

SKIP: {
    eval { require DBI; require DBD::SQLite; };
    if ($@) {
Xavier Guimard's avatar
Xavier Guimard committed
14
        skip 'DBD::SQLite not found', $maintests;
Xavier Guimard's avatar
Xavier Guimard committed
15 16 17 18 19 20 21 22
    }

    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)}
Xavier Guimard's avatar
Xavier Guimard committed
23
    )->execute(
Xavier Guimard's avatar
Xavier Guimard committed
24 25 26 27 28 29 30 31 32 33 34
        '[
{
  "uid": "dwho",
  "date": "2016-05-30",
  "reference": "testref",
  "title": "Test title",
  "subtitle": "Test subtitle",
  "text": "This is a test text",
  "check": ["Accept test"]
}
]'
Xavier Guimard's avatar
Xavier Guimard committed
35
    );
Xavier Guimard's avatar
Xavier Guimard committed
36 37 38 39 40 41

    my $client = LLNG::Manager::Test->new(
        {
            ini => {
                logLevel                   => 'error',
                useSafeJail                => 1,
Xavier Guimard's avatar
Xavier Guimard committed
42
                notification               => 1,
Xavier Guimard's avatar
Xavier Guimard committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
                templatesDir               => 'site/templates/',
                notificationStorage        => 'DBI',
                notificationStorageOptions => {
                    dbiChain => "dbi:SQLite:dbname=$file",
                },
                oldNotifFormat => 0,
            }
        }
    );

    # Try yo authenticate
    # -------------------
    ok(
        $res = $client->_post(
            '/',
            IO::String->new(
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
            ),
            accept => 'text/html',
            length => 64,
        ),
        'Auth query'
    );
    expectOK($res);
    my $id = expectCookie($res);
68
    ok( $res->[2]->[0] =~ /1x1x1/, ' Found ref' );
69
    expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
Xavier Guimard's avatar
Xavier Guimard committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

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

    # Try to validate notification
    $str =
'reference1x1=testref&check1x1x1=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
    ok(
        $res = $client->_post(
            '/notifback',
            IO::String->new($str),
            cookie => "lemonldap=$id",
            accept => 'text/html',
            length => length($str),
        ),
        "Accept notification"
    );
    expectRedirection( $res, 'http://test1.example.com/' );

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

    eval { unlink $file };

}

Xavier Guimard's avatar
Xavier Guimard committed
125
count($maintests);
Xavier Guimard's avatar
Xavier Guimard committed
126 127
done_testing( count() );