default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / rt / t / web / gnupg-select-keys-on-update.t
1 use strict;
2 use warnings;
3
4 use RT::Test::GnuPG tests => undef, 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->GetKeysInfo( Key => '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     $m->next_warning_like(qr/public key not found/) for 1 .. 2;
86     $m->no_leftover_warnings_ok;
87 }
88
89
90 diag "import first key of rt-test\@example.com";
91 my $fpr1 = '';
92 {
93     RT::Test->import_gnupg_key('rt-test@example.com', 'secret');
94     my %res = RT::Crypt->GetKeysInfo( Key => 'rt-test@example.com' );
95     is $res{'info'}[0]{'TrustLevel'}, 0, 'is not trusted key';
96     $fpr1 = $res{'info'}[0]{'Fingerprint'};
97 }
98
99 diag "check that things still doesn't work if key is not trusted";
100 {
101     RT::Test->clean_caught_mails;
102
103     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
104     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
105     $m->form_name('TicketUpdate');
106     $m->tick( Encrypt => 1 );
107     $m->field( UpdateCc => 'rt-test@example.com' );
108     $m->field( UpdateContent => 'Some content' );
109     $m->click('SubmitTicket');
110     $m->content_contains(
111         'You are going to encrypt outgoing email messages',
112         'problems with keys'
113     );
114     $m->content_contains(
115         'There is one suitable key, but trust level is not set',
116         'problems with keys'
117     );
118
119     my $form = $m->form_name('TicketUpdate');
120     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
121     is scalar $input->possible_values, 1, 'one option';
122
123     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
124     $m->click('SubmitTicket');
125     $m->content_contains(
126         'You are going to encrypt outgoing email messages',
127         'problems with keys'
128     );
129     $m->content_contains(
130         'Selected key either is not trusted',
131         'problems with keys'
132     );
133
134     my @mail = RT::Test->fetch_caught_mails;
135     ok !@mail, 'there are no outgoing emails';
136
137     $m->no_warnings_ok;
138 }
139
140 diag "import a second key of rt-test\@example.com";
141 my $fpr2 = '';
142 {
143     RT::Test->import_gnupg_key('rt-test@example.com.2', 'secret');
144     my %res = RT::Crypt->GetKeysInfo( Key => 'rt-test@example.com' );
145     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
146     $fpr2 = $res{'info'}[2]{'Fingerprint'};
147 }
148
149 diag "check that things still doesn't work if two keys are not trusted";
150 {
151     RT::Test->clean_caught_mails;
152
153     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
154     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
155     $m->form_name('TicketUpdate');
156     $m->tick( Encrypt => 1 );
157     $m->field( UpdateCc => 'rt-test@example.com' );
158     $m->field( UpdateContent => 'Some content' );
159     $m->click('SubmitTicket');
160     $m->content_contains(
161         'You are going to encrypt outgoing email messages',
162         'problems with keys'
163     );
164     $m->content_contains(
165         'There are several keys suitable for encryption',
166         'problems with keys'
167     );
168
169     my $form = $m->form_name('TicketUpdate');
170     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
171     is scalar $input->possible_values, 2, 'two options';
172
173     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
174     $m->click('SubmitTicket');
175     $m->content_contains(
176         'You are going to encrypt outgoing email messages',
177         'problems with keys'
178     );
179     $m->content_contains(
180         'Selected key either is not trusted',
181         'problems with keys'
182     );
183
184     my @mail = RT::Test->fetch_caught_mails;
185     ok !@mail, 'there are no outgoing emails';
186
187     $m->no_warnings_ok;
188 }
189
190 {
191     RT::Test->lsign_gnupg_key( $fpr1 );
192     my %res = RT::Crypt->GetKeysInfo( Key => 'rt-test@example.com' );
193     ok $res{'info'}[0]{'TrustLevel'} > 0, 'trusted key';
194     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
195 }
196
197 diag "check that we see key selector even if only one key is trusted but there are more keys";
198 {
199     RT::Test->clean_caught_mails;
200
201     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
202     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
203     $m->form_name('TicketUpdate');
204     $m->tick( Encrypt => 1 );
205     $m->field( UpdateCc => 'rt-test@example.com' );
206     $m->field( UpdateContent => 'Some content' );
207     $m->click('SubmitTicket');
208     $m->content_contains(
209         'You are going to encrypt outgoing email messages',
210         'problems with keys'
211     );
212     $m->content_contains(
213         'There are several keys suitable for encryption',
214         'problems with keys'
215     );
216
217     my $form = $m->form_name('TicketUpdate');
218     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
219     is scalar $input->possible_values, 2, 'two options';
220
221     my @mail = RT::Test->fetch_caught_mails;
222     ok !@mail, 'there are no outgoing emails';
223
224     $m->no_warnings_ok;
225 }
226
227 diag "check that key selector works and we can select trusted key";
228 {
229     RT::Test->clean_caught_mails;
230
231     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
232     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
233     $m->form_name('TicketUpdate');
234     $m->tick( Encrypt => 1 );
235     $m->field( UpdateCc => 'rt-test@example.com' );
236     $m->field( UpdateContent => 'Some content' );
237     $m->click('SubmitTicket');
238     $m->content_contains(
239         'You are going to encrypt outgoing email messages',
240         'problems with keys'
241     );
242     $m->content_contains(
243         'There are several keys suitable for encryption',
244         'problems with keys'
245     );
246
247     my $form = $m->form_name('TicketUpdate');
248     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
249     is scalar $input->possible_values, 2, 'two options';
250
251     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
252     $m->click('SubmitTicket');
253     $m->content_contains('Correspondence added', 'Correspondence added' );
254
255     my @mail = RT::Test->fetch_caught_mails;
256     ok @mail, 'there are some emails';
257     check_text_emails( { Encrypt => 1 }, @mail );
258
259     $m->no_warnings_ok;
260 }
261
262 diag "check encrypting of attachments";
263 for my $encrypt (0, 1) {
264     RT::Test->clean_caught_mails;
265
266     ok $m->goto_ticket( $tid ), "UI -> ticket #$tid";
267     $m->follow_link_ok( { text => 'Reply' }, 'ticket -> reply' );
268     $m->form_name('TicketUpdate');
269     $m->tick( Encrypt => 1 ) if $encrypt;
270     $m->field( UpdateCc => 'rt-test@example.com' );
271     $m->field( UpdateContent => 'Some content' );
272     $m->field( Attach => $0 );
273     $m->click('SubmitTicket');
274
275     if ($encrypt) {
276         $m->content_contains(
277             'You are going to encrypt outgoing email messages',
278             'problems with keys'
279         );
280         $m->content_contains(
281             'There are several keys suitable for encryption',
282             'problems with keys'
283         );
284
285         my $form = $m->form_name('TicketUpdate');
286         ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
287         is scalar $input->possible_values, 2, 'two options';
288
289         $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
290         $m->click('SubmitTicket');
291     }
292
293     $m->content_contains('Correspondence added', 'Correspondence added' );
294
295     my @mail = RT::Test->fetch_caught_mails;
296     ok @mail, 'there are some emails';
297     check_text_emails( { Encrypt => $encrypt, Attachment => "Attachment content" }, @mail );
298
299     $m->no_warnings_ok;
300 }
301
302 undef $m;
303 done_testing;