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