import rt 3.8.10
[freeside.git] / rt / t / mail / gateway.t
1 #!/usr/bin/perl -w
2 # BEGIN BPS TAGGED BLOCK {{{
3
4 # COPYRIGHT:
5 #  
6 # This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
7 #                                          <jesse.com>
8
9 # (Except where explicitly superseded by other copyright notices)
10
11
12 # LICENSE:
13
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
17 # from www.gnu.org.
18
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.
23
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.
29
30
31 # CONTRIBUTION SUBMISSION POLICY:
32
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.)
38
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.
47
48 # END BPS TAGGED BLOCK }}}
49
50 =head1 NAME
51
52 rt-mailgate - Mail interface to RT3.
53
54 =cut
55
56 use strict;
57 use warnings;
58
59
60 use RT::Test config => 'Set( $UnsafeEmailCommands, 1);', tests => 159;
61 my ($baseurl, $m) = RT::Test->started_ok;
62
63 use RT::Tickets;
64
65 use MIME::Entity;
66 use Digest::MD5 qw(md5_base64);
67 use LWP::UserAgent;
68
69 # TODO: --extension queue
70
71 my $url = $m->rt_base_url;
72
73 diag "Make sure that when we call the mailgate without URL, it fails" if $ENV{'TEST_VERBOSE'};
74 {
75     my $text = <<EOF;
76 From: root\@localhost
77 To: rt\@@{[RT->Config->Get('rtname')]}
78 Subject: This is a test of new ticket creation
79
80 Foob!
81 EOF
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";
85 }
86
87 diag "Make sure that when we call the mailgate with wrong URL, it tempfails" if $ENV{'TEST_VERBOSE'};
88 {
89     my $text = <<EOF;
90 From: root\@localhost
91 To: rt\@@{[RT->Config->Get('rtname')]}
92 Subject: This is a test of new ticket creation
93
94 Foob!
95 EOF
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");
99 }
100
101 my $everyone_group;
102 diag "revoke rights tests depend on" if $ENV{'TEST_VERBOSE'};
103 {
104     $everyone_group = RT::Group->new( $RT::SystemUser );
105     $everyone_group->LoadSystemInternalGroup( 'Everyone' );
106     ok ($everyone_group->Id, "Found group 'everyone'");
107
108     foreach( qw(CreateTicket ReplyToTicket CommentOnTicket) ) {
109         $everyone_group->PrincipalObj->RevokeRight(Right => $_);
110     }
111 }
112
113 diag "Test new ticket creation by root who is privileged and superuser" if $ENV{'TEST_VERBOSE'};
114 {
115     my $text = <<EOF;
116 From: root\@localhost
117 To: rt\@@{[RT->Config->Get('rtname')]}
118 Subject: This is a test of new ticket creation
119
120 Blah!
121 Foob!
122 EOF
123
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");
127
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");
132 }
133
134 diag "Test the 'X-RT-Mail-Extension' field in the header of a ticket" if $ENV{'TEST_VERBOSE'};
135 {
136     my $text = <<EOF;
137 From: root\@localhost
138 To: rt\@@{[RT->Config->Get('rtname')]}
139 Subject: This is a test of the X-RT-Mail-Extension field
140 Blah!
141 Foob!
142 EOF
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");
147
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");
152
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");
159
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
163     is (
164         $attachment->GetHeader('X-RT-Mail-Extension'),
165         "bad value with newlines",
166         'header is in place, without trailing newline char'
167     );
168 }
169
170 diag "Make sure that not standard --extension is passed" if $ENV{'TEST_VERBOSE'};
171 {
172     my $text = <<EOF;
173 From: root\@localhost
174 To: rt\@@{[RT->Config->Get('rtname')]}
175 Subject: This is a test of new ticket creation
176
177 Foob!
178 EOF
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");
182
183     my $tick = RT::Test->last_ticket;
184     isa_ok ($tick, 'RT::Ticket');
185     is ($tick->Id, $id, "correct ticket id");
186
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");
193
194     my $attachment = $txn->Attachments->First;
195     isa_ok ($attachment, 'RT::Attachment');
196     is (
197         $attachment->GetHeader('X-RT-Mail-Extension'),
198         'some-extension-arg',
199         'header is in place'
200     );
201 }
202
203 diag "Test new ticket creation without --action argument" if $ENV{'TEST_VERBOSE'};
204 {
205     my $text = <<EOF;
206 From: root\@localhost
207 To: rt\@$RT::rtname
208 Subject: using mailgate without --action arg
209
210 Blah!
211 Foob!
212 EOF
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");
216
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");
221 }
222
223 diag "This is a test of new ticket creation as an unknown user" if $ENV{'TEST_VERBOSE'};
224 {
225     my $text = <<EOF;
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
229
230 Blah!
231 Foob!
232 EOF
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");
236
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");
241
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");
245 }
246
247 diag "grant everybody with CreateTicket right" if $ENV{'TEST_VERBOSE'};
248 {
249     ok( RT::Test->set_rights(
250         { Principal => $everyone_group->PrincipalObj,
251           Right => [qw(CreateTicket)],
252         },
253     ), "Granted everybody the right to create tickets");
254 }
255
256 my $ticket_id;
257 diag "now everybody can create tickets. can a random unkown user create tickets?" if $ENV{'TEST_VERBOSE'};
258 {
259     my $text = <<EOF;
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
263
264 Blah!
265 Foob!
266 EOF
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");
270
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");
276
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");
280     $ticket_id = $id;
281 }
282
283 diag "can another random reply to a ticket without being granted privs? answer should be no." if $ENV{'TEST_VERBOSE'};
284 {
285     my $text = <<EOF;
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
289
290 Blah!  (Should not work.)
291 Foob!
292 EOF
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");
296
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");
300 }
301
302 diag "grant everyone 'ReplyToTicket' right" if $ENV{'TEST_VERBOSE'};
303 {
304     ok( RT::Test->set_rights(
305         { Principal => $everyone_group->PrincipalObj,
306           Right => [qw(CreateTicket ReplyToTicket)],
307         },
308     ), "Granted everybody the right to reply to tickets" );
309 }
310
311 diag "can another random reply to a ticket after being granted privs? answer should be yes" if $ENV{'TEST_VERBOSE'};
312 {
313     my $text = <<EOF;
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
317
318 Blah!
319 Foob!
320 EOF
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");
324
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");
328 }
329
330 diag "add a reply to the ticket using '--extension ticket' feature" if $ENV{'TEST_VERBOSE'};
331 {
332     my $text = <<EOF;
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
336
337 Blah!
338 Foob!
339 EOF
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");
344
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");
349
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");
356
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');
360 }
361
362 diag "can another random comment on a ticket without being granted privs? answer should be no" if $ENV{'TEST_VERBOSE'};
363 {
364     my $text = <<EOF;
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
368
369 Blah!  (Should not work.)
370 Foob!
371 EOF
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");
375
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");
379 }
380
381
382 diag "grant everyone 'CommentOnTicket' right" if $ENV{'TEST_VERBOSE'};
383 {
384     ok( RT::Test->set_rights(
385         { Principal => $everyone_group->PrincipalObj,
386           Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
387         },
388     ), "Granted everybody the right to comment on tickets");
389 }
390
391 diag "can another random reply to a ticket after being granted privs? answer should be yes" if $ENV{'TEST_VERBOSE'};
392 {
393     my $text = <<EOF;
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
397
398 Blah!
399 Foob!
400 EOF
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");
404
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");
408 }
409
410 diag "add comment to the ticket using '--extension action' feature" if $ENV{'TEST_VERBOSE'};
411 {
412     my $text = <<EOF;
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'
416
417 Blah!
418 Foob!
419 EOF
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");
424
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");
429
430     my $transactions = $tick->Transactions;
431     $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
432     $transactions->Limit(
433         FIELD => 'Type',
434         OPERATOR => 'NOT ENDSWITH',
435         VALUE => 'EmailRecord',
436         ENTRYAGGREGATOR => 'AND',
437     );
438     my $txn = $transactions->First;
439     isa_ok ($txn, 'RT::Transaction');
440     is ($txn->Type, 'Comment', "correct type");
441
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');
445 }
446
447 diag "Testing preservation of binary attachments" if $ENV{'TEST_VERBOSE'};
448 {
449     # Get a binary blob (Best Practical logo) 
450     my $LOGO_FILE = $RT::MasonComponentRoot .'/NoAuth/images/bplogo.gif';
451
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'],
458     );
459
460     $entity->attach(
461         Path     => $LOGO_FILE,
462         Type     => 'image/gif',
463         Encoding => 'base64',
464     );
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");
469
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);
475
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'};
479
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(
484         ALIAS1 => 'main',
485         FIELD1 => 'TransactionId',
486         TABLE2 => 'Transactions',
487         FIELD2 => 'id',
488     );
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;
495
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.');
498
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 );
504
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');
507 }
508
509 diag "Simple I18N testing" if $ENV{'TEST_VERBOSE'};
510 {
511     my $text = <<EOF;
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"
516
517 2 accented lines
518 \303\242\303\252\303\256\303\264\303\273
519 \303\241\303\251\303\255\303\263\303\272
520 bye
521 EOF
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");
525
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);
531
532     my $unistring = "\303\241\303\251\303\255\303\263\303\272";
533     Encode::_utf8_on($unistring);
534     is (
535         $tick->Transactions->First->Content,
536         $tick->Transactions->First->Attachments->First->Content,
537         "Content is ". $tick->Transactions->First->Attachments->First->Content
538     );
539     ok (
540         $tick->Transactions->First->Content =~ /$unistring/i,
541         $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
542     );
543 }
544
545 diag "supposedly I18N fails on the second message sent in." if $ENV{'TEST_VERBOSE'};
546 {
547     my $text = <<EOF;
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"
552
553 2 accented lines
554 \303\242\303\252\303\256\303\264\303\273
555 \303\241\303\251\303\255\303\263\303\272
556 bye
557 EOF
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");
561
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");
567
568     my $unistring = "\303\241\303\251\303\255\303\263\303\272";
569     Encode::_utf8_on($unistring);
570
571     ok (
572         $tick->Transactions->First->Content =~ $unistring,
573         "It appears to be unicode - ". $tick->Transactions->First->Content
574     );
575 }
576
577 diag "check that mailgate doesn't suffer from empty Reply-To:" if $ENV{'TEST_VERBOSE'};
578 {
579     my $text = <<EOF;
580 From: root\@localhost
581 Reply-To: 
582 To: rtemail\@@{[RT->Config->Get('rtname')]}
583 Subject: test
584 Content-Type: text/plain; charset="utf-8"
585
586 test
587 EOF
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");
591
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");
596
597     like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
598 }
599
600
601 my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
602 ok ($val, $msg);
603
604 SKIP: {
605 skip "Advanced mailgate actions require an unsafe configuration", 47
606     unless RT->Config->Get('UnsafeEmailCommands');
607
608 # create new queue to be shure we don't mess with rights
609 use RT::Queue;
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' );
613
614 # {{{ Check take and resolve actions
615
616 # create ticket that is owned by nobody
617 use RT::Ticket;
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' );
622
623 $! = 0;
624 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
625 print MAIL <<EOF;
626 From: root\@localhost
627 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
628
629 EOF
630 close (MAIL);
631 is ($? >> 8, 0, "The mail gateway exited normally");
632
633 $tick = RT::Ticket->new($RT::SystemUser);
634 $tick->Load( $id );
635 is( $tick->Id, $id, 'load correct ticket');
636 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
637
638 # check that there is no text transactions writen
639 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
640
641 my $status;
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');
645
646
647 $! = 0;
648 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
649 print MAIL <<EOF;
650 From: root\@localhost
651 Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
652
653 test
654 EOF
655 close (MAIL);
656 is ($? >> 8, 0, "The mail gateway exited normally");
657
658 DBIx::SearchBuilder::Record::Cachable->FlushCache;
659
660 $tick = RT::Ticket->new($RT::SystemUser);
661 $tick->Load( $id );
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' );
670
671 $! = 0;
672 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
673 print MAIL <<EOF;
674 From: root\@localhost
675 Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
676
677 EOF
678 close (MAIL);
679 is ($? >> 8, 0, "The mail gateway exited normally");
680
681 DBIx::SearchBuilder::Record::Cachable->FlushCache;
682
683 $tick = RT::Ticket->new($RT::SystemUser);
684 $tick->Load( $id );
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');
688
689 use RT::User;
690 my $user = RT::User->new( $RT::SystemUser );
691 my ($uid) = $user->Create( Name => 'ext-mailgate',
692                            EmailAddress => 'ext-mailgate@localhost',
693                            Privileged => 1,
694                            Password => 'qwe123',
695                          );
696 ok( $uid, 'user created for ext-mailgate tests' );
697 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
698
699 $tick = RT::Ticket->new($RT::SystemUser);
700 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
701 ok( $id, 'create new ticket' );
702
703 my $rtname = RT->Config->Get('rtname');
704
705 $! = 0;
706 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
707 print MAIL <<EOF;
708 From: ext-mailgate\@localhost
709 Subject: [$rtname \#$id] test
710
711 EOF
712 close (MAIL);
713 is ( $? >> 8, 0, "mailgate exited normally" );
714 DBIx::SearchBuilder::Record::Cachable->FlushCache;
715
716 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
717
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" );
722
723 $! = 0;
724 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
725 print MAIL <<EOF;
726 From: ext-mailgate\@localhost
727 Subject: [$rtname \#$id] test
728
729 correspond-take
730 EOF
731 close (MAIL);
732 is ( $? >> 8, 0, "mailgate exited normally" );
733 DBIx::SearchBuilder::Record::Cachable->FlushCache;
734
735 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
736 is( $tick->Transactions->Count, 3, "one transactions added" );
737
738 $! = 0;
739 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
740 print MAIL <<EOF;
741 From: ext-mailgate\@localhost
742 Subject: [$rtname \#$id] test
743
744 correspond-take
745 EOF
746 close (MAIL);
747 is ( $? >> 8, 0, "mailgate exited normally" );
748 DBIx::SearchBuilder::Record::Cachable->FlushCache;
749
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" );
752
753 # revoke ReplyToTicket right
754 use RT::ACE;
755 my $ace = RT::ACE->new($RT::SystemUser);
756 $ace->Load( $ace_id );
757 $ace->Delete;
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 ) {
762         $ace->Delete;
763 }
764
765 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
766
767
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" );
773
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" );
778
779 $! = 0;
780 ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
781 print MAIL <<EOF;
782 From: ext-mailgate\@localhost
783 Subject: [$rtname \#$id] test
784
785 take-correspond with reply right granted to owner role
786 EOF
787 close (MAIL);
788 is ( $? >> 8, 0, "mailgate exited normally" );
789 DBIx::SearchBuilder::Record::Cachable->FlushCache;
790
791 $tick->Load( $id );
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" );
795
796
797 # }}}
798 };
799
800
801 1;
802