[Rt-commit] rt branch, 5.0/rest2-support-attachments-when-creating-ticket, created. rt-5.0.0-53-ge4efdd1e67

Dianne Skoll dianne at bestpractical.com
Wed Oct 21 11:15:58 EDT 2020


The branch, 5.0/rest2-support-attachments-when-creating-ticket has been created
        at  e4efdd1e67288f6152e9076530fdf3af6104c5c7 (commit)

- Log -----------------------------------------------------------------
commit e4efdd1e67288f6152e9076530fdf3af6104c5c7
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Wed Oct 21 11:14:44 2020 -0400

    Allow attachments to be added when a ticket is created.
    
    The attachments are supplied in an Attachments array, each element
    of which is a three-element hash containing FileName, FileType and
    FileContent members.  FileContent is the base64-encoded content.

diff --git a/lib/RT/REST2/Resource/Ticket.pm b/lib/RT/REST2/Resource/Ticket.pm
index 02a020d02c..61ebbc6a7d 100644
--- a/lib/RT/REST2/Resource/Ticket.pm
+++ b/lib/RT/REST2/Resource/Ticket.pm
@@ -98,8 +98,23 @@ sub create_record {
             Body      => delete $data->{Content},
             Type      => delete $data->{ContentType} || 'text/plain',
         );
+        if ( defined $data->{Attachments} ) {
+            return (\400, "Attachments must be an array") unless ref($data->{Attachments}) eq 'ARRAY';
+            foreach my $attachment (@{$data->{Attachments}}) {
+                return (\400, "Each element of Attachments must be a hash") unless ref($attachment) eq 'HASH';
+                foreach my $field (qw(FileName FileType FileContent)) {
+                    return (\400, "Field $field is required for each attachment in Attachments") unless $attachment->{$field};
+                }
+                $data->{MIMEObj}->attach(
+                    Type     => $attachment->{FileType},
+                    Filename => $attachment->{FileName},
+                    Data     => MIME::Base64::decode_base64($attachment->{FileContent}));
+            }
+        }
     }
 
+    delete $data->{Attachments};
+
     my ($ok, $txn, $msg) = $self->_create_record($data);
     return ($ok, $msg);
 }
diff --git a/lib/RT/REST2/Util.pm b/lib/RT/REST2/Util.pm
index ccfca41d22..a7c2a6c05a 100644
--- a/lib/RT/REST2/Util.pm
+++ b/lib/RT/REST2/Util.pm
@@ -217,6 +217,7 @@ sub deserialize_record {
     # Sanitize input for the Perl API
     for my $field (sort keys %$data) {
         next if $field eq 'CustomFields';
+        next if $field eq 'Attachments';
 
         my $value = $data->{$field};
         next unless ref $value;
diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 837efd2992..e716fe3c46 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -2,6 +2,8 @@ use strict;
 use warnings;
 use RT::Test::REST2 tests => undef;
 use Test::Deep;
+use MIME::Base64;
+use Encode qw(decode encode);
 
 # Test using integer priorities
 RT->Config->Set(EnablePriorityAsString => 0);
@@ -505,4 +507,68 @@ my ($ticket_url, $ticket_id);
     like($third_ticket->{_url}, qr{$rest_base_path/ticket/$ticket3_id$});
 }
 
+# Ticket Creation - with attachments
+{
+    my $payload = {
+        Subject => 'Ticket creation using REST, with attachments.',
+        Queue   => 'General',
+        Content => 'Testing ticket creation with attachments using REST API.',
+        Attachments => [
+            {   FileName    => 'plain.txt',
+                FileType    => 'text/plain',
+                FileContent => MIME::Base64::encode_base64('Hello, World!')
+            },
+            {   FileName    => 'html.htm',
+                FileType    => 'text/html',
+                FileContent => MIME::Base64::encode_base64(
+                    encode( 'UTF-8', "<p><i>Easy</i> as \x{03c0}</p>" )
+                ),
+            },
+            {   FileName => 'moon.png',
+                FileType => 'image/png',
+                FileContent =>
+                    'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAGQAAABkABchkaRQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAADfSURBVDiNndM9TsNAFATgzy5yjZSAE85JBygETgENUPF3iBCitHAFQkcIhZ/Ryn9gRlrZmp2Z3ef3TBOHOMULPrDBMrhpi/4HI5xjix2+4nmJRbx/Yh7ahvkpRPVV4QDXwT3UQy46zGkAZDgK/iytefvHgCrkJsqZUH6cLnNbABSxd5Jhhf1IbkMXv8Qux7hH1Ic1xvk/jBWy6gavumvtwx7ectwZXkKh7MA95XgObeOtpI2U4zl0kGbpxgiPvwQUcXLrKFchc82f6Ur0PK49azOnmOI4TBu84zm4SV38DeIVYkrYJyNbAAAAAElFTkSuQmCC'
+            },
+        ]
+    };
+
+    my $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+)] );
+}
+
+# We have the attachments added above
+{
+    my $ticket = RT::Ticket->new($user);
+    $ticket->Load($ticket_id);
+    my $transaction_id = $ticket->Transactions->Last->id;
+    my $attachments    = $ticket->Attachments->ItemsArrayRef;
+
+    # The 5 attachments are:
+    # 1) Top-level multipart
+    # 2) Top-level ticket content
+    # 3-5) The three attachments added in the Attachments array
+    is( scalar(@$attachments), 5 );
+
+    is( $attachments->[0]->ContentType, 'multipart/mixed' );
+
+    is( $attachments->[1]->ContentType, 'text/plain' );
+    is( $attachments->[1]->Content,
+        'Testing ticket creation with attachments using REST API.' );
+
+    is( $attachments->[2]->ContentType, 'text/plain' );
+    is( $attachments->[2]->Filename,    'plain.txt' );
+    is( $attachments->[2]->Content,     'Hello, World!' );
+
+    is( $attachments->[3]->ContentType, 'text/html' );
+    is( $attachments->[3]->Filename,    'html.htm' );
+    is( $attachments->[3]->Content,     "<p><i>Easy</i> as \x{03c0}</p>" );
+
+    is( $attachments->[4]->ContentType, 'image/png' );
+    is( $attachments->[4]->Filename,    'moon.png' );
+    like( $attachments->[4]->Content, qr/IHDR/, "Looks like a PNG image" );
+}
+
 done_testing;

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


More information about the rt-commit mailing list