import rt 3.8.7
[freeside.git] / rt / t / web / gnupg-select-keys-on-create.t
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 60;
6
7 plan skip_all => 'GnuPG required.'
8     unless eval 'use GnuPG::Interface; 1';
9 plan skip_all => 'gpg executable is required.'
10     unless RT::Test->find_executable('gpg');
11
12
13 use RT::Action::SendEmail;
14 use File::Temp qw(tempdir);
15
16 RT::Test->set_mail_catcher;
17
18 use_ok('RT::Crypt::GnuPG');
19
20 RT->Config->Set( GnuPG =>
21     Enable => 1,
22     OutgoingMessagesFormat => 'RFC',
23 );
24
25 RT->Config->Set( GnuPGOptions =>
26     homedir => scalar tempdir( CLEANUP => 0 ),
27     passphrase => 'rt-test',
28     'no-permission-warning' => undef,
29 );
30 diag "GnuPG --homedir ". RT->Config->Get('GnuPGOptions')->{'homedir'} if $ENV{TEST_VERBOSE};
31
32 RT->Config->Set( 'MailPlugins' => 'Auth::MailFrom', 'Auth::GnuPG' );
33
34 my $queue = RT::Test->load_or_create_queue(
35     Name              => 'Regression',
36     CorrespondAddress => 'rt-recipient@example.com',
37     CommentAddress    => 'rt-recipient@example.com',
38 );
39 ok $queue && $queue->id, 'loaded or created queue';
40
41 RT::Test->set_rights(
42     Principal => 'Everyone',
43     Right => ['CreateTicket', 'ShowTicket', 'SeeQueue', 'ReplyToTicket', 'ModifyTicket'],
44 );
45
46 my ($baseurl, $m) = RT::Test->started_ok;
47 ok $m->login, 'logged in';
48
49 diag "check that signing doesn't work if there is no key" if $ENV{TEST_VERBOSE};
50 {
51     RT::Test->clean_caught_mails;
52
53     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
54     $m->form_number(3);
55     $m->tick( Sign => 1 );
56     $m->field( Requestors => 'rt-test@example.com' );
57     $m->field( Content => 'Some content' );
58     $m->submit;
59     $m->content_like(
60         qr/unable to sign outgoing email messages/i,
61         'problems with passphrase'
62     );
63
64     my @mail = RT::Test->fetch_caught_mails;
65     ok !@mail, 'there are no outgoing emails';
66 }
67
68 {
69     RT::Test->import_gnupg_key('rt-recipient@example.com');
70     RT::Test->trust_gnupg_key('rt-recipient@example.com');
71     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-recipient@example.com');
72     is $res{'info'}[0]{'TrustTerse'}, 'ultimate', 'ultimately trusted key';
73 }
74
75 diag "check that things don't work if there is no key" if $ENV{TEST_VERBOSE};
76 {
77     RT::Test->clean_caught_mails;
78
79     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
80     $m->form_number(3);
81     $m->tick( Encrypt => 1 );
82     $m->field( Requestors => 'rt-test@example.com' );
83     $m->field( Content => 'Some content' );
84     $m->submit;
85     $m->content_like(
86         qr/You are going to encrypt outgoing email messages/i,
87         'problems with keys'
88     );
89     $m->content_like(
90         qr/There is no key suitable for encryption/i,
91         'problems with keys'
92     );
93
94     my $form = $m->form_number(3);
95     ok !$form->find_input( 'UseKey-rt-test@example.com' ), 'no key selector';
96
97     my @mail = RT::Test->fetch_caught_mails;
98     ok !@mail, 'there are no outgoing emails';
99 }
100
101 diag "import first key of rt-test\@example.com" if $ENV{TEST_VERBOSE};
102 my $fpr1 = '';
103 {
104     RT::Test->import_gnupg_key('rt-test@example.com', 'public');
105     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
106     is $res{'info'}[0]{'TrustLevel'}, 0, 'is not trusted key';
107     $fpr1 = $res{'info'}[0]{'Fingerprint'};
108 }
109
110 diag "check that things still doesn't work if key is not trusted" if $ENV{TEST_VERBOSE};
111 {
112     RT::Test->clean_caught_mails;
113
114     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
115     $m->form_number(3);
116     $m->tick( Encrypt => 1 );
117     $m->field( Requestors => 'rt-test@example.com' );
118     $m->field( Content => 'Some content' );
119     $m->submit;
120     $m->content_like(
121         qr/You are going to encrypt outgoing email messages/i,
122         'problems with keys'
123     );
124     $m->content_like(
125         qr/There is one suitable key, but trust level is not set/i,
126         'problems with keys'
127     );
128
129     my $form = $m->form_number(3);
130     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
131     is scalar $input->possible_values, 1, 'one option';
132
133     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
134     $m->submit;
135     $m->content_like(
136         qr/You are going to encrypt outgoing email messages/i,
137         'problems with keys'
138     );
139     $m->content_like(
140         qr/Selected key either is not trusted/i,
141         'problems with keys'
142     );
143
144     my @mail = RT::Test->fetch_caught_mails;
145     ok !@mail, 'there are no outgoing emails';
146 }
147
148 diag "import a second key of rt-test\@example.com" if $ENV{TEST_VERBOSE};
149 my $fpr2 = '';
150 {
151     RT::Test->import_gnupg_key('rt-test@example.com.2', 'public');
152     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
153     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
154     $fpr2 = $res{'info'}[2]{'Fingerprint'};
155 }
156
157 diag "check that things still doesn't work if two keys are not trusted" if $ENV{TEST_VERBOSE};
158 {
159     RT::Test->clean_caught_mails;
160
161     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
162     $m->form_number(3);
163     $m->tick( Encrypt => 1 );
164     $m->field( Requestors => 'rt-test@example.com' );
165     $m->field( Content => 'Some content' );
166     $m->submit;
167     $m->content_like(
168         qr/You are going to encrypt outgoing email messages/i,
169         'problems with keys'
170     );
171     $m->content_like(
172         qr/There are several keys suitable for encryption/i,
173         'problems with keys'
174     );
175
176     my $form = $m->form_number(3);
177     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
178     is scalar $input->possible_values, 2, 'two options';
179
180     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
181     $m->submit;
182     $m->content_like(
183         qr/You are going to encrypt outgoing email messages/i,
184         'problems with keys'
185     );
186     $m->content_like(
187         qr/Selected key either is not trusted/i,
188         'problems with keys'
189     );
190
191     my @mail = RT::Test->fetch_caught_mails;
192     ok !@mail, 'there are no outgoing emails';
193 }
194
195 {
196     RT::Test->lsign_gnupg_key( $fpr1 );
197     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
198     ok $res{'info'}[0]{'TrustLevel'} > 0, 'trusted key';
199     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
200 }
201
202 diag "check that we see key selector even if only one key is trusted but there are more keys";
203 {
204     RT::Test->clean_caught_mails;
205
206     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
207     $m->form_number(3);
208     $m->tick( Encrypt => 1 );
209     $m->field( Requestors => 'rt-test@example.com' );
210     $m->field( Content => 'Some content' );
211     $m->submit;
212     $m->content_like(
213         qr/You are going to encrypt outgoing email messages/i,
214         'problems with keys'
215     );
216     $m->content_like(
217         qr/There are several keys suitable for encryption/i,
218         'problems with keys'
219     );
220
221     my $form = $m->form_number(3);
222     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
223     is scalar $input->possible_values, 2, 'two options';
224
225     my @mail = RT::Test->fetch_caught_mails;
226     ok !@mail, 'there are no outgoing emails';
227 }
228
229 diag "check that key selector works and we can select trusted key";
230 {
231     RT::Test->clean_caught_mails;
232
233     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
234     $m->form_number(3);
235     $m->tick( Encrypt => 1 );
236     $m->field( Requestors => 'rt-test@example.com' );
237     $m->field( Content => 'Some content' );
238     $m->submit;
239     $m->content_like(
240         qr/You are going to encrypt outgoing email messages/i,
241         'problems with keys'
242     );
243     $m->content_like(
244         qr/There are several keys suitable for encryption/i,
245         'problems with keys'
246     );
247
248     my $form = $m->form_number(3);
249     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
250     is scalar $input->possible_values, 2, 'two options';
251
252     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
253     $m->submit;
254     $m->content_like( qr/Ticket \d+ created in queue/i, 'ticket created' );
255
256     my @mail = RT::Test->fetch_caught_mails;
257     ok @mail, 'there are some emails';
258     check_text_emails( { Encrypt => 1 }, @mail );
259 }
260
261 diag "check encrypting of attachments";
262 {
263     RT::Test->clean_caught_mails;
264
265     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
266     $m->form_number(3);
267     $m->tick( Encrypt => 1 );
268     $m->field( Requestors => 'rt-test@example.com' );
269     $m->field( Content => 'Some content' );
270     $m->field( Attach => $0 );
271     $m->submit;
272     $m->content_like(
273         qr/You are going to encrypt outgoing email messages/i,
274         'problems with keys'
275     );
276     $m->content_like(
277         qr/There are several keys suitable for encryption/i,
278         'problems with keys'
279     );
280
281     my $form = $m->form_number(3);
282     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
283     is scalar $input->possible_values, 2, 'two options';
284
285     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
286     $m->submit;
287     $m->content_like( qr/Ticket \d+ created in queue/i, 'ticket created' );
288
289     my @mail = RT::Test->fetch_caught_mails;
290     ok @mail, 'there are some emails';
291     check_text_emails( { Encrypt => 1, Attachment => 1 }, @mail );
292 }
293
294 sub check_text_emails {
295     my %args = %{ shift @_ };
296     my @mail = @_;
297
298     ok scalar @mail, "got some mail";
299     for my $mail (@mail) {
300         for my $type ('email', 'attachment') {
301             next if $type eq 'attachment' && !$args{'Attachment'};
302
303             my $content = $type eq 'email'
304                         ? "Some content"
305                         : "Attachment content";
306
307             if ( $args{'Encrypt'} ) {
308                 unlike $mail, qr/$content/, "outgoing $type was encrypted";
309             } else {
310                 like $mail, qr/$content/, "outgoing $type was not encrypted";
311             } 
312
313             next unless $type eq 'email';
314
315             if ( $args{'Sign'} && $args{'Encrypt'} ) {
316                 like $mail, qr/BEGIN PGP MESSAGE/, 'outgoing email was signed';
317             } elsif ( $args{'Sign'} ) {
318                 like $mail, qr/SIGNATURE/, 'outgoing email was signed';
319             } else {
320                 unlike $mail, qr/SIGNATURE/, 'outgoing email was not signed';
321             }
322         }
323     }
324 }
325