diff --git a/lemonldap-ng-portal/t/40-Notifications-explorer-JSON-File.t b/lemonldap-ng-portal/t/40-Notifications-explorer-JSON-File.t new file mode 100644 index 000000000..e691d79cb --- /dev/null +++ b/lemonldap-ng-portal/t/40-Notifications-explorer-JSON-File.t @@ -0,0 +1,243 @@ +use Test::More; +use strict; +use IO::String; +use JSON qw(from_json); + +require 't/test-lib.pm'; + +my $res; +my $file = "$main::tmpDir/20160530_dwho_dGVzdHJlZg==.json"; + +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", + "check": ["Accept test","Accept test2"] +} +]'; +close F; + +my $client = LLNG::Manager::Test->new( { + ini => { + logLevel => 'error', + useSafeJail => 1, + notification => 1, + notificationStorage => 'File', + notificationStorageOptions => { dirName => $main::tmpDir }, + oldNotifFormat => 0, + notificationsExplorer => 1, + portalMainLogo => 'common/logos/logo_llng_old.png', } + } +); + +# Try to authenticate +# ------------------- +ok( + $res = $client->_post( + '/', + IO::String->new( + 'user=dwho&password=dwho'), + accept => 'text/html', + length => 23, + ), + 'Auth query' +); +count(1); +expectOK($res); +my $id = expectCookie($res); +expectForm( $res, undef, '/notifback', 'reference1x1' ); + +# Verify that cookie is ciphered (session unvalid) +ok( + $res = $client->_get( + '/', cookie => "lemonldap=$id", + ), + 'Test cookie received' +); +count(1); +expectReject($res); + +# Try to authenticate +# ------------------- +ok( + $res = $client->_post( + '/', + IO::String->new( + 'user=dwho&password=dwho'), + accept => 'text/html', + length => 23, + ), + 'Auth query' +); +count(1); +expectOK($res); +$id = expectCookie($res); +expectForm( $res, undef, '/notifback', 'reference1x1' ); + +# Verify that cookie is ciphered (session unvalid) +ok( + $res = $client->_get( + '/', + cookie => "lemonldap=$id", + ), + 'Test cookie received' +); +count(1); +expectReject($res); + +# Try to validate notification with accepting all checkboxes +my $str = 'reference1x1=testref&check1x1x1=accepted&check1x1x2=accepted'; +ok( + $res = $client->_post( + '/notifback', + IO::String->new($str), + cookie => "lemonldap=$id", + accept => 'text/html', + length => length($str), + ), + "Accept notification" +); +$file =~ s/json$/done/; +ok( -e $file, 'Notification was deleted' ); +count(2); + +$id = expectCookie($res); + +ok( + $res = $client->_get( + '/', + cookie => "lemonldap=$id", + accept => 'text/html', + ), + 'New auth query' +); +expectAuthenticatedAs( $res, 'dwho' ); +ok( $res->[2]->[0] =~ /yourApp/s, 'Menu displayed' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( + $res->[2]->[0] =~ + m%notificationsExplorer%, + 'Link found' +) or print STDERR Dumper( $res->[2]->[0] ); +count(3); + +# GET notifications explorer +ok( + $res = $client->_get( + '/mynotifications', + cookie => "lemonldap=$id", + accept => 'text/html', + ), + 'Notifications explorer query' +); +ok( $res->[2]->[0] =~ m%%, ' Language icons found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%%, + ' trspan="myNotification" found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%Date%, + ' trspan="date" found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%Reference%, + ' trspan="reference" found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%Action%, + ' trspan="action" found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%\d{10}%, ' epoch found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%testref%, + ' testref found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%class="fa fa-eye">%, ' fa-eye found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%%, + ' Notififcation container found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ m%class="verify" trspan="verify">Verify%, + ' trspan="verify" found' ) + or print STDERR Dumper( $res->[2]->[0] ); +ok( $res->[2]->[0] =~ /notifications\.(?:min\.)?js/, 'Found NOTIFICATIONS js' ) + or print STDERR Dumper( $res->[2]->[0] ); +count(12); + +# GET notification testref +$res->[2]->[0] =~ +m%%; +ok( + $res = $client->_get( + '/mynotifications/testref', + query => "epoch=$1", + cookie => "lemonldap=$id", + ), + 'Display testref notification query' +); +my $json; +ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); +ok( $json->{result} == 1, ' Result is 1' ) + or explain( $json, "result => 1" ); +ok( + $json->{notification} =~ + m%%, + ' Hidden input found' +) or explain( $json, "Hidden input" ); +ok( $json->{notification} =~ m%

