RT 4.0.22
[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 = Encode::decode("UTF-8","\303\241\303\251\303\255\303\263\303\272");
508     is (
509         $tick->Transactions->First->Content,
510         $tick->Transactions->First->Attachments->First->Content,
511         "Content is ". $tick->Transactions->First->Attachments->First->Content
512     );
513     ok (
514         $tick->Transactions->First->Content =~ /$unistring/i,
515         $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
516     );
517
518     $m->no_warnings_ok;
519 }
520
521 diag "supposedly I18N fails on the second message sent in.";
522 {
523     my $text = <<EOF;
524 From: root\@localhost
525 To: rtemail\@@{[RT->Config->Get('rtname')]}
526 Subject: This is a test of I18N ticket creation
527 Content-Type: text/plain; charset="utf-8"
528
529 2 accented lines
530 \303\242\303\252\303\256\303\264\303\273
531 \303\241\303\251\303\255\303\263\303\272
532 bye
533 EOF
534     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
535     is ($status >> 8, 0, "The mail gateway exited normally");
536     ok ($id, "created ticket");
537
538     my $tick = RT::Test->last_ticket;
539     isa_ok ($tick, 'RT::Ticket');
540     ok ($tick->Id, "found ticket ". $tick->Id);
541     is ($tick->Id, $id, "correct ticket");
542     is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket");
543
544     my $unistring = Encode::decode("UTF-8","\303\241\303\251\303\255\303\263\303\272");
545
546     ok (
547         $tick->Transactions->First->Content =~ $unistring,
548         "It appears to be unicode - ". $tick->Transactions->First->Content
549     );
550
551     $m->no_warnings_ok;
552 }
553
554 diag "make sure we check that UTF-8 is really UTF-8";
555 {
556     my $text = <<EOF;
557 From: root\@localhost
558 To: rtemail\@@{[RT->Config->Get('rtname')]}
559 Subject: This is test wrong utf-8 chars
560 Content-Type: text/plain; charset="utf-8"
561
562 utf-8: informaci\303\263n confidencial
563 latin1: informaci\363n confidencial
564
565 bye
566 EOF
567     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
568     is ($status >> 8, 0, "The mail gateway exited normally");
569     ok ($id, "created ticket");
570
571     my $tick = RT::Test->last_ticket;
572     is ($tick->Id, $id, "correct ticket");
573
574     my $content = Encode::encode("UTF-8",$tick->Transactions->First->Content);
575
576     like $content, qr{informaci\303\263n confidencial};
577     like $content, qr{informaci\357\277\275n confidencial};
578
579     $m->no_warnings_ok;
580 }
581
582 diag "check that mailgate doesn't suffer from empty Reply-To:";
583 {
584     my $text = <<EOF;
585 From: root\@localhost
586 Reply-To: 
587 To: rtemail\@@{[RT->Config->Get('rtname')]}
588 Subject: test
589 Content-Type: text/plain; charset="utf-8"
590
591 test
592 EOF
593     my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
594     is ($status >> 8, 0, "The mail gateway exited normally");
595     ok ($id, "created ticket");
596
597     my $tick = RT::Test->last_ticket;
598     isa_ok ($tick, 'RT::Ticket');
599     ok ($tick->Id, "found ticket ". $tick->Id);
600     is ($tick->Id, $id, "correct ticket");
601
602     like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
603
604     $m->no_warnings_ok;
605 }
606
607
608 my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
609 ok ($val, $msg);
610
611 SKIP: {
612 skip "Advanced mailgate actions require an unsafe configuration", 47
613     unless RT->Config->Get('UnsafeEmailCommands');
614
615 # create new queue to be shure we don't mess with rights
616 use RT::Queue;
617 my $queue = RT::Queue->new(RT->SystemUser);
618 my ($qid) = $queue->Create( Name => 'ext-mailgate');
619 ok( $qid, 'queue created for ext-mailgate tests' );
620
621
622 # create ticket that is owned by nobody
623 use RT::Ticket;
624 my $tick = RT::Ticket->new(RT->SystemUser);
625 my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
626 ok( $id, 'new ticket created' );
627 is( $tick->Owner, RT->Nobody->Id, 'owner of the new ticket is nobody' );
628
629 $! = 0;
630 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
631 print MAIL <<EOF;
632 From: root\@localhost
633 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
634
635 EOF
636 close (MAIL);
637 is ($? >> 8, 0, "The mail gateway exited normally");
638
639 $tick = RT::Ticket->new(RT->SystemUser);
640 $tick->Load( $id );
641 is( $tick->Id, $id, 'load correct ticket');
642 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
643
644 # check that there is no text transactions writen
645 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
646
647 my $status;
648 ($status, $msg) = $tick->SetOwner( RT->Nobody->Id, 'Force' );
649 ok( $status, 'successfuly changed owner: '. ($msg||'') );
650 is( $tick->Owner, RT->Nobody->Id, 'set owner back to nobody');
651
652 $m->no_warnings_ok;
653
654
655 $! = 0;
656 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
657 print MAIL <<EOF;
658 From: root\@localhost
659 Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
660
661 test
662 EOF
663 close (MAIL);
664 is ($? >> 8, 0, "The mail gateway exited normally");
665
666 DBIx::SearchBuilder::Record::Cachable->FlushCache;
667
668 $tick = RT::Ticket->new(RT->SystemUser);
669 $tick->Load( $id );
670 is( $tick->Id, $id, "load correct ticket #$id");
671 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
672 my $txns = $tick->Transactions;
673 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
674 $txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
675 # +1 because of auto open
676 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
677 is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
678
679 $m->no_warnings_ok;
680
681
682 $! = 0;
683 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
684 print MAIL <<EOF;
685 From: root\@localhost
686 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
687
688 EOF
689 close (MAIL);
690 is ($? >> 8, 0, "The mail gateway exited normally");
691
692 DBIx::SearchBuilder::Record::Cachable->FlushCache;
693
694 $tick = RT::Ticket->new(RT->SystemUser);
695 $tick->Load( $id );
696 is( $tick->Id, $id, 'load correct ticket');
697 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
698 is( $tick->Transactions->Count, 7, 'no superfluous transactions');
699
700 use RT::User;
701 my $user = RT::User->new( RT->SystemUser );
702 my ($uid) = $user->Create( Name => 'ext-mailgate',
703                            EmailAddress => 'ext-mailgate@localhost',
704                            Privileged => 1,
705                            Password => 'qwe123',
706                          );
707 ok( $uid, 'user created for ext-mailgate tests' );
708 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
709
710 $tick = RT::Ticket->new(RT->SystemUser);
711 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
712 ok( $id, 'create new ticket' );
713
714 my $rtname = RT->Config->Get('rtname');
715
716 $m->no_warnings_ok;
717
718 $! = 0;
719 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
720 print MAIL <<EOF;
721 From: ext-mailgate\@localhost
722 Subject: [$rtname \#$id] test
723
724 EOF
725 close (MAIL);
726 is ( $? >> 8, 0, "mailgate exited normally" );
727 DBIx::SearchBuilder::Record::Cachable->FlushCache;
728
729 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
730
731 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
732 ok( $status, "successfuly granted right: $msg" );
733 my $ace_id = $status;
734 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );
735
736 $m->next_warning_like(qr/Permission Denied/);
737 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
738 $m->no_leftover_warnings_ok;
739
740 $! = 0;
741 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
742 print MAIL <<EOF;
743 From: ext-mailgate\@localhost
744 Subject: [$rtname \#$id] test
745
746 correspond-take
747 EOF
748 close (MAIL);
749 is ( $? >> 8, 0, "mailgate exited normally" );
750 DBIx::SearchBuilder::Record::Cachable->FlushCache;
751
752 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
753 is( $tick->Transactions->Count, 3, "one transactions added" );
754
755 $m->next_warning_like(qr/Permission Denied/);
756 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
757 $m->no_leftover_warnings_ok;
758
759 $! = 0;
760 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
761 print MAIL <<EOF;
762 From: ext-mailgate\@localhost
763 Subject: [$rtname \#$id] test
764
765 correspond-take
766 EOF
767 close (MAIL);
768 is ( $? >> 8, 0, "mailgate exited normally" );
769 DBIx::SearchBuilder::Record::Cachable->FlushCache;
770
771 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
772 is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );
773
774 $m->next_warning_like(qr/Permission Denied/);
775 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
776 $m->no_leftover_warnings_ok;
777
778 # revoke ReplyToTicket right
779 use RT::ACE;
780 my $ace = RT::ACE->new(RT->SystemUser);
781 $ace->Load( $ace_id );
782 $ace->Delete;
783 my $acl = RT::ACL->new(RT->SystemUser);
784 $acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
785 $acl->LimitToObject( $RT::System );
786 while( my $ace = $acl->Next ) {
787         $ace->Delete;
788 }
789
790 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
791
792
793 my $group = RT::Group->new( RT->SystemUser );
794 ok( $group->LoadQueueRoleGroup( Queue => $qid, Type=> 'Owner' ), "load queue owners role group" );
795 $ace = RT::ACE->new( RT->SystemUser );
796 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
797 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );
798
799 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
800 ok( $status, "successfuly granted right: $msg" );
801 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
802 ok( $status, "successfuly granted right: $msg" );
803
804 $! = 0;
805 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
806 print MAIL <<EOF;
807 From: ext-mailgate\@localhost
808 Subject: [$rtname \#$id] test
809
810 take-correspond with reply right granted to owner role
811 EOF
812 close (MAIL);
813 is ( $? >> 8, 0, "mailgate exited normally" );
814 DBIx::SearchBuilder::Record::Cachable->FlushCache;
815
816 $tick->Load( $id );
817 is( $tick->Owner, $user->id, "we changed owner" );
818 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
819 is( $tick->Transactions->Count, 5, "transactions added" );
820
821 $m->no_warnings_ok;
822
823 };
824