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