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