RT 4.0.19
[freeside.git] / rt / t / web / gnupg-select-keys-on-update.t
1 use strict;
2 use warnings;
3
4 use RT::Test::GnuPG tests => 88, gnupg_options => { passphrase => 'rt-test' };
5
6 use RT::Action::SendEmail;
7
8 my $queue = RT::Test->load_or_create_queue(
9     Name              => 'Regression',
10     CorrespondAddress => 'rt-recipient@example.com',
11     CommentAddress    => 'rt-recipient@example.com',
12 );
13 ok $queue && $queue->id, 'loaded or created queue';
14
15 my ($baseurl, $m) = RT::Test->started_ok;
16 ok $m->login, 'logged in';
17
18
19 my $tid;
20 {
21     my $ticket = RT::Ticket->new( RT->SystemUser );
22     ($tid) = $ticket->Create(
23         Subject   => 'test',
24         Queue     => $queue->id,
25     );
26     ok $tid, 'ticket created';
27 }
28
29 diag "check that signing doesn't work if there is no key";
30 {
31     RT::Test->clean_caught_mails;
32
33     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
34     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
35     $m->form_name('TicketUpdate');
36     $m->tick( Sign => 1 );
37     $m->field( UpdateCc => 'rt-test@example.com' );
38     $m->field( UpdateContent => 'Some content' );
39     $m->click('SubmitTicket');
40     $m->content_contains(
41         'unable to sign outgoing email messages',
42         'problems with passphrase'
43     );
44
45     my @mail = RT::Test->fetch_caught_mails;
46     ok !@mail, 'there are no outgoing emails';
47
48     $m->next_warning_like(qr/secret key not available/);
49     $m->no_leftover_warnings_ok;
50 }
51
52 {
53     RT::Test->import_gnupg_key('rt-recipient@example.com');
54     RT::Test->trust_gnupg_key('rt-recipient@example.com');
55     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-recipient@example.com');
56     is $res{'info'}[0]{'TrustTerse'}, 'ultimate', 'ultimately trusted key';
57 }
58
59 diag "check that things don't work if there is no key";
60 {
61     RT::Test->clean_caught_mails;
62
63     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
64     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
65     $m->form_name('TicketUpdate');
66     $m->tick( Encrypt => 1 );
67     $m->field( UpdateCc => 'rt-test@example.com' );
68     $m->field( UpdateContent => 'Some content' );
69     $m->click('SubmitTicket');
70     $m->content_contains(
71         'You are going to encrypt outgoing email messages',
72         'problems with keys'
73     );
74     $m->content_contains(
75         'There is no key suitable for encryption',
76         'problems with keys'
77     );
78
79     my $form = $m->form_name('TicketUpdate');
80     ok !$form->find_input( 'UseKey-rt-test@example.com' ), 'no key selector';
81
82     my @mail = RT::Test->fetch_caught_mails;
83     ok !@mail, 'there are no outgoing emails';
84
85     for (1 .. 2) {
86         $m->next_warning_like(qr/public key not found/);
87         $m->next_warning_like(qr/above error may result from an unconfigured RT\/GPG/);
88     }
89     $m->no_leftover_warnings_ok;
90 }
91
92
93 diag "import first key of rt-test\@example.com";
94 my $fpr1 = '';
95 {
96     RT::Test->import_gnupg_key('rt-test@example.com', 'public');
97     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
98     is $res{'info'}[0]{'TrustLevel'}, 0, 'is not trusted key';
99     $fpr1 = $res{'info'}[0]{'Fingerprint'};
100 }
101
102 diag "check that things still doesn't work if key is not trusted";
103 {
104     RT::Test->clean_caught_mails;
105
106     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
107     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
108     $m->form_name('TicketUpdate');
109     $m->tick( Encrypt => 1 );
110     $m->field( UpdateCc => 'rt-test@example.com' );
111     $m->field( UpdateContent => 'Some content' );
112     $m->click('SubmitTicket');
113     $m->content_contains(
114         'You are going to encrypt outgoing email messages',
115         'problems with keys'
116     );
117     $m->content_contains(
118         'There is one suitable key, but trust level is not set',
119         'problems with keys'
120     );
121
122     my $form = $m->form_name('TicketUpdate');
123     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
124     is scalar $input->possible_values, 1, 'one option';
125
126     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
127     $m->click('SubmitTicket');
128     $m->content_contains(
129         'You are going to encrypt outgoing email messages',
130         'problems with keys'
131     );
132     $m->content_contains(
133         'Selected key either is not trusted',
134         'problems with keys'
135     );
136
137     my @mail = RT::Test->fetch_caught_mails;
138     ok !@mail, 'there are no outgoing emails';
139
140     $m->no_warnings_ok;
141 }
142
143 diag "import a second key of rt-test\@example.com";
144 my $fpr2 = '';
145 {
146     RT::Test->import_gnupg_key('rt-test@example.com.2', 'public');
147     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
148     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
149     $fpr2 = $res{'info'}[2]{'Fingerprint'};
150 }
151
152 diag "check that things still doesn't work if two keys are not trusted";
153 {
154     RT::Test->clean_caught_mails;
155
156     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
157     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
158     $m->form_name('TicketUpdate');
159     $m->tick( Encrypt => 1 );
160     $m->field( UpdateCc => 'rt-test@example.com' );
161     $m->field( UpdateContent => 'Some content' );
162     $m->click('SubmitTicket');
163     $m->content_contains(
164         'You are going to encrypt outgoing email messages',
165         'problems with keys'
166     );
167     $m->content_contains(
168         'There are several keys suitable for encryption',
169         'problems with keys'
170     );
171
172     my $form = $m->form_name('TicketUpdate');
173     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
174     is scalar $input->possible_values, 2, 'two options';
175
176     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
177     $m->click('SubmitTicket');
178     $m->content_contains(
179         'You are going to encrypt outgoing email messages',
180         'problems with keys'
181     );
182     $m->content_contains(
183         'Selected key either is not trusted',
184         'problems with keys'
185     );
186
187     my @mail = RT::Test->fetch_caught_mails;
188     ok !@mail, 'there are no outgoing emails';
189
190     $m->no_warnings_ok;
191 }
192
193 {
194     RT::Test->lsign_gnupg_key( $fpr1 );
195     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
196     ok $res{'info'}[0]{'TrustLevel'} > 0, 'trusted key';
197     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
198 }
199
200 diag "check that we see key selector even if only one key is trusted but there are more keys";
201 {
202     RT::Test->clean_caught_mails;
203
204     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
205     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
206     $m->form_name('TicketUpdate');
207     $m->tick( Encrypt => 1 );
208     $m->field( UpdateCc => 'rt-test@example.com' );
209     $m->field( UpdateContent => 'Some content' );
210     $m->click('SubmitTicket');
211     $m->content_contains(
212         'You are going to encrypt outgoing email messages',
213         'problems with keys'
214     );
215     $m->content_contains(
216         'There are several keys suitable for encryption',
217         'problems with keys'
218     );
219
220     my $form = $m->form_name('TicketUpdate');
221     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
222     is scalar $input->possible_values, 2, 'two options';
223
224     my @mail = RT::Test->fetch_caught_mails;
225     ok !@mail, 'there are no outgoing emails';
226
227     $m->no_warnings_ok;
228 }
229
230 diag "check that key selector works and we can select trusted key";
231 {
232     RT::Test->clean_caught_mails;
233
234     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
235     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
236     $m->form_name('TicketUpdate');
237     $m->tick( Encrypt => 1 );
238     $m->field( UpdateCc => 'rt-test@example.com' );
239     $m->field( UpdateContent => 'Some content' );
240     $m->click('SubmitTicket');
241     $m->content_contains(
242         'You are going to encrypt outgoing email messages',
243         'problems with keys'
244     );
245     $m->content_contains(
246         'There are several keys suitable for encryption',
247         'problems with keys'
248     );
249
250     my $form = $m->form_name('TicketUpdate');
251     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
252     is scalar $input->possible_values, 2, 'two options';
253
254     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
255     $m->click('SubmitTicket');
256     $m->content_contains('Message recorded', 'Message recorded' );
257
258     my @mail = RT::Test->fetch_caught_mails;
259     ok @mail, 'there are some emails';
260     check_text_emails( { Encrypt => 1 }, @mail );
261
262     $m->no_warnings_ok;
263 }
264
265 diag "check encrypting of attachments";
266 {
267     RT::Test->clean_caught_mails;
268
269     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
270     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
271     $m->form_name('TicketUpdate');
272     $m->tick( Encrypt => 1 );
273     $m->field( UpdateCc => 'rt-test@example.com' );
274     $m->field( UpdateContent => 'Some content' );
275     $m->field( Attach => $0 );
276     $m->click('SubmitTicket');
277     $m->content_contains(
278         'You are going to encrypt outgoing email messages',
279         'problems with keys'
280     );
281     $m->content_contains(
282         'There are several keys suitable for encryption',
283         'problems with keys'
284     );
285
286     my $form = $m->form_name('TicketUpdate');
287     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
288     is scalar $input->possible_values, 2, 'two options';
289
290     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
291     $m->click('SubmitTicket');
292     $m->content_contains('Message recorded', 'Message recorded' );
293
294     my @mail = RT::Test->fetch_caught_mails;
295     ok @mail, 'there are some emails';
296     check_text_emails( { Encrypt => 1, Attachment => 1 }, @mail );
297
298     $m->no_warnings_ok;
299 }