7e50819e8824361cc51dba8cf5f861e654efa6fa
[freeside.git] / rt / t / mail / gnupg-special.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 11;
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 File::Temp qw(tempdir);
13 my $homedir = tempdir( CLEANUP => 1 );
14
15 # catch any outgoing emails
16 RT::Test->set_mail_catcher;
17
18 RT->Config->Set( 'GnuPG',
19                  Enable => 1,
20                  OutgoingMessagesFormat => 'RFC' );
21
22 RT->Config->Set( 'GnuPGOptions',
23                  homedir => $homedir,
24                  'no-permission-warning' => undef);
25
26 RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
27
28 RT::Test->import_gnupg_key('rt-recipient@example.com');
29 RT::Test->import_gnupg_key('rt-test@example.com', 'public');
30
31 my ($baseurl, $m) = RT::Test->started_ok;
32
33 ok( $m->login, 'we did log in' );
34
35 # configure key for General queue
36 {
37     $m->get( $baseurl.'/Admin/Queues/');
38     $m->follow_link_ok( {text => 'General'} );
39     $m->submit_form(
40         form_number => 3,
41         fields      => { CorrespondAddress => 'rt-recipient@example.com' },
42     );
43     $m->content_like(qr/rt-recipient\@example.com.* - never/, 'has key info.');
44 }
45
46 ok(my $user = RT::User->new($RT::SystemUser));
47 ok($user->Load('root'), "Loaded user 'root'");
48 $user->SetEmailAddress('recipient@example.com');
49
50 RT::Test->set_rights(
51     Principal => 'Everyone',
52     Right => ['CreateTicket'],
53 );
54
55 {
56     my $id = send_via_mailgate('quoted_inline_signature.txt');
57
58     my $tick = RT::Ticket->new( $RT::SystemUser );
59     $tick->Load( $id );
60     ok ($tick->id, "loaded ticket #$id");
61
62     my $txn = $tick->Transactions->First;
63     my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
64
65     is( $msg->GetHeader('X-RT-Privacy'),
66         undef,
67         "no privacy is set as this ticket is not encrypted"
68     );
69
70     my @mail = RT::Test->fetch_caught_mails;
71     is(scalar @mail, 1, "autoreply only");
72 }
73
74 sub send_via_mailgate {
75     my $fname = shift;
76     my $emaildatadir = RT::Test::get_relocatable_dir(File::Spec->updir(),
77         qw(data gnupg emails special));
78     my $file = File::Spec->catfile( $emaildatadir, $fname );
79     my $mail = RT::Test->file_content($file);
80
81     my ($status, $id) = RT::Test->send_via_mailgate($mail);
82     is ($status >> 8, 0, "the mail gateway exited normally");
83     ok ($id, "got id of a newly created ticket - $id");
84     return $id;
85 }
86