5 use RT::Test config => 'Set( $UnsafeEmailCommands, 1);', tests => 228, actual_server => 1;
6 my ($baseurl, $m) = RT::Test->started_ok;
11 use Digest::MD5 qw(md5_base64);
14 # TODO: --extension queue
16 my $url = $m->rt_base_url;
18 diag "Make sure that when we call the mailgate without URL, it fails";
22 To: rt\@@{[RT->Config->Get('rtname')]}
23 Subject: This is a test of new ticket creation
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";
33 diag "Make sure that when we call the mailgate with wrong URL, it tempfails";
37 To: rt\@@{[RT->Config->Get('rtname')]}
38 Subject: This is a test of new ticket creation
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");
49 diag "revoke rights tests depend on";
51 $everyone_group = RT::Group->new( RT->SystemUser );
52 $everyone_group->LoadSystemInternalGroup( 'Everyone' );
53 ok ($everyone_group->Id, "Found group 'everyone'");
55 foreach( qw(CreateTicket ReplyToTicket CommentOnTicket) ) {
56 $everyone_group->PrincipalObj->RevokeRight(Right => $_);
60 diag "Test new ticket creation by root who is privileged and superuser";
64 To: rt\@@{[RT->Config->Get('rtname')]}
65 Subject: This is a test of new ticket creation
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");
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");
82 diag "Test the 'X-RT-Mail-Extension' field in the header of a ticket";
86 To: rt\@@{[RT->Config->Get('rtname')]}
87 Subject: This is a test of the X-RT-Mail-Extension field
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");
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");
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");
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
112 $attachment->GetHeader('X-RT-Mail-Extension'),
113 "bad value with newlines",
114 'header is in place, without trailing newline char'
119 diag "Make sure that not standard --extension is passed";
122 From: root\@localhost
123 To: rt\@@{[RT->Config->Get('rtname')]}
124 Subject: This is a test of new ticket creation
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");
132 my $tick = RT::Test->last_ticket;
133 isa_ok ($tick, 'RT::Ticket');
134 is ($tick->Id, $id, "correct ticket id");
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");
143 my $attachment = $txn->Attachments->First;
144 isa_ok ($attachment, 'RT::Attachment');
146 $attachment->GetHeader('X-RT-Mail-Extension'),
147 'some-extension-arg',
153 diag "Test new ticket creation without --action argument";
156 From: root\@localhost
158 Subject: using mailgate without --action arg
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");
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");
174 diag "This is a test of new ticket creation as an unknown user";
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
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");
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");
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");
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/);
200 local $TODO = "we're a bit noisy for this warning case";
201 $m->no_leftover_warnings_ok;
205 diag "grant everybody with CreateTicket right";
207 ok( RT::Test->set_rights(
208 { Principal => $everyone_group->PrincipalObj,
209 Right => [qw(CreateTicket)],
211 ), "Granted everybody the right to create tickets");
215 diag "now everybody can create tickets. can a random unkown user create tickets?";
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
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");
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");
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");
242 diag "can another random reply to a ticket without being granted privs? answer should be no.";
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
249 Blah! (Should not work.)
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");
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\)/);
261 local $TODO = "we're a bit noisy for this warning case";
262 $m->no_leftover_warnings_ok;
266 diag "grant everyone 'ReplyToTicket' right";
268 ok( RT::Test->set_rights(
269 { Principal => $everyone_group->PrincipalObj,
270 Right => [qw(CreateTicket ReplyToTicket)],
272 ), "Granted everybody the right to reply to tickets" );
275 diag "can another random reply to a ticket after being granted privs? answer should be yes";
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
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");
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");
295 diag "add a reply to the ticket using '--extension ticket' feature";
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
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");
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");
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");
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');
328 diag "can another random comment on a ticket without being granted privs? answer should be no";
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
335 Blah! (Should not work.)
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");
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\)/);
347 local $TODO = "we're a bit noisy for this warning case";
348 $m->no_leftover_warnings_ok;
353 diag "grant everyone 'CommentOnTicket' right";
355 ok( RT::Test->set_rights(
356 { Principal => $everyone_group->PrincipalObj,
357 Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
359 ), "Granted everybody the right to comment on tickets");
362 diag "can another random reply to a ticket after being granted privs? answer should be yes";
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
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");
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");
382 diag "add comment to the ticket using '--extension action' feature";
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'
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");
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");
402 my $transactions = $tick->Transactions;
403 $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
404 $transactions->Limit(
406 OPERATOR => 'NOT ENDSWITH',
407 VALUE => 'EmailRecord',
408 ENTRYAGGREGATOR => 'AND',
410 my $txn = $transactions->First;
411 isa_ok ($txn, 'RT::Transaction');
412 is ($txn->Type, 'Comment', "correct type");
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');
420 diag "Testing preservation of binary attachments";
422 # Get a binary blob (Best Practical logo)
423 my $LOGO_FILE = $RT::MasonComponentRoot .'/NoAuth/images/bpslogo.png';
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'],
436 Encoding => 'base64',
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");
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);
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);
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(
458 FIELD1 => 'TransactionId',
459 TABLE2 => 'Transactions',
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;
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.');
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 );
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');
484 diag "Simple I18N testing";
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"
493 \303\242\303\252\303\256\303\264\303\273
494 \303\241\303\251\303\255\303\263\303\272
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");
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);
507 my $unistring = Encode::decode("UTF-8","\303\241\303\251\303\255\303\263\303\272");
509 $tick->Transactions->First->Content,
510 $tick->Transactions->First->Attachments->First->Content,
511 "Content is ". $tick->Transactions->First->Attachments->First->Content
514 $tick->Transactions->First->Content =~ /$unistring/i,
515 $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
521 diag "supposedly I18N fails on the second message sent in.";
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"
530 \303\242\303\252\303\256\303\264\303\273
531 \303\241\303\251\303\255\303\263\303\272
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");
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");
544 my $unistring = Encode::decode("UTF-8","\303\241\303\251\303\255\303\263\303\272");
547 $tick->Transactions->First->Content =~ $unistring,
548 "It appears to be unicode - ". $tick->Transactions->First->Content
554 diag "make sure we check that UTF-8 is really UTF-8";
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"
562 utf-8: informaci\303\263n confidencial
563 latin1: informaci\363n confidencial
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");
571 my $tick = RT::Test->last_ticket;
572 is ($tick->Id, $id, "correct ticket");
574 my $content = Encode::encode("UTF-8",$tick->Transactions->First->Content);
576 like $content, qr{informaci\303\263n confidencial};
577 like $content, qr{informaci\357\277\275n confidencial};
582 diag "check that mailgate doesn't suffer from empty Reply-To:";
585 From: root\@localhost
587 To: rtemail\@@{[RT->Config->Get('rtname')]}
589 Content-Type: text/plain; charset="utf-8"
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");
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");
602 like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
608 my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
612 skip "Advanced mailgate actions require an unsafe configuration", 47
613 unless RT->Config->Get('UnsafeEmailCommands');
615 # create new queue to be shure we don't mess with rights
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' );
622 # create ticket that is owned by nobody
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' );
630 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
632 From: root\@localhost
633 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
637 is ($? >> 8, 0, "The mail gateway exited normally");
639 $tick = RT::Ticket->new(RT->SystemUser);
641 is( $tick->Id, $id, 'load correct ticket');
642 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
644 # check that there is no text transactions writen
645 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
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');
656 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
658 From: root\@localhost
659 Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
664 is ($? >> 8, 0, "The mail gateway exited normally");
666 DBIx::SearchBuilder::Record::Cachable->FlushCache;
668 $tick = RT::Ticket->new(RT->SystemUser);
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' );
683 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
685 From: root\@localhost
686 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
690 is ($? >> 8, 0, "The mail gateway exited normally");
692 DBIx::SearchBuilder::Record::Cachable->FlushCache;
694 $tick = RT::Ticket->new(RT->SystemUser);
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');
701 my $user = RT::User->new( RT->SystemUser );
702 my ($uid) = $user->Create( Name => 'ext-mailgate',
703 EmailAddress => 'ext-mailgate@localhost',
705 Password => 'qwe123',
707 ok( $uid, 'user created for ext-mailgate tests' );
708 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
710 $tick = RT::Ticket->new(RT->SystemUser);
711 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
712 ok( $id, 'create new ticket' );
714 my $rtname = RT->Config->Get('rtname');
719 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
721 From: ext-mailgate\@localhost
722 Subject: [$rtname \#$id] test
726 is ( $? >> 8, 0, "mailgate exited normally" );
727 DBIx::SearchBuilder::Record::Cachable->FlushCache;
729 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
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" );
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;
741 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
743 From: ext-mailgate\@localhost
744 Subject: [$rtname \#$id] test
749 is ( $? >> 8, 0, "mailgate exited normally" );
750 DBIx::SearchBuilder::Record::Cachable->FlushCache;
752 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
753 is( $tick->Transactions->Count, 3, "one transactions added" );
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;
760 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
762 From: ext-mailgate\@localhost
763 Subject: [$rtname \#$id] test
768 is ( $? >> 8, 0, "mailgate exited normally" );
769 DBIx::SearchBuilder::Record::Cachable->FlushCache;
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" );
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;
778 # revoke ReplyToTicket right
780 my $ace = RT::ACE->new(RT->SystemUser);
781 $ace->Load( $ace_id );
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 ) {
790 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
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" );
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" );
805 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
807 From: ext-mailgate\@localhost
808 Subject: [$rtname \#$id] test
810 take-correspond with reply right granted to owner role
813 is ( $? >> 8, 0, "mailgate exited normally" );
814 DBIx::SearchBuilder::Record::Cachable->FlushCache;
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" );