From e11c18914137d7473bfc7137e97087b8e801c759 Mon Sep 17 00:00:00 2001 From: Christophe Maudoux Date: Fri, 3 Apr 2020 22:00:20 +0200 Subject: [PATCH] Remove underscores in notifications reference (#2135) --- .../lib/Lemonldap/NG/Common/Notifications/JSON.pm | 3 +++ .../lib/Lemonldap/NG/Common/Notifications/XML.pm | 3 +++ .../lib/Lemonldap/NG/Manager/Notifications.pm | 2 ++ .../t/40-Notifications-JSON-Server.t | 14 +++++++------- .../t/40-Notifications-XML-Server.t | 9 +++++++-- .../t/70-2F-TOTP-with-TTL-and-JSON.t | 4 ++-- .../t/70-2F-TOTP-with-TTL-and-XML.t | 4 ++-- 7 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/JSON.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/JSON.pm index c0e8c4f9a..542263fb8 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/JSON.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/JSON.pm @@ -15,10 +15,13 @@ sub newNotification { eval { $self->logger->error("Unable to decode JSON file: $err") }; return 0; } + my @notifs; $json = [$json] unless ( ref($json) eq 'ARRAY' ); + foreach my $notif (@$json) { my @data; + $notif->{reference} =~ s/_/-/g; # Remove underscores (#2135) # Mandatory information foreach (qw(date uid reference)) { diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/XML.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/XML.pm index 450ae3df4..0a660ab66 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/XML.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/XML.pm @@ -25,12 +25,15 @@ sub newNotification { eval { $self->logger->error("Unable to read XML file : $err") }; return 0; } + my @notifs; my ( $version, $encoding ) = ( $xml->version(), $xml->encoding() ); + foreach my $notif ( $xml->documentElement->getElementsByTagName('notification') ) { my @data = (); + $notif->{reference} =~ s/_/-/g; # Remove underscores (#2135) # Mandatory information foreach (qw(date uid reference)) { diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Notifications.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Notifications.pm index 89dff3581..06dd3b24a 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Notifications.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Notifications.pm @@ -271,6 +271,8 @@ sub newNotification { return $self->sendError( $req, undef, 200 ); } + $json->{reference} =~ s/_/-/g; # Remove underscores (#2135) + foreach my $r (qw(uid reference xml)) { return $self->sendError( $req, "Missing $r", 200 ) unless ( $json->{$r} ); diff --git a/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t b/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t index 807b848a3..92cd9c19e 100644 --- a/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t +++ b/lemonldap-ng-portal/t/40-Notifications-JSON-Server.t @@ -41,7 +41,7 @@ my $jsonbis = '{ my $json2 = q%{ "date": "2016-05-30", -"reference": "testref2", +"reference": "test_ref2", "uid": "dwho", "title": "Test2 title", "text": "This is a second test text", @@ -187,7 +187,7 @@ ok( scalar @{ $json->{result} } == 3, 'Three notifications found' ) or print STDERR Dumper($json); foreach ( @{ $json->{result} } ) { - ok( $_->{reference} =~ /^testref/, "Reference \'$_->{reference}\' found" ) + ok( $_->{reference} =~ /^test-?ref/, "Reference \'$_->{reference}\' found" ) or print STDERR Dumper($json); ok( $_->{uid} =~ /^(dwho|everyone)$/, "UID \'$_->{uid}\' found" ) or print STDERR Dumper($json); @@ -204,7 +204,7 @@ ok( $res->[2]->[0] =~ /"result"\s*:\s*/, 'Result found' ) or print STDERR Dumper( $res->[2]->[0] ); ok( $res->[2]->[0] =~ /"reference":"testref"/, 'First notification found' ) or print STDERR Dumper( $res->[2]->[0] ); -ok( $res->[2]->[0] =~ /"reference":"testref2"/, 'Second notification found' ) +ok( $res->[2]->[0] =~ /"reference":"test-ref2"/, 'Second notification found' ) or print STDERR Dumper( $res->[2]->[0] ); ok( $res->[2]->[0] =~ /"reference":"testrefall"/, @@ -236,14 +236,14 @@ count(7); ok( $res = $client->_get( - '/notifications/dwho/testref2', + '/notifications/dwho/test-ref2', type => 'application/json', ), - 'List notification with reference "testref2"' + 'List notification with reference "test-ref2"' ); ok( $res->[2]->[0] =~ /"result"\s*:\s*/, 'Result found' ) or print STDERR Dumper( $res->[2]->[0] ); -ok( $res->[2]->[0] =~ /"reference"\s*:\s*"testref2"/, +ok( $res->[2]->[0] =~ /"reference"\s*:\s*"test-ref2"/, 'Notification reference found' ) or print STDERR Dumper( $res->[2]->[0] ); ok( $res->[2]->[0] =~ /"title"\s*:\s*"Test2 title"/, @@ -275,7 +275,7 @@ ok( ok( $res->[2]->[0] =~ /"error"\s*:\s*"Bad request"/, 'Bad method is refused' ); count(2); -foreach (qw(testrefall testref2)) { +foreach (qw(testrefall test-ref2)) { my $user = $_ eq 'testrefall' ? 'everyone' : 'dwho'; ok( $res = $client->_delete( diff --git a/lemonldap-ng-portal/t/40-Notifications-XML-Server.t b/lemonldap-ng-portal/t/40-Notifications-XML-Server.t index 9a77004e5..7425f7070 100644 --- a/lemonldap-ng-portal/t/40-Notifications-XML-Server.t +++ b/lemonldap-ng-portal/t/40-Notifications-XML-Server.t @@ -9,7 +9,7 @@ BEGIN { require 't/test-lib.pm'; } -my $maintests = 12; +my $maintests = 13; my $client; # Redefine LWP methods for tests @@ -63,7 +63,7 @@ my $combined = ' This is a test text I agree - + Test title Test subtitle This is a test text @@ -202,6 +202,11 @@ SKIP: { expectOK($res); $id = expectCookie($res); expectForm( $res, undef, '/notifback', 'reference1x1' ); + ok( + $res->[2]->[0] =~ + m%%, + 'Reference found' + ) or print STDERR Dumper( $res->[2]->[0] ); ok( $res->[2]->[0] =~ m%I agree%, diff --git a/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-JSON.t b/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-JSON.t index b36bf7184..486b44e61 100644 --- a/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-JSON.t +++ b/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-JSON.t @@ -20,7 +20,7 @@ SKIP: { totp2fTTL => 120, sfRemovedMsgRule => '$uid eq "dwho"', sfRemovedUseNotif => 1, - sfRemovedNotifRef => 'RemoveTOTP', + sfRemovedNotifRef => 'Remov_e_TOTP', portalMainLogo => 'common/logos/logo_llng_old.png', notification => 1, notificationStorage => 'File', @@ -146,7 +146,7 @@ SKIP: { expectOK($res); ok( $res->[2]->[0] =~ -qr%%, +qr%%, 'Notification reference found' ) or print STDERR Dumper( $res->[2]->[0] ); ok( time() + 300 <= $1 && $1 <= time() + 305, 'Right reference found' ) diff --git a/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-XML.t b/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-XML.t index 927a08d0e..b1bcc99cf 100644 --- a/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-XML.t +++ b/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL-and-XML.t @@ -24,7 +24,7 @@ SKIP: { totp2fTTL => 120, sfRemovedMsgRule => '$uid eq "dwho"', sfRemovedUseNotif => 1, - sfRemovedNotifRef => 'RemoveTOTP', + sfRemovedNotifRef => 'Remove_TOTP', portalMainLogo => 'common/logos/logo_llng_old.png', notification => 1, notificationStorage => 'File', @@ -150,7 +150,7 @@ SKIP: { expectOK($res); ok( $res->[2]->[0] =~ -qr%%, +qr%%, 'Notification reference found' ) or print STDERR Dumper( $res->[2]->[0] ); ok( time() + 300 <= $1 && $1 <= time() + 305, 'Right reference found' )