4 use RT::Test tests => 94;
6 plan skip_all => 'GnuPG required.'
7 unless eval 'use GnuPG::Interface; 1';
8 plan skip_all => 'gpg executable is required.'
9 unless RT::Test->find_executable('gpg');
13 use RT::Action::SendEmail;
15 eval 'use GnuPG::Interface; 1' or plan skip_all => 'GnuPG required.';
17 RT::Test->set_mail_catcher;
19 RT->Config->Set( CommentAddress => 'general@example.com');
20 RT->Config->Set( CorrespondAddress => 'general@example.com');
21 RT->Config->Set( DefaultSearchResultFormat => qq{
22 '<B><A HREF="__WebPath__/Ticket/Display.html?id=__id__">__id__</a></B>/TITLE:#',
23 '<B><A HREF="__WebPath__/Ticket/Display.html?id=__id__">__Subject__</a></B>/TITLE:Subject',
25 'OR-__Requestors__-O',
26 'KO-__KeyOwnerName__-K',
27 'KR-__KeyRequestors__-K',
32 use File::Temp qw(tempdir);
33 my $homedir = tempdir( CLEANUP => 1 );
35 use_ok('RT::Crypt::GnuPG');
37 RT->Config->Set( 'GnuPG',
39 OutgoingMessagesFormat => 'RFC' );
41 RT->Config->Set( 'GnuPGOptions',
43 passphrase => 'recipient',
44 'no-permission-warning' => undef,
45 'trust-model' => 'always');
46 RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
48 RT::Test->import_gnupg_key('recipient@example.com', 'public');
49 RT::Test->import_gnupg_key('recipient@example.com', 'secret');
50 RT::Test->import_gnupg_key('general@example.com', 'public');
51 RT::Test->import_gnupg_key('general@example.com', 'secret');
52 RT::Test->import_gnupg_key('general@example.com.2', 'public');
53 RT::Test->import_gnupg_key('general@example.com.2', 'secret');
55 ok(my $user = RT::User->new($RT::SystemUser));
56 ok($user->Load('root'), "Loaded user 'root'");
57 $user->SetEmailAddress('recipient@example.com');
59 my $queue = RT::Test->load_or_create_queue(
61 CorrespondAddress => 'general@example.com',
63 ok $queue && $queue->id, 'loaded or created queue';
67 Principal => 'Everyone',
68 Right => ['CreateTicket', 'ShowTicket', 'SeeQueue', 'ModifyTicket'],
71 my ($baseurl, $m) = RT::Test->started_ok;
72 ok $m->login, 'logged in';
74 $m->get_ok("/Admin/Queues/Modify.html?id=$qid");
75 $m->form_with_fields('Sign', 'Encrypt');
76 $m->field(Encrypt => 1);
79 RT::Test->clean_caught_mails;
81 $m->goto_create_ticket( $queue );
82 $m->form_name('TicketCreate');
83 $m->field('Subject', 'Encryption test');
84 $m->field('Content', 'Some content');
85 ok($m->value('Encrypt', 2), "encrypt tick box is checked");
86 ok(!$m->value('Sign', 2), "sign tick box is unchecked");
88 is($m->status, 200, "request successful");
90 $m->get($baseurl); # ensure that the mail has been processed
92 my @mail = RT::Test->fetch_caught_mails;
93 ok(@mail, "got some mail");
95 $user->SetEmailAddress('general@example.com');
96 for my $mail (@mail) {
97 unlike $mail, qr/Some content/, "outgoing mail was encrypted";
99 my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
100 my $body = strip_headers($mail);
103 Subject: RT mail sent back into RT
104 From: general\@example.com
105 To: recipient\@example.com
112 my ($status, $id) = RT::Test->send_via_mailgate($mail);
113 is ($status >> 8, 0, "The mail gateway exited normally");
114 ok ($id, "got id of a newly created ticket - $id");
116 my $tick = RT::Ticket->new( $RT::SystemUser );
118 ok ($tick->id, "loaded ticket #$id");
121 "RT mail sent back into RT",
125 my $txn = $tick->Transactions->First;
126 my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
128 is( $msg->GetHeader('X-RT-Privacy'),
130 "RT's outgoing mail has crypto"
132 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
134 "RT's outgoing mail looks encrypted"
137 like($attachments[0]->Content, qr/Some content/, "RT's mail includes copy of ticket text");
138 like($attachments[0]->Content, qr/$RT::rtname/, "RT's mail includes this instance's name");
141 $m->get("$baseurl/Admin/Queues/Modify.html?id=$qid");
142 $m->form_with_fields('Sign', 'Encrypt');
143 $m->field(Encrypt => undef);
144 $m->field(Sign => 1);
147 RT::Test->clean_caught_mails;
149 $m->goto_create_ticket( $queue );
150 $m->form_name('TicketCreate');
151 $m->field('Subject', 'Signing test');
152 $m->field('Content', 'Some other content');
153 ok(!$m->value('Encrypt', 2), "encrypt tick box is unchecked");
154 ok($m->value('Sign', 2), "sign tick box is checked");
156 is($m->status, 200, "request successful");
158 $m->get($baseurl); # ensure that the mail has been processed
160 @mail = RT::Test->fetch_caught_mails;
161 ok(@mail, "got some mail");
162 for my $mail (@mail) {
163 like $mail, qr/Some other content/, "outgoing mail was not encrypted";
164 like $mail, qr/-----BEGIN PGP SIGNATURE-----[\s\S]+-----END PGP SIGNATURE-----/, "data has some kind of signature";
166 my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
167 my $body = strip_headers($mail);
170 Subject: More RT mail sent back into RT
171 From: general\@example.com
172 To: recipient\@example.com
179 my ($status, $id) = RT::Test->send_via_mailgate($mail);
180 is ($status >> 8, 0, "The mail gateway exited normally");
181 ok ($id, "got id of a newly created ticket - $id");
183 my $tick = RT::Ticket->new( $RT::SystemUser );
185 ok ($tick->id, "loaded ticket #$id");
188 "More RT mail sent back into RT",
192 my $txn = $tick->Transactions->First;
193 my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
195 is( $msg->GetHeader('X-RT-Privacy'),
197 "RT's outgoing mail has crypto"
199 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
201 "RT's outgoing mail looks unencrypted"
203 is( $msg->GetHeader('X-RT-Incoming-Signature'),
204 'general <general@example.com>',
205 "RT's outgoing mail looks signed"
208 like($attachments[0]->Content, qr/Some other content/, "RT's mail includes copy of ticket text");
209 like($attachments[0]->Content, qr/$RT::rtname/, "RT's mail includes this instance's name");
212 $m->get("$baseurl/Admin/Queues/Modify.html?id=$qid");
213 $m->form_with_fields('Sign', 'Encrypt');
214 $m->field(Encrypt => 1);
215 $m->field(Sign => 1);
218 RT::Test->clean_caught_mails;
221 $m->goto_create_ticket( $queue );
222 $m->form_name('TicketCreate');
223 $m->field('Subject', 'Crypt+Sign test');
224 $m->field('Content', 'Some final? content');
225 ok($m->value('Encrypt', 2), "encrypt tick box is checked");
226 ok($m->value('Sign', 2), "sign tick box is checked");
228 is($m->status, 200, "request successful");
230 $m->get($baseurl); # ensure that the mail has been processed
232 @mail = RT::Test->fetch_caught_mails;
233 ok(@mail, "got some mail");
234 for my $mail (@mail) {
235 unlike $mail, qr/Some other content/, "outgoing mail was encrypted";
237 my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
238 my $body = strip_headers($mail);
241 Subject: Final RT mail sent back into RT
242 From: general\@example.com
243 To: recipient\@example.com
250 my ($status, $id) = RT::Test->send_via_mailgate($mail);
251 is ($status >> 8, 0, "The mail gateway exited normally");
252 ok ($id, "got id of a newly created ticket - $id");
254 my $tick = RT::Ticket->new( $RT::SystemUser );
256 ok ($tick->id, "loaded ticket #$id");
259 "Final RT mail sent back into RT",
263 my $txn = $tick->Transactions->First;
264 my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
266 is( $msg->GetHeader('X-RT-Privacy'),
268 "RT's outgoing mail has crypto"
270 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
272 "RT's outgoing mail looks encrypted"
274 is( $msg->GetHeader('X-RT-Incoming-Signature'),
275 'general <general@example.com>',
276 "RT's outgoing mail looks signed"
279 like($attachments[0]->Content, qr/Some final\? content/, "RT's mail includes copy of ticket text");
280 like($attachments[0]->Content, qr/$RT::rtname/, "RT's mail includes this instance's name");
283 RT::Test->clean_caught_mails;
285 $m->goto_create_ticket( $queue );
286 $m->form_name('TicketCreate');
287 $m->field('Subject', 'Test crypt-off on encrypted queue');
288 $m->field('Content', 'Thought you had me figured out didya');
289 $m->field(Encrypt => undef, 2); # turn off encryption
290 ok(!$m->value('Encrypt', 2), "encrypt tick box is now unchecked");
291 ok($m->value('Sign', 2), "sign tick box is still checked");
293 is($m->status, 200, "request successful");
295 $m->get($baseurl); # ensure that the mail has been processed
297 @mail = RT::Test->fetch_caught_mails;
298 ok(@mail, "got some mail");
299 for my $mail (@mail) {
300 like $mail, qr/Thought you had me figured out didya/, "outgoing mail was unencrypted";
302 my ($content_type, $mime_version) = get_headers($mail, "Content-Type", "MIME-Version");
303 my $body = strip_headers($mail);
306 Subject: Post-final! RT mail sent back into RT
307 From: general\@example.com
308 To: recipient\@example.com
315 my ($status, $id) = RT::Test->send_via_mailgate($mail);
316 is ($status >> 8, 0, "The mail gateway exited normally");
317 ok ($id, "got id of a newly created ticket - $id");
319 my $tick = RT::Ticket->new( $RT::SystemUser );
321 ok ($tick->id, "loaded ticket #$id");
324 "Post-final! RT mail sent back into RT",
328 my $txn = $tick->Transactions->First;
329 my ($msg, @attachments) = @{$txn->Attachments->ItemsArrayRef};
331 is( $msg->GetHeader('X-RT-Privacy'),
333 "RT's outgoing mail has crypto"
335 is( $msg->GetHeader('X-RT-Incoming-Encryption'),
337 "RT's outgoing mail looks unencrypted"
339 is( $msg->GetHeader('X-RT-Incoming-Signature'),
340 'general <general@example.com>',
341 "RT's outgoing mail looks signed"
344 like($attachments[0]->Content, qr/Thought you had me figured out didya/, "RT's mail includes copy of ticket text");
345 like($attachments[0]->Content, qr/$RT::rtname/, "RT's mail includes this instance's name");
350 open my $fh, "<", \$mail or die $!;
351 my $head = MIME::Head->read($fh);
354 my $hdr = "$_: " . $head->get($_);
365 $mail =~ s/.*?\n\n//s;
369 # now test the OwnerNameKey and RequestorsKey fields
371 my $nokey = RT::Test->load_or_create_user(Name => 'nokey', EmailAddress => 'nokey@example.com');
372 $nokey->PrincipalObj->GrantRight(Right => 'CreateTicket');
373 $nokey->PrincipalObj->GrantRight(Right => 'OwnTicket');
375 my $tick = RT::Ticket->new( $RT::SystemUser );
376 $tick->Create(Subject => 'owner lacks pubkey', Queue => 'general',
378 ok(my $id = $tick->id, 'created ticket for owner-without-pubkey');
380 $tick = RT::Ticket->new( $RT::SystemUser );
381 $tick->Create(Subject => 'owner has pubkey', Queue => 'general',
383 ok($id = $tick->id, 'created ticket for owner-with-pubkey');
385 my $mail = << "MAIL";
386 Subject: Nokey requestor
387 From: nokey\@example.com
388 To: general\@example.com
393 ((my $status), $id) = RT::Test->send_via_mailgate($mail);
394 is ($status >> 8, 0, "The mail gateway exited normally");
395 ok ($id, "got id of a newly created ticket - $id");
397 $tick = RT::Ticket->new( $RT::SystemUser );
399 ok ($tick->id, "loaded ticket #$id");
407 my $key1 = "EC1E81E7DC3DB42788FB0E4E9FA662C06DE22FC2";
408 my $key2 = "75E156271DCCF02DDD4A7A8CDF651FA0632C4F50";
410 ok($user = RT::User->new($RT::SystemUser));
411 ok($user->Load('root'), "Loaded user 'root'");
412 is($user->PreferredKey, $key1, "preferred key is set correctly");
413 $m->get("$baseurl/Prefs/Other.html");
414 like($m->content, qr/Preferred key/, "preferred key option shows up in preference");
416 # XXX: mech doesn't let us see the current value of the select, apparently
417 like($m->content, qr/$key1/, "first key shows up in preferences");
418 like($m->content, qr/$key2/, "second key shows up in preferences");
419 like($m->content, qr/$key1.*?$key2/s, "first key shows up before the second");
422 $m->select("PreferredKey" => $key2);
425 ok($user = RT::User->new($RT::SystemUser));
426 ok($user->Load('root'), "Loaded user 'root'");
427 is($user->PreferredKey, $key2, "preferred key is set correctly to the new value");
429 $m->get("$baseurl/Prefs/Other.html");
430 like($m->content, qr/Preferred key/, "preferred key option shows up in preference");
432 # XXX: mech doesn't let us see the current value of the select, apparently
433 like($m->content, qr/$key2/, "second key shows up in preferences");
434 like($m->content, qr/$key1/, "first key shows up in preferences");
435 like($m->content, qr/$key2.*?$key1/s, "second key (now preferred) shows up before the first");
437 # test that the new fields work
438 $m->get("$baseurl/Search/Simple.html?q=General");
439 my $content = $m->content;
440 $content =~ s/(/(/g;
441 $content =~ s/)/)/g;
443 like($content, qr/OO-Nobody-O/, "original OwnerName untouched");
444 like($content, qr/OO-nokey-O/, "original OwnerName untouched");
445 like($content, qr/OO-root-O/, "original OwnerName untouched");
447 like($content, qr/OR-recipient\@example.com-O/, "original Requestors untouched");
448 like($content, qr/OR-nokey\@example.com-O/, "original Requestors untouched");
450 like($content, qr/KO-root-K/, "KeyOwnerName does not issue no-pubkey warning for recipient");
451 like($content, qr/KO-nokey \(no pubkey!\)-K/, "KeyOwnerName issues no-pubkey warning for root");
452 like($content, qr/KO-Nobody \(no pubkey!\)-K/, "KeyOwnerName issues no-pubkey warning for nobody");
454 like($content, qr/KR-recipient\@example.com-K/, "KeyRequestors does not issue no-pubkey warning for recipient\@example.com");
455 like($content, qr/KR-general\@example.com-K/, "KeyRequestors does not issue no-pubkey warning for general\@example.com");
456 like($content, qr/KR-nokey\@example.com \(no pubkey!\)-K/, "KeyRequestors DOES issue no-pubkey warning for nokey\@example.com");