Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / rt / t / web / crypt-gnupg.t
index fb28c88..85e090c 100644 (file)
@@ -1,20 +1,17 @@
-#!/usr/bin/perl -w
 use strict;
+use warnings;
 
-use RT::Test tests => 94;
-
-plan skip_all => 'GnuPG required.'
-    unless eval 'use GnuPG::Interface; 1';
-plan skip_all => 'gpg executable is required.'
-    unless RT::Test->find_executable('gpg');
-
+use RT::Test::GnuPG
+  tests         => 104,
+  gnupg_options => {
+    passphrase    => 'recipient',
+    'trust-model' => 'always',
+};
+use Test::Warn;
+use MIME::Head;
 
 use RT::Action::SendEmail;
 
-eval 'use GnuPG::Interface; 1' or plan skip_all => 'GnuPG required.';
-
-RT::Test->set_mail_catcher;
-
 RT->Config->Set( CommentAddress => 'general@example.com');
 RT->Config->Set( CorrespondAddress => 'general@example.com');
 RT->Config->Set( DefaultSearchResultFormat => qq{
@@ -26,23 +23,6 @@ RT->Config->Set( DefaultSearchResultFormat => qq{
    'KR-__KeyRequestors__-K',
    Status});
 
-use File::Spec ();
-use Cwd;
-use File::Temp qw(tempdir);
-my $homedir = tempdir( CLEANUP => 1 );
-
-use_ok('RT::Crypt::GnuPG');
-
-RT->Config->Set( 'GnuPG',
-                 Enable => 1,
-                 OutgoingMessagesFormat => 'RFC' );
-
-RT->Config->Set( 'GnuPGOptions',
-                 homedir => $homedir,
-                 passphrase => 'recipient',
-                 'no-permission-warning' => undef,
-                 'trust-model' => 'always');
-RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
 
 RT::Test->import_gnupg_key('recipient@example.com', 'public');
 RT::Test->import_gnupg_key('recipient@example.com', 'secret');
@@ -51,7 +31,7 @@ RT::Test->import_gnupg_key('general@example.com', 'secret');
 RT::Test->import_gnupg_key('general@example.com.2', 'public');
 RT::Test->import_gnupg_key('general@example.com.2', 'secret');
 
-ok(my $user = RT::User->new($RT::SystemUser));
+ok(my $user = RT::User->new(RT->SystemUser));
 ok($user->Load('root'), "Loaded user 'root'");
 $user->SetEmailAddress('recipient@example.com');
 
@@ -62,11 +42,6 @@ my $queue = RT::Test->load_or_create_queue(
 ok $queue && $queue->id, 'loaded or created queue';
 my $qid = $queue->id;
 
-RT::Test->set_rights(
-    Principal => 'Everyone',
-    Right => ['CreateTicket', 'ShowTicket', 'SeeQueue', 'ModifyTicket'],
-);
-
 my ($baseurl, $m) = RT::Test->started_ok;
 ok $m->login, 'logged in';
 
@@ -79,6 +54,7 @@ RT::Test->clean_caught_mails;
 
 $m->goto_create_ticket( $queue );
 $m->form_name('TicketCreate');
+$m->field('Requestors', 'recipient@example.com');
 $m->field('Subject', 'Encryption test');
 $m->field('Content', 'Some content');
 ok($m->value('Encrypt', 2), "encrypt tick box is checked");
@@ -95,8 +71,7 @@ $user->SetEmailAddress('general@example.com');
 for my $mail (@mail) {
     unlike $mail, qr/Some content/, "outgoing mail was encrypted";
 
-    my ($content_type) = $mail =~ /^(Content-Type: .*)/m;
-    my ($mime_version) = $mail =~ /^(MIME-Version: .*)/m;
+    my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
     my $body = strip_headers($mail);
 
     $mail = << "MAIL";
@@ -113,7 +88,7 @@ MAIL
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "got id of a newly created ticket - $id");
 
-    my $tick = RT::Ticket->new( $RT::SystemUser );
+    my $tick = RT::Ticket->new( RT->SystemUser );
     $tick->Load( $id );
     ok ($tick->id, "loaded ticket #$id");
 
@@ -148,6 +123,7 @@ RT::Test->clean_caught_mails;
 
 $m->goto_create_ticket( $queue );
 $m->form_name('TicketCreate');
+$m->field('Requestors', 'recipient@example.com');
 $m->field('Subject', 'Signing test');
 $m->field('Content', 'Some other content');
 ok(!$m->value('Encrypt', 2), "encrypt tick box is unchecked");
@@ -163,8 +139,7 @@ for my $mail (@mail) {
     like $mail, qr/Some other content/, "outgoing mail was not encrypted";
     like $mail, qr/-----BEGIN PGP SIGNATURE-----[\s\S]+-----END PGP SIGNATURE-----/, "data has some kind of signature";
 
-    my ($content_type) = $mail =~ /^(Content-Type: .*)/m;
-    my ($mime_version) = $mail =~ /^(MIME-Version: .*)/m;
+    my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
     my $body = strip_headers($mail);
 
     $mail = << "MAIL";
@@ -181,7 +156,7 @@ MAIL
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "got id of a newly created ticket - $id");
 
-    my $tick = RT::Ticket->new( $RT::SystemUser );
+    my $tick = RT::Ticket->new( RT->SystemUser );
     $tick->Load( $id );
     ok ($tick->id, "loaded ticket #$id");
 
@@ -221,6 +196,7 @@ RT::Test->clean_caught_mails;
 
 $m->goto_create_ticket( $queue );
 $m->form_name('TicketCreate');
+$m->field('Requestors', 'recipient@example.com');
 $m->field('Subject', 'Crypt+Sign test');
 $m->field('Content', 'Some final? content');
 ok($m->value('Encrypt', 2), "encrypt tick box is checked");
@@ -235,8 +211,7 @@ ok(@mail, "got some mail");
 for my $mail (@mail) {
     unlike $mail, qr/Some other content/, "outgoing mail was encrypted";
 
-    my ($content_type) = $mail =~ /^(Content-Type: .*)/m;
-    my ($mime_version) = $mail =~ /^(MIME-Version: .*)/m;
+    my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
     my $body = strip_headers($mail);
 
     $mail = << "MAIL";
@@ -253,7 +228,7 @@ MAIL
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "got id of a newly created ticket - $id");
 
-    my $tick = RT::Ticket->new( $RT::SystemUser );
+    my $tick = RT::Ticket->new( RT->SystemUser );
     $tick->Load( $id );
     ok ($tick->id, "loaded ticket #$id");
 
@@ -286,6 +261,7 @@ RT::Test->clean_caught_mails;
 
 $m->goto_create_ticket( $queue );
 $m->form_name('TicketCreate');
+$m->field('Requestors', 'recipient@example.com');
 $m->field('Subject', 'Test crypt-off on encrypted queue');
 $m->field('Content', 'Thought you had me figured out didya');
 $m->field(Encrypt => undef, 2); # turn off encryption
@@ -301,8 +277,7 @@ ok(@mail, "got some mail");
 for my $mail (@mail) {
     like $mail, qr/Thought you had me figured out didya/, "outgoing mail was unencrypted";
 
-    my ($content_type) = $mail =~ /^(Content-Type: .*)/m;
-    my ($mime_version) = $mail =~ /^(MIME-Version: .*)/m;
+    my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
     my $body = strip_headers($mail);
 
     $mail = << "MAIL";
@@ -319,7 +294,7 @@ MAIL
     is ($status >> 8, 0, "The mail gateway exited normally");
     ok ($id, "got id of a newly created ticket - $id");
 
-    my $tick = RT::Ticket->new( $RT::SystemUser );
+    my $tick = RT::Ticket->new( RT->SystemUser );
     $tick->Load( $id );
     ok ($tick->id, "loaded ticket #$id");
 
@@ -348,6 +323,20 @@ MAIL
     like($attachments[0]->Content, qr/$RT::rtname/, "RT's mail includes this instance's name");
 }
 
+sub get_headers {
+    my $mail = shift;
+    open my $fh, "<", \$mail or die $!;
+    my $head = MIME::Head->read($fh);
+    return @{[
+        map {
+            my $hdr = "$_: " . $head->get($_);
+            chomp $hdr;
+            $hdr;
+        }
+        @_
+    ]};
+}
+
 sub strip_headers
 {
     my $mail = shift;
@@ -361,12 +350,12 @@ my $nokey = RT::Test->load_or_create_user(Name => 'nokey', EmailAddress => 'noke
 $nokey->PrincipalObj->GrantRight(Right => 'CreateTicket');
 $nokey->PrincipalObj->GrantRight(Right => 'OwnTicket');
 
-my $tick = RT::Ticket->new( $RT::SystemUser );
+my $tick = RT::Ticket->new( RT->SystemUser );
 $tick->Create(Subject => 'owner lacks pubkey', Queue => 'general',
               Owner => $nokey);
 ok(my $id = $tick->id, 'created ticket for owner-without-pubkey');
 
-$tick = RT::Ticket->new( $RT::SystemUser );
+$tick = RT::Ticket->new( RT->SystemUser );
 $tick->Create(Subject => 'owner has pubkey', Queue => 'general',
               Owner => 'root');
 ok($id = $tick->id, 'created ticket for owner-with-pubkey');
@@ -379,11 +368,18 @@ To: general\@example.com
 hello
 MAIL
  
-((my $status), $id) = RT::Test->send_via_mailgate($mail);
+my $status;
+warning_like {
+    ($status, $id) = RT::Test->send_via_mailgate($mail);
+} [
+    qr/nokey\@example.com: skipped: public key not found/,
+    qr/Recipient 'nokey\@example.com' is unusable/,
+];
+
 is ($status >> 8, 0, "The mail gateway exited normally");
 ok ($id, "got id of a newly created ticket - $id");
 
-$tick = RT::Ticket->new( $RT::SystemUser );
+$tick = RT::Ticket->new( RT->SystemUser );
 $tick->Load( $id );
 ok ($tick->id, "loaded ticket #$id");
 
@@ -396,7 +392,7 @@ is ($tick->Subject,
 my $key1 = "EC1E81E7DC3DB42788FB0E4E9FA662C06DE22FC2";
 my $key2 = "75E156271DCCF02DDD4A7A8CDF651FA0632C4F50";
 
-ok($user = RT::User->new($RT::SystemUser));
+ok($user = RT::User->new(RT->SystemUser));
 ok($user->Load('root'), "Loaded user 'root'");
 is($user->PreferredKey, $key1, "preferred key is set correctly");
 $m->get("$baseurl/Prefs/Other.html");
@@ -407,11 +403,11 @@ like($m->content, qr/$key1/, "first key shows up in preferences");
 like($m->content, qr/$key2/, "second key shows up in preferences");
 like($m->content, qr/$key1.*?$key2/s, "first key shows up before the second");
 
-$m->form_number(3);
+$m->form_name('ModifyPreferences');
 $m->select("PreferredKey" => $key2);
 $m->submit;
 
-ok($user = RT::User->new($RT::SystemUser));
+ok($user = RT::User->new(RT->SystemUser));
 ok($user->Load('root'), "Loaded user 'root'");
 is($user->PreferredKey, $key2, "preferred key is set correctly to the new value");
 
@@ -423,6 +419,8 @@ like($m->content, qr/$key2/, "second key shows up in preferences");
 like($m->content, qr/$key1/, "first key shows up in preferences");
 like($m->content, qr/$key2.*?$key1/s, "second key (now preferred) shows up before the first");
 
+$m->no_warnings_ok;
+
 # test that the new fields work
 $m->get("$baseurl/Search/Simple.html?q=General");
 my $content = $m->content;
@@ -441,6 +439,12 @@ like($content, qr/KO-nokey \(no pubkey!\)-K/, "KeyOwnerName issues no-pubkey war
 like($content, qr/KO-Nobody \(no pubkey!\)-K/, "KeyOwnerName issues no-pubkey warning for nobody");
 
 like($content, qr/KR-recipient\@example.com-K/, "KeyRequestors does not issue no-pubkey warning for recipient\@example.com");
+
 like($content, qr/KR-general\@example.com-K/, "KeyRequestors does not issue no-pubkey warning for general\@example.com");
 like($content, qr/KR-nokey\@example.com \(no pubkey!\)-K/, "KeyRequestors DOES issue no-pubkey warning for nokey\@example.com");
 
+$m->next_warning_like(qr/public key not found/);
+$m->next_warning_like(qr/above error may result from an unconfigured RT\/GPG/);
+$m->next_warning_like(qr/public key not found/);
+$m->next_warning_like(qr/above error may result from an unconfigured RT\/GPG/);
+$m->no_leftover_warnings_ok;