RT 4.0.13
[freeside.git] / rt / t / mail / gnupg-realmail.t
1 use strict;
2 use warnings;
3
4 use RT::Test::GnuPG tests => 198, gnupg_options => { passphrase => 'rt-test' };
5
6 use Digest::MD5 qw(md5_hex);
7
8 RT::Test->import_gnupg_key('rt-recipient@example.com');
9 RT::Test->import_gnupg_key('rt-test@example.com', 'public');
10 RT::Test->trust_gnupg_key('rt-test@example.com');
11
12 my ($baseurl, $m) = RT::Test->started_ok;
13 ok $m->login, 'we did log in';
14 $m->get_ok( '/Admin/Queues/');
15 $m->follow_link_ok( {text => 'General'} );
16 $m->submit_form( form_number => 3,
17          fields      => { CorrespondAddress => 'rt-recipient@example.com' } );
18 $m->content_like(qr/rt-recipient\@example.com.* - never/, 'has key info.');
19
20 my $eid = 0;
21 for my $usage (qw/signed encrypted signed&encrypted/) {
22     for my $format (qw/MIME inline/) {
23         for my $attachment (qw/plain text-attachment binary-attachment/) {
24             ++$eid;
25             diag "Email $eid: $usage, $attachment email with $format format";
26             eval { email_ok($eid, $usage, $format, $attachment) };
27         }
28     }
29 }
30
31 $eid = 18;
32 {
33     my ($usage, $format, $attachment) = ('signed', 'inline', 'plain');
34     ++$eid;
35     diag "Email $eid: $usage, $attachment email with $format format";
36     eval { email_ok($eid, $usage, $format, $attachment) };
37 }
38
39 sub email_ok {
40     my ($eid, $usage, $format, $attachment) = @_;
41     diag "email_ok $eid: $usage, $format, $attachment";
42
43     my $emaildatadir = RT::Test::get_relocatable_dir(File::Spec->updir(),
44         qw(data gnupg emails));
45     my ($file) = glob("$emaildatadir/$eid-*");
46     my $mail = RT::Test->file_content($file);
47
48     my ($status, $id) = RT::Test->send_via_mailgate($mail);
49     is ($status >> 8, 0, "$eid: The mail gateway exited normally");
50     ok ($id, "$eid: got id of a newly created ticket - $id");
51
52     my $tick = RT::Ticket->new( RT->SystemUser );
53     $tick->Load( $id );
54     ok ($tick->id, "$eid: loaded ticket #$id");
55
56     is ($tick->Subject,
57         "Test Email ID:$eid",
58         "$eid: Created the ticket"
59     );
60
61     my $txn = $tick->Transactions->First;
62     my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
63
64     is( $msg->GetHeader('X-RT-Privacy'),
65         'PGP',
66         "$eid: recorded incoming mail that is encrypted"
67     );
68
69     if ($usage =~ /encrypted/) {
70         if ( $format eq 'MIME' || $attachment eq 'plain' ) {
71             is( $msg->GetHeader('X-RT-Incoming-Encryption'),
72                 'Success',
73                 "$eid: recorded incoming mail that is encrypted"
74             );
75         } else {
76             is( $attachments[0]->GetHeader('X-RT-Incoming-Encryption'),
77                 'Success',
78                 "$eid: recorded incoming mail that is encrypted"
79             );
80             is( $attachments[1]->GetHeader('X-RT-Incoming-Encryption'),
81                 'Success',
82                 "$eid: recorded incoming mail that is encrypted"
83             );
84         }
85         like( $attachments[0]->Content, qr/ID:$eid/,
86                 "$eid: incoming mail did NOT have original body"
87         );
88     }
89     else {
90         is( $msg->GetHeader('X-RT-Incoming-Encryption'),
91             'Not encrypted',
92             "$eid: recorded incoming mail that is not encrypted"
93         );
94         like( $msg->Content || $attachments[0]->Content, qr/ID:$eid/,
95               "$eid: got original content"
96         );
97     }
98
99     if ($usage =~ /signed/) {
100 # XXX: FIXME: TODO: 6-signed-inline-with-attachment should be re-generated as it's actually RFC format
101         if ( $format eq 'MIME' || $attachment eq 'plain' || ($format eq 'inline' && $attachment =~ /binary/ && $usage !~ /encrypted/) ) {
102             is( $msg->GetHeader('X-RT-Incoming-Signature'),
103                 'RT Test <rt-test@example.com>',
104                 "$eid: recorded incoming mail that is signed"
105             );
106         }
107         else {
108             is( $attachments[0]->GetHeader('X-RT-Incoming-Signature'),
109                 'RT Test <rt-test@example.com>',
110                 "$eid: recorded incoming mail that is signed"
111             );
112             is( $attachments[1]->GetHeader('X-RT-Incoming-Signature'),
113                 'RT Test <rt-test@example.com>',
114                 "$eid: recorded incoming mail that is signed"
115             );
116         }
117     }
118     else {
119         is( $msg->GetHeader('X-RT-Incoming-Signature'),
120             undef,
121             "$eid: recorded incoming mail that is not signed"
122         );
123     }
124
125     if ($attachment =~ /attachment/) {
126         # signed messages should sign each attachment too
127         if ($usage =~ /signed/) {
128             my $sig = pop @attachments;
129             ok ($sig->Id, "$eid: loaded attachment.sig object");
130             my $acontent = $sig->Content;
131         }
132
133         my ($a) = grep $_->Filename, @attachments;
134         ok ($a && $a->Id, "$eid: found attachment with filename");
135
136         my $acontent = $a->Content;
137         if ($attachment =~ /binary/)
138         {
139             is(md5_hex($acontent), '1e35f1aa90c98ca2bab85c26ae3e1ba7', "$eid: The binary attachment's md5sum matches");
140         }
141         else
142         {
143             like($acontent, qr/zanzibar/, "$eid: The attachment isn't screwed up in the database.");
144         }
145
146     }
147
148     return 0;
149 }
150