import rt 3.8.11
[freeside.git] / rt / t / mail / gnupg-special.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 23;
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 use Digest::MD5 qw(md5_hex);
13
14 use File::Temp qw(tempdir);
15 my $homedir = tempdir( CLEANUP => 1 );
16
17 # catch any outgoing emails
18 RT::Test->set_mail_catcher;
19
20 RT->Config->Set( 'GnuPG',
21                  Enable => 1,
22                  OutgoingMessagesFormat => 'RFC' );
23
24 RT->Config->Set( 'GnuPGOptions',
25                  homedir => $homedir,
26                  'passphrase' => 'rt-test',
27                  'no-permission-warning' => undef);
28
29 RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
30
31 RT::Test->import_gnupg_key('rt-recipient@example.com');
32 RT::Test->import_gnupg_key('rt-test@example.com', 'public');
33
34 my ($baseurl, $m) = RT::Test->started_ok;
35
36 ok( $m->login, 'we did log in' );
37
38 # configure key for General queue
39 {
40     $m->get( $baseurl.'/Admin/Queues/');
41     $m->follow_link_ok( {text => 'General'} );
42     $m->submit_form(
43         form_number => 3,
44         fields      => { CorrespondAddress => 'rt-recipient@example.com' },
45     );
46     $m->content_like(qr/rt-recipient\@example.com.* - never/, 'has key info.');
47 }
48
49 ok(my $user = RT::User->new($RT::SystemUser));
50 ok($user->Load('root'), "Loaded user 'root'");
51 $user->SetEmailAddress('recipient@example.com');
52
53 RT::Test->set_rights(
54     Principal => 'Everyone',
55     Right => ['CreateTicket'],
56 );
57
58 {
59     my $id = send_via_mailgate('quoted_inline_signature.txt');
60
61     my $tick = RT::Ticket->new( $RT::SystemUser );
62     $tick->Load( $id );
63     ok ($tick->id, "loaded ticket #$id");
64
65     my $txn = $tick->Transactions->First;
66     my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
67
68     is( $msg->GetHeader('X-RT-Privacy'),
69         undef,
70         "no privacy is set as this ticket is not encrypted"
71     );
72
73     my @mail = RT::Test->fetch_caught_mails;
74     is(scalar @mail, 1, "autoreply only");
75 }
76
77 {
78     my $id = send_via_mailgate('binary-asc-attach-marked-plain-text.txt');
79
80     my $tick = RT::Ticket->new( $RT::SystemUser );
81     $tick->Load( $id );
82     ok ($tick->id, "loaded ticket #$id");
83
84     my $txn = $tick->Transactions->First;
85     my ($msg, @attachs) = @{$txn->Attachments->ItemsArrayRef};
86
87     is (scalar @attachs, 3, 'text, attachment and original');
88     my $bin = $attachs[1];
89     is(
90         (split /;/, $bin->GetHeader('Content-Type'))[0],
91         'application/octet-stream',
92         'binary attachment'
93     );
94     is(md5_hex($bin->Content), '1e35f1aa90c98ca2bab85c26ae3e1ba7', "correct png");
95 }
96
97 {
98     my $id = send_via_mailgate('inline-binary-attachment-with-wrap.txt');
99
100     my $tick = RT::Ticket->new( $RT::SystemUser );
101     $tick->Load( $id );
102     ok ($tick->id, "loaded ticket #$id");
103
104     my $txn = $tick->Transactions->First;
105     my ($msg, @attachs) = @{$txn->Attachments->ItemsArrayRef};
106
107     is (scalar @attachs, 3, 'text, attachment and original');
108     my $bin = $attachs[1];
109     is(
110         (split /;/, $bin->GetHeader('Content-Type'))[0],
111         'application/octet-stream',
112         'binary attachment'
113     );
114     is(md5_hex($bin->Content), '1e35f1aa90c98ca2bab85c26ae3e1ba7', "correct png");
115 }
116
117 sub send_via_mailgate {
118     my $fname = shift;
119     my $emaildatadir = RT::Test::get_relocatable_dir(File::Spec->updir(),
120         qw(data gnupg emails special));
121     my $file = File::Spec->catfile( $emaildatadir, $fname );
122     my $mail = RT::Test->file_content($file);
123
124     my ($status, $id) = RT::Test->send_via_mailgate($mail);
125     is ($status >> 8, 0, "the mail gateway exited normally");
126     ok ($id, "got id of a newly created ticket - $id");
127     return $id;
128 }
129