5 use RT::Test tests => 196;
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');
13 use Digest::MD5 qw(md5_hex);
15 use File::Temp qw(tempdir);
16 my $homedir = tempdir( CLEANUP => 1 );
18 RT->Config->Set( 'GnuPG',
20 OutgoingMessagesFormat => 'RFC' );
22 RT->Config->Set( 'GnuPGOptions',
24 passphrase => 'rt-test',
25 'no-permission-warning' => undef);
27 RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
29 RT::Test->import_gnupg_key('rt-recipient@example.com');
30 RT::Test->import_gnupg_key('rt-test@example.com', 'public');
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.');
41 Principal => 'Everyone',
42 Right => ['CreateTicket'],
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/) {
50 diag "Email $eid: $usage, $attachment email with $format format" if $ENV{TEST_VERBOSE};
51 eval { email_ok($eid, $usage, $format, $attachment) };
58 my ($usage, $format, $attachment) = ('signed', 'inline', 'plain');
60 diag "Email $eid: $usage, $attachment email with $format format" if $ENV{TEST_VERBOSE};
61 eval { email_ok($eid, $usage, $format, $attachment) };
65 my ($eid, $usage, $format, $attachment) = @_;
66 diag "email_ok $eid: $usage, $format, $attachment" if $ENV{'TEST_VERBOSE'};
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);
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");
77 my $tick = RT::Ticket->new( $RT::SystemUser );
79 ok ($tick->id, "$eid: loaded ticket #$id");
83 "$eid: Created the ticket"
86 my $txn = $tick->Transactions->First;
87 my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
89 is( $msg->GetHeader('X-RT-Privacy'),
91 "$eid: recorded incoming mail that is encrypted"
94 if ($usage =~ /encrypted/) {
95 if ( $format eq 'MIME' || $attachment eq 'plain' ) {
96 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
98 "$eid: recorded incoming mail that is encrypted"
101 is( $attachments[0]->GetHeader('X-RT-Incoming-Encryption'),
103 "$eid: recorded incoming mail that is encrypted"
105 is( $attachments[1]->GetHeader('X-RT-Incoming-Encryption'),
107 "$eid: recorded incoming mail that is encrypted"
110 like( $attachments[0]->Content, qr/ID:$eid/,
111 "$eid: incoming mail did NOT have original body"
115 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
117 "$eid: recorded incoming mail that is not encrypted"
119 like( $msg->Content || $attachments[0]->Content, qr/ID:$eid/,
120 "$eid: got original content"
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"
133 is( $attachments[0]->GetHeader('X-RT-Incoming-Signature'),
134 'RT Test <rt-test@example.com>',
135 "$eid: recorded incoming mail that is signed"
137 is( $attachments[1]->GetHeader('X-RT-Incoming-Signature'),
138 'RT Test <rt-test@example.com>',
139 "$eid: recorded incoming mail that is signed"
144 is( $msg->GetHeader('X-RT-Incoming-Signature'),
146 "$eid: recorded incoming mail that is not signed"
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;
158 my ($a) = grep $_->Filename, @attachments;
159 ok ($a && $a->Id, "$eid: found attachment with filename");
161 my $acontent = $a->Content;
162 if ($attachment =~ /binary/)
164 is(md5_hex($acontent), '1e35f1aa90c98ca2bab85c26ae3e1ba7', "$eid: The binary attachment's md5sum matches");
168 like($acontent, qr/zanzibar/, "$eid: The attachment isn't screwed up in the database.");