Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / rt / t / web / gnupg-select-keys-on-create.t
1 use strict;
2 use warnings;
3
4 use RT::Test::GnuPG tests => 83, 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::GnuPG::GetKeysInfo('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     for (1 .. 4) {
70         $m->next_warning_like(qr/public key not found/) ;
71         $m->next_warning_like(qr/above error may result from an unconfigured RT\/GPG/);
72     }
73
74     $m->no_leftover_warnings_ok;
75 }
76
77 diag "import first key of rt-test\@example.com";
78 my $fpr1 = '';
79 {
80     RT::Test->import_gnupg_key('rt-test@example.com', 'public');
81     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
82     is $res{'info'}[0]{'TrustLevel'}, 0, 'is not trusted key';
83     $fpr1 = $res{'info'}[0]{'Fingerprint'};
84 }
85
86 diag "check that things still doesn't work if key is not trusted";
87 {
88     RT::Test->clean_caught_mails;
89
90     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
91     $m->form_name('TicketCreate');
92     $m->tick( Encrypt => 1 );
93     $m->field( Requestors => 'rt-test@example.com' );
94     $m->field( Content => 'Some content' );
95     $m->submit;
96     $m->content_contains(
97         'You are going to encrypt outgoing email messages',
98         'problems with keys'
99     );
100     $m->content_contains(
101         'There is one suitable key, but trust level is not set',
102         'problems with keys'
103     );
104
105     my $form = $m->form_name('TicketCreate');
106     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
107     is scalar $input->possible_values, 1, 'one option';
108
109     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
110     $m->submit;
111     $m->content_contains(
112         'You are going to encrypt outgoing email messages',
113         'problems with keys'
114     );
115     $m->content_contains(
116         'Selected key either is not trusted',
117         'problems with keys'
118     );
119
120     my @mail = RT::Test->fetch_caught_mails;
121     ok !@mail, 'there are no outgoing emails';
122
123     $m->no_warnings_ok;
124 }
125
126 diag "import a second key of rt-test\@example.com";
127 my $fpr2 = '';
128 {
129     RT::Test->import_gnupg_key('rt-test@example.com.2', 'public');
130     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
131     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
132     $fpr2 = $res{'info'}[2]{'Fingerprint'};
133 }
134
135 diag "check that things still doesn't work if two keys are not trusted";
136 {
137     RT::Test->clean_caught_mails;
138
139     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
140     $m->form_name('TicketCreate');
141     $m->tick( Encrypt => 1 );
142     $m->field( Requestors => 'rt-test@example.com' );
143     $m->field( Content => 'Some content' );
144     $m->submit;
145     $m->content_contains(
146         'You are going to encrypt outgoing email messages',
147         'problems with keys'
148     );
149     $m->content_contains(
150         'There are several keys suitable for encryption',
151         'problems with keys'
152     );
153
154     my $form = $m->form_name('TicketCreate');
155     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
156     is scalar $input->possible_values, 2, 'two options';
157
158     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
159     $m->submit;
160     $m->content_contains(
161         'You are going to encrypt outgoing email messages',
162         'problems with keys'
163     );
164     $m->content_contains(
165         'Selected key either is not trusted',
166         'problems with keys'
167     );
168
169     my @mail = RT::Test->fetch_caught_mails;
170     ok !@mail, 'there are no outgoing emails';
171
172     $m->no_warnings_ok;
173 }
174
175 {
176     RT::Test->lsign_gnupg_key( $fpr1 );
177     my %res = RT::Crypt::GnuPG::GetKeysInfo('rt-test@example.com');
178     ok $res{'info'}[0]{'TrustLevel'} > 0, 'trusted key';
179     is $res{'info'}[1]{'TrustLevel'}, 0, 'is not trusted key';
180 }
181
182 diag "check that we see key selector even if only one key is trusted but there are more keys";
183 {
184     RT::Test->clean_caught_mails;
185
186     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
187     $m->form_name('TicketCreate');
188     $m->tick( Encrypt => 1 );
189     $m->field( Requestors => 'rt-test@example.com' );
190     $m->field( Content => 'Some content' );
191     $m->submit;
192     $m->content_contains(
193         'You are going to encrypt outgoing email messages',
194         'problems with keys'
195     );
196     $m->content_contains(
197         'There are several keys suitable for encryption',
198         'problems with keys'
199     );
200
201     my $form = $m->form_name('TicketCreate');
202     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
203     is scalar $input->possible_values, 2, 'two options';
204
205     my @mail = RT::Test->fetch_caught_mails;
206     ok !@mail, 'there are no outgoing emails';
207
208     $m->no_warnings_ok;
209 }
210
211 diag "check that key selector works and we can select trusted key";
212 {
213     RT::Test->clean_caught_mails;
214
215     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
216     $m->form_name('TicketCreate');
217     $m->tick( Encrypt => 1 );
218     $m->field( Requestors => 'rt-test@example.com' );
219     $m->field( Content => 'Some content' );
220     $m->submit;
221     $m->content_contains(
222         'You are going to encrypt outgoing email messages',
223         'problems with keys'
224     );
225     $m->content_contains(
226         'There are several keys suitable for encryption',
227         'problems with keys'
228     );
229
230     my $form = $m->form_name('TicketCreate');
231     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
232     is scalar $input->possible_values, 2, 'two options';
233
234     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
235     $m->submit;
236     $m->content_like( qr/Ticket \d+ created in queue/i, 'ticket created' );
237
238     my @mail = RT::Test->fetch_caught_mails;
239     ok @mail, 'there are some emails';
240     check_text_emails( { Encrypt => 1 }, @mail );
241
242     $m->no_warnings_ok;
243 }
244
245 diag "check encrypting of attachments";
246 {
247     RT::Test->clean_caught_mails;
248
249     ok $m->goto_create_ticket( $queue ), "UI -> create ticket";
250     $m->form_name('TicketCreate');
251     $m->tick( Encrypt => 1 );
252     $m->field( Requestors => 'rt-test@example.com' );
253     $m->field( Content => 'Some content' );
254     $m->field( Attach => $0 );
255     $m->submit;
256     $m->content_contains(
257         'You are going to encrypt outgoing email messages',
258         'problems with keys'
259     );
260     $m->content_contains(
261         'There are several keys suitable for encryption',
262         'problems with keys'
263     );
264
265     my $form = $m->form_name('TicketCreate');
266     ok my $input = $form->find_input( 'UseKey-rt-test@example.com' ), 'found key selector';
267     is scalar $input->possible_values, 2, 'two options';
268
269     $m->select( 'UseKey-rt-test@example.com' => $fpr1 );
270     $m->submit;
271     $m->content_like( qr/Ticket \d+ created in queue/i, 'ticket created' );
272
273     my @mail = RT::Test->fetch_caught_mails;
274     ok @mail, 'there are some emails';
275     check_text_emails( { Encrypt => 1, Attachment => 1 }, @mail );
276
277     $m->no_warnings_ok;
278 }
279