From 057cfb29e8e7b60e782448cf43a71272328c91f0 Mon Sep 17 00:00:00 2001 From: Christophe Maudoux Date: Thu, 18 Feb 2021 23:35:08 +0100 Subject: [PATCH] Improve code (#2458) --- .../NG/Portal/Plugins/CheckDevOps.pm | 35 ++++++++----------- .../t/56-CheckDevOps-with-Download.t | 2 +- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/CheckDevOps.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/CheckDevOps.pm index 5b8b835b8..53eb01ac3 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/CheckDevOps.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/CheckDevOps.pm @@ -66,7 +66,6 @@ sub display { DOWNLOAD => $self->conf->{checkDevOpsDownload}, MSG => 'checkDevOps', ALERTE => 'alert-info', - FILE => '', TOKEN => ( $self->ottRule->( $req, {} ) ? $self->ott->createToken() @@ -82,7 +81,8 @@ sub display { sub run { my ( $self, $req ) = @_; my ( $headers, $rules ) = ( [], [] ); - my ( $msg, $json, $alert, $url ); + my ( $msg, $json, $url ); + my $alert = 'alert-danger'; # Check token if ( $self->ottRule->( $req, {} ) ) { @@ -111,7 +111,6 @@ sub run { DOWNLOAD => $self->conf->{checkDevOpsDownload}, MSG => "PE$msg", ALERTE => 'alert-warning', - FILE => '', TOKEN => $token, }; return $self->p->sendJSONresponse( $req, $params ) @@ -132,22 +131,20 @@ sub run { my ($proto) = $url =~ m#^(https?://).*#i; $proto ||= 'http://'; $url = "$proto$vhost/rules.json"; - my $response = - $self->ua->get( $url, 'Accept' => 'application/json' ); + my $resp = $self->ua->get( $url, 'Accept' => 'application/json' ); $self->logger->debug( "Code/Message from $url: " - . $response->{_rc} . '/' - . $response->{_msg} ); - $self->logger->debug( - "Content from $url: " . $response->{_content} ) if $response->{_content}; + . $resp->code . '/' + . $resp->message ); + my $content = $resp->decoded_content; + $self->logger->debug("Content received from $url: $content") if $content; - if ( $response->{_rc} == 200 ) { - $json = eval { from_json( $response->{_content} ) }; + if ( $resp->is_success ) { + $json = eval { from_json($content, { allow_nonref => 1 }) }; if ($@) { # Prepare form params + undef $json; $msg = 'PE' . PE_BAD_DEVOPS_FILE; - $alert = 'alert-danger'; - $json = ''; $self->userLogger->error( "CheckDevOps: bad 'rules.json' file retrieved from $url ($@)" ); @@ -157,8 +154,6 @@ sub run { # Prepare form params $msg = 'PE' . PE_FILENOTFOUND; - $alert = 'alert-danger'; - $json = ''; $self->userLogger->error( "CheckDevOps: Unable to download 'rules.json' file from $url" ); @@ -168,21 +163,21 @@ sub run { # Prepare form params $msg = 'PE' . PE_BADURL; - $alert = 'alert-danger'; - $json = ''; $self->userLogger->error('CheckDevOps: bad provided URL'); } } unless ( $json || $msg ) { - $json = eval { from_json( $req->param('checkDevOpsFile') ) }; + $json = eval { + from_json( $req->param('checkDevOpsFile'), { allow_nonref => 1 } ); + }; if ($@) { # Prepare form params + undef $json; $msg = 'PE' . PE_BAD_DEVOPS_FILE; $alert = 'alert-danger'; - $json = ''; $self->userLogger->error( - "CheckDevOps: bad 'rules.json' file provided ($@)"); + "CheckDevOps: bad provided 'rules.json' file ($@)"); } } diff --git a/lemonldap-ng-portal/t/56-CheckDevOps-with-Download.t b/lemonldap-ng-portal/t/56-CheckDevOps-with-Download.t index a3689856a..222499f49 100644 --- a/lemonldap-ng-portal/t/56-CheckDevOps-with-Download.t +++ b/lemonldap-ng-portal/t/56-CheckDevOps-with-Download.t @@ -25,7 +25,7 @@ my $file = '{ }'; my $client = LLNG::Manager::Test->new( { ini => { - logLevel => 'debug', + logLevel => 'error', authentication => 'Demo', userDB => 'Same', requireToken => 0,