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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # CONTRIBUTION SUBMISSION POLICY:
31 # (The following paragraph is not intended to limit the rights granted
32 # to you to modify and distribute this software under the terms of
33 # the GNU General Public License and is only of importance to you if
34 # you choose to contribute your changes and enhancements to the
35 # community by submitting them to Best Practical Solutions, LLC.)
37 # By intentionally submitting any modifications, corrections or
38 # derivatives to this work, or any other work intended for use with
39 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
40 # you are the copyright holder for those contributions and you grant
41 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
42 # royalty-free, perpetual, license to use, copy, create derivative
43 # works based on those contributions, and sublicense and distribute
44 # those contributions and any derivatives thereof.
46 # END BPS TAGGED BLOCK }}}
50 rt-mailgate - Mail interface to RT3.
55 use Test::More tests => 109;
61 use Digest::MD5 qw(md5_base64);
64 my $url = join( ':', grep $_, "http://localhost", $RT::WebPort ) . $RT::WebPath ."/";
66 # Make sure that when we call the mailgate wrong, it tempfails
69 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url http://this.test.for.non-connection.is.expected.to.generate.an.error"), "Opened the mailgate - The error below is expected - $@");
73 Subject: This is a test of new ticket creation
79 # Check the return value
80 is ( $? >> 8, 75, "The error message above is expected The mail gateway exited with a failure. yay");
83 # {{{ Test new ticket creation by root who is privileged and superuser
86 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --debug --url $url --queue general --action correspond"), "Opened the mailgate - $!");
90 Subject: This is a test of new ticket creation
97 #Check the return value
98 is ($? >> 8, 0, "The mail gateway exited normally. yay");
101 my $tickets = RT::Tickets->new($RT::SystemUser);
102 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
103 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
104 my $tick = $tickets->First();
105 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
106 ok ($tick->Id, "found ticket ".$tick->Id);
107 ok ($tick->Subject eq 'This is a test of new ticket creation', "Created the ticket");
111 # {{{ Test new ticket creation without --action argument
114 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --debug --url $url --queue general"), "Opened the mailgate - $!");
116 From: root\@localhost
118 Subject: using mailgate without --action arg
125 #Check the return value
126 is ($? >> 8, 0, "The mail gateway exited normally. yay");
128 $tickets = RT::Tickets->new($RT::SystemUser);
129 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
130 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
131 $tick = $tickets->First;
132 isa_ok ($tick,'RT::Ticket');
133 ok ($tick->Id, "found ticket ".$tick->Id);
134 is ($tick->Subject, 'using mailgate without --action arg', "using mailgate without --action arg");
138 # {{{This is a test of new ticket creation as an unknown user
141 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
143 From: doesnotexist\@$RT::rtname
145 Subject: This is a test of new ticket creation as an unknown user
151 #Check the return value
152 is ($? >> 8, 0, "The mail gateway exited normally. yay");
154 $tickets = RT::Tickets->new($RT::SystemUser);
155 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
156 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
157 $tick = $tickets->First();
158 ok ($tick->Id, "found ticket ".$tick->Id);
159 ok ($tick->Subject ne 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account");
160 my $u = RT::User->new($RT::SystemUser);
161 $u->Load("doesnotexist\@$RT::rtname");
162 ok( !$u->Id, " user does not exist and was not created by failed ticket submission");
167 # {{{ now everybody can create tickets. can a random unkown user create tickets?
169 my $g = RT::Group->new($RT::SystemUser);
170 $g->LoadSystemInternalGroup('Everyone');
171 ok( $g->Id, "Found 'everybody'");
173 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
174 ok ($val, "Granted everybody the right to create tickets - $msg");
178 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
180 From: doesnotexist\@$RT::rtname
182 Subject: This is a test of new ticket creation as an unknown user
188 #Check the return value
189 is ($? >> 8, 0, "The mail gateway exited normally. yay");
192 $tickets = RT::Tickets->new($RT::SystemUser);
193 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
194 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
195 $tick = $tickets->First();
196 ok ($tick->Id, "found ticket ".$tick->Id);
197 ok ($tick->Subject eq 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account");
198 $u = RT::User->new($RT::SystemUser);
199 $u->Load("doesnotexist\@$RT::rtname");
200 ok( $u->Id != 0, " user does not exist and was created by ticket submission");
205 # {{{ can another random reply to a ticket without being granted privs? answer should be no.
208 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
209 #ok ($val, "Granted everybody the right to create tickets - $msg");
212 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
214 From: doesnotexist-2\@$RT::rtname
216 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
218 Blah! (Should not work.)
222 #Check the return value
223 is ($? >> 8, 0, "The mail gateway exited normally. yay");
225 $u = RT::User->new($RT::SystemUser);
226 $u->Load('doesnotexist-2@$RT::rtname');
227 ok( !$u->Id, " user does not exist and was not created by ticket correspondence submission");
231 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
234 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
235 ok ($val, "Granted everybody the right to reply to tickets - $msg");
238 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
240 From: doesnotexist-2\@$RT::rtname
242 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
248 #Check the return value
249 is ($? >> 8, 0, "The mail gateway exited normally. yay");
252 $u = RT::User->new($RT::SystemUser);
253 $u->Load("doesnotexist-2\@$RT::rtname");
254 ok( $u->Id != 0, " user exists and was created by ticket correspondence submission");
258 # {{{ can another random comment on a ticket without being granted privs? answer should be no.
261 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
262 #ok ($val, "Granted everybody the right to create tickets - $msg");
265 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action comment"), "Opened the mailgate - $!");
267 From: doesnotexist-3\@$RT::rtname
269 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
271 Blah! (Should not work.)
276 #Check the return value
277 is ($? >> 8, 0, "The mail gateway exited normally. yay");
279 $u = RT::User->new($RT::SystemUser);
280 $u->Load("doesnotexist-3\@$RT::rtname");
281 ok( !$u->Id, " user does not exist and was not created by ticket comment submission");
284 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
287 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
288 ok ($val, "Granted everybody the right to reply to tickets - $msg");
291 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action comment"), "Opened the mailgate - $!");
293 From: doesnotexist-3\@$RT::rtname
295 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
302 #Check the return value
303 is ($? >> 8, 0, "The mail gateway exited normally. yay");
305 $u = RT::User->new($RT::SystemUser);
306 $u->Load("doesnotexist-3\@$RT::rtname");
307 ok( $u->Id != 0, " user exists and was created by ticket comment submission");
311 # {{{ Testing preservation of binary attachments
313 # Get a binary blob (Best Practical logo)
315 # Create a mime entity with an attachment
318 my $entity = MIME::Entity->build( From => 'root@localhost',
319 To => 'rt@localhost',
320 Subject => 'binary attachment test',
321 Data => ['This is a test of a binary attachment']);
323 # currently in lib/t/autogen
325 my $LOGO_FILE = $RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif';
327 $entity->attach(Path => $LOGO_FILE,
329 Encoding => 'base64');
331 # Create a ticket with a binary attachment
333 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
335 $entity->print(\*MAIL);
339 #Check the return value
340 is ($? >> 8, 0, "The mail gateway exited normally. yay");
342 $tickets = RT::Tickets->new($RT::SystemUser);
343 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
344 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
345 $tick = $tickets->First();
346 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
347 ok ($tick->Id, "found ticket ".$tick->Id);
348 ok ($tick->Subject eq 'binary attachment test', "Created the ticket - ".$tick->Id);
350 my $file = `cat $LOGO_FILE`;
351 ok ($file, "Read in the logo image");
354 diag( "for the raw file the content is ". md5_base64($file) );
358 # Verify that the binary attachment is valid in the database
359 my $attachments = RT::Attachments->new($RT::SystemUser);
360 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/gif');
361 ok ($attachments->Count == 1, 'Found only one gif in the database');
362 my $attachment = $attachments->First;
364 my $acontent = $attachment->Content;
366 diag( "coming from the database, the content is ". md5_base64($acontent) );
368 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
374 # Grab the binary attachment via the web ui
375 my $ua = LWP::UserAgent->new();
377 my $full_url = "$url/Ticket/Attachment/".$attachment->TransactionId."/".$attachment->id."/bplogo.gif?&user=root&pass=password";
378 my $r = $ua->get( $full_url);
381 # Verify that the downloaded attachment is the same as what we uploaded.
382 is($file, $r->content, 'The attachment isn\'t screwed up in download');
388 # {{{ Simple I18N testing
391 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
394 From: root\@localhost
395 To: rtemail\@$RT::rtname
396 Subject: This is a test of I18N ticket creation
397 Content-Type: text/plain; charset="utf-8"
400 \303\242\303\252\303\256\303\264\303\273
401 \303\241\303\251\303\255\303\263\303\272
406 #Check the return value
407 is ($? >> 8, 0, "The mail gateway exited normally. yay");
409 my $unitickets = RT::Tickets->new($RT::SystemUser);
410 $unitickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
411 $unitickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
412 my $unitick = $unitickets->First();
413 ok (UNIVERSAL::isa($unitick,'RT::Ticket'));
414 ok ($unitick->Id, "found ticket ".$unitick->Id);
415 ok ($unitick->Subject eq 'This is a test of I18N ticket creation', "Created the ticket - ". $unitick->Subject);
419 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
420 Encode::_utf8_on($unistring);
421 is ($unitick->Transactions->First->Content, $unitick->Transactions->First->Attachments->First->Content, "Content is ". $unitick->Transactions->First->Attachments->First->Content);
422 ok($unitick->Transactions->First->Attachments->First->Content =~ /$unistring/i, $unitick->Id." appears to be unicode ". $unitick->Transactions->First->Attachments->First->Id);
423 # supposedly I18N fails on the second message sent in.
426 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
429 From: root\@localhost
430 To: rtemail\@$RT::rtname
431 Subject: This is a test of I18N ticket creation
432 Content-Type: text/plain; charset="utf-8"
435 \303\242\303\252\303\256\303\264\303\273
436 \303\241\303\251\303\255\303\263\303\272
441 #Check the return value
442 is ($? >> 8, 0, "The mail gateway exited normally. yay");
444 my $tickets2 = RT::Tickets->new($RT::SystemUser);
445 $tickets2->OrderBy(FIELD => 'id', ORDER => 'DESC');
446 $tickets2->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
447 my $tick2 = $tickets2->First();
448 ok (UNIVERSAL::isa($tick2,'RT::Ticket'));
449 ok ($tick2->Id, "found ticket ".$tick2->Id);
450 ok ($tick2->Subject eq 'This is a test of I18N ticket creation', "Created the ticket");
454 $unistring = "\303\241\303\251\303\255\303\263\303\272";
455 Encode::_utf8_on($unistring);
457 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
462 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
468 skip "Advanced mailgate actions require an unsafe configuration", 47 unless $RT::UnsafeEmailCommands;
470 #create new queue to be shure we don't mess with rights
472 my $queue = RT::Queue->new($RT::SystemUser);
473 my ($qid) = $queue->Create( Name => 'ext-mailgate');
474 ok( $qid, 'queue created for ext-mailgate tests' );
476 # {{{ Check take and resolve actions
478 # create ticket that is owned by nobody
480 $tick = RT::Ticket->new($RT::SystemUser);
481 my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
482 ok( $id, 'new ticket created' );
483 is( $tick->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' );
486 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
488 From: root\@localhost
489 Subject: [$RT::rtname \#$id] test
493 is ($? >> 8, 0, "The mail gateway exited normally");
495 $tick = RT::Ticket->new($RT::SystemUser);
497 is( $tick->Id, $id, 'load correct ticket');
498 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
500 # check that there is no text transactions writen
501 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
504 ($status, $msg) = $tick->SetOwner( $RT::Nobody->Id, 'Force' );
505 ok( $status, 'successfuly changed owner: '. ($msg||'') );
506 is( $tick->Owner, $RT::Nobody->Id, 'set owner back to nobody');
511 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
513 From: root\@localhost
514 Subject: [$RT::rtname \#$id] correspondence
519 is ($? >> 8, 0, "The mail gateway exited normally");
521 DBIx::SearchBuilder::Record::Cachable->FlushCache;
523 $tick = RT::Ticket->new($RT::SystemUser);
525 is( $tick->Id, $id, "load correct ticket #$id");
526 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
527 my $txns = $tick->Transactions;
528 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
529 $txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
530 # +1 because of auto open
531 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
532 is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
535 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve --debug"), "Opened the mailgate - $!");
537 From: root\@localhost
538 Subject: [$RT::rtname \#$id] test
542 is ($? >> 8, 0, "The mail gateway exited normally");
544 DBIx::SearchBuilder::Record::Cachable->FlushCache;
546 $tick = RT::Ticket->new($RT::SystemUser);
548 is( $tick->Id, $id, 'load correct ticket');
549 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
550 is( $tick->Transactions->Count, 7, 'no superfluous transactions');
553 my $user = RT::User->new( $RT::SystemUser );
554 my ($uid) = $user->Create( Name => 'ext-mailgate',
555 EmailAddress => 'ext-mailgate@localhost',
557 Password => 'qwe123',
559 ok( $uid, 'user created for ext-mailgate tests' );
560 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
562 $tick = RT::Ticket->new($RT::SystemUser);
563 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
564 ok( $id, 'create new ticket' );
567 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
569 From: ext-mailgate\@localhost
570 Subject: [example.com \#$id] test
574 is ( $? >> 8, 0, "mailgate exited normally" );
575 DBIx::SearchBuilder::Record::Cachable->FlushCache;
577 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
579 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
580 ok( $status, "successfuly granted right: $msg" );
581 my $ace_id = $status;
582 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );
585 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
587 From: ext-mailgate\@localhost
588 Subject: [example.com \#$id] test
593 is ( $? >> 8, 0, "mailgate exited normally" );
594 DBIx::SearchBuilder::Record::Cachable->FlushCache;
596 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
597 is( $tick->Transactions->Count, 3, "one transactions added" );
600 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
602 From: ext-mailgate\@localhost
603 Subject: [example.com \#$id] test
608 is ( $? >> 8, 0, "mailgate exited normally" );
609 DBIx::SearchBuilder::Record::Cachable->FlushCache;
611 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
612 is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );
614 # revoke ReplyToTicket right
616 my $ace = RT::ACE->new($RT::SystemUser);
617 $ace->Load( $ace_id );
619 my $acl = RT::ACL->new($RT::SystemUser);
620 $acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
621 $acl->LimitToObject( $RT::System );
622 while( my $ace = $acl->Next ) {
626 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
629 my $group = RT::Group->new( $RT::SystemUser );
630 ok( $group->LoadQueueRoleGroup( Queue => $qid, Type=> 'Owner' ), "load queue owners role group" );
631 $ace = RT::ACE->new( $RT::SystemUser );
632 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
633 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );
635 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
636 ok( $status, "successfuly granted right: $msg" );
637 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
638 ok( $status, "successfuly granted right: $msg" );
641 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
643 From: ext-mailgate\@localhost
644 Subject: [example.com \#$id] test
646 take-correspond with reply right granted to owner role
649 is ( $? >> 8, 0, "mailgate exited normally" );
650 DBIx::SearchBuilder::Record::Cachable->FlushCache;
653 is( $tick->Owner, $user->id, "we changed owner" );
654 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
655 is( $tick->Transactions->Count, 5, "transactions added" );