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 => 159;
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" if $ENV{'TEST_VERBOSE'};
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($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";
87 diag "Make sure that when we call the mailgate with wrong URL, it tempfails" if $ENV{'TEST_VERBOSE'};
91 To: rt\@@{[RT->Config->Get('rtname')]}
92 Subject: This is a test of new ticket creation
96 my ($status, $id) = RT::Test->send_via_mailgate($text, url => 'http://this.test.for.non-connection.is.expected.to.generate.an.error');
97 is ($status >> 8, 75, "The mail gateway exited with a failure");
98 ok (!$id, "No ticket id");
102 diag "revoke rights tests depend on" if $ENV{'TEST_VERBOSE'};
104 $everyone_group = RT::Group->new( $RT::SystemUser );
105 $everyone_group->LoadSystemInternalGroup( 'Everyone' );
106 ok ($everyone_group->Id, "Found group 'everyone'");
108 foreach( qw(CreateTicket ReplyToTicket CommentOnTicket) ) {
109 $everyone_group->PrincipalObj->RevokeRight(Right => $_);
113 diag "Test new ticket creation by root who is privileged and superuser" if $ENV{'TEST_VERBOSE'};
116 From: root\@localhost
117 To: rt\@@{[RT->Config->Get('rtname')]}
118 Subject: This is a test of new ticket creation
124 my ($status, $id) = RT::Test->send_via_mailgate($text);
125 is ($status >> 8, 0, "The mail gateway exited normally");
126 ok ($id, "Created ticket");
128 my $tick = RT::Test->last_ticket;
129 isa_ok ($tick, 'RT::Ticket');
130 is ($tick->Id, $id, "correct ticket id");
131 is ($tick->Subject , 'This is a test of new ticket creation', "Created the ticket");
134 diag "Test the 'X-RT-Mail-Extension' field in the header of a ticket" if $ENV{'TEST_VERBOSE'};
137 From: root\@localhost
138 To: rt\@@{[RT->Config->Get('rtname')]}
139 Subject: This is a test of the X-RT-Mail-Extension field
143 local $ENV{'EXTENSION'} = "bad value with\nnewlines\n";
144 my ($status, $id) = RT::Test->send_via_mailgate($text);
145 is ($status >> 8, 0, "The mail gateway exited normally");
146 ok ($id, "Created ticket #$id");
148 my $tick = RT::Test->last_ticket;
149 isa_ok ($tick, 'RT::Ticket');
150 is ($tick->Id, $id, "correct ticket id");
151 is ($tick->Subject, 'This is a test of the X-RT-Mail-Extension field', "Created the ticket");
153 my $transactions = $tick->Transactions;
154 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
155 $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
156 my $txn = $transactions->First;
157 isa_ok ($txn, 'RT::Transaction');
158 is ($txn->Type, 'Create', "correct type");
160 my $attachment = $txn->Attachments->First;
161 isa_ok ($attachment, 'RT::Attachment');
162 # XXX: We eat all newlines in header, that's not what RFC's suggesting
164 $attachment->GetHeader('X-RT-Mail-Extension'),
165 "bad value with newlines",
166 'header is in place, without trailing newline char'
170 diag "Make sure that not standard --extension is passed" if $ENV{'TEST_VERBOSE'};
173 From: root\@localhost
174 To: rt\@@{[RT->Config->Get('rtname')]}
175 Subject: This is a test of new ticket creation
179 my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'some-extension-arg' );
180 is ($status >> 8, 0, "The mail gateway exited normally");
181 ok ($id, "Created ticket #$id");
183 my $tick = RT::Test->last_ticket;
184 isa_ok ($tick, 'RT::Ticket');
185 is ($tick->Id, $id, "correct ticket id");
187 my $transactions = $tick->Transactions;
188 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
189 $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
190 my $txn = $transactions->First;
191 isa_ok ($txn, 'RT::Transaction');
192 is ($txn->Type, 'Create', "correct type");
194 my $attachment = $txn->Attachments->First;
195 isa_ok ($attachment, 'RT::Attachment');
197 $attachment->GetHeader('X-RT-Mail-Extension'),
198 'some-extension-arg',
203 diag "Test new ticket creation without --action argument" if $ENV{'TEST_VERBOSE'};
206 From: root\@localhost
208 Subject: using mailgate without --action arg
213 my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'some-extension-arg' );
214 is ($status >> 8, 0, "The mail gateway exited normally");
215 ok ($id, "Created ticket #$id");
217 my $tick = RT::Test->last_ticket;
218 isa_ok ($tick, 'RT::Ticket');
219 is ($tick->Id, $id, "correct ticket id");
220 is ($tick->Subject, 'using mailgate without --action arg', "using mailgate without --action arg");
223 diag "This is a test of new ticket creation as an unknown user" if $ENV{'TEST_VERBOSE'};
226 From: doesnotexist\@@{[RT->Config->Get('rtname')]}
227 To: rt\@@{[RT->Config->Get('rtname')]}
228 Subject: This is a test of new ticket creation as an unknown user
233 my ($status, $id) = RT::Test->send_via_mailgate($text);
234 is ($status >> 8, 0, "The mail gateway exited normally");
235 ok (!$id, "no ticket created");
237 my $tick = RT::Test->last_ticket;
238 isa_ok ($tick, 'RT::Ticket');
239 ok ($tick->Id, "found ticket ".$tick->Id);
240 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");
242 my $u = RT::User->new($RT::SystemUser);
243 $u->Load("doesnotexist\@@{[RT->Config->Get('rtname')]}");
244 ok( !$u->Id, "user does not exist and was not created by failed ticket submission");
247 diag "grant everybody with CreateTicket right" if $ENV{'TEST_VERBOSE'};
249 ok( RT::Test->set_rights(
250 { Principal => $everyone_group->PrincipalObj,
251 Right => [qw(CreateTicket)],
253 ), "Granted everybody the right to create tickets");
257 diag "now everybody can create tickets. can a random unkown user create tickets?" if $ENV{'TEST_VERBOSE'};
260 From: doesnotexist\@@{[RT->Config->Get('rtname')]}
261 To: rt\@@{[RT->Config->Get('rtname')]}
262 Subject: This is a test of new ticket creation as an unknown user
267 my ($status, $id) = RT::Test->send_via_mailgate($text);
268 is ($status >> 8, 0, "The mail gateway exited normally");
269 ok ($id, "ticket created");
271 my $tick = RT::Test->last_ticket;
272 isa_ok ($tick, 'RT::Ticket');
273 ok ($tick->Id, "found ticket ".$tick->Id);
274 is ($tick->Id, $id, "correct ticket id");
275 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");
277 my $u = RT::User->new( $RT::SystemUser );
278 $u->Load( "doesnotexist\@@{[RT->Config->Get('rtname')]}" );
279 ok ($u->Id, "user does not exist and was created by ticket submission");
283 diag "can another random reply to a ticket without being granted privs? answer should be no." if $ENV{'TEST_VERBOSE'};
286 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
287 To: rt\@@{[RT->Config->Get('rtname')]}
288 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
290 Blah! (Should not work.)
293 my ($status, $id) = RT::Test->send_via_mailgate($text);
294 is ($status >> 8, 0, "The mail gateway exited normally");
295 ok (!$id, "no way to reply to the ticket");
297 my $u = RT::User->new($RT::SystemUser);
298 $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
299 ok( !$u->Id, " user does not exist and was not created by ticket correspondence submission");
302 diag "grant everyone 'ReplyToTicket' right" if $ENV{'TEST_VERBOSE'};
304 ok( RT::Test->set_rights(
305 { Principal => $everyone_group->PrincipalObj,
306 Right => [qw(CreateTicket ReplyToTicket)],
308 ), "Granted everybody the right to reply to tickets" );
311 diag "can another random reply to a ticket after being granted privs? answer should be yes" if $ENV{'TEST_VERBOSE'};
314 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
315 To: rt\@@{[RT->Config->Get('rtname')]}
316 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
321 my ($status, $id) = RT::Test->send_via_mailgate($text);
322 is ($status >> 8, 0, "The mail gateway exited normally");
323 is ($id, $ticket_id, "replied to the ticket");
325 my $u = RT::User->new($RT::SystemUser);
326 $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
327 ok ($u->Id, "user exists and was created by ticket correspondence submission");
330 diag "add a reply to the ticket using '--extension ticket' feature" if $ENV{'TEST_VERBOSE'};
333 From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
334 To: rt\@@{[RT->Config->Get('rtname')]}
335 Subject: This is a test of a reply as an unknown user
340 local $ENV{'EXTENSION'} = $ticket_id;
341 my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'ticket');
342 is ($status >> 8, 0, "The mail gateway exited normally");
343 is ($id, $ticket_id, "replied to the ticket");
345 my $tick = RT::Test->last_ticket;
346 isa_ok ($tick, 'RT::Ticket');
347 ok ($tick->Id, "found ticket ".$tick->Id);
348 is ($tick->Id, $id, "correct ticket id");
350 my $transactions = $tick->Transactions;
351 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
352 $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
353 my $txn = $transactions->First;
354 isa_ok ($txn, 'RT::Transaction');
355 is ($txn->Type, 'Correspond', "correct type");
357 my $attachment = $txn->Attachments->First;
358 isa_ok ($attachment, 'RT::Attachment');
359 is ($attachment->GetHeader('X-RT-Mail-Extension'), $id, 'header is in place');
362 diag "can another random comment on a ticket without being granted privs? answer should be no" if $ENV{'TEST_VERBOSE'};
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
369 Blah! (Should not work.)
372 my ($status, $id) = RT::Test->send_via_mailgate($text, action => 'comment');
373 is ($status >> 8, 0, "The mail gateway exited normally");
374 ok (!$id, "no way to comment on the ticket");
376 my $u = RT::User->new($RT::SystemUser);
377 $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
378 ok( !$u->Id, " user does not exist and was not created by ticket comment submission");
382 diag "grant everyone 'CommentOnTicket' right" if $ENV{'TEST_VERBOSE'};
384 ok( RT::Test->set_rights(
385 { Principal => $everyone_group->PrincipalObj,
386 Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
388 ), "Granted everybody the right to comment on tickets");
391 diag "can another random reply to a ticket after being granted privs? answer should be yes" if $ENV{'TEST_VERBOSE'};
394 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
395 To: rt\@@{[RT->Config->Get('rtname')]}
396 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
401 my ($status, $id) = RT::Test->send_via_mailgate($text, action => 'comment');
402 is ($status >> 8, 0, "The mail gateway exited normally");
403 is ($id, $ticket_id, "replied to the ticket");
405 my $u = RT::User->new($RT::SystemUser);
406 $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
407 ok ($u->Id, " user exists and was created by ticket comment submission");
410 diag "add comment to the ticket using '--extension action' feature" if $ENV{'TEST_VERBOSE'};
413 From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
414 To: rt\@@{[RT->Config->Get('rtname')]}
415 Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment via '--extension action'
420 local $ENV{'EXTENSION'} = 'comment';
421 my ($status, $id) = RT::Test->send_via_mailgate($text, extension => 'action');
422 is ($status >> 8, 0, "The mail gateway exited normally");
423 is ($id, $ticket_id, "added comment to the ticket");
425 my $tick = RT::Test->last_ticket;
426 isa_ok ($tick, 'RT::Ticket');
427 ok ($tick->Id, "found ticket ".$tick->Id);
428 is ($tick->Id, $id, "correct ticket id");
430 my $transactions = $tick->Transactions;
431 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
432 $transactions->Limit(
434 OPERATOR => 'NOT ENDSWITH',
435 VALUE => 'EmailRecord',
436 ENTRYAGGREGATOR => 'AND',
438 my $txn = $transactions->First;
439 isa_ok ($txn, 'RT::Transaction');
440 is ($txn->Type, 'Comment', "correct type");
442 my $attachment = $txn->Attachments->First;
443 isa_ok ($attachment, 'RT::Attachment');
444 is ($attachment->GetHeader('X-RT-Mail-Extension'), 'comment', 'header is in place');
447 diag "Testing preservation of binary attachments" if $ENV{'TEST_VERBOSE'};
449 # Get a binary blob (Best Practical logo)
450 my $LOGO_FILE = $RT::MasonComponentRoot .'/NoAuth/images/bplogo.gif';
452 # Create a mime entity with an attachment
453 my $entity = MIME::Entity->build(
454 From => 'root@localhost',
455 To => 'rt@localhost',
456 Subject => 'binary attachment test',
457 Data => ['This is a test of a binary attachment'],
463 Encoding => 'base64',
465 # Create a ticket with a binary attachment
466 my ($status, $id) = RT::Test->send_via_mailgate($entity);
467 is ($status >> 8, 0, "The mail gateway exited normally");
468 ok ($id, "created ticket");
470 my $tick = RT::Test->last_ticket;
471 isa_ok ($tick, 'RT::Ticket');
472 ok ($tick->Id, "found ticket ".$tick->Id);
473 is ($tick->Id, $id, "correct ticket id");
474 is ($tick->Subject , 'binary attachment test', "Created the ticket - ".$tick->Id);
476 my $file = `cat $LOGO_FILE`;
477 ok ($file, "Read in the logo image");
478 diag "for the raw file the md5 hex is ". Digest::MD5::md5_hex($file) if $ENV{'TEST_VERBOSE'};
480 # Verify that the binary attachment is valid in the database
481 my $attachments = RT::Attachments->new($RT::SystemUser);
482 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/gif');
483 my $txn_alias = $attachments->Join(
485 FIELD1 => 'TransactionId',
486 TABLE2 => 'Transactions',
489 $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectType', VALUE => 'RT::Ticket' );
490 $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectId', VALUE => $id );
491 is ($attachments->Count, 1, 'Found only one gif attached to the ticket');
492 my $attachment = $attachments->First;
493 ok ($attachment->Id, 'loaded attachment object');
494 my $acontent = $attachment->Content;
496 diag "coming from the database, md5 hex is ".Digest::MD5::md5_hex($acontent) if $ENV{'TEST_VERBOSE'};
497 is ($acontent, $file, 'The attachment isn\'t screwed up in the database.');
499 # Grab the binary attachment via the web ui
500 my $ua = new LWP::UserAgent;
501 my $full_url = "$url/Ticket/Attachment/". $attachment->TransactionId
502 ."/". $attachment->id. "/bplogo.gif?&user=root&pass=password";
503 my $r = $ua->get( $full_url );
505 # Verify that the downloaded attachment is the same as what we uploaded.
506 is ($file, $r->content, 'The attachment isn\'t screwed up in download');
509 diag "Simple I18N testing" if $ENV{'TEST_VERBOSE'};
512 From: root\@localhost
513 To: rtemail\@@{[RT->Config->Get('rtname')]}
514 Subject: This is a test of I18N ticket creation
515 Content-Type: text/plain; charset="utf-8"
518 \303\242\303\252\303\256\303\264\303\273
519 \303\241\303\251\303\255\303\263\303\272
522 my ($status, $id) = RT::Test->send_via_mailgate($text);
523 is ($status >> 8, 0, "The mail gateway exited normally");
524 ok ($id, "created ticket");
526 my $tick = RT::Test->last_ticket;
527 isa_ok ($tick, 'RT::Ticket');
528 ok ($tick->Id, "found ticket ". $tick->Id);
529 is ($tick->Id, $id, "correct ticket");
530 is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket - ". $tick->Subject);
532 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
533 Encode::_utf8_on($unistring);
535 $tick->Transactions->First->Content,
536 $tick->Transactions->First->Attachments->First->Content,
537 "Content is ". $tick->Transactions->First->Attachments->First->Content
540 $tick->Transactions->First->Content =~ /$unistring/i,
541 $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
545 diag "supposedly I18N fails on the second message sent in." if $ENV{'TEST_VERBOSE'};
548 From: root\@localhost
549 To: rtemail\@@{[RT->Config->Get('rtname')]}
550 Subject: This is a test of I18N ticket creation
551 Content-Type: text/plain; charset="utf-8"
554 \303\242\303\252\303\256\303\264\303\273
555 \303\241\303\251\303\255\303\263\303\272
558 my ($status, $id) = RT::Test->send_via_mailgate($text);
559 is ($status >> 8, 0, "The mail gateway exited normally");
560 ok ($id, "created ticket");
562 my $tick = RT::Test->last_ticket;
563 isa_ok ($tick, 'RT::Ticket');
564 ok ($tick->Id, "found ticket ". $tick->Id);
565 is ($tick->Id, $id, "correct ticket");
566 is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket");
568 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
569 Encode::_utf8_on($unistring);
572 $tick->Transactions->First->Content =~ $unistring,
573 "It appears to be unicode - ". $tick->Transactions->First->Content
577 diag "check that mailgate doesn't suffer from empty Reply-To:" if $ENV{'TEST_VERBOSE'};
580 From: root\@localhost
582 To: rtemail\@@{[RT->Config->Get('rtname')]}
584 Content-Type: text/plain; charset="utf-8"
588 my ($status, $id) = RT::Test->send_via_mailgate($text);
589 is ($status >> 8, 0, "The mail gateway exited normally");
590 ok ($id, "created ticket");
592 my $tick = RT::Test->last_ticket;
593 isa_ok ($tick, 'RT::Ticket');
594 ok ($tick->Id, "found ticket ". $tick->Id);
595 is ($tick->Id, $id, "correct ticket");
597 like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
601 my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
605 skip "Advanced mailgate actions require an unsafe configuration", 47
606 unless RT->Config->Get('UnsafeEmailCommands');
608 # create new queue to be shure we don't mess with rights
610 my $queue = RT::Queue->new($RT::SystemUser);
611 my ($qid) = $queue->Create( Name => 'ext-mailgate');
612 ok( $qid, 'queue created for ext-mailgate tests' );
614 # {{{ Check take and resolve actions
616 # create ticket that is owned by nobody
618 my $tick = RT::Ticket->new($RT::SystemUser);
619 my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
620 ok( $id, 'new ticket created' );
621 is( $tick->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' );
624 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
626 From: root\@localhost
627 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
631 is ($? >> 8, 0, "The mail gateway exited normally");
633 $tick = RT::Ticket->new($RT::SystemUser);
635 is( $tick->Id, $id, 'load correct ticket');
636 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
638 # check that there is no text transactions writen
639 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
642 ($status, $msg) = $tick->SetOwner( $RT::Nobody->Id, 'Force' );
643 ok( $status, 'successfuly changed owner: '. ($msg||'') );
644 is( $tick->Owner, $RT::Nobody->Id, 'set owner back to nobody');
648 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
650 From: root\@localhost
651 Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
656 is ($? >> 8, 0, "The mail gateway exited normally");
658 DBIx::SearchBuilder::Record::Cachable->FlushCache;
660 $tick = RT::Ticket->new($RT::SystemUser);
662 is( $tick->Id, $id, "load correct ticket #$id");
663 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
664 my $txns = $tick->Transactions;
665 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
666 $txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
667 # +1 because of auto open
668 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
669 is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
672 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
674 From: root\@localhost
675 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
679 is ($? >> 8, 0, "The mail gateway exited normally");
681 DBIx::SearchBuilder::Record::Cachable->FlushCache;
683 $tick = RT::Ticket->new($RT::SystemUser);
685 is( $tick->Id, $id, 'load correct ticket');
686 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
687 is( $tick->Transactions->Count, 7, 'no superfluous transactions');
690 my $user = RT::User->new( $RT::SystemUser );
691 my ($uid) = $user->Create( Name => 'ext-mailgate',
692 EmailAddress => 'ext-mailgate@localhost',
694 Password => 'qwe123',
696 ok( $uid, 'user created for ext-mailgate tests' );
697 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
699 $tick = RT::Ticket->new($RT::SystemUser);
700 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
701 ok( $id, 'create new ticket' );
703 my $rtname = RT->Config->Get('rtname');
706 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
708 From: ext-mailgate\@localhost
709 Subject: [$rtname \#$id] test
713 is ( $? >> 8, 0, "mailgate exited normally" );
714 DBIx::SearchBuilder::Record::Cachable->FlushCache;
716 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
718 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
719 ok( $status, "successfuly granted right: $msg" );
720 my $ace_id = $status;
721 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );
724 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
726 From: ext-mailgate\@localhost
727 Subject: [$rtname \#$id] test
732 is ( $? >> 8, 0, "mailgate exited normally" );
733 DBIx::SearchBuilder::Record::Cachable->FlushCache;
735 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
736 is( $tick->Transactions->Count, 3, "one transactions added" );
739 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
741 From: ext-mailgate\@localhost
742 Subject: [$rtname \#$id] test
747 is ( $? >> 8, 0, "mailgate exited normally" );
748 DBIx::SearchBuilder::Record::Cachable->FlushCache;
750 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
751 is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );
753 # revoke ReplyToTicket right
755 my $ace = RT::ACE->new($RT::SystemUser);
756 $ace->Load( $ace_id );
758 my $acl = RT::ACL->new($RT::SystemUser);
759 $acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
760 $acl->LimitToObject( $RT::System );
761 while( my $ace = $acl->Next ) {
765 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
768 my $group = RT::Group->new( $RT::SystemUser );
769 ok( $group->LoadQueueRoleGroup( Queue => $qid, Type=> 'Owner' ), "load queue owners role group" );
770 $ace = RT::ACE->new( $RT::SystemUser );
771 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
772 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );
774 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
775 ok( $status, "successfuly granted right: $msg" );
776 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
777 ok( $status, "successfuly granted right: $msg" );
780 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
782 From: ext-mailgate\@localhost
783 Subject: [$rtname \#$id] test
785 take-correspond with reply right granted to owner role
788 is ( $? >> 8, 0, "mailgate exited normally" );
789 DBIx::SearchBuilder::Record::Cachable->FlushCache;
792 is( $tick->Owner, $user->id, "we changed owner" );
793 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
794 is( $tick->Transactions->Count, 5, "transactions added" );