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