Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / t / mail / gnupg-reverification.t
1 use strict;
2 use warnings;
3
4 use RT::Test::GnuPG tests => 216, gnupg_options => { passphrase => 'rt-test' };
5
6 diag "load Everyone group";
7 my $everyone;
8 {
9     $everyone = RT::Group->new( RT->SystemUser );
10     $everyone->LoadSystemInternalGroup('Everyone');
11     ok $everyone->id, "loaded 'everyone' group";
12 }
13
14 my ($baseurl, $m) = RT::Test->started_ok;
15 ok $m->login, 'we get log in';
16
17 RT::Test->import_gnupg_key('rt-recipient@example.com');
18
19 my @ticket_ids;
20
21 my $emaildatadir = RT::Test::get_relocatable_dir(File::Spec->updir(),
22     qw(data gnupg emails));
23 my @files = glob("$emaildatadir/*-signed-*");
24 foreach my $file ( @files ) {
25     diag "testing $file";
26
27     my ($eid) = ($file =~ m{(\d+)[^/\\]+$});
28     ok $eid, 'figured id of a file';
29
30     my $email_content = RT::Test->file_content( $file );
31     ok $email_content, "$eid: got content of email";
32
33     my $warnings;
34     my ($status, $id);
35
36     {
37         # We don't use Test::Warn here because we get multi-line
38         # warnings, which Test::Warn only records the first line of.
39         local $SIG{__WARN__} = sub {
40             $warnings .= "@_";
41         };
42
43         ($status, $id) = RT::Test->send_via_mailgate( $email_content );
44     }
45
46     is $status >> 8, 0, "$eid: the mail gateway exited normally";
47     ok $id, "$eid: got id of a newly created ticket - $id";
48
49     like($warnings, qr/Had a problem during decrypting and verifying/);
50     like($warnings, qr/public key not found/);
51
52     my $ticket = RT::Ticket->new( RT->SystemUser );
53     $ticket->Load( $id );
54     ok $ticket->id, "$eid: loaded ticket #$id";
55     is $ticket->Subject, "Test Email ID:$eid", "$eid: correct subject";
56
57     $m->goto_ticket( $id );
58     $m->content_contains(
59         "Not possible to check the signature, the reason is missing public key",
60         "$eid: signature is not verified",
61     );
62     $m->content_like(qr/This is .*ID:$eid/ims, "$eid: content is there and message is decrypted");
63
64     $m->next_warning_like(qr/public key not found/);
65
66     # some mails contain multiple signatures
67     if ($eid == 5 || $eid == 17 || $eid == 18) {
68         $m->next_warning_like(qr/public key not found/);
69     }
70
71     $m->no_leftover_warnings_ok;
72
73     push @ticket_ids, $id;
74 }
75
76 diag "import key into keyring";
77 RT::Test->import_gnupg_key('rt-test@example.com', 'public');
78
79 foreach my $id ( @ticket_ids ) {
80     diag "testing ticket #$id";
81
82     $m->goto_ticket( $id );
83     $m->content_contains(
84         "The signature is good",
85         "signature is re-verified and now good",
86     );
87
88     $m->no_warnings_ok;
89 }
90