import rt 3.8.7
[freeside.git] / rt / t / mail / gnupg-realmail.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 197;
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 diag "load Everyone group" if $ENV{'TEST_VERBOSE'};
41 my $everyone;
42 {
43     $everyone = RT::Group->new( $RT::SystemUser );
44     $everyone->LoadSystemInternalGroup('Everyone');
45     ok $everyone->id, "loaded 'everyone' group";
46 }
47
48 RT::Test->set_rights(
49     Principal => $everyone,
50     Right => ['CreateTicket'],
51 );
52
53
54 my $eid = 0;
55 for my $usage (qw/signed encrypted signed&encrypted/) {
56     for my $format (qw/MIME inline/) {
57         for my $attachment (qw/plain text-attachment binary-attachment/) {
58             ++$eid;
59             diag "Email $eid: $usage, $attachment email with $format format" if $ENV{TEST_VERBOSE};
60             eval { email_ok($eid, $usage, $format, $attachment) };
61         }
62     }
63 }
64
65 $eid = 18;
66 {
67     my ($usage, $format, $attachment) = ('signed', 'inline', 'plain');
68     ++$eid;
69     diag "Email $eid: $usage, $attachment email with $format format" if $ENV{TEST_VERBOSE};
70     eval { email_ok($eid, $usage, $format, $attachment) };
71 }
72
73 sub email_ok {
74     my ($eid, $usage, $format, $attachment) = @_;
75     diag "email_ok $eid: $usage, $format, $attachment" if $ENV{'TEST_VERBOSE'};
76
77     my $emaildatadir = RT::Test::get_relocatable_dir(File::Spec->updir(),
78         qw(data gnupg emails));
79     my ($file) = glob("$emaildatadir/$eid-*");
80     my $mail = RT::Test->file_content($file);
81
82     my ($status, $id) = RT::Test->send_via_mailgate($mail);
83     is ($status >> 8, 0, "$eid: The mail gateway exited normally");
84     ok ($id, "$eid: got id of a newly created ticket - $id");
85
86     my $tick = RT::Ticket->new( $RT::SystemUser );
87     $tick->Load( $id );
88     ok ($tick->id, "$eid: loaded ticket #$id");
89
90     is ($tick->Subject,
91         "Test Email ID:$eid",
92         "$eid: Created the ticket"
93     );
94
95     my $txn = $tick->Transactions->First;
96     my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
97
98     is( $msg->GetHeader('X-RT-Privacy'),
99         'PGP',
100         "$eid: recorded incoming mail that is encrypted"
101     );
102
103     if ($usage =~ /encrypted/) {
104         if ( $format eq 'MIME' || $attachment eq 'plain' ) {
105             is( $msg->GetHeader('X-RT-Incoming-Encryption'),
106                 'Success',
107                 "$eid: recorded incoming mail that is encrypted"
108             );
109         } else {
110             is( $attachments[0]->GetHeader('X-RT-Incoming-Encryption'),
111                 'Success',
112                 "$eid: recorded incoming mail that is encrypted"
113             );
114             is( $attachments[1]->GetHeader('X-RT-Incoming-Encryption'),
115                 'Success',
116                 "$eid: recorded incoming mail that is encrypted"
117             );
118         }
119         like( $attachments[0]->Content, qr/ID:$eid/,
120                 "$eid: incoming mail did NOT have original body"
121         );
122     }
123     else {
124         is( $msg->GetHeader('X-RT-Incoming-Encryption'),
125             'Not encrypted',
126             "$eid: recorded incoming mail that is not encrypted"
127         );
128         like( $msg->Content || $attachments[0]->Content, qr/ID:$eid/,
129               "$eid: got original content"
130         );
131     }
132
133     if ($usage =~ /signed/) {
134 # XXX: FIXME: TODO: 6-signed-inline-with-attachment should be re-generated as it's actually RFC format
135         if ( $format eq 'MIME' || $attachment eq 'plain' || ($format eq 'inline' && $attachment =~ /binary/ && $usage !~ /encrypted/) ) {
136             is( $msg->GetHeader('X-RT-Incoming-Signature'),
137                 'RT Test <rt-test@example.com>',
138                 "$eid: recorded incoming mail that is signed"
139             );
140         }
141         else {
142             is( $attachments[0]->GetHeader('X-RT-Incoming-Signature'),
143                 'RT Test <rt-test@example.com>',
144                 "$eid: recorded incoming mail that is signed"
145             );
146             is( $attachments[1]->GetHeader('X-RT-Incoming-Signature'),
147                 'RT Test <rt-test@example.com>',
148                 "$eid: recorded incoming mail that is signed"
149             );
150         }
151     }
152     else {
153         is( $msg->GetHeader('X-RT-Incoming-Signature'),
154             undef,
155             "$eid: recorded incoming mail that is not signed"
156         );
157     }
158
159     if ($attachment =~ /attachment/) {
160         # signed messages should sign each attachment too
161         if ($usage =~ /signed/) {
162             my $sig = pop @attachments;
163             ok ($sig->Id, "$eid: loaded attachment.sig object");
164             my $acontent = $sig->Content;
165         }
166
167         my ($a) = grep $_->Filename, @attachments;
168         ok ($a && $a->Id, "$eid: found attachment with filename");
169
170         my $acontent = $a->Content;
171         if ($attachment =~ /binary/)
172         {
173             is(md5_hex($acontent), '1e35f1aa90c98ca2bab85c26ae3e1ba7', "$eid: The binary attachment's md5sum matches");
174         }
175         else
176         {
177             like($acontent, qr/zanzibar/, "$eid: The attachment isn't screwed up in the database.");
178         }
179
180     }
181
182     return 0;
183 }
184