RT 4.2.11, ticket#13852
[freeside.git] / rt / t / crypt / smime / bad-recipients.t
1 use strict;
2 use warnings;
3
4 use RT::Test::SMIME tests => undef;
5
6 use RT::Tickets;
7
8 RT::Test::SMIME->import_key('sender@example.com');
9 my $queue = RT::Test->load_or_create_queue(
10     Name              => 'General',
11     CorrespondAddress => 'sender@example.com',
12 );
13 ok $queue && $queue->id, 'loaded or created queue';
14
15 {
16     my ($status, $msg) = $queue->SetEncrypt(1);
17     ok $status, "turn on encyption by default"
18         or diag "error: $msg";
19 }
20
21 my $root;
22 {
23     $root = RT::User->new($RT::SystemUser);
24     ok($root->LoadByEmail('root@localhost'), "Loaded user 'root'");
25     ok($root->Load('root'), "Loaded user 'root'");
26     is($root->EmailAddress, 'root@localhost');
27
28     RT::Test::SMIME->import_key( 'root@example.com.crt' => $root );
29 }
30
31 my $bad_user;
32 {
33     $bad_user = RT::Test->load_or_create_user(
34         Name => 'bad_user',
35         EmailAddress => 'baduser@example.com',
36     );
37     ok $bad_user && $bad_user->id, 'created a user without key';
38 }
39
40 RT::Test->clean_caught_mails;
41
42 use Test::Warn;
43
44 warnings_like {
45     my $ticket = RT::Ticket->new(RT->SystemUser);
46     my ($status, undef, $msg) = $ticket->Create( Queue => $queue->id, Requestor => [$root->id, $bad_user->id] );
47     ok $status, "created a ticket" or diag "error: $msg";
48
49     my @mails = RT::Test->fetch_caught_mails;
50     is scalar @mails, 3, "autoreply, to bad user, to RT owner";
51
52     like $mails[0], qr{To: baduser\@example\.com}, "notification to bad user";
53     like $mails[1], qr{To: root}, "notification to RT owner";
54     like $mails[1], qr{Recipient 'baduser\@example\.com' is unusable, the reason is 'Key not found'},
55         "notification to owner has error";
56 } [qr{Recipient 'baduser\@example\.com' is unusable, the reason is 'Key not found'}];
57
58 done_testing;