~bestpractical/rt/5.0

« back to all changes in this revision

Viewing changes to t/rest2/tickets.t

  • Committer: Jim Brandt
  • Date: 2021-01-08 16:49:02 UTC
  • mfrom: (8586.1.4)
  • Revision ID: git-v1:2bbdbb13d79b13229adcd093d1bb66bbb24e0d74
Merge branch '5.0/rest2-support-attachments-when-creating-ticket' into 5.0-trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
use Test::Deep;
5
5
use MIME::Base64;
6
6
 
 
7
use Encode qw(decode encode);
 
8
 
7
9
# Test using integer priorities
8
10
RT->Config->Set(EnablePriorityAsString => 0);
9
11
my $mech = RT::Test::REST2->mech;
542
544
    like($third_ticket->{_url}, qr{$rest_base_path/ticket/$ticket3_id$});
543
545
}
544
546
 
 
547
# Ticket Creation - with attachments
 
548
{
 
549
    my $payload = {
 
550
        Subject => 'Ticket creation using REST, with attachments.',
 
551
        Queue   => 'General',
 
552
        Content => 'Testing ticket creation with attachments using REST API.',
 
553
        Attachments => [
 
554
            {   FileName    => 'plain.txt',
 
555
                FileType    => 'text/plain',
 
556
                FileContent => MIME::Base64::encode_base64('Hello, World!')
 
557
            },
 
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>" )
 
562
                ),
 
563
            },
 
564
            {   FileName => 'moon.png',
 
565
                FileType => 'image/png',
 
566
                FileContent =>
 
567
                    'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAGQAAABkABchkaRQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAADfSURBVDiNndM9TsNAFATgzy5yjZSAE85JBygETgENUPF3iBCitHAFQkcIhZ/Ryn9gRlrZmp2Z3ef3TBOHOMULPrDBMrhpi/4HI5xjix2+4nmJRbx/Yh7ahvkpRPVV4QDXwT3UQy46zGkAZDgK/iytefvHgCrkJsqZUH6cLnNbABSxd5Jhhf1IbkMXv8Qux7hH1Ic1xvk/jBWy6gavumvtwx7ectwZXkKh7MA95XgObeOtpI2U4zl0kGbpxgiPvwQUcXLrKFchc82f6Ur0PK49azOnmOI4TBu84zm4SV38DeIVYkrYJyNbAAAAAElFTkSuQmCC'
 
568
            },
 
569
        ]
 
570
    };
 
571
 
 
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+)] );
 
577
}
 
578
 
 
579
# We have the attachments added above
 
580
{
 
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;
 
585
 
 
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 );
 
591
 
 
592
    is( $attachments->[0]->ContentType, 'multipart/mixed' );
 
593
 
 
594
    is( $attachments->[1]->ContentType, 'text/plain' );
 
595
    is( $attachments->[1]->Content,
 
596
        'Testing ticket creation with attachments using REST API.' );
 
597
 
 
598
    is( $attachments->[2]->ContentType, 'text/plain' );
 
599
    is( $attachments->[2]->Filename,    'plain.txt' );
 
600
    is( $attachments->[2]->Content,     'Hello, World!' );
 
601
 
 
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>" );
 
605
 
 
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" );
 
609
}
 
610
 
 
611
# Ticket Creation - with attachments, using multipart/form-data
 
612
my $json = JSON->new->utf8;
 
613
{
 
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');
 
620
 
 
621
    my $payload = {
 
622
        Subject => 'Ticket creation using REST, multipart/form-data, with attachments.',
 
623
        Queue   => 'General',
 
624
        Content => 'Testing ticket creation, multipart/form-data, with attachments using REST API.',
 
625
    };
 
626
 
 
627
    my $res = $mech->post( "$rest_base_path/ticket", $payload,
 
628
                           'Authorization' => $auth,
 
629
                           'Content_Type' => 'form-data',
 
630
                           'Content' => [
 
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' ]
 
635
                           ]);
 
636
    is( $res->code, 201 );
 
637
    ok( $ticket_url = $res->header('location') );
 
638
    ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
 
639
}
 
640
 
 
641
# Validate that the ticket was created correctly
 
642
 
 
643
{
 
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;
 
648
 
 
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 );
 
654
 
 
655
    is( $attachments->[0]->ContentType, 'multipart/mixed' );
 
656
 
 
657
    is( $attachments->[1]->ContentType, 'text/plain' );
 
658
    is( $attachments->[1]->Content,
 
659
        'Testing ticket creation, multipart/form-data, with attachments using REST API.' );
 
660
 
 
661
    is( $attachments->[2]->ContentType, 'text/plain' );
 
662
    is( $attachments->[2]->Filename,    'plain.txt' );
 
663
    is( $attachments->[2]->Content,     "Hello, World!\n" );
 
664
 
 
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" );
 
668
 
 
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" );
 
672
}
 
673
 
 
674
# Ticket Creation - with custom field uploads, using multipart/form-data
 
675
{
 
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/;
 
678
 
 
679
    my $img_name = 'image.png';
 
680
    my $img_path = RT::Test::get_relocatable_file( $img_name, 'data' );
 
681
    my $img_content = do {
 
682
        local $/;
 
683
        open my $fh, '<', $img_path or die $!;
 
684
        <$fh>;
 
685
    };
 
686
 
 
687
    my $payload  = {
 
688
        Subject      => 'Ticket creation using REST, multipart/form-data, with custom field uploads',
 
689
        Queue        => 'General',
 
690
        Content      => 'Testing ticket creation, multipart/form-data, with custom field uploads using REST API.',
 
691
        CustomFields => { Image => { UploadField => 'image' } },
 
692
    };
 
693
 
 
694
    my $res = $mech->post(
 
695
        "$rest_base_path/ticket",
 
696
        $payload,
 
697
        'Authorization' => $auth,
 
698
        'Content_Type'  => 'form-data',
 
699
        'Content'       => [
 
700
            'JSON'  => $json->encode($payload),
 
701
            'image' => [ $img_path, $img_name, 'image/png' ],
 
702
        ],
 
703
    );
 
704
    is( $res->code, 201 );
 
705
    ok( $ticket_url = $res->header('location') );
 
706
    ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
 
707
 
 
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');
 
713
}
 
714
 
545
715
done_testing;