[Bps-public-commit] rt-extension-rest2 branch, add-validation-hook-for-status-updates, created. 1.09-7-g15e394c

Jim Brandt jbrandt at bestpractical.com
Wed Dec 23 16:14:40 EST 2020


The branch, add-validation-hook-for-status-updates has been created
        at  15e394cc6829207a49782bd0368530893f9448bc (commit)

- Log -----------------------------------------------------------------
commit 16ac55a3f5e3d432c2874a8c8ff5f199fbd4457b
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Tue Dec 8 15:49:35 2020 -0500

    Stub out validation hooks for ticket creation and ticket update.

diff --git a/lib/RT/Extension/REST2/Resource/Message.pm b/lib/RT/Extension/REST2/Resource/Message.pm
index 9b05b00..ab241ad 100644
--- a/lib/RT/Extension/REST2/Resource/Message.pm
+++ b/lib/RT/Extension/REST2/Resource/Message.pm
@@ -116,6 +116,11 @@ sub add_message {
         Subject   => $args{Subject},
     );
 
+    my ($http_code, $errmsg) = RT::Extension::REST2::Resource::Ticket->validate_hook_before_update($self->record, \%args);
+    if ($http_code != 200) {
+        return(\$http_code, $errmsg);
+    }
+
     # Process attachments
     foreach my $attachment (@{$args{Attachments}}) {
         $MIME->attach(
diff --git a/lib/RT/Extension/REST2/Resource/Ticket.pm b/lib/RT/Extension/REST2/Resource/Ticket.pm
index f8cf37a..3b390f6 100644
--- a/lib/RT/Extension/REST2/Resource/Ticket.pm
+++ b/lib/RT/Extension/REST2/Resource/Ticket.pm
@@ -12,7 +12,8 @@ with (
         => { -alias => { hypermedia_links => '_default_hypermedia_links' } },
     'RT::Extension::REST2::Resource::Record::Deletable',
     'RT::Extension::REST2::Resource::Record::Writable'
-        => { -alias => { create_record => '_create_record' } },
+        => { -alias => { create_record => '_create_record',
+                         update_record => '_update_record'} },
 );
 
 sub dispatch_rules {
@@ -52,10 +53,50 @@ sub create_record {
         );
     }
 
+    my ($retcode, $mesg) = $self->validate_hook_before_create($queue, $data);
+    if ($retcode != 200) {
+        return (\$retcode, $mesg);
+    }
+
     my ($ok, $txn, $msg) = $self->_create_record($data);
     return ($ok, $msg);
 }
 
+sub update_record
+{
+    my ($self, $data) = @_;
+
+    my ($retcode, $msg) = $self->validate_hook_before_update($self->record, $data);
+    if ($retcode != 200) {
+        return (\$retcode, $msg);
+    }
+    return $self->_update_record($data);
+}
+
+# This function is called just before ticket creation; it is passed
+# the REST data and the queue in which the ticket should be created.
+# It should return (200, '') if all is OK, or (4xx, $msg) if something
+# is wrong and the creation should be aborted.
+
+sub validate_hook_before_create
+{
+    my ($self, $queue, $data) = @_;
+
+    return (200, '');
+}
+
+# This function is called just before ticket update; it is passed the
+# REST data and the ticket being updated.  It should return (200, '')
+# if all is OK, or (4xx, $msg) if something is wrong and the creation
+# should be aborted.
+
+sub validate_hook_before_update
+{
+    my ($self, $ticket, $data) = @_;
+
+    return (200, '');
+}
+
 sub forbidden {
     my $self = shift;
     return 0 unless $self->record->id;

commit 15e394cc6829207a49782bd0368530893f9448bc
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Wed Dec 23 16:13:31 2020 -0500

    Add tests for validation hooks

diff --git a/lib/RT/Extension/REST2/Test.pm.in b/lib/RT/Extension/REST2/Test.pm.in
index 3ff37c7..e81ed6b 100644
--- a/lib/RT/Extension/REST2/Test.pm.in
+++ b/lib/RT/Extension/REST2/Test.pm.in
@@ -24,6 +24,11 @@ sub import {
     $class->export_to_level(1);
 
     require RT::Extension::REST2;
+
+    # Explicitly require here to load the base subroutines
+    # before overriding
+    require RT::Extension::REST2::Resource::Ticket;
+    require RT::Extension::REST2::TestValidate;
 }
 
 sub mech { RT::Extension::REST2::Test::Mechanize->new }
@@ -117,4 +122,5 @@ sub mech { RT::Extension::REST2::Test::Mechanize->new }
     }
 }
 
+
 1;
diff --git a/lib/RT/Extension/REST2/TestValidate.pm b/lib/RT/Extension/REST2/TestValidate.pm
new file mode 100644
index 0000000..4a50164
--- /dev/null
+++ b/lib/RT/Extension/REST2/TestValidate.pm
@@ -0,0 +1,29 @@
+# For testing override of validation methods
+
+package RT::Extension::REST2::Resource::Ticket;
+
+no warnings qw(redefine);
+
+sub validate_hook_before_create
+{
+    my ($self, $queue, $data) = @_;
+
+    if ( $data->{'VALIDATE'} and $data->{'VALIDATE'} eq 'BAD_CREATE!' ) {
+        return (400, 'Bad data');
+    }
+
+    return (200, '');
+}
+
+sub validate_hook_before_update
+{
+    my ($self, $ticket, $data) = @_;
+
+    if ( $data->{'VALIDATE'} and $data->{'VALIDATE'} eq 'BAD_UPDATE!' ) {
+        return (400, 'Bad data');
+    }
+
+    return (200, '');
+}
+
+1;
diff --git a/xt/ticket_validate.t b/xt/ticket_validate.t
new file mode 100644
index 0000000..9551e24
--- /dev/null
+++ b/xt/ticket_validate.t
@@ -0,0 +1,65 @@
+use strict;
+use warnings;
+use RT::Extension::REST2::Test tests => undef;
+use Test::Deep;
+
+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);
+{
+    $user->PrincipalObj->GrantRight( Right => 'CreateTicket' );
+    my $payload = {
+        Subject => 'Ticket creation using REST',
+        Queue   => 'General',
+        Content => 'Testing ticket creation using REST API.',
+        VALIDATE => 'BAD_CREATE!',
+    };
+
+    my $res = $mech->post_json("$rest_base_path/ticket",
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 400, 'Validation returned 400');
+
+    # Do a successful create for later test
+    $payload = {
+        Subject => 'Ticket creation using REST',
+        Queue   => 'General',
+        Content => 'Testing ticket creation using REST API.',
+    };
+
+    $res = $mech->post_json("$rest_base_path/ticket",
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 201);
+    ok($ticket_url = $res->header('location'));
+    ok(($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)]);
+}
+
+# Ticket Update
+{
+    my $payload = {
+        Subject  => 'Ticket update using REST',
+        Priority => 42,
+        VALIDATE => 'BAD_UPDATE!',
+    };
+
+    $user->PrincipalObj->GrantRight( Right => 'ShowTicket' );
+    $user->PrincipalObj->GrantRight( Right => 'ModifyTicket' );
+
+    my $res = $mech->put_json($ticket_url,
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 400, 'Validation returned 400');
+}
+
+
+done_testing;

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


More information about the Bps-public-commit mailing list