[Bps-public-commit] rt-extension-rest2 branch, add-validation-hook-for-status-updates, updated. 1.09-8-g7095e5a

Dianne Skoll dianne at bestpractical.com
Wed Dec 30 16:36:39 EST 2020


The branch, add-validation-hook-for-status-updates has been updated
       via  7095e5a7599ca6217f30a1ece8cd56356e02e300 (commit)
      from  15e394cc6829207a49782bd0368530893f9448bc (commit)

Summary of changes:
 lib/RT/Extension/REST2/Resource/Ticket.pm |  2 +-
 lib/RT/Extension/REST2/TestValidate.pm    |  4 +---
 xt/ticket_validate.t                      | 21 ++++++++++++++++++---
 3 files changed, 20 insertions(+), 7 deletions(-)

- Log -----------------------------------------------------------------
commit 7095e5a7599ca6217f30a1ece8cd56356e02e300
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Wed Dec 30 16:34:44 2020 -0500

    Fix unit test failures:
    
    1) We can't just override the subroutines because we cannot
       guarantee the order in which modules are "use"d.  So use
       ugly glob assignment to override the validation hooks.
    
    2) The update_record function cannot indicate failure by
       returning a reference to an HTTP code.  It can only return
       an array of (success, "message") indicators.  See the comment
       below in lib/RT/Extension/REST2/Resource/Record/Writable.pm:
    
       # XXX TODO: Figure out how to return success/failure?  Core RT::Record's
       # ->Update will need to be replaced or improved.

diff --git a/lib/RT/Extension/REST2/Resource/Ticket.pm b/lib/RT/Extension/REST2/Resource/Ticket.pm
index 3b390f6..454c340 100644
--- a/lib/RT/Extension/REST2/Resource/Ticket.pm
+++ b/lib/RT/Extension/REST2/Resource/Ticket.pm
@@ -68,7 +68,7 @@ sub update_record
 
     my ($retcode, $msg) = $self->validate_hook_before_update($self->record, $data);
     if ($retcode != 200) {
-        return (\$retcode, $msg);
+        return (0, $msg);
     }
     return $self->_update_record($data);
 }
diff --git a/lib/RT/Extension/REST2/TestValidate.pm b/lib/RT/Extension/REST2/TestValidate.pm
index 4a50164..22957de 100644
--- a/lib/RT/Extension/REST2/TestValidate.pm
+++ b/lib/RT/Extension/REST2/TestValidate.pm
@@ -1,8 +1,6 @@
 # For testing override of validation methods
 
-package RT::Extension::REST2::Resource::Ticket;
-
-no warnings qw(redefine);
+package RT::Extension::REST2::TestValidate;
 
 sub validate_hook_before_create
 {
diff --git a/xt/ticket_validate.t b/xt/ticket_validate.t
index 9551e24..b97a4a0 100644
--- a/xt/ticket_validate.t
+++ b/xt/ticket_validate.t
@@ -3,16 +3,23 @@ use warnings;
 use RT::Extension::REST2::Test tests => undef;
 use Test::Deep;
 
+use RT::Extension::REST2::TestValidate;
+
 my $mech = RT::Extension::REST2::Test->mech;
 
 my $auth = RT::Extension::REST2::Test->authorization_header;
 my $rest_base_path = '/REST/2.0';
 my $user = RT::Extension::REST2::Test->user;
 
-
 diag 'Check validation on create';
 my ($ticket_url, $ticket_id);
 {
+    {
+        no warnings;
+        *RT::Extension::REST2::Resource::Ticket::validate_hook_before_create = *RT::Extension::REST2::TestValidate::validate_hook_before_create;
+        *RT::Extension::REST2::Resource::Ticket::validate_hook_before_update = *RT::Extension::REST2::TestValidate::validate_hook_before_update;
+    }
+
     $user->PrincipalObj->GrantRight( Right => 'CreateTicket' );
     my $payload = {
         Subject => 'Ticket creation using REST',
@@ -45,6 +52,12 @@ my ($ticket_url, $ticket_id);
 
 # Ticket Update
 {
+    {
+        no warnings;
+        *RT::Extension::REST2::Resource::Ticket::validate_hook_before_create = *RT::Extension::REST2::TestValidate::validate_hook_before_create;
+        *RT::Extension::REST2::Resource::Ticket::validate_hook_before_update = *RT::Extension::REST2::TestValidate::validate_hook_before_update;
+    }
+
     my $payload = {
         Subject  => 'Ticket update using REST',
         Priority => 42,
@@ -57,8 +70,10 @@ my ($ticket_url, $ticket_id);
     my $res = $mech->put_json($ticket_url,
         $payload,
         'Authorization' => $auth,
-    );
-    is($res->code, 400, 'Validation returned 400');
+        );
+    is($res->code, 200, 'Validation returned 200');
+    cmp_deeply($mech->json_response, [0, 'Bad data'], "Validation error is indicated by JSON response");
+
 }
 
 

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list