Append to JSON format condition check & improve unit test (#2012)

merge-requests/133/head
Christophe Maudoux 6 years ago
parent c548a4d03e
commit d935753eaf
  1. 29
      lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/Notifications/JSON.pm
  2. 40
      lemonldap-ng-portal/t/40-Notifications-JSON-DBI.t

@ -4,7 +4,7 @@ use strict;
use Mouse;
use JSON qw(from_json);
our $VERSION = '2.0.6';
our $VERSION = '2.0.7';
no warnings 'redefine';
@ -44,9 +44,26 @@ sub checkForNotifications {
# Get the reference
my $reference = $notif->{reference};
$self->logger->debug("Get reference $reference");
# Check condition if any
if ( my $condition = $notif->{condition} ) {
$self->logger->debug("Get condition $condition");
$condition = $self->p->HANDLER->substitute($condition);
unless ( $condition = $self->p->HANDLER->buildSub($condition) )
{
$self->logger->error( 'Notification condition error: '
. $self->p->HANDLER->tsv->{jail}->error );
next LOOP;
}
unless ( $condition->( $req, $req->sessionInfo ) ) {
$self->logger->debug(
'Notification condition not authorized');
next LOOP;
}
}
# Check it in session
if ( exists $req->{sessionInfo}->{"notification_$reference"} ) {
@ -149,7 +166,8 @@ sub getNotifBack {
# Verity that checkboxes have been checked
if ( $notif->{check} ) {
$notif->{check} = [$notif->{check}] unless ( ref($notif->{check}) eq 'ARRAY' );
$notif->{check} = [ $notif->{check} ]
unless ( ref( $notif->{check} ) eq 'ARRAY' );
if ( my $toCheckCount = @{ $notif->{check} } ) {
unless ($checks->{$refId}
and $toCheckCount == @{ $checks->{$refId} } )
@ -218,7 +236,8 @@ sub toForm {
@notifs = map {
$i++;
if ( $_->{check} ) {
$_->{check} = [$_->{check}] unless ( ref($_->{check}) eq 'ARRAY' );
$_->{check} = [ $_->{check} ]
unless ( ref( $_->{check} ) eq 'ARRAY' );
my $j = 0;
$_->{check} =
[ map { $j++; { id => '1x' . $i . 'x' . $j, value => $_ } }
@ -241,7 +260,7 @@ sub notificationServer {
if ( $req->method =~ /^POST$/i ) {
$self->p->logger->debug("POST request");
( $res, $err ) =
eval { $self->notifObject->newNotification( $req->content ) };
eval { $self->notifObject->newNotification( $req->content, 1 ) };
return $self->p->sendError( $req, $@, 500 ) if ($@);
}
elsif ( $req->method =~ /^GET$/i ) {

@ -3,7 +3,7 @@ use strict;
use IO::String;
my $res;
my $maintests = 9;
my $maintests = 10;
require 't/test-lib.pm';
my $file = tempdb();
@ -28,7 +28,7 @@ q{INSERT INTO notifications VALUES ('dwho','testref','2016-05-30 00:00:00',?,nul
"title": "Test title",
"subtitle": "Test subtitle",
"text": "This is a test text",
"check": ["Accept test"]
"check": "Accept test"
}
]'
);
@ -40,11 +40,28 @@ q{INSERT INTO notifications VALUES ('dwho','testref2','2016-05-30 00:00:00',?,nu
"uid": "dwho",
"date": "2016-05-29",
"reference": "testref2",
"condition": "\'1\'",
"title": "Test2 title",
"subtitle": "Test2 subtitle",
"text": "This is a second test text",
"check": ["Accept test"]
}
]'
);
$dbh->prepare(
q{INSERT INTO notifications VALUES ('rtyler','testref','2016-05-30 00:00:00',?,null,null)}
)->execute(
'[
{
"uid": "rtyler",
"date": "2016-05-29",
"reference": "testref",
"condition": "\'0\'",
"title": "Test title",
"subtitle": "Test subtitle",
"text": "This is a test text",
"check": ["Accept test"]
}
]'
);
@ -131,6 +148,7 @@ q{INSERT INTO notifications VALUES ('dwho','testref2','2016-05-30 00:00:00',?,nu
!defined( $cookies->{lemonldappdata} ),
" Make sure no pdata is returned"
);
$client->logout($id);
# Verify that notification was tagged as 'done'
my $sth =
@ -140,10 +158,26 @@ q{INSERT INTO notifications VALUES ('dwho','testref2','2016-05-30 00:00:00',?,nu
while ( $sth->fetchrow_hashref ) { $i++ }
ok( $i == 2, 'Notification was deleted' );
# Try to authenticate
# -------------------
ok(
$res = $client->_post(
'/',
IO::String->new(
'user=rtyler&password=rtyler&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
),
accept => 'text/html',
length => 68,
),
'Auth query'
);
expectRedirection( $res, 'http://test1.example.com/' );
$id = expectCookie($res);
$client->logout($id);
clean_sessions();
eval { unlink $file };
}
count($maintests);

Loading…
Cancel
Save