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 => 57;
60 # Make sure that when we call the mailgate wrong, it tempfails
62 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 - $@");
66 Subject: This is a test of new ticket creation
72 # Check the return value
73 is ( $? >> 8, 75, "The error message above is expected The mail gateway exited with a failure. yay");
76 # {{{ Test new ticket creation by root who is privileged and superuser
78 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
82 Subject: This is a test of new ticket creation
89 #Check the return value
90 is ($? >> 8, 0, "The mail gateway exited normally. yay");
93 my $tickets = RT::Tickets->new($RT::SystemUser);
94 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
95 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
96 my $tick = $tickets->First();
97 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
98 ok ($tick->Id, "found ticket ".$tick->Id);
99 ok ($tick->Subject eq 'This is a test of new ticket creation', "Created the ticket");
104 # {{{This is a test of new ticket creation as an unknown user
106 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
108 From: doesnotexist\@$RT::rtname
110 Subject: This is a test of new ticket creation as an unknown user
116 #Check the return value
117 is ($? >> 8, 0, "The mail gateway exited normally. yay");
119 $tickets = RT::Tickets->new($RT::SystemUser);
120 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
121 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
122 $tick = $tickets->First();
123 ok ($tick->Id, "found ticket ".$tick->Id);
124 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");
125 my $u = RT::User->new($RT::SystemUser);
126 $u->Load("doesnotexist\@$RT::rtname");
127 ok( $u->Id == 0, " user does not exist and was not created by failed ticket submission");
132 # {{{ now everybody can create tickets. can a random unkown user create tickets?
134 my $g = RT::Group->new($RT::SystemUser);
135 $g->LoadSystemInternalGroup('Everyone');
136 ok( $g->Id, "Found 'everybody'");
138 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
139 ok ($val, "Granted everybody the right to create tickets - $msg");
142 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
144 From: doesnotexist\@$RT::rtname
146 Subject: This is a test of new ticket creation as an unknown user
152 #Check the return value
153 is ($? >> 8, 0, "The mail gateway exited normally. yay");
156 $tickets = RT::Tickets->new($RT::SystemUser);
157 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
158 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
159 $tick = $tickets->First();
160 ok ($tick->Id, "found ticket ".$tick->Id);
161 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");
162 $u = RT::User->new($RT::SystemUser);
163 $u->Load("doesnotexist\@$RT::rtname");
164 ok( $u->Id != 0, " user does not exist and was created by ticket submission");
169 # {{{ can another random reply to a ticket without being granted privs? answer should be no.
172 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
173 #ok ($val, "Granted everybody the right to create tickets - $msg");
175 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
177 From: doesnotexist-2\@$RT::rtname
179 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
181 Blah! (Should not work.)
185 #Check the return value
186 is ($? >> 8, 0, "The mail gateway exited normally. yay");
188 $u = RT::User->new($RT::SystemUser);
189 $u->Load('doesnotexist-2@$RT::rtname');
190 ok( $u->Id == 0, " user does not exist and was not created by ticket correspondence submission");
194 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
197 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
198 ok ($val, "Granted everybody the right to reply to tickets - $msg");
200 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
202 From: doesnotexist-2\@$RT::rtname
204 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
210 #Check the return value
211 is ($? >> 8, 0, "The mail gateway exited normally. yay");
214 $u = RT::User->new($RT::SystemUser);
215 $u->Load("doesnotexist-2\@$RT::rtname");
216 ok( $u->Id != 0, " user exists and was created by ticket correspondence submission");
220 # {{{ can another random comment on a ticket without being granted privs? answer should be no.
223 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
224 #ok ($val, "Granted everybody the right to create tickets - $msg");
226 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action comment"), "Opened the mailgate - $@");
228 From: doesnotexist-3\@$RT::rtname
230 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
232 Blah! (Should not work.)
237 #Check the return value
238 is ($? >> 8, 0, "The mail gateway exited normally. yay");
240 $u = RT::User->new($RT::SystemUser);
241 $u->Load("doesnotexist-3\@$RT::rtname");
242 ok( $u->Id == 0, " user does not exist and was not created by ticket comment submission");
245 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
248 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
249 ok ($val, "Granted everybody the right to reply to tickets - $msg");
251 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action comment"), "Opened the mailgate - $@");
253 From: doesnotexist-3\@$RT::rtname
255 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
262 #Check the return value
263 is ($? >> 8, 0, "The mail gateway exited normally. yay");
265 $u = RT::User->new($RT::SystemUser);
266 $u->Load("doesnotexist-3\@$RT::rtname");
267 ok( $u->Id != 0, " user exists and was created by ticket comment submission");
271 # {{{ Testing preservation of binary attachments
273 # Get a binary blob (Best Practical logo)
275 # Create a mime entity with an attachment
278 my $entity = MIME::Entity->build( From => 'root@localhost',
279 To => 'rt@localhost',
280 Subject => 'binary attachment test',
281 Data => ['This is a test of a binary attachment']);
283 # currently in lib/t/autogen
285 my $LOGO_FILE = $RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif';
287 $entity->attach(Path => $LOGO_FILE,
289 Encoding => 'base64');
291 # Create a ticket with a binary attachment
292 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
294 $entity->print(\*MAIL);
298 #Check the return value
299 is ($? >> 8, 0, "The mail gateway exited normally. yay");
301 $tickets = RT::Tickets->new($RT::SystemUser);
302 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
303 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
304 $tick = $tickets->First();
305 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
306 ok ($tick->Id, "found ticket ".$tick->Id);
307 ok ($tick->Subject eq 'binary attachment test', "Created the ticket - ".$tick->Id);
309 my $file = `cat $LOGO_FILE`;
310 ok ($file, "Read in the logo image");
314 warn "for the raw file the content is ".Digest::MD5::md5_base64($file);
318 # Verify that the binary attachment is valid in the database
319 my $attachments = RT::Attachments->new($RT::SystemUser);
320 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/gif');
321 ok ($attachments->Count == 1, 'Found only one gif in the database');
322 my $attachment = $attachments->First;
324 my $acontent = $attachment->Content;
326 warn "coming from the database, the content is ".Digest::MD5::md5_base64($acontent);
328 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
334 # Grab the binary attachment via the web ui
335 my $ua = LWP::UserAgent->new();
337 my $full_url = "$RT::WebURL/Ticket/Attachment/".$attachment->TransactionId."/".$attachment->id."/bplogo.gif?&user=root&pass=password";
338 my $r = $ua->get( $full_url);
341 # Verify that the downloaded attachment is the same as what we uploaded.
342 is($file, $r->content, 'The attachment isn\'t screwed up in download');
348 # {{{ Simple I18N testing
350 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
353 From: root\@localhost
354 To: rtemail\@$RT::rtname
355 Subject: This is a test of I18N ticket creation
356 Content-Type: text/plain; charset="utf-8"
359 \303\242\303\252\303\256\303\264\303\273
360 \303\241\303\251\303\255\303\263\303\272
365 #Check the return value
366 is ($? >> 8, 0, "The mail gateway exited normally. yay");
368 my $unitickets = RT::Tickets->new($RT::SystemUser);
369 $unitickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
370 $unitickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
371 my $unitick = $unitickets->First();
372 ok (UNIVERSAL::isa($unitick,'RT::Ticket'));
373 ok ($unitick->Id, "found ticket ".$unitick->Id);
374 ok ($unitick->Subject eq 'This is a test of I18N ticket creation', "Created the ticket - ". $unitick->Subject);
378 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
379 Encode::_utf8_on($unistring);
380 is ($unitick->Transactions->First->Content, $unitick->Transactions->First->Attachments->First->Content, "Content is ". $unitick->Transactions->First->Attachments->First->Content);
381 ok($unitick->Transactions->First->Attachments->First->Content =~ /$unistring/i, $unitick->Id." appears to be unicode ". $unitick->Transactions->First->Attachments->First->Id);
382 # supposedly I18N fails on the second message sent in.
384 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
387 From: root\@localhost
388 To: rtemail\@$RT::rtname
389 Subject: This is a test of I18N ticket creation
390 Content-Type: text/plain; charset="utf-8"
393 \303\242\303\252\303\256\303\264\303\273
394 \303\241\303\251\303\255\303\263\303\272
399 #Check the return value
400 is ($? >> 8, 0, "The mail gateway exited normally. yay");
402 my $tickets2 = RT::Tickets->new($RT::SystemUser);
403 $tickets2->OrderBy(FIELD => 'id', ORDER => 'DESC');
404 $tickets2->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
405 my $tick2 = $tickets2->First();
406 ok (UNIVERSAL::isa($tick2,'RT::Ticket'));
407 ok ($tick2->Id, "found ticket ".$tick2->Id);
408 ok ($tick2->Subject eq 'This is a test of I18N ticket creation', "Created the ticket");
412 $unistring = "\303\241\303\251\303\255\303\263\303\272";
413 Encode::_utf8_on($unistring);
415 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
420 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
427 # {{{ Check take and resolve actions
429 # create ticket that is owned by nobody
431 $tick = RT::Ticket->new($RT::SystemUser);
432 my ($id) = $tick->Create( Queue => 'general', Subject => 'test');
433 ok( $id, 'new ticket created' );
434 is( $tick->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' );
436 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action take"), "Opened the mailgate - $@");
438 From: root\@localhost
439 Subject: [$RT::rtname \#$id] test
443 is ($? >> 8, 0, "The mail gateway exited normally");
445 $tick = RT::Ticket->new($RT::SystemUser);
447 is( $tick->Id, $id, 'load correct ticket');
448 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
450 # check that there is no text transactions writen
451 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
454 ($status, $msg) = $tick->SetOwner( $RT::Nobody->Id, 'Force' );
455 ok( $status, 'successfuly changed owner: '. ($msg||'') );
456 is( $tick->Owner, $RT::Nobody->Id, 'set owner back to nobody');
460 local $TODO = "Advanced mailgate actions require an unsafe configuration";
461 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action take-correspond"), "Opened the mailgate - $@");
463 From: root\@localhost
464 Subject: [$RT::rtname \#$id] correspondence
469 is ($? >> 8, 0, "The mail gateway exited normally");
471 $tick = RT::Ticket->new($RT::SystemUser);
473 is( $tick->Id, $id, 'load correct ticket');
474 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
475 my $txns = $tick->Transactions;
476 $txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
477 is( $txns->Last->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
478 # +1 because of auto open
479 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
481 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action resolve"), "Opened the mailgate - $@");
483 From: root\@localhost
484 Subject: [$RT::rtname \#$id] test
488 is ($? >> 8, 0, "The mail gateway exited normally");
490 DBIx::SearchBuilder::Record::Cachable->FlushCache;
492 $tick = RT::Ticket->new($RT::SystemUser);
494 is( $tick->Id, $id, 'load correct ticket');
495 is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
496 is( $tick->Transactions->Count, 7, 'no superfluous transactions');