2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/copyleft/gpl.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
52 rt-mailgate - Mail interface to RT3.
60 use RT::Test config => 'Set( $UnsafeEmailCommands, 1);', tests => 228, actual_server => 1;
61 my ($baseurl, $m) = RT::Test->started_ok;
66 use Digest::MD5 qw(md5_base64);
69 # TODO: --extension queue
71 my $url = $m->rt_base_url;
73 diag "Make sure that when we call the mailgate without URL, it fails";
77 To: rt\@@{[RT->Config->Get('rtname')]}
78 Subject: This is a test of new ticket creation
82 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => undef);
83 is ($status >> 8, 1, "The mail gateway exited with a failure");
84 ok (!$id, "No ticket id") or diag "by mistake ticket #$id";
88 diag "Make sure that when we call the mailgate with wrong URL, it tempfails";
92 To: rt\@@{[RT->Config->Get('rtname')]}
93 Subject: This is a test of new ticket creation
97 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => 'http://this.test.for.non-connection.is.expected.to.generate.an.error');
98 is ($status >> 8, 75, "The mail gateway exited with a failure");
99 ok (!$id, "No ticket id");
104 diag "revoke rights tests depend on";
106 $everyone_group = RT::Group->new( RT->SystemUser );
107 $everyone_group->LoadSystemInternalGroup( 'Everyone' );
108 ok ($everyone_group->Id, "Found group 'everyone'");
110 foreach( qw(CreateTicket ReplyToTicket CommentOnTicket) ) {
111 $everyone_group->PrincipalObj->RevokeRight(Right => $_);
115 diag "Test new ticket creation by root who is privileged and superuser";
118 From: root\@localhost
119 To: rt\@@{[RT->Config->Get('rtname')]}
120 Subject: This is a test of new ticket creation
126 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
127 is ($status >> 8, 0, "The mail gateway exited normally");
128 ok ($id, "Created ticket");
130 my $tick = RT::Test->last_ticket;
131 isa_ok ($tick, 'RT::Ticket');
132 is ($tick->Id, $id, "correct ticket id");
133 is ($tick->Subject , 'This is a test of new ticket creation', "Created the ticket");
137 diag "Test the 'X-RT-Mail-Extension' field in the header of a ticket";
140 From: root\@localhost
141 To: rt\@@{[RT->Config->Get('rtname')]}
142 Subject: This is a test of the X-RT-Mail-Extension field
146 local $ENV{'EXTENSION'} = "bad value with\nnewlines\n";
147 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
148 is ($status >> 8, 0, "The mail gateway exited normally");
149 ok ($id, "Created ticket #$id");
151 my $tick = RT::Test->last_ticket;
152 isa_ok ($tick, 'RT::Ticket');
153 is ($tick->Id, $id, "correct ticket id");
154 is ($tick->Subject, 'This is a test of the X-RT-Mail-Extension field', "Created the ticket");
156 my $transactions = $tick->Transactions;
157 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
158 $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
159 my $txn = $transactions->First;
160 isa_ok ($txn, 'RT::Transaction');
161 is ($txn->Type, 'Create', "correct type");
163 my $attachment = $txn->Attachments->First;
164 isa_ok ($attachment, 'RT::Attachment');
165 # XXX: We eat all newlines in header, that's not what RFC's suggesting
167 $attachment->GetHeader('X-RT-Mail-Extension'),
168 "bad value with newlines",
169 'header is in place, without trailing newline char'
174 diag "Make sure that not standard --extension is passed";
177 From: root\@localhost
178 To: rt\@@{[RT->Config->Get('rtname')]}
179 Subject: This is a test of new ticket creation
183 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
184 is ($status >> 8, 0, "The mail gateway exited normally");
185 ok ($id, "Created ticket #$id");
187 my $tick = RT::Test->last_ticket;
188 isa_ok ($tick, 'RT::Ticket');
189 is ($tick->Id, $id, "correct ticket id");
191 my $transactions = $tick->Transactions;
192 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
193 $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
194 my $txn = $transactions->First;
195 isa_ok ($txn, 'RT::Transaction');
196 is ($txn->Type, 'Create', "correct type");
198 my $attachment = $txn->Attachments->First;
199 isa_ok ($attachment, 'RT::Attachment');
201 $attachment->GetHeader('X-RT-Mail-Extension'),
202 'some-extension-arg',
208 diag "Test new ticket creation without --action argument";
211 From: root\@localhost
213 Subject: using mailgate without --action arg
218 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
219 is ($status >> 8, 0, "The mail gateway exited normally");
220 ok ($id, "Created ticket #$id");
222 my $tick = RT::Test->last_ticket;
223 isa_ok ($tick, 'RT::Ticket');
224 is ($tick->Id, $id, "correct ticket id");
225 is ($tick->Subject, 'using mailgate without --action arg', "using mailgate without --action arg");
229 diag "This is a test of new ticket creation as an unknown user";
232 From: doesnotexist\@@{[RT->Config->Get('rtname')]}
233 To: rt\@@{[RT->Config->Get('rtname')]}
234 Subject: This is a test of new ticket creation as an unknown user
239 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
240 is ($status >> 8, 0, "The mail gateway exited normally");
241 ok (!$id, "no ticket created");
243 my $tick = RT::Test->last_ticket;
244 isa_ok ($tick, 'RT::Ticket');
245 ok ($tick->Id, "found ticket ".$tick->Id);
246 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");
248 my $u = RT::User->new(RT->SystemUser);
249 $u->Load("doesnotexist\@@{[RT->Config->Get('rtname')]}");
250 ok( !$u->Id, "user does not exist and was not created by failed ticket submission");
252 $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email/);
253 $m->next_warning_like(qr/RT could not load a valid user/);
255 local $TODO = "we're a bit noisy for this warning case";
256 $m->no_leftover_warnings_ok;
260 diag "grant everybody with CreateTicket right";
262 ok( RT::Test->set_rights(
263 { Principal => $everyone_group->PrincipalObj,
264 Right => [qw(CreateTicket)],
266 ), "Granted everybody the right to create tickets");
270 diag "now everybody can create tickets. can a random unkown user create tickets?";
273 From: doesnotexist\@@{[RT->Config->Get('rtname')]}
274 To: rt\@@{[RT->Config->Get('rtname')]}
275 Subject: This is a test of new ticket creation as an unknown user
280 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
281 is ($status >> 8, 0, "The mail gateway exited normally");
282 ok ($id, "ticket created");
284 my $tick = RT::Test->last_ticket;
285 isa_ok ($tick, 'RT::Ticket');
286 ok ($tick->Id, "found ticket ".$tick->Id);
287 is ($tick->Id, $id, "correct ticket id");
288 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");
290 my $u = RT::User->new( RT->SystemUser );
291 $u->Load( "doesnotexist\@@{[RT->Config->Get('rtname')]}" );
292 ok ($u->Id, "user does not exist and was created by ticket submission");
297 diag "can another random reply to a ticket without being granted privs? answer should be no.";
300 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
301 To: rt\@@{[RT->Config->Get('rtname')]}
302 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
304 Blah! (Should not work.)
307 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
308 is ($status >> 8, 0, "The mail gateway exited normally");
309 ok (!$id, "no way to reply to the ticket");
311 my $u = RT::User->new(RT->SystemUser);
312 $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
313 ok( !$u->Id, " user does not exist and was not created by ticket correspondence submission");
314 $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\)/);
316 local $TODO = "we're a bit noisy for this warning case";
317 $m->no_leftover_warnings_ok;
321 diag "grant everyone 'ReplyToTicket' right";
323 ok( RT::Test->set_rights(
324 { Principal => $everyone_group->PrincipalObj,
325 Right => [qw(CreateTicket ReplyToTicket)],
327 ), "Granted everybody the right to reply to tickets" );
330 diag "can another random reply to a ticket after being granted privs? answer should be yes";
333 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
334 To: rt\@@{[RT->Config->Get('rtname')]}
335 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
340 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
341 is ($status >> 8, 0, "The mail gateway exited normally");
342 is ($id, $ticket_id, "replied to the ticket");
344 my $u = RT::User->new(RT->SystemUser);
345 $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
346 ok ($u->Id, "user exists and was created by ticket correspondence submission");
350 diag "add a reply to the ticket using '--extension ticket' feature";
353 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
354 To: rt\@@{[RT->Config->Get('rtname')]}
355 Subject: This is a test of a reply as an unknown user
360 local $ENV{'EXTENSION'} = $ticket_id;
361 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'ticket');
362 is ($status >> 8, 0, "The mail gateway exited normally");
363 is ($id, $ticket_id, "replied to the ticket");
365 my $tick = RT::Test->last_ticket;
366 isa_ok ($tick, 'RT::Ticket');
367 ok ($tick->Id, "found ticket ".$tick->Id);
368 is ($tick->Id, $id, "correct ticket id");
370 my $transactions = $tick->Transactions;
371 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
372 $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
373 my $txn = $transactions->First;
374 isa_ok ($txn, 'RT::Transaction');
375 is ($txn->Type, 'Correspond', "correct type");
377 my $attachment = $txn->Attachments->First;
378 isa_ok ($attachment, 'RT::Attachment');
379 is ($attachment->GetHeader('X-RT-Mail-Extension'), $id, 'header is in place');
383 diag "can another random comment on a ticket without being granted privs? answer should be no";
386 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
387 To: rt\@@{[RT->Config->Get('rtname')]}
388 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
390 Blah! (Should not work.)
393 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
394 is ($status >> 8, 0, "The mail gateway exited normally");
395 ok (!$id, "no way to comment on the ticket");
397 my $u = RT::User->new(RT->SystemUser);
398 $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
399 ok( !$u->Id, " user does not exist and was not created by ticket comment submission");
400 $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\)/);
402 local $TODO = "we're a bit noisy for this warning case";
403 $m->no_leftover_warnings_ok;
408 diag "grant everyone 'CommentOnTicket' right";
410 ok( RT::Test->set_rights(
411 { Principal => $everyone_group->PrincipalObj,
412 Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
414 ), "Granted everybody the right to comment on tickets");
417 diag "can another random reply to a ticket after being granted privs? answer should be yes";
420 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
421 To: rt\@@{[RT->Config->Get('rtname')]}
422 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
427 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
428 is ($status >> 8, 0, "The mail gateway exited normally");
429 is ($id, $ticket_id, "replied to the ticket");
431 my $u = RT::User->new(RT->SystemUser);
432 $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
433 ok ($u->Id, " user exists and was created by ticket comment submission");
437 diag "add comment to the ticket using '--extension action' feature";
440 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
441 To: rt\@@{[RT->Config->Get('rtname')]}
442 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment via '--extension action'
447 local $ENV{'EXTENSION'} = 'comment';
448 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'action');
449 is ($status >> 8, 0, "The mail gateway exited normally");
450 is ($id, $ticket_id, "added comment to the ticket");
452 my $tick = RT::Test->last_ticket;
453 isa_ok ($tick, 'RT::Ticket');
454 ok ($tick->Id, "found ticket ".$tick->Id);
455 is ($tick->Id, $id, "correct ticket id");
457 my $transactions = $tick->Transactions;
458 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
459 $transactions->Limit(
461 OPERATOR => 'NOT ENDSWITH',
462 VALUE => 'EmailRecord',
463 ENTRYAGGREGATOR => 'AND',
465 my $txn = $transactions->First;
466 isa_ok ($txn, 'RT::Transaction');
467 is ($txn->Type, 'Comment', "correct type");
469 my $attachment = $txn->Attachments->First;
470 isa_ok ($attachment, 'RT::Attachment');
471 is ($attachment->GetHeader('X-RT-Mail-Extension'), 'comment', 'header is in place');
475 diag "Testing preservation of binary attachments";
477 # Get a binary blob (Best Practical logo)
478 my $LOGO_FILE = $RT::MasonComponentRoot .'/NoAuth/images/bpslogo.png';
480 # Create a mime entity with an attachment
481 my $entity = MIME::Entity->build(
482 From => 'root@localhost',
483 To => 'rt@localhost',
484 Subject => 'binary attachment test',
485 Data => ['This is a test of a binary attachment'],
491 Encoding => 'base64',
493 # Create a ticket with a binary attachment
494 my ($status, $id) = RT::Test->send_via_mailgate_and_http($entity);
495 is ($status >> 8, 0, "The mail gateway exited normally");
496 ok ($id, "created ticket");
498 my $tick = RT::Test->last_ticket;
499 isa_ok ($tick, 'RT::Ticket');
500 ok ($tick->Id, "found ticket ".$tick->Id);
501 is ($tick->Id, $id, "correct ticket id");
502 is ($tick->Subject , 'binary attachment test', "Created the ticket - ".$tick->Id);
504 my $file = `cat $LOGO_FILE`;
505 ok ($file, "Read in the logo image");
506 diag "for the raw file the md5 hex is ". Digest::MD5::md5_hex($file);
508 # Verify that the binary attachment is valid in the database
509 my $attachments = RT::Attachments->new(RT->SystemUser);
510 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/png');
511 my $txn_alias = $attachments->Join(
513 FIELD1 => 'TransactionId',
514 TABLE2 => 'Transactions',
517 $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectType', VALUE => 'RT::Ticket' );
518 $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectId', VALUE => $id );
519 is ($attachments->Count, 1, 'Found only one png attached to the ticket');
520 my $attachment = $attachments->First;
521 ok ($attachment->Id, 'loaded attachment object');
522 my $acontent = $attachment->Content;
524 diag "coming from the database, md5 hex is ".Digest::MD5::md5_hex($acontent);
525 is ($acontent, $file, 'The attachment isn\'t screwed up in the database.');
527 # Grab the binary attachment via the web ui
528 my $ua = new LWP::UserAgent;
529 my $full_url = "$url/Ticket/Attachment/". $attachment->TransactionId
530 ."/". $attachment->id. "/bpslogo.png?&user=root&pass=password";
531 my $r = $ua->get( $full_url );
533 # Verify that the downloaded attachment is the same as what we uploaded.
534 is ($file, $r->content, 'The attachment isn\'t screwed up in download');
539 diag "Simple I18N testing";
542 From: root\@localhost
543 To: rtemail\@@{[RT->Config->Get('rtname')]}
544 Subject: This is a test of I18N ticket creation
545 Content-Type: text/plain; charset="utf-8"
548 \303\242\303\252\303\256\303\264\303\273
549 \303\241\303\251\303\255\303\263\303\272
552 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
553 is ($status >> 8, 0, "The mail gateway exited normally");
554 ok ($id, "created ticket");
556 my $tick = RT::Test->last_ticket;
557 isa_ok ($tick, 'RT::Ticket');
558 ok ($tick->Id, "found ticket ". $tick->Id);
559 is ($tick->Id, $id, "correct ticket");
560 is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket - ". $tick->Subject);
562 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
563 Encode::_utf8_on($unistring);
565 $tick->Transactions->First->Content,
566 $tick->Transactions->First->Attachments->First->Content,
567 "Content is ". $tick->Transactions->First->Attachments->First->Content
570 $tick->Transactions->First->Content =~ /$unistring/i,
571 $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
577 diag "supposedly I18N fails on the second message sent in.";
580 From: root\@localhost
581 To: rtemail\@@{[RT->Config->Get('rtname')]}
582 Subject: This is a test of I18N ticket creation
583 Content-Type: text/plain; charset="utf-8"
586 \303\242\303\252\303\256\303\264\303\273
587 \303\241\303\251\303\255\303\263\303\272
590 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
591 is ($status >> 8, 0, "The mail gateway exited normally");
592 ok ($id, "created ticket");
594 my $tick = RT::Test->last_ticket;
595 isa_ok ($tick, 'RT::Ticket');
596 ok ($tick->Id, "found ticket ". $tick->Id);
597 is ($tick->Id, $id, "correct ticket");
598 is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket");
600 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
601 Encode::_utf8_on($unistring);
604 $tick->Transactions->First->Content =~ $unistring,
605 "It appears to be unicode - ". $tick->Transactions->First->Content
611 diag "make sure we check that UTF-8 is really UTF-8";
614 From: root\@localhost
615 To: rtemail\@@{[RT->Config->Get('rtname')]}
616 Subject: This is test wrong utf-8 chars
617 Content-Type: text/plain; charset="utf-8"
619 utf-8: informaci\303\263n confidencial
620 latin1: informaci\363n confidencial
624 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
625 is ($status >> 8, 0, "The mail gateway exited normally");
626 ok ($id, "created ticket");
628 my $tick = RT::Test->last_ticket;
629 is ($tick->Id, $id, "correct ticket");
631 my $content = $tick->Transactions->First->Content;
632 Encode::_utf8_off($content);
634 like $content, qr{informaci\303\263n confidencial};
635 like $content, qr{informaci\357\277\275n confidencial};
640 diag "check that mailgate doesn't suffer from empty Reply-To:";
643 From: root\@localhost
645 To: rtemail\@@{[RT->Config->Get('rtname')]}
647 Content-Type: text/plain; charset="utf-8"
651 my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
652 is ($status >> 8, 0, "The mail gateway exited normally");
653 ok ($id, "created ticket");
655 my $tick = RT::Test->last_ticket;
656 isa_ok ($tick, 'RT::Ticket');
657 ok ($tick->Id, "found ticket ". $tick->Id);
658 is ($tick->Id, $id, "correct ticket");
660 like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
666 my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
670 skip "Advanced mailgate actions require an unsafe configuration", 47
671 unless RT->Config->Get('UnsafeEmailCommands');
673 # create new queue to be shure we don't mess with rights
675 my $queue = RT::Queue->new(RT->SystemUser);
676 my ($qid) = $queue->Create( Name => 'ext-mailgate');
677 ok( $qid, 'queue created for ext-mailgate tests' );
680 # create ticket that is owned by nobody
682 my $tick = RT::Ticket->new(RT->SystemUser);
683 my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
684 ok( $id, 'new ticket created' );
685 is( $tick->Owner, RT->Nobody->Id, 'owner of the new ticket is nobody' );
688 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
690 From: root\@localhost
691 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
695 is ($? >> 8, 0, "The mail gateway exited normally");
697 $tick = RT::Ticket->new(RT->SystemUser);
699 is( $tick->Id, $id, 'load correct ticket');
700 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
702 # check that there is no text transactions writen
703 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
706 ($status, $msg) = $tick->SetOwner( RT->Nobody->Id, 'Force' );
707 ok( $status, 'successfuly changed owner: '. ($msg||'') );
708 is( $tick->Owner, RT->Nobody->Id, 'set owner back to nobody');
714 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
716 From: root\@localhost
717 Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
722 is ($? >> 8, 0, "The mail gateway exited normally");
724 DBIx::SearchBuilder::Record::Cachable->FlushCache;
726 $tick = RT::Ticket->new(RT->SystemUser);
728 is( $tick->Id, $id, "load correct ticket #$id");
729 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
730 my $txns = $tick->Transactions;
731 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
732 $txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
733 # +1 because of auto open
734 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
735 is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
741 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
743 From: root\@localhost
744 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
748 is ($? >> 8, 0, "The mail gateway exited normally");
750 DBIx::SearchBuilder::Record::Cachable->FlushCache;
752 $tick = RT::Ticket->new(RT->SystemUser);
754 is( $tick->Id, $id, 'load correct ticket');
755 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
756 is( $tick->Transactions->Count, 7, 'no superfluous transactions');
759 my $user = RT::User->new( RT->SystemUser );
760 my ($uid) = $user->Create( Name => 'ext-mailgate',
761 EmailAddress => 'ext-mailgate@localhost',
763 Password => 'qwe123',
765 ok( $uid, 'user created for ext-mailgate tests' );
766 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
768 $tick = RT::Ticket->new(RT->SystemUser);
769 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
770 ok( $id, 'create new ticket' );
772 my $rtname = RT->Config->Get('rtname');
777 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
779 From: ext-mailgate\@localhost
780 Subject: [$rtname \#$id] test
784 is ( $? >> 8, 0, "mailgate exited normally" );
785 DBIx::SearchBuilder::Record::Cachable->FlushCache;
787 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
789 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
790 ok( $status, "successfuly granted right: $msg" );
791 my $ace_id = $status;
792 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );
794 $m->next_warning_like(qr/Permission Denied/);
795 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
796 $m->no_leftover_warnings_ok;
799 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
801 From: ext-mailgate\@localhost
802 Subject: [$rtname \#$id] test
807 is ( $? >> 8, 0, "mailgate exited normally" );
808 DBIx::SearchBuilder::Record::Cachable->FlushCache;
810 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
811 is( $tick->Transactions->Count, 3, "one transactions added" );
813 $m->next_warning_like(qr/Permission Denied/);
814 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
815 $m->no_leftover_warnings_ok;
818 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
820 From: ext-mailgate\@localhost
821 Subject: [$rtname \#$id] test
826 is ( $? >> 8, 0, "mailgate exited normally" );
827 DBIx::SearchBuilder::Record::Cachable->FlushCache;
829 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
830 is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );
832 $m->next_warning_like(qr/Permission Denied/);
833 $m->next_warning_like(qr/Could not record email: Ticket not taken/);
834 $m->no_leftover_warnings_ok;
836 # revoke ReplyToTicket right
838 my $ace = RT::ACE->new(RT->SystemUser);
839 $ace->Load( $ace_id );
841 my $acl = RT::ACL->new(RT->SystemUser);
842 $acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
843 $acl->LimitToObject( $RT::System );
844 while( my $ace = $acl->Next ) {
848 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
851 my $group = RT::Group->new( RT->SystemUser );
852 ok( $group->LoadQueueRoleGroup( Queue => $qid, Type=> 'Owner' ), "load queue owners role group" );
853 $ace = RT::ACE->new( RT->SystemUser );
854 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
855 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );
857 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
858 ok( $status, "successfuly granted right: $msg" );
859 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
860 ok( $status, "successfuly granted right: $msg" );
863 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
865 From: ext-mailgate\@localhost
866 Subject: [$rtname \#$id] test
868 take-correspond with reply right granted to owner role
871 is ( $? >> 8, 0, "mailgate exited normally" );
872 DBIx::SearchBuilder::Record::Cachable->FlushCache;
875 is( $tick->Owner, $user->id, "we changed owner" );
876 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
877 is( $tick->Transactions->Count, 5, "transactions added" );