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