RT 4.2.11, ticket#13852
[freeside.git] / rt / t / mail / smime / realmail.t
1 use strict;
2 use warnings;
3
4 use RT::Test::SMIME tests => undef;
5 use Digest::MD5 qw(md5_hex);
6
7 my $test = 'RT::Test::SMIME';
8 my $mails = $test->mail_set_path;
9
10 RT->Config->Get('SMIME')->{AcceptUntrustedCAs} = 1;
11
12 RT::Test::SMIME->import_key('root@example.com');
13 RT::Test::SMIME->import_key('sender@example.com');
14
15 my ($baseurl, $m) = RT::Test->started_ok;
16 ok $m->login, 'we did log in';
17 $m->get_ok( '/Admin/Queues/');
18 $m->follow_link_ok( {text => 'General'} );
19 $m->submit_form( form_number => 3,
20          fields      => { CorrespondAddress => 'root@example.com' } );
21
22 diag "load Everyone group" if $ENV{'TEST_VERBOSE'};
23 my $everyone;
24 {
25     $everyone = RT::Group->new( $RT::SystemUser );
26     $everyone->LoadSystemInternalGroup('Everyone');
27     ok $everyone->id, "loaded 'everyone' group";
28 }
29
30 RT::Test->set_rights(
31     Principal => $everyone,
32     Right => ['CreateTicket'],
33 );
34
35
36 my $eid = 0;
37 for my $usage (qw/signed encrypted signed&encrypted/) {
38     for my $attachment (qw/plain text-attachment binary-attachment/) {
39         ++$eid;
40         diag "Email $eid: $usage, $attachment email" if $ENV{TEST_VERBOSE};
41         eval { email_ok($eid, $usage, $attachment) };
42     }
43 }
44
45 undef $m;
46 done_testing;
47
48 sub email_ok {
49     my ($eid, $usage, $attachment) = @_;
50     diag "email_ok $eid: $usage, $attachment" if $ENV{'TEST_VERBOSE'};
51
52     my ($file) = glob("$mails/$eid-*");
53     my $mail = RT::Test->file_content($file);
54
55     my ($status, $id) = RT::Test->send_via_mailgate($mail);
56     is ($status >> 8, 0, "$eid: The mail gateway exited normally");
57     ok ($id, "$eid: got id of a newly created ticket - $id");
58
59     my $tick = RT::Ticket->new( $RT::SystemUser );
60     $tick->Load( $id );
61     ok ($tick->id, "$eid: loaded ticket #$id");
62
63     is ($tick->Subject,
64         "Test Email ID:$eid",
65         "$eid: Created the ticket"
66     );
67
68     my $txn = $tick->Transactions->First;
69     my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
70
71     is( $msg->GetHeader('X-RT-Privacy'),
72         'SMIME',
73         "$eid: recorded incoming mail that is secured"
74     );
75
76     if ($usage =~ /encrypted/) {
77         is( $msg->GetHeader('X-RT-Incoming-Encryption'),
78             'Success',
79             "$eid: recorded incoming mail that is encrypted"
80         );
81         like( $attachments[0]->Content, qr/ID:$eid/,
82                 "$eid: incoming mail did NOT have original body"
83         );
84     }
85     else {
86         is( $msg->GetHeader('X-RT-Incoming-Encryption'),
87             'Not encrypted',
88             "$eid: recorded incoming mail that is not encrypted"
89         );
90         like( $msg->Content || $attachments[0]->Content, qr/ID:$eid/,
91             "$eid: got original content"
92         );
93     }
94
95     if ($usage =~ /signed/) {
96         is( $msg->GetHeader('X-RT-Incoming-Signature'),
97             '"sender" <sender@example.com>',
98             "$eid: recorded incoming mail that is signed"
99         );
100     }
101     else {
102         is( $msg->GetHeader('X-RT-Incoming-Signature'),
103             undef,
104             "$eid: recorded incoming mail that is not signed"
105         );
106     }
107
108     if ($attachment =~ /attachment/) {
109         my ($a) = grep $_->Filename, @attachments;
110         ok ($a && $a->Id, "$eid: found attachment with filename");
111
112         my $acontent = $a->Content;
113         if ($attachment =~ /binary/)
114         {
115             is(md5_hex($acontent), '1e35f1aa90c98ca2bab85c26ae3e1ba7', "$eid: The binary attachment's md5sum matches");
116         }
117         else
118         {
119             like($acontent, qr/zanzibar/, "$eid: The attachment isn't screwed up in the database.");
120         }
121     }
122
123     return 0;
124 }
125