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 => 104;
63 my $url = join( ':', grep $_, "http://localhost", $RT::WebPort ) . $RT::WebPath ."/";
65 # Make sure that when we call the mailgate wrong, it tempfails
68 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 - $@");
72 Subject: This is a test of new ticket creation
78 # Check the return value
79 is ( $? >> 8, 75, "The error message above is expected The mail gateway exited with a failure. yay");
82 # {{{ Test new ticket creation by root who is privileged and superuser
85 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --debug --url $url --queue general --action correspond"), "Opened the mailgate - $!");
89 Subject: This is a test of new ticket creation
96 #Check the return value
97 is ($? >> 8, 0, "The mail gateway exited normally. yay");
100 my $tickets = RT::Tickets->new($RT::SystemUser);
101 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
102 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
103 my $tick = $tickets->First();
104 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
105 ok ($tick->Id, "found ticket ".$tick->Id);
106 ok ($tick->Subject eq 'This is a test of new ticket creation', "Created the ticket");
111 # {{{This is a test of new ticket creation as an unknown user
114 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
116 From: doesnotexist\@$RT::rtname
118 Subject: This is a test of new ticket creation as an unknown user
124 #Check the return value
125 is ($? >> 8, 0, "The mail gateway exited normally. yay");
127 $tickets = RT::Tickets->new($RT::SystemUser);
128 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
129 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
130 $tick = $tickets->First();
131 ok ($tick->Id, "found ticket ".$tick->Id);
132 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");
133 my $u = RT::User->new($RT::SystemUser);
134 $u->Load("doesnotexist\@$RT::rtname");
135 ok( $u->Id == 0, " user does not exist and was not created by failed ticket submission");
140 # {{{ now everybody can create tickets. can a random unkown user create tickets?
142 my $g = RT::Group->new($RT::SystemUser);
143 $g->LoadSystemInternalGroup('Everyone');
144 ok( $g->Id, "Found 'everybody'");
146 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
147 ok ($val, "Granted everybody the right to create tickets - $msg");
151 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
153 From: doesnotexist\@$RT::rtname
155 Subject: This is a test of new ticket creation as an unknown user
161 #Check the return value
162 is ($? >> 8, 0, "The mail gateway exited normally. yay");
165 $tickets = RT::Tickets->new($RT::SystemUser);
166 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
167 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
168 $tick = $tickets->First();
169 ok ($tick->Id, "found ticket ".$tick->Id);
170 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");
171 $u = RT::User->new($RT::SystemUser);
172 $u->Load("doesnotexist\@$RT::rtname");
173 ok( $u->Id != 0, " user does not exist and was created by ticket submission");
178 # {{{ can another random reply to a ticket without being granted privs? answer should be no.
181 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
182 #ok ($val, "Granted everybody the right to create tickets - $msg");
185 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
187 From: doesnotexist-2\@$RT::rtname
189 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
191 Blah! (Should not work.)
195 #Check the return value
196 is ($? >> 8, 0, "The mail gateway exited normally. yay");
198 $u = RT::User->new($RT::SystemUser);
199 $u->Load('doesnotexist-2@$RT::rtname');
200 ok( $u->Id == 0, " user does not exist and was not created by ticket correspondence submission");
204 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
207 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
208 ok ($val, "Granted everybody the right to reply to tickets - $msg");
211 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
213 From: doesnotexist-2\@$RT::rtname
215 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
221 #Check the return value
222 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 != 0, " user exists and was created by ticket correspondence submission");
231 # {{{ can another random comment on a ticket without being granted privs? answer should be no.
234 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
235 #ok ($val, "Granted everybody the right to create tickets - $msg");
238 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action comment"), "Opened the mailgate - $!");
240 From: doesnotexist-3\@$RT::rtname
242 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
244 Blah! (Should not work.)
249 #Check the return value
250 is ($? >> 8, 0, "The mail gateway exited normally. yay");
252 $u = RT::User->new($RT::SystemUser);
253 $u->Load("doesnotexist-3\@$RT::rtname");
254 ok( $u->Id == 0, " user does not exist and was not created by ticket comment submission");
257 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
260 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
261 ok ($val, "Granted everybody the right to reply to tickets - $msg");
264 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action comment"), "Opened the mailgate - $!");
266 From: doesnotexist-3\@$RT::rtname
268 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
275 #Check the return value
276 is ($? >> 8, 0, "The mail gateway exited normally. yay");
278 $u = RT::User->new($RT::SystemUser);
279 $u->Load("doesnotexist-3\@$RT::rtname");
280 ok( $u->Id != 0, " user exists and was created by ticket comment submission");
284 # {{{ Testing preservation of binary attachments
286 # Get a binary blob (Best Practical logo)
288 # Create a mime entity with an attachment
291 my $entity = MIME::Entity->build( From => 'root@localhost',
292 To => 'rt@localhost',
293 Subject => 'binary attachment test',
294 Data => ['This is a test of a binary attachment']);
296 # currently in lib/t/autogen
298 my $LOGO_FILE = $RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif';
300 $entity->attach(Path => $LOGO_FILE,
302 Encoding => 'base64');
304 # Create a ticket with a binary attachment
306 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
308 $entity->print(\*MAIL);
312 #Check the return value
313 is ($? >> 8, 0, "The mail gateway exited normally. yay");
315 $tickets = RT::Tickets->new($RT::SystemUser);
316 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
317 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
318 $tick = $tickets->First();
319 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
320 ok ($tick->Id, "found ticket ".$tick->Id);
321 ok ($tick->Subject eq 'binary attachment test', "Created the ticket - ".$tick->Id);
323 my $file = `cat $LOGO_FILE`;
324 ok ($file, "Read in the logo image");
328 warn "for the raw file the content is ".Digest::MD5::md5_base64($file);
332 # Verify that the binary attachment is valid in the database
333 my $attachments = RT::Attachments->new($RT::SystemUser);
334 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/gif');
335 ok ($attachments->Count == 1, 'Found only one gif in the database');
336 my $attachment = $attachments->First;
338 my $acontent = $attachment->Content;
340 warn "coming from the database, the content is ".Digest::MD5::md5_base64($acontent);
342 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
348 # Grab the binary attachment via the web ui
349 my $ua = LWP::UserAgent->new();
351 my $full_url = "$url/Ticket/Attachment/".$attachment->TransactionId."/".$attachment->id."/bplogo.gif?&user=root&pass=password";
352 my $r = $ua->get( $full_url);
355 # Verify that the downloaded attachment is the same as what we uploaded.
356 is($file, $r->content, 'The attachment isn\'t screwed up in download');
362 # {{{ Simple I18N testing
365 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
368 From: root\@localhost
369 To: rtemail\@$RT::rtname
370 Subject: This is a test of I18N ticket creation
371 Content-Type: text/plain; charset="utf-8"
374 \303\242\303\252\303\256\303\264\303\273
375 \303\241\303\251\303\255\303\263\303\272
380 #Check the return value
381 is ($? >> 8, 0, "The mail gateway exited normally. yay");
383 my $unitickets = RT::Tickets->new($RT::SystemUser);
384 $unitickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
385 $unitickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
386 my $unitick = $unitickets->First();
387 ok (UNIVERSAL::isa($unitick,'RT::Ticket'));
388 ok ($unitick->Id, "found ticket ".$unitick->Id);
389 ok ($unitick->Subject eq 'This is a test of I18N ticket creation', "Created the ticket - ". $unitick->Subject);
393 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
394 Encode::_utf8_on($unistring);
395 is ($unitick->Transactions->First->Content, $unitick->Transactions->First->Attachments->First->Content, "Content is ". $unitick->Transactions->First->Attachments->First->Content);
396 ok($unitick->Transactions->First->Attachments->First->Content =~ /$unistring/i, $unitick->Id." appears to be unicode ". $unitick->Transactions->First->Attachments->First->Id);
397 # supposedly I18N fails on the second message sent in.
400 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
403 From: root\@localhost
404 To: rtemail\@$RT::rtname
405 Subject: This is a test of I18N ticket creation
406 Content-Type: text/plain; charset="utf-8"
409 \303\242\303\252\303\256\303\264\303\273
410 \303\241\303\251\303\255\303\263\303\272
415 #Check the return value
416 is ($? >> 8, 0, "The mail gateway exited normally. yay");
418 my $tickets2 = RT::Tickets->new($RT::SystemUser);
419 $tickets2->OrderBy(FIELD => 'id', ORDER => 'DESC');
420 $tickets2->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
421 my $tick2 = $tickets2->First();
422 ok (UNIVERSAL::isa($tick2,'RT::Ticket'));
423 ok ($tick2->Id, "found ticket ".$tick2->Id);
424 ok ($tick2->Subject eq 'This is a test of I18N ticket creation', "Created the ticket");
428 $unistring = "\303\241\303\251\303\255\303\263\303\272";
429 Encode::_utf8_on($unistring);
431 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
436 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
442 skip "Advanced mailgate actions require an unsafe configuration", 47 unless $RT::UnsafeEmailCommands;
444 #create new queue to be shure we don't mess with rights
446 my $queue = RT::Queue->new($RT::SystemUser);
447 my ($qid) = $queue->Create( Name => 'ext-mailgate');
448 ok( $qid, 'queue created for ext-mailgate tests' );
450 # {{{ Check take and resolve actions
452 # create ticket that is owned by nobody
454 $tick = RT::Ticket->new($RT::SystemUser);
455 my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
456 ok( $id, 'new ticket created' );
457 is( $tick->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' );
460 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
462 From: root\@localhost
463 Subject: [$RT::rtname \#$id] test
467 is ($? >> 8, 0, "The mail gateway exited normally");
469 $tick = RT::Ticket->new($RT::SystemUser);
471 is( $tick->Id, $id, 'load correct ticket');
472 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
474 # check that there is no text transactions writen
475 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
478 ($status, $msg) = $tick->SetOwner( $RT::Nobody->Id, 'Force' );
479 ok( $status, 'successfuly changed owner: '. ($msg||'') );
480 is( $tick->Owner, $RT::Nobody->Id, 'set owner back to nobody');
485 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
487 From: root\@localhost
488 Subject: [$RT::rtname \#$id] correspondence
493 is ($? >> 8, 0, "The mail gateway exited normally");
495 DBIx::SearchBuilder::Record::Cachable->FlushCache;
497 $tick = RT::Ticket->new($RT::SystemUser);
499 is( $tick->Id, $id, "load correct ticket #$id");
500 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
501 my $txns = $tick->Transactions;
502 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
503 $txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
504 # +1 because of auto open
505 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
506 is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
509 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve --debug"), "Opened the mailgate - $!");
511 From: root\@localhost
512 Subject: [$RT::rtname \#$id] test
516 is ($? >> 8, 0, "The mail gateway exited normally");
518 DBIx::SearchBuilder::Record::Cachable->FlushCache;
520 $tick = RT::Ticket->new($RT::SystemUser);
522 is( $tick->Id, $id, 'load correct ticket');
523 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
524 is( $tick->Transactions->Count, 7, 'no superfluous transactions');
527 my $user = RT::User->new( $RT::SystemUser );
528 my ($uid) = $user->Create( Name => 'ext-mailgate',
529 EmailAddress => 'ext-mailgate@localhost',
531 Password => 'qwe123',
533 ok( $uid, 'user created for ext-mailgate tests' );
534 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
536 $tick = RT::Ticket->new($RT::SystemUser);
537 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
538 ok( $id, 'create new ticket' );
541 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
543 From: ext-mailgate\@localhost
544 Subject: [example.com \#$id] test
548 is ( $? >> 8, 0, "mailgate exited normally" );
549 DBIx::SearchBuilder::Record::Cachable->FlushCache;
551 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
553 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
554 ok( $status, "successfuly granted right: $msg" );
555 my $ace_id = $status;
556 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );
559 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
561 From: ext-mailgate\@localhost
562 Subject: [example.com \#$id] test
567 is ( $? >> 8, 0, "mailgate exited normally" );
568 DBIx::SearchBuilder::Record::Cachable->FlushCache;
570 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
571 is( $tick->Transactions->Count, 3, "one transactions added" );
574 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
576 From: ext-mailgate\@localhost
577 Subject: [example.com \#$id] test
582 is ( $? >> 8, 0, "mailgate exited normally" );
583 DBIx::SearchBuilder::Record::Cachable->FlushCache;
585 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
586 is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );
588 # revoke ReplyToTicket right
590 my $ace = RT::ACE->new($RT::SystemUser);
591 $ace->Load( $ace_id );
593 my $acl = RT::ACL->new($RT::SystemUser);
594 $acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
595 $acl->LimitToObject( $RT::System );
596 while( my $ace = $acl->Next ) {
600 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
603 my $group = RT::Group->new( $RT::SystemUser );
604 ok( $group->LoadQueueRoleGroup( Queue => $qid, Type=> 'Owner' ), "load queue owners role group" );
605 $ace = RT::ACE->new( $RT::SystemUser );
606 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
607 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );
609 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
610 ok( $status, "successfuly granted right: $msg" );
611 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
612 ok( $status, "successfuly granted right: $msg" );
615 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
617 From: ext-mailgate\@localhost
618 Subject: [example.com \#$id] test
620 take-correspond with reply right granted to owner role
623 is ( $? >> 8, 0, "mailgate exited normally" );
624 DBIx::SearchBuilder::Record::Cachable->FlushCache;
627 is( $tick->Owner, $user->id, "we changed owner" );
628 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
629 is( $tick->Transactions->Count, 5, "transactions added" );