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