RT 4.2.11, ticket#13852
[freeside.git] / rt / t / mail / smime / outgoing.t
1 use strict;
2 use warnings;
3
4 use RT::Test::SMIME tests => undef;
5 my $test = 'RT::Test::SMIME';
6
7 use IPC::Run3 'run3';
8 use RT::Interface::Email;
9
10 my ($url, $m) = RT::Test->started_ok;
11 ok $m->login, "logged in";
12
13 my $queue = RT::Test->load_or_create_queue(
14     Name              => 'General',
15     CorrespondAddress => 'sender@example.com',
16     CommentAddress    => 'sender@example.com',
17 );
18 ok $queue && $queue->id, 'loaded or created queue';
19
20 {
21     my ($status, $msg) = $queue->SetEncrypt(1);
22     ok $status, "turn on encyption by default"
23         or diag "error: $msg";
24 }
25
26 my $user;
27 {
28     $user = RT::User->new($RT::SystemUser);
29     ok($user->LoadByEmail('root@localhost'), "Loaded user 'root'");
30     ok($user->Load('root'), "Loaded user 'root'");
31     is($user->EmailAddress, 'root@localhost');
32
33     RT::Test::SMIME->import_key( 'root@example.com.crt' => $user );
34 }
35
36 RT::Test->clean_caught_mails;
37
38 {
39     my $mail = <<END;
40 From: root\@localhost
41 To: rt\@example.com
42 Subject: This is a test of new ticket creation as an unknown user
43
44 Blah!
45 Foob!
46
47 END
48
49     my ($status, $id) = RT::Test->send_via_mailgate(
50         $mail, queue => $queue->Name,
51     );
52     is $status >> 8, 0, "successfuly executed mailgate";
53
54     my $ticket = RT::Ticket->new($RT::SystemUser);
55     $ticket->Load( $id );
56     ok ($ticket->id, "found ticket ". $ticket->id);
57 }
58
59 {
60     my @mails = RT::Test->fetch_caught_mails;
61     is scalar @mails, 1, "autoreply";
62
63     my ($buf, $err);
64     local $@;
65     ok(eval {
66         run3([
67             qw(openssl smime -decrypt -passin pass:123456),
68             '-inkey', $test->key_path('root@example.com.key'),
69             '-recip', $test->key_path('root@example.com.crt')
70         ], \$mails[0], \$buf, \$err )
71         }, 'can decrypt'
72     );
73     diag $@ if $@;
74     diag $err if $err;
75     diag "Error code: $?" if $?;
76     like($buf, qr'This message has been automatically generated in response');
77 }
78
79 undef $m;
80 done_testing;