542
544
like($third_ticket->{_url}, qr{$rest_base_path/ticket/$ticket3_id$});
547
# Ticket Creation - with attachments
550
Subject => 'Ticket creation using REST, with attachments.',
552
Content => 'Testing ticket creation with attachments using REST API.',
554
{ FileName => 'plain.txt',
555
FileType => 'text/plain',
556
FileContent => MIME::Base64::encode_base64('Hello, World!')
558
{ FileName => 'html.htm',
559
FileType => 'text/html',
560
FileContent => MIME::Base64::encode_base64(
561
encode( 'UTF-8', "<p><i>Easy</i> as \x{03c0}</p>" )
564
{ FileName => 'moon.png',
565
FileType => 'image/png',
567
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAGQAAABkABchkaRQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAADfSURBVDiNndM9TsNAFATgzy5yjZSAE85JBygETgENUPF3iBCitHAFQkcIhZ/Ryn9gRlrZmp2Z3ef3TBOHOMULPrDBMrhpi/4HI5xjix2+4nmJRbx/Yh7ahvkpRPVV4QDXwT3UQy46zGkAZDgK/iytefvHgCrkJsqZUH6cLnNbABSxd5Jhhf1IbkMXv8Qux7hH1Ic1xvk/jBWy6gavumvtwx7ectwZXkKh7MA95XgObeOtpI2U4zl0kGbpxgiPvwQUcXLrKFchc82f6Ur0PK49azOnmOI4TBu84zm4SV38DeIVYkrYJyNbAAAAAElFTkSuQmCC'
572
my $res = $mech->post_json( "$rest_base_path/ticket", $payload,
573
'Authorization' => $auth, );
574
is( $res->code, 201 );
575
ok( $ticket_url = $res->header('location') );
576
ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
579
# We have the attachments added above
581
my $ticket = RT::Ticket->new($user);
582
$ticket->Load($ticket_id);
583
my $transaction_id = $ticket->Transactions->Last->id;
584
my $attachments = $ticket->Attachments->ItemsArrayRef;
586
# The 5 attachments are:
587
# 1) Top-level multipart
588
# 2) Top-level ticket content
589
# 3-5) The three attachments added in the Attachments array
590
is( scalar(@$attachments), 5 );
592
is( $attachments->[0]->ContentType, 'multipart/mixed' );
594
is( $attachments->[1]->ContentType, 'text/plain' );
595
is( $attachments->[1]->Content,
596
'Testing ticket creation with attachments using REST API.' );
598
is( $attachments->[2]->ContentType, 'text/plain' );
599
is( $attachments->[2]->Filename, 'plain.txt' );
600
is( $attachments->[2]->Content, 'Hello, World!' );
602
is( $attachments->[3]->ContentType, 'text/html' );
603
is( $attachments->[3]->Filename, 'html.htm' );
604
is( $attachments->[3]->Content, "<p><i>Easy</i> as \x{03c0}</p>" );
606
is( $attachments->[4]->ContentType, 'image/png' );
607
is( $attachments->[4]->Filename, 'moon.png' );
608
like( $attachments->[4]->Content, qr/IHDR/, "Looks like a PNG image" );
611
# Ticket Creation - with attachments, using multipart/form-data
612
my $json = JSON->new->utf8;
614
my $plain_name = 'plain.txt';
615
my $plain_path = RT::Test::get_relocatable_file($plain_name, 'data');
616
my $html_name = 'html.htm';
617
my $html_path = RT::Test::get_relocatable_file($html_name, 'data');
618
my $img_name = 'image.png';
619
my $img_path = RT::Test::get_relocatable_file($img_name, 'data');
622
Subject => 'Ticket creation using REST, multipart/form-data, with attachments.',
624
Content => 'Testing ticket creation, multipart/form-data, with attachments using REST API.',
627
my $res = $mech->post( "$rest_base_path/ticket", $payload,
628
'Authorization' => $auth,
629
'Content_Type' => 'form-data',
631
'JSON' => $json->encode($payload),
632
'Attachments' => [$plain_path, $plain_name, 'text/plain'],
633
'Attachments' => [$html_path, $html_name, 'text/html' ],
634
'Attachments' => [$img_path, $img_name, 'image/png' ]
636
is( $res->code, 201 );
637
ok( $ticket_url = $res->header('location') );
638
ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
641
# Validate that the ticket was created correctly
644
my $ticket = RT::Ticket->new($user);
645
$ticket->Load($ticket_id);
646
my $transaction_id = $ticket->Transactions->Last->id;
647
my $attachments = $ticket->Attachments->ItemsArrayRef;
649
# The 5 attachments are:
650
# 1) Top-level multipart
651
# 2) Top-level ticket content
652
# 3-5) The three attachments added in the Attachments array
653
is( scalar(@$attachments), 5 );
655
is( $attachments->[0]->ContentType, 'multipart/mixed' );
657
is( $attachments->[1]->ContentType, 'text/plain' );
658
is( $attachments->[1]->Content,
659
'Testing ticket creation, multipart/form-data, with attachments using REST API.' );
661
is( $attachments->[2]->ContentType, 'text/plain' );
662
is( $attachments->[2]->Filename, 'plain.txt' );
663
is( $attachments->[2]->Content, "Hello, World!\n" );
665
is( $attachments->[3]->ContentType, 'text/html' );
666
is( $attachments->[3]->Filename, 'html.htm' );
667
is( $attachments->[3]->Content, "<p><i>Easy</i> as \x{03c0}</p>\n" );
669
is( $attachments->[4]->ContentType, 'image/png' );
670
is( $attachments->[4]->Filename, 'image.png' );
671
like( $attachments->[4]->Content, qr/IHDR/, "Looks like a PNG image" );
674
# Ticket Creation - with custom field uploads, using multipart/form-data
676
my $image_cf = RT::Test->load_or_create_custom_field( Name => 'Image', Queue => 'General', Type => 'Image' );
677
$user->PrincipalObj->GrantRight( Right => $_ ) for qw/SeeCustomField ModifyCustomField/;
679
my $img_name = 'image.png';
680
my $img_path = RT::Test::get_relocatable_file( $img_name, 'data' );
681
my $img_content = do {
683
open my $fh, '<', $img_path or die $!;
688
Subject => 'Ticket creation using REST, multipart/form-data, with custom field uploads',
690
Content => 'Testing ticket creation, multipart/form-data, with custom field uploads using REST API.',
691
CustomFields => { Image => { UploadField => 'image' } },
694
my $res = $mech->post(
695
"$rest_base_path/ticket",
697
'Authorization' => $auth,
698
'Content_Type' => 'form-data',
700
'JSON' => $json->encode($payload),
701
'image' => [ $img_path, $img_name, 'image/png' ],
704
is( $res->code, 201 );
705
ok( $ticket_url = $res->header('location') );
706
ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
708
my $ticket = RT::Ticket->new($user);
709
$ticket->Load($ticket_id);
710
my $value = $ticket->CustomFieldValues( $image_cf )->First;
711
is( $value->Content, 'image.png', 'image file name');
712
is( $value->LargeContent, $img_content, 'image file content');