Test title

%, + '

tag found' ) + or explain( $json, "

tag" ); +ok( $json->{notification} =~ m%

Test subtitle

%, + '

tag found' ) + or explain( $json, "

tag" ); +ok( $json->{notification} =~ m%

This is a test text

%, + '

tag found' ) + or explain( $json, "

tag" ); +ok( $json->{notification} =~ m%checked disabled name="check1x1x1"%, + ' Checkbox 1 found' ) + or explain( $json, "Checkbox 1" ); +ok( $json->{notification} =~ m%checked disabled name="check1x1x2"%, + ' Checkbox 2 found' ) + or explain( $json, "Chekbox 2" ); +count(9); + +# Malformed request +ok( + $res = $client->_get( + '/mynotifications/testref', cookie => "lemonldap=$id", + ), + 'Malformed query' +); +my $json; +ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); +ok( $json->{error} eq 'Missing parameter', ' Missing parameter' ) + or explain( $json, "Missing parameter" ); +count(3); + +# Bad request +ok( + $res = $client->_get( + '/mynotifications/testref', + query => "epoch=1234567890", + cookie => "lemonldap=$id", + ), + 'Bad query' +); +my $json; +ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); +ok( !$json->{result}, ' Result is 0' ) + or explain( $json, "result => 0" ); +ok( !$json->{notification}, ' Notification is 0' ) + or explain( $json, "notification => 0" ); +count(4); + +clean_sessions(); + +unlink $file; + +done_testing( count() ); diff --git a/lemonldap-ng-portal/t/40-Notifications-explorer-XML-File.t b/lemonldap-ng-portal/t/40-Notifications-explorer-XML-File.t new file mode 100644 index 000000000..e04ea2280 --- /dev/null +++ b/lemonldap-ng-portal/t/40-Notifications-explorer-XML-File.t @@ -0,0 +1,253 @@ +use Test::More; +use strict; +use IO::String; +use JSON qw(from_json); + +require 't/test-lib.pm'; + +my $res; +my $file = "$main::tmpDir/20160530_dwho_dGVzdHJlZg==.xml"; +my $maintests = 34; + +SKIP: { + eval { require XML::LibXML; require XML::LibXSLT; }; + if ($@) { + skip 'XML::LibX* not found', $maintests; + } + + open F, "> $file" or die($!); + print F ' + +Test title +Test subtitle +This is a test text +Accept test +Accept test2 +'; + close F; + + my $client = LLNG::Manager::Test->new( { + ini => { + logLevel => 'error', + useSafeJail => 1, + notification => 1, + notificationStorage => 'File', + notificationStorageOptions => { dirName => $main::tmpDir }, + oldNotifFormat => 1, + notificationsExplorer => 1, + portalMainLogo => 'common/logos/logo_llng_old.png', + } + } + ); + + # Try to authenticate + # ------------------- + ok( + $res = $client->_post( + '/', + IO::String->new( + 'user=dwho&password=dwho'), + accept => 'text/html', + length => 23, + ), + 'Auth query' + ); + count(1); + expectOK($res); + my $id = expectCookie($res); + expectForm( $res, undef, '/notifback', 'reference1x1' ); + + # Verify that cookie is ciphered (session unvalid) + ok( + $res = $client->_get( + '/', cookie => "lemonldap=$id", + ), + 'Test cookie received' + ); + expectReject($res); + + # Try to authenticate + # ------------------- + ok( + $res = $client->_post( + '/', + IO::String->new( + 'user=dwho&password=dwho'), + accept => 'text/html', + length => 23, + ), + 'Auth query' + ); + count(1); + expectOK($res); + $id = expectCookie($res); + expectForm( $res, undef, '/notifback', 'reference1x1' ); + + # Verify that cookie is ciphered (session unvalid) + ok( + $res = $client->_get( + '/', + cookie => "lemonldap=$id", + ), + 'Test cookie received' + ); + count(1); + expectReject($res); + + # Try to validate notification with accepting all checkboxes + my $str = 'reference1x1=testref&check1x1x1=accepted&check1x1x2=accepted'; + ok( + $res = $client->_post( + '/notifback', + IO::String->new($str), + cookie => "lemonldap=$id", + accept => 'text/html', + length => length($str), + ), + "Accept notification" + ); + $file =~ s/xml$/done/; + +sleep 60; + + ok( -e $file, 'Notification was deleted' ); + + $id = expectCookie($res); + + ok( + $res = $client->_get( + '/', + cookie => "lemonldap=$id", + accept => 'text/html', + ), + 'New auth query' + ); + expectAuthenticatedAs( $res, 'dwho' ); + ok( $res->[2]->[0] =~ /yourApp/s, 'Menu displayed' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( + $res->[2]->[0] =~ + m%notificationsExplorer%, + 'Link found' + ) or print STDERR Dumper( $res->[2]->[0] ); + + # GET notifications explorer + ok( + $res = $client->_get( + '/mynotifications', + cookie => "lemonldap=$id", + accept => 'text/html', + ), + 'Notifications explorer query' + ); + ok( $res->[2]->[0] =~ m%%, + ' Language icons found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%%, + ' trspan="myNotification" found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%Date%, + ' trspan="date" found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( + $res->[2]->[0] =~ m%Reference%, + ' trspan="reference" found' + ) or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%Action%, + ' trspan="action" found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%\d{10}%, + ' epoch found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%testref%, + ' testref found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%class="fa fa-eye">%, ' fa-eye found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%%, + ' Notififcation container found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ m%class="verify" trspan="verify">Verify%, + ' trspan="verify" found' ) + or print STDERR Dumper( $res->[2]->[0] ); + ok( $res->[2]->[0] =~ /notifications\.(?:min\.)?js/, + 'Found NOTIFICATIONS js' ) + or print STDERR Dumper( $res->[2]->[0] ); + + # GET notification testref + $res->[2]->[0] =~ +m%%; + ok( + $res = $client->_get( + '/mynotifications/testref', + query => "epoch=$1", + cookie => "lemonldap=$id", + ), + 'Display testref notification query' + ); + my $json; + ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); + ok( $json->{result} == 1, ' Result is 1' ) + or explain( $json, "result => 1" ); + ok( + $json->{notification} =~ + m%%, + ' Hidden input found' + ) or explain( $json, "Hidden input" ); + ok( $json->{notification} =~ m%

Test title

%, + '

tag found' ) + or explain( $json, "

tag" ); + ok( $json->{notification} =~ m%

Test subtitle

%, + '

tag found' ) + or explain( $json, "

tag" ); + ok( + $json->{notification} =~ + m%

This is a test text

%, + '

tag found' + ) or explain( $json, "

tag" ); + ok( $json->{notification} =~ m%checked disabled name="check1x1x1"%, + ' Checkbox 1 found' ) + or explain( $json, "Checkbox 1" ); + ok( $json->{notification} =~ m%checked disabled name="check1x1x2"%, + ' Checkbox 2 found' ) + or explain( $json, "Chekbox 2" ); + + # Malformed request + ok( + $res = $client->_get( + '/mynotifications/testref', cookie => "lemonldap=$id", + ), + 'Malformed query' + ); + my $json; + ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); + ok( $json->{error} eq 'Missing parameter', ' Missing parameter' ) + or explain( $json, "Missing parameter" ); + + # Bad request + ok( + $res = $client->_get( + '/mynotifications/testref', + query => "epoch=1234567890", + cookie => "lemonldap=$id", + ), + 'Bad query' + ); + my $json; + ok( $json = eval { from_json( $res->[2]->[0] ) }, 'Response is JSON' ) + or print STDERR "$@\n" . Dumper($res); + ok( !$json->{result}, ' Result is 0' ) + or explain( $json, "result => 0" ); + ok( !$json->{notification}, ' Notification is 0' ) + or explain( $json, "notification => 0" ); + + clean_sessions(); + + unlink $file; + +} + +count($maintests); +done_testing( count() );