RT-Extension-REST2

 view release on metacpan or  search on metacpan

xt/attachments.t  view on Meta::CPAN

use strict;
use warnings;
use lib 't/lib';
use RT::Extension::REST2::Test tests => undef;
use Test::Deep;
use MIME::Base64;

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;

$user->PrincipalObj->GrantRight(Right => 'CreateTicket');
$user->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
$user->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
$user->PrincipalObj->GrantRight(Right => 'ShowTicket');
$user->PrincipalObj->GrantRight(Right => 'ShowTicketComments');

my $ticket = RT::Ticket->new($user);
$ticket->Create(Queue => 'General', Subject => 'hello world');
my $ticket_id = $ticket->id;

my $image_name = 'image.png';
my $image_path = RT::Test::get_relocatable_file($image_name, 'data');
my $image_content;
open my $fh, '<', $image_path or die "Cannot read $image_path: $!\n";
{
    local $/;
    $image_content = <$fh>;
}
close $fh;

$image_content = MIME::Base64::encode_base64($image_content);

# Comment ticket with image and text attachments through JSON Base64
{
    my $payload = {
        Content     => 'Have you seen this <b>image</b>',
        ContentType => 'text/html',
        Subject     => 'HTML comment with PNG image and text file',
        Attachments => [
            {
                FileName => $image_name,
                FileType => 'image/png',
                FileContent => $image_content,
            },
            {
                FileName => 'password',
                FileType => 'text/plain',
                FileContent => MIME::Base64::encode_base64('Hey this is secret!'),
            },
        ],
    };
    my $res = $mech->post_json("$rest_base_path/ticket/$ticket_id/comment",
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 201);
    cmp_deeply($mech->json_response, [re(qr/Comments added|Message recorded/)]);

    my $transaction_id = $ticket->Transactions->Last->id;
    my $attachments = $ticket->Attachments->ItemsArrayRef;

    # 3 attachments + 1 wrapper
    is(scalar(@$attachments), 4);

    # 1st attachment is wrapper
    is($attachments->[0]->TransactionId, $transaction_id);
    is($attachments->[0]->Parent, 0);
    is($attachments->[0]->Subject, 'HTML comment with PNG image and text file');
    ok(!$attachments->[0]->Filename);
    is($attachments->[0]->ContentType, 'multipart/mixed');

    # 2nd attachment is comment's content
    is($attachments->[1]->Parent, $attachments->[0]->id);
    is($attachments->[1]->TransactionId, $transaction_id);
    is($attachments->[1]->ContentType, 'text/html');
    is($attachments->[1]->ContentEncoding, 'none');
    is($attachments->[1]->Content, 'Have you seen this <b>image</b>');
    ok(!$attachments->[1]->Subject);

    # 3rd attachment is image
    my $expected_encoding = $RT::Handle->BinarySafeBLOBs ? 'none' : 'base64';
    is($attachments->[2]->Parent, $attachments->[0]->id);



( run in 0.710 second using v1.01-cache-2.11-cpan-71847e10f99 )