9482ffcb296a2289a7abada6983b9221971a722c
[freeside.git] / rt / t / mail / gateway.t
1 use strict;
2 use warnings;
3
4
5 use RT::Test config => 'Set( $UnsafeEmailCommands, 1);', tests => 228, actual_server => 1;
6 my ($baseurl, $m) = RT::Test->started_ok;
7
8 use RT::Tickets;
9
10 use MIME::Entity;
11 use Digest::MD5 qw(md5_base64);
12 use LWP::UserAgent;
13
14 # TODO: --extension queue
15
16 my $url = $m->rt_base_url;
17
18 diag "Make sure that when we call the mailgate without URL, it fails";
19 {
20     my $text = <<EOF;
21 From: root\@localhost
22 To: rt\@@{[RT->Config->Get('rtname')]}
23 Subject: This is a test of new ticket creation
24
25 Foob!
26 EOF
27     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => undef);
28     is ($status >> 8, 1, "The mail gateway exited with a failure");
29     ok (!$id, "No ticket id") or diag "by mistake ticket #$id";
30     $m->no_warnings_ok;
31 }
32
33 diag "Make sure that when we call the mailgate with wrong URL, it tempfails";
34 {
35     my $text = <<EOF;
36 From: root\@localhost
37 To: rt\@@{[RT->Config->Get('rtname')]}
38 Subject: This is a test of new ticket creation
39
40 Foob!
41 EOF
42     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => 'http://this.test.for.non-connection.is.expected.to.generate.an.error');
43     is ($status >> 8, 75, "The mail gateway exited with a failure");
44     ok (!$id, "No ticket id");
45     $m->no_warnings_ok;
46 }
47
48 my $everyone_group;
49 diag "revoke rights tests depend on";
50 {
51     $everyone_group = RT::Group->new( RT->SystemUser );
52     $everyone_group->LoadSystemInternalGroup( 'Everyone' );
53     ok ($everyone_group->Id, "Found group 'everyone'");
54
55     foreach( qw(CreateTicket ReplyToTicket CommentOnTicket) ) {
56         $everyone_group->PrincipalObj->RevokeRight(Right => $_);
57     }
58 }
59
60 diag "Test new ticket creation by root who is privileged and superuser";
61 {
62     my $text = <<EOF;
63 From: root\@localhost
64 To: rt\@@{[RT->Config->Get('rtname')]}
65 Subject: This is a test of new ticket creation
66
67 Blah!
68 Foob!
69 EOF
70
71     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
72     is ($status >> 8, 0, "The mail gateway exited normally");
73     ok ($id, "Created ticket");
74
75     my $tick = RT::Test->last_ticket;
76     isa_ok ($tick, 'RT::Ticket');
77     is ($tick->Id, $id, "correct ticket id");
78     is ($tick->Subject , 'This is a test of new ticket creation', "Created the ticket");
79     $m->no_warnings_ok;
80 }
81
82 diag "Test the 'X-RT-Mail-Extension' field in the header of a ticket";
83 {
84     my $text = <<EOF;
85 From: root\@localhost
86 To: rt\@@{[RT->Config->Get('rtname')]}
87 Subject: This is a test of the X-RT-Mail-Extension field
88 Blah!
89 Foob!
90 EOF
91     local $ENV{'EXTENSION'} = "bad value with\nnewlines\n";
92     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
93     is ($status >> 8, 0, "The mail gateway exited normally");
94     ok ($id, "Created ticket #$id");
95
96     my $tick = RT::Test->last_ticket;
97     isa_ok ($tick, 'RT::Ticket');
98     is ($tick->Id, $id, "correct ticket id");
99     is ($tick->Subject, 'This is a test of the X-RT-Mail-Extension field', "Created the ticket");
100
101     my $transactions = $tick->Transactions;
102     $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
103     $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
104     my $txn = $transactions->First;
105     isa_ok ($txn, 'RT::Transaction');
106     is ($txn->Type, 'Create', "correct type");
107
108     my $attachment = $txn->Attachments->First;
109     isa_ok ($attachment, 'RT::Attachment');
110     # XXX: We eat all newlines in header, that's not what RFC's suggesting
111     is (
112         $attachment->GetHeader('X-RT-Mail-Extension'),
113         "bad value with newlines",
114         'header is in place, without trailing newline char'
115     );
116     $m->no_warnings_ok;
117 }
118
119 diag "Make sure that not standard --extension is passed";
120 {
121     my $text = <<EOF;
122 From: root\@localhost
123 To: rt\@@{[RT->Config->Get('rtname')]}
124 Subject: This is a test of new ticket creation
125
126 Foob!
127 EOF
128     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
129     is ($status >> 8, 0, "The mail gateway exited normally");
130     ok ($id, "Created ticket #$id");
131
132     my $tick = RT::Test->last_ticket;
133     isa_ok ($tick, 'RT::Ticket');
134     is ($tick->Id, $id, "correct ticket id");
135
136     my $transactions = $tick->Transactions;
137     $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
138     $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
139     my $txn = $transactions->First;
140     isa_ok ($txn, 'RT::Transaction');
141     is ($txn->Type, 'Create', "correct type");
142
143     my $attachment = $txn->Attachments->First;
144     isa_ok ($attachment, 'RT::Attachment');
145     is (
146         $attachment->GetHeader('X-RT-Mail-Extension'),
147         'some-extension-arg',
148         'header is in place'
149     );
150     $m->no_warnings_ok;
151 }
152
153 diag "Test new ticket creation without --action argument";
154 {
155     my $text = <<EOF;
156 From: root\@localhost
157 To: rt\@$RT::rtname
158 Subject: using mailgate without --action arg
159
160 Blah!
161 Foob!
162 EOF
163     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
164     is ($status >> 8, 0, "The mail gateway exited normally");
165     ok ($id, "Created ticket #$id");
166
167     my $tick = RT::Test->last_ticket;
168     isa_ok ($tick, 'RT::Ticket');
169     is ($tick->Id, $id, "correct ticket id");
170     is ($tick->Subject, 'using mailgate without --action arg', "using mailgate without --action arg");
171     $m->no_warnings_ok;
172 }
173
174 diag "This is a test of new ticket creation as an unknown user";
175 {
176     my $text = <<EOF;
177 From: doesnotexist\@@{[RT->Config->Get('rtname')]}
178 To: rt\@@{[RT->Config->Get('rtname')]}
179 Subject: This is a test of new ticket creation as an unknown user
180
181 Blah!
182 Foob!
183 EOF
184     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
185     is ($status >> 8, 0, "The mail gateway exited normally");
186     ok (!$id, "no ticket created");
187
188     my $tick = RT::Test->last_ticket;
189     isa_ok ($tick, 'RT::Ticket');
190     ok ($tick->Id, "found ticket ".$tick->Id);
191     isnt ($tick->Subject , 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account");
192
193     my $u = RT::User->new(RT->SystemUser);
194     $u->Load("doesnotexist\@@{[RT->Config->Get('rtname')]}");
195     ok( !$u->Id, "user does not exist and was not created by failed ticket submission");
196
197     $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email/);
198     $m->next_warning_like(qr/RT could not load a valid user/);
199     TODO: {
200         local $TODO = "we're a bit noisy for this warning case";
201         $m->no_leftover_warnings_ok;
202     }
203 }
204
205 diag "grant everybody with CreateTicket right";
206 {
207     ok( RT::Test->set_rights(
208         { Principal => $everyone_group->PrincipalObj,
209           Right => [qw(CreateTicket)],
210         },
211     ), "Granted everybody the right to create tickets");
212 }
213
214 my $ticket_id;
215 diag "now everybody can create tickets. can a random unkown user create tickets?";
216 {
217     my $text = <<EOF;
218 From: doesnotexist\@@{[RT->Config->Get('rtname')]}
219 To: rt\@@{[RT->Config->Get('rtname')]}
220 Subject: This is a test of new ticket creation as an unknown user
221
222 Blah!
223 Foob!
224 EOF
225     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
226     is ($status >> 8, 0, "The mail gateway exited normally");
227     ok ($id, "ticket created");
228
229     my $tick = RT::Test->last_ticket;
230     isa_ok ($tick, 'RT::Ticket');
231     ok ($tick->Id, "found ticket ".$tick->Id);
232     is ($tick->Id, $id, "correct ticket id");
233     is ($tick->Subject , 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account");
234
235     my $u = RT::User->new( RT->SystemUser );
236     $u->Load( "doesnotexist\@@{[RT->Config->Get('rtname')]}" );
237     ok ($u->Id, "user does not exist and was created by ticket submission");
238     $ticket_id = $id;
239     $m->no_warnings_ok;
240 }
241
242 diag "can another random reply to a ticket without being granted privs? answer should be no.";
243 {
244     my $text = <<EOF;
245 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
246 To: rt\@@{[RT->Config->Get('rtname')]}
247 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
248
249 Blah!  (Should not work.)
250 Foob!
251 EOF
252     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
253     is ($status >> 8, 0, "The mail gateway exited normally");
254     ok (!$id, "no way to reply to the ticket");
255
256     my $u = RT::User->new(RT->SystemUser);
257     $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
258     ok( !$u->Id, " user does not exist and was not created by ticket correspondence submission");
259     $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email \(doesnotexist-2\@example\.com\)/);
260     TODO: {
261         local $TODO = "we're a bit noisy for this warning case";
262         $m->no_leftover_warnings_ok;
263     }
264 }
265
266 diag "grant everyone 'ReplyToTicket' right";
267 {
268     ok( RT::Test->set_rights(
269         { Principal => $everyone_group->PrincipalObj,
270           Right => [qw(CreateTicket ReplyToTicket)],
271         },
272     ), "Granted everybody the right to reply to tickets" );
273 }
274
275 diag "can another random reply to a ticket after being granted privs? answer should be yes";
276 {
277     my $text = <<EOF;
278 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
279 To: rt\@@{[RT->Config->Get('rtname')]}
280 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
281
282 Blah!
283 Foob!
284 EOF
285     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
286     is ($status >> 8, 0, "The mail gateway exited normally");
287     is ($id, $ticket_id, "replied to the ticket");
288
289     my $u = RT::User->new(RT->SystemUser);
290     $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
291     ok ($u->Id, "user exists and was created by ticket correspondence submission");
292     $m->no_warnings_ok;
293 }
294
295 diag "add a reply to the ticket using '--extension ticket' feature";
296 {
297     my $text = <<EOF;
298 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
299 To: rt\@@{[RT->Config->Get('rtname')]}
300 Subject: This is a test of a reply as an unknown user
301
302 Blah!
303 Foob!
304 EOF
305     local $ENV{'EXTENSION'} = $ticket_id;
306     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'ticket');
307     is ($status >> 8, 0, "The mail gateway exited normally");
308     is ($id, $ticket_id, "replied to the ticket");
309
310     my $tick = RT::Test->last_ticket;
311     isa_ok ($tick, 'RT::Ticket');
312     ok ($tick->Id, "found ticket ".$tick->Id);
313     is ($tick->Id, $id, "correct ticket id");
314
315     my $transactions = $tick->Transactions;
316     $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
317     $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
318     my $txn = $transactions->First;
319     isa_ok ($txn, 'RT::Transaction');
320     is ($txn->Type, 'Correspond', "correct type");
321
322     my $attachment = $txn->Attachments->First;
323     isa_ok ($attachment, 'RT::Attachment');
324     is ($attachment->GetHeader('X-RT-Mail-Extension'), $id, 'header is in place');
325     $m->no_warnings_ok;
326 }
327
328 diag "can another random comment on a ticket without being granted privs? answer should be no";
329 {
330     my $text = <<EOF;
331 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
332 To: rt\@@{[RT->Config->Get('rtname')]}
333 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
334
335 Blah!  (Should not work.)
336 Foob!
337 EOF
338     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
339     is ($status >> 8, 0, "The mail gateway exited normally");
340     ok (!$id, "no way to comment on the ticket");
341
342     my $u = RT::User->new(RT->SystemUser);
343     $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
344     ok( !$u->Id, " user does not exist and was not created by ticket comment submission");
345     $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email \(doesnotexist-3\@example\.com\)/);
346     TODO: {
347         local $TODO = "we're a bit noisy for this warning case";
348         $m->no_leftover_warnings_ok;
349     }
350 }
351
352
353 diag "grant everyone 'CommentOnTicket' right";
354 {
355     ok( RT::Test->set_rights(
356         { Principal => $everyone_group->PrincipalObj,
357           Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
358         },
359     ), "Granted everybody the right to comment on tickets");
360 }
361
362 diag "can another random reply to a ticket after being granted privs? answer should be yes";
363 {
364     my $text = <<EOF;
365 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
366 To: rt\@@{[RT->Config->Get('rtname')]}
367 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
368
369 Blah!
370 Foob!
371 EOF
372     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
373     is ($status >> 8, 0, "The mail gateway exited normally");
374     is ($id, $ticket_id, "replied to the ticket");
375
376     my $u = RT::User->new(RT->SystemUser);
377     $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
378     ok ($u->Id, " user exists and was created by ticket comment submission");
379     $m->no_warnings_ok;
380 }
381
382 diag "add comment to the ticket using '--extension action' feature";
383 {
384     my $text = <<EOF;
385 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
386 To: rt\@@{[RT->Config->Get('rtname')]}
387 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment via '--extension action'
388
389 Blah!
390 Foob!
391 EOF
392     local $ENV{'EXTENSION'} = 'comment';
393     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'action');
394     is ($status >> 8, 0, "The mail gateway exited normally");
395     is ($id, $ticket_id, "added comment to the ticket");
396
397     my $tick = RT::Test->last_ticket;
398     isa_ok ($tick, 'RT::Ticket');
399     ok ($tick->Id, "found ticket ".$tick->Id);
400     is ($tick->Id, $id, "correct ticket id");
401
402     my $transactions = $tick->Transactions;
403     $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
404     $transactions->Limit(
405         FIELD => 'Type',
406         OPERATOR => 'NOT ENDSWITH',
407         VALUE => 'EmailRecord',
408         ENTRYAGGREGATOR => 'AND',
409     );
410     my $txn = $transactions->First;
411     isa_ok ($txn, 'RT::Transaction');
412     is ($txn->Type, 'Comment', "correct type");
413
414     my $attachment = $txn->Attachments->First;
415     isa_ok ($attachment, 'RT::Attachment');
416     is ($attachment->GetHeader('X-RT-Mail-Extension'), 'comment', 'header is in place');
417     $m->no_warnings_ok;
418 }
419
420 diag "Testing preservation of binary attachments";
421 {
422     # Get a binary blob (Best Practical logo) 
423     my $LOGO_FILE = $RT::MasonComponentRoot .'/NoAuth/images/bpslogo.png';
424
425     # Create a mime entity with an attachment
426     my $entity = MIME::Entity->build(
427         From    => 'root@localhost',
428         To      => 'rt@localhost',
429         Subject => 'binary attachment test',
430         Data    => ['This is a test of a binary attachment'],
431     );
432
433     $entity->attach(
434         Path     => $LOGO_FILE,
435         Type     => 'image/png',
436         Encoding => 'base64',
437     );
438     # Create a ticket with a binary attachment
439     my ($status, $id) = RT::Test->send_via_mailgate_and_http($entity);
440     is ($status >> 8, 0, "The mail gateway exited normally");
441     ok ($id, "created ticket");
442
443     my $tick = RT::Test->last_ticket;
444     isa_ok ($tick, 'RT::Ticket');
445     ok ($tick->Id, "found ticket ".$tick->Id);
446     is ($tick->Id, $id, "correct ticket id");
447     is ($tick->Subject , 'binary attachment test', "Created the ticket - ".$tick->Id);
448
449     my $file = `cat $LOGO_FILE`;
450     ok ($file, "Read in the logo image");
451     diag "for the raw file the md5 hex is ". Digest::MD5::md5_hex($file);
452
453     # Verify that the binary attachment is valid in the database
454     my $attachments = RT::Attachments->new(RT->SystemUser);
455     $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/png');
456     my $txn_alias = $attachments->Join(
457         ALIAS1 => 'main',
458         FIELD1 => 'TransactionId',
459         TABLE2 => 'Transactions',
460         FIELD2 => 'id',
461     );
462     $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectType', VALUE => 'RT::Ticket' );
463     $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectId', VALUE => $id );
464     is ($attachments->Count, 1, 'Found only one png attached to the ticket');
465     my $attachment = $attachments->First;
466     ok ($attachment->Id, 'loaded attachment object');
467     my $acontent = $attachment->Content;
468
469     diag "coming from the database, md5 hex is ".Digest::MD5::md5_hex($acontent);
470     is ($acontent, $file, 'The attachment isn\'t screwed up in the database.');
471
472     # Grab the binary attachment via the web ui
473     my $ua = new LWP::UserAgent;
474     my $full_url = "$url/Ticket/Attachment/". $attachment->TransactionId
475         ."/". $attachment->id. "/bpslogo.png?&user=root&pass=password";
476     my $r = $ua->get( $full_url );
477
478     # Verify that the downloaded attachment is the same as what we uploaded.
479     is ($file, $r->content, 'The attachment isn\'t screwed up in download');
480
481     $m->no_warnings_ok;
482 }
483
484 diag "Simple I18N testing";
485 {
486     my $text = <<EOF;
487 From: root\@localhost
488 To: rtemail\@@{[RT->Config->Get('rtname')]}
489 Subject: This is a test of I18N ticket creation
490 Content-Type: text/plain; charset="utf-8"
491
492 2 accented lines
493 \303\242\303\252\303\256\303\264\303\273
494 \303\241\303\251\303\255\303\263\303\272
495 bye
496 EOF
497     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
498     is ($status >> 8, 0, "The mail gateway exited normally");
499     ok ($id, "created ticket");
500
501     my $tick = RT::Test->last_ticket;
502     isa_ok ($tick, 'RT::Ticket');
503     ok ($tick->Id, "found ticket ". $tick->Id);
504     is ($tick->Id, $id, "correct ticket");
505     is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket - ". $tick->Subject);
506
507     my $unistring = "\303\241\303\251\303\255\303\263\303\272";
508     Encode::_utf8_on($unistring);
509     is (
510         $tick->Transactions->First->Content,
511         $tick->Transactions->First->Attachments->First->Content,
512         "Content is ". $tick->Transactions->First->Attachments->First->Content
513     );
514     ok (
515         $tick->Transactions->First->Content =~ /$unistring/i,
516         $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
517     );
518
519     $m->no_warnings_ok;
520 }
521
522 diag "supposedly I18N fails on the second message sent in.";
523 {
524     my $text = <<EOF;
525 From: root\@localhost
526 To: rtemail\@@{[RT->Config->Get('rtname')]}
527 Subject: This is a test of I18N ticket creation
528 Content-Type: text/plain; charset="utf-8"
529
530 2 accented lines
531 \303\242\303\252\303\256\303\264\303\273
532 \303\241\303\251\303\255\303\263\303\272
533 bye
534 EOF
535     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
536     is ($status >> 8, 0, "The mail gateway exited normally");
537     ok ($id, "created ticket");
538
539     my $tick = RT::Test->last_ticket;
540     isa_ok ($tick, 'RT::Ticket');
541     ok ($tick->Id, "found ticket ". $tick->Id);
542     is ($tick->Id, $id, "correct ticket");
543     is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket");
544
545     my $unistring = "\303\241\303\251\303\255\303\263\303\272";
546     Encode::_utf8_on($unistring);
547
548     ok (
549         $tick->Transactions->First->Content =~ $unistring,
550         "It appears to be unicode - ". $tick->Transactions->First->Content
551     );
552
553     $m->no_warnings_ok;
554 }
555
556 diag "make sure we check that UTF-8 is really UTF-8";
557 {
558     my $text = <<EOF;
559 From: root\@localhost
560 To: rtemail\@@{[RT->Config->Get('rtname')]}
561 Subject: This is test wrong utf-8 chars
562 Content-Type: text/plain; charset="utf-8"
563
564 utf-8: informaci\303\263n confidencial
565 latin1: informaci\363n confidencial
566
567 bye
568 EOF
569     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
570     is ($status >> 8, 0, "The mail gateway exited normally");
571     ok ($id, "created ticket");
572
573     my $tick = RT::Test->last_ticket;
574     is ($tick->Id, $id, "correct ticket");
575
576     my $content = $tick->Transactions->First->Content;
577     Encode::_utf8_off($content);
578
579     like $content, qr{informaci\303\263n confidencial};
580     like $content, qr{informaci\357\277\275n confidencial};
581
582     $m->no_warnings_ok;
583 }
584
585 diag "check that mailgate doesn't suffer from empty Reply-To:";
586 {
587     my $text = <<EOF;
588 From: root\@localhost
589 Reply-To: 
590 To: rtemail\@@{[RT->Config->Get('rtname')]}
591 Subject: test
592 Content-Type: text/plain; charset="utf-8"
593
594 test
595 EOF
596     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
597     is ($status >> 8, 0, "The mail gateway exited normally");
598     ok ($id, "created ticket");
599
600     my $tick = RT::Test->last_ticket;
601     isa_ok ($tick, 'RT::Ticket');
602     ok ($tick->Id, "found ticket ". $tick->Id);
603     is ($tick->Id, $id, "correct ticket");
604
605     like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
606
607     $m->no_warnings_ok;
608 }
609
610
611 my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
612 ok ($val, $msg);
613
614 SKIP: {
615 skip "Advanced mailgate actions require an unsafe configuration", 47
616     unless RT->Config->Get('UnsafeEmailCommands');
617
618 # create new queue to be shure we don't mess with rights
619 use RT::Queue;
620 my $queue = RT::Queue->new(RT->SystemUser);
621 my ($qid) = $queue->Create( Name => 'ext-mailgate');
622 ok( $qid, 'queue created for ext-mailgate tests' );
623
624
625 # create ticket that is owned by nobody
626 use RT::Ticket;
627 my $tick = RT::Ticket->new(RT->SystemUser);
628 my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
629 ok( $id, 'new ticket created' );
630 is( $tick->Owner, RT->Nobody->Id, 'owner of the new ticket is nobody' );
631
632 $! = 0;
633 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
634 print MAIL <<EOF;
635 From: root\@localhost
636 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
637
638 EOF
639 close (MAIL);
640 is ($? >> 8, 0, "The mail gateway exited normally");
641
642 $tick = RT::Ticket->new(RT->SystemUser);
643 $tick->Load( $id );
644 is( $tick->Id, $id, 'load correct ticket');
645 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
646
647 # check that there is no text transactions writen
648 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
649
650 my $status;
651 ($status, $msg) = $tick->SetOwner( RT->Nobody->Id, 'Force' );
652 ok( $status, 'successfuly changed owner: '. ($msg||'') );
653 is( $tick->Owner, RT->Nobody->Id, 'set owner back to nobody');
654
655 $m->no_warnings_ok;
656
657
658 $! = 0;
659 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
660 print MAIL <<EOF;
661 From: root\@localhost
662 Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
663
664 test
665 EOF
666 close (MAIL);
667 is ($? >> 8, 0, "The mail gateway exited normally");
668
669 DBIx::SearchBuilder::Record::Cachable->FlushCache;
670
671 $tick = RT::Ticket->new(RT->SystemUser);
672 $tick->Load( $id );
673 is( $tick->Id, $id, "load correct ticket #$id");
674 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
675 my $txns = $tick->Transactions;
676 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
677 $txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
678 # +1 because of auto open
679 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
680 is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
681
682 $m->no_warnings_ok;
683
684
685 $! = 0;
686 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
687 print MAIL <<EOF;
688 From: root\@localhost
689 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
690
691 EOF
692 close (MAIL);
693 is ($? >> 8, 0, "The mail gateway exited normally");
694
695 DBIx::SearchBuilder::Record::Cachable->FlushCache;
696
697 $tick = RT::Ticket->new(RT->SystemUser);
698 $tick->Load( $id );
699 is( $tick->Id, $id, 'load correct ticket');
700 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
701 is( $tick->Transactions->Count, 7, 'no superfluous transactions');
702
703 use RT::User;
704 my $user = RT::User->new( RT->SystemUser );
705 my ($uid) = $user->Create( Name => 'ext-mailgate',
706                            EmailAddress => 'ext-mailgate@localhost',
707                            Privileged => 1,
708                            Password => 'qwe123',
709                          );
710 ok( $uid, 'user created for ext-mailgate tests' );
711 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
712
713 $tick = RT::Ticket->new(RT->SystemUser);
714 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
715 ok( $id, 'create new ticket' );
716
717 my $rtname = RT->Config->Get('rtname');
718
719 $m->no_warnings_ok;
720
721 $! = 0;
722 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
723 print MAIL <<EOF;
724 From: ext-mailgate\@localhost
725 Subject: [$rtname \#$id] test
726
727 EOF
728 close (MAIL);
729 is ( $? >> 8, 0, "mailgate exited normally" );
730 DBIx::SearchBuilder::Record::Cachable->FlushCache;
731
732 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
733
734 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
735 ok( $status, "successfuly granted right: $msg" );
736 my $ace_id = $status;
737 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );
738
739 $m->next_warning_like(qr/Permission Denied/);
740 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
741 $m->no_leftover_warnings_ok;
742
743 $! = 0;
744 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
745 print MAIL <<EOF;
746 From: ext-mailgate\@localhost
747 Subject: [$rtname \#$id] test
748
749 correspond-take
750 EOF
751 close (MAIL);
752 is ( $? >> 8, 0, "mailgate exited normally" );
753 DBIx::SearchBuilder::Record::Cachable->FlushCache;
754
755 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
756 is( $tick->Transactions->Count, 3, "one transactions added" );
757
758 $m->next_warning_like(qr/Permission Denied/);
759 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
760 $m->no_leftover_warnings_ok;
761
762 $! = 0;
763 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
764 print MAIL <<EOF;
765 From: ext-mailgate\@localhost
766 Subject: [$rtname \#$id] test
767
768 correspond-take
769 EOF
770 close (MAIL);
771 is ( $? >> 8, 0, "mailgate exited normally" );
772 DBIx::SearchBuilder::Record::Cachable->FlushCache;
773
774 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
775 is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );
776
777 $m->next_warning_like(qr/Permission Denied/);
778 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
779 $m->no_leftover_warnings_ok;
780
781 # revoke ReplyToTicket right
782 use RT::ACE;
783 my $ace = RT::ACE->new(RT->SystemUser);
784 $ace->Load( $ace_id );
785 $ace->Delete;
786 my $acl = RT::ACL->new(RT->SystemUser);
787 $acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
788 $acl->LimitToObject( $RT::System );
789 while( my $ace = $acl->Next ) {
790         $ace->Delete;
791 }
792
793 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
794
795
796 my $group = RT::Group->new( RT->SystemUser );
797 ok( $group->LoadQueueRoleGroup( Queue => $qid, Type=> 'Owner' ), "load queue owners role group" );
798 $ace = RT::ACE->new( RT->SystemUser );
799 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
800 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );
801
802 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
803 ok( $status, "successfuly granted right: $msg" );
804 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
805 ok( $status, "successfuly granted right: $msg" );
806
807 $! = 0;
808 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
809 print MAIL <<EOF;
810 From: ext-mailgate\@localhost
811 Subject: [$rtname \#$id] test
812
813 take-correspond with reply right granted to owner role
814 EOF
815 close (MAIL);
816 is ( $? >> 8, 0, "mailgate exited normally" );
817 DBIx::SearchBuilder::Record::Cachable->FlushCache;
818
819 $tick->Load( $id );
820 is( $tick->Owner, $user->id, "we changed owner" );
821 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
822 is( $tick->Transactions->Count, 5, "transactions added" );
823
824 $m->no_warnings_ok;
825
826 };
827