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

255 lines
6.4 KiB
Perl
Raw Normal View History

2017-02-01 06:30:44 +01:00
use Test::More;
use strict;
use IO::String;
2022-07-19 20:02:22 +02:00
use JSON;
2017-02-01 06:30:44 +01:00
require 't/test-lib.pm';
my $res;
2022-07-19 20:02:22 +02:00
my $json;
2019-08-29 10:04:06 +02:00
my $file = "$main::tmpDir/20160530_dwho_dGVzdHJlZg==.json";
2017-02-01 06:30:44 +01:00
open F, "> $file" or die($!);
print F '[
{
"uid": "dwho",
"date": "2016-05-30",
"reference": "testref",
"title": "Test title",
"subtitle": "Test subtitle",
"text": "This is a test text",
2018-10-16 22:09:14 +02:00
"check": ["Accept test","Accept test2"]
2017-02-01 06:30:44 +01:00
}
]';
close F;
2019-02-07 09:27:56 +01:00
my $client = LLNG::Manager::Test->new( {
2018-11-26 14:40:21 +01:00
ini => {
2017-02-01 06:30:44 +01:00
logLevel => 'error',
useSafeJail => 1,
2017-02-02 06:37:58 +01:00
notification => 1,
2017-02-01 06:30:44 +01:00
notificationStorage => 'File',
2019-08-29 10:04:06 +02:00
notificationStorageOptions => { dirName => $main::tmpDir },
oldNotifFormat => 0,
portalMainLogo => 'common/logos/logo_llng_old.png',
2017-02-01 06:30:44 +01:00
}
}
);
2022-07-19 20:02:22 +02:00
use Lemonldap::NG::Portal::Main::Constants 'PE_NOTIFICATION';
2017-02-01 06:30:44 +01:00
2019-07-17 11:58:49 +02:00
# Try to authenticate
# -------------------
2022-07-19 20:02:22 +02:00
ok(
$res = $client->_post(
'/',
IO::String->new(
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='),
length => 64,
),
'Auth query (JSON required)'
);
ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' )
or print STDERR "$@\n" . Dumper($res);
ok( $json->{result} == 0, ' Good result' )
or explain( $json, 'result => 0' );
ok( $json->{error} == PE_NOTIFICATION, ' Notificationtion is pending' )
or explain( $json, 'error => 36' );
my $id = $json->{ciphered_id};
# Verify that cookie can be deciphered (ciphered_id is valid)
ok(
$res = $client->_get(
'/notifback',
accept => 'text/html',
cookie => "lemonldap=$id"
),
'Test received Id'
);
count(5);
expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
2019-07-17 11:58:49 +02:00
ok(
$res = $client->_post(
'/',
IO::String->new(
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='),
accept => 'text/html',
length => 64,
),
'Auth query'
);
count(1);
expectOK($res);
2022-07-19 20:02:22 +02:00
$id = expectCookie($res);
2019-07-17 11:58:49 +02:00
expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
2020-08-28 21:53:19 +02:00
# Verify that cookie is ciphered (session invalid)
2019-07-17 11:58:49 +02:00
ok(
$res = $client->_get(
'/',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
cookie => "lemonldap=$id",
),
2022-07-19 20:02:22 +02:00
'Test received cookie'
2019-07-17 11:58:49 +02:00
);
count(1);
expectReject($res);
# Try to cancel notification
ok(
$res = $client->_get(
'/notifback',
query => "cancel=1",
cookie => "lemonldap=$id",
length => 64,
accept => 'text/html',
),
"Cancel notification"
);
my $c = getCookies($res);
2019-08-02 09:51:05 +02:00
ok( not( $c->{'lemonldap'} ), 'Cookie expired' ) or print STDERR Dumper($c);
2019-07-17 11:58:49 +02:00
count(2);
expectRedirection( $res, 'http://auth.example.com/' );
# Try to authenticate
2017-02-01 06:30:44 +01:00
# -------------------
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2017-02-01 06:30:44 +01:00
'/',
IO::String->new(
2018-11-26 14:40:21 +01:00
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='),
2017-02-01 06:30:44 +01:00
accept => 'text/html',
length => 64,
),
'Auth query'
);
count(1);
expectOK($res);
2020-02-12 18:49:16 +01:00
$id = expectCookie($res);
2017-02-02 13:29:59 +01:00
expectForm( $res, undef, '/notifback', 'reference1x1', 'url' );
2017-02-01 06:30:44 +01:00
2020-08-28 21:53:19 +02:00
# Verify that cookie is ciphered (session invalid)
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_get(
2017-02-01 06:30:44 +01:00
'/',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
cookie => "lemonldap=$id",
),
'Test cookie received'
);
count(1);
expectReject($res);
# Try to validate notification without accepting it
my $str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2017-02-01 06:30:44 +01:00
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
"Don't accept notification"
);
ok( $res->[2]->[0] =~ qr%<h2 class="notifText">Test title</h2>%,
'Notification displayed' )
2018-11-26 14:40:21 +01:00
or print STDERR Dumper( $res->[2]->[0] );
count(2);
ok( $res->[2]->[0] =~ qr%<img src="/static/common/logos/logo_llng_old.png"%,
2018-10-16 22:09:14 +02:00
'Found custom Main Logo' )
2018-11-26 14:40:21 +01:00
or print STDERR Dumper( $res->[2]->[0] );
2018-10-16 22:09:14 +02:00
count(1);
# Try to validate notification without accepting it
$str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
"Don't accept notification"
);
ok( $res->[2]->[0] =~ qr%<h2 class="notifText">Test title</h2>%,
'Notification displayed' )
2018-11-26 14:40:21 +01:00
or print STDERR Dumper( $res->[2]->[0] );
count(2);
# Try to validate notification without accepting it
$str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
"Don't accept notification"
);
ok( $res->[2]->[0] =~ qr%<h2 class="notifText">Test title</h2>%,
'Notification displayed' )
2018-11-26 14:40:21 +01:00
or print STDERR Dumper( $res->[2]->[0] );
2017-02-01 06:30:44 +01:00
count(2);
2018-10-16 22:09:14 +02:00
# Try to validate notification with accepting just one checkbox
2018-11-26 14:40:21 +01:00
$str =
'reference1x1=testref&check1x1x1=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
ok(
$res = $client->_post(
2018-10-16 22:09:14 +02:00
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
"Don't accept notification - Accept just one checkbox"
);
ok( $res->[2]->[0] =~ qr%<h2 class="notifText">Test title</h2>%,
'Notification displayed' )
2018-11-26 14:40:21 +01:00
or print STDERR Dumper( $res->[2]->[0] );
2018-10-16 22:09:14 +02:00
count(2);
# Try to validate notification with accepting all checkboxes
2018-11-26 14:40:21 +01:00
$str =
'reference1x1=testref&check1x1x1=accepted&check1x1x2=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
ok(
$res = $client->_post(
2017-02-01 06:30:44 +01:00
'/notifback',
IO::String->new($str),
cookie => "lemonldap=$id",
accept => 'text/html',
length => length($str),
),
"Accept notification"
);
expectRedirection( $res, qr/./ );
$file =~ s/json$/done/;
ok( -e $file, 'Notification was deleted' );
count(2);
2018-09-05 13:35:50 +02:00
$id = expectCookie($res);
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_get(
2018-09-05 13:35:50 +02:00
'/',
cookie => "lemonldap=$id",
accept => 'text/html',
),
'New auth query'
);
2018-10-12 10:04:03 +02:00
expectAuthenticatedAs( $res, 'dwho' );
ok( $res->[2]->[0] =~ /yourApp/s, 'Menu displayed' );
2018-09-05 13:35:50 +02:00
count(2);
2017-02-01 06:30:44 +01:00
clean_sessions();
unlink $file;
done_testing( count() );