import rt 3.8.11
[freeside.git] / rt / lib / t / regression / 06mailgateway.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., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28
29 # CONTRIBUTION SUBMISSION POLICY:
30
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.)
36
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.
45
46 # END BPS TAGGED BLOCK }}}
47
48 =head1 NAME
49
50 rt-mailgate - Mail interface to RT3.
51
52 =cut
53
54 use strict;
55 use Test::More tests => 109;
56
57 use RT;
58 RT::LoadConfig();
59 RT::Init();
60 use RT::I18N;
61 use Digest::MD5 qw(md5_base64);
62
63 no warnings 'once';
64 my $url = join( ':', grep $_, "http://localhost", $RT::WebPort ) . $RT::WebPath ."/";
65
66 # Make sure that when we call the mailgate wrong, it tempfails
67
68 $! = 0;
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 - $@");
70 print MAIL <<EOF;
71 From: root\@localhost
72 To: rt\@$RT::rtname
73 Subject: This is a test of new ticket creation
74
75 Foob!
76 EOF
77 close (MAIL);
78
79 # Check the return value
80 is ( $? >> 8, 75, "The error message above is expected The mail gateway exited with a failure. yay");
81
82
83 # {{{ Test new ticket creation by root who is privileged and superuser
84
85 $! = 0;
86 ok(open(MAIL, "|$RT::BinPath/rt-mailgate  --debug --url $url --queue general --action correspond"), "Opened the mailgate - $!");
87 print MAIL <<EOF;
88 From: root\@localhost
89 To: rt\@$RT::rtname
90 Subject: This is a test of new ticket creation
91
92 Blah!
93 Foob!
94 EOF
95 close (MAIL);
96
97 #Check the return value
98 is ($? >> 8, 0, "The mail gateway exited normally. yay");
99
100 use RT::Tickets;
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");
108
109 # }}}
110
111 # {{{ Test new ticket creation without --action argument
112
113 $! = 0;
114 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --debug --url $url --queue general"), "Opened the mailgate - $!");
115 print MAIL <<EOF;
116 From: root\@localhost
117 To: rt\@$RT::rtname
118 Subject: using mailgate without --action arg
119
120 Blah!
121 Foob!
122 EOF
123 close (MAIL);
124
125 #Check the return value
126 is ($? >> 8, 0, "The mail gateway exited normally. yay");
127
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");
135
136 # }}}
137
138 # {{{This is a test of new ticket creation as an unknown user
139
140 $! = 0;
141 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
142 print MAIL <<EOF;
143 From: doesnotexist\@$RT::rtname
144 To: rt\@$RT::rtname
145 Subject: This is a test of new ticket creation as an unknown user
146
147 Blah!
148 Foob!
149 EOF
150 close (MAIL);
151 #Check the return value
152 is ($? >> 8, 0, "The mail gateway exited normally. yay");
153
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");
163
164
165 # }}}
166
167 # {{{ now everybody can create tickets.  can a random unkown user create tickets?
168
169 my $g = RT::Group->new($RT::SystemUser);
170 $g->LoadSystemInternalGroup('Everyone');
171 ok( $g->Id, "Found 'everybody'");
172
173 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
174 ok ($val, "Granted everybody the right to create tickets - $msg");
175
176
177 $! = 0;
178 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
179 print MAIL <<EOF;
180 From: doesnotexist\@$RT::rtname
181 To: rt\@$RT::rtname
182 Subject: This is a test of new ticket creation as an unknown user
183
184 Blah!
185 Foob!
186 EOF
187 close (MAIL);
188 #Check the return value
189 is ($? >> 8, 0, "The mail gateway exited normally. yay");
190
191
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");
201
202 # }}}
203
204
205 # {{{  can another random reply to a ticket without being granted privs? answer should be no.
206
207
208 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
209 #ok ($val, "Granted everybody the right to create tickets - $msg");
210
211 $! = 0;
212 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
213 print MAIL <<EOF;
214 From: doesnotexist-2\@$RT::rtname
215 To: rt\@$RT::rtname
216 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
217
218 Blah!  (Should not work.)
219 Foob!
220 EOF
221 close (MAIL);
222 #Check the return value
223 is ($? >> 8, 0, "The mail gateway exited normally. yay");
224
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");
228 # }}}
229
230
231 # {{{  can another random reply to a ticket after being granted privs? answer should be yes
232
233
234 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
235 ok ($val, "Granted everybody the right to reply to  tickets - $msg");
236
237 $! = 0;
238 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
239 print MAIL <<EOF;
240 From: doesnotexist-2\@$RT::rtname
241 To: rt\@$RT::rtname
242 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user
243
244 Blah!
245 Foob!
246 EOF
247 close (MAIL);
248 #Check the return value
249 is ($? >> 8, 0, "The mail gateway exited normally. yay");
250
251
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");
255
256 # }}}
257
258 # {{{  can another random comment on a ticket without being granted privs? answer should be no.
259
260
261 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
262 #ok ($val, "Granted everybody the right to create tickets - $msg");
263
264 $! = 0;
265 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action comment"), "Opened the mailgate - $!");
266 print MAIL <<EOF;
267 From: doesnotexist-3\@$RT::rtname
268 To: rt\@$RT::rtname
269 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
270
271 Blah!  (Should not work.)
272 Foob!
273 EOF
274 close (MAIL);
275
276 #Check the return value
277 is ($? >> 8, 0, "The mail gateway exited normally. yay");
278
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");
282
283 # }}}
284 # {{{  can another random reply to a ticket after being granted privs? answer should be yes
285
286
287 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
288 ok ($val, "Granted everybody the right to reply to  tickets - $msg");
289
290 $! = 0;
291 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action comment"), "Opened the mailgate - $!");
292 print MAIL <<EOF;
293 From: doesnotexist-3\@$RT::rtname
294 To: rt\@$RT::rtname
295 Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user
296
297 Blah!
298 Foob!
299 EOF
300 close (MAIL);
301
302 #Check the return value
303 is ($? >> 8, 0, "The mail gateway exited normally. yay");
304
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");
308
309 # }}}
310
311 # {{{ Testing preservation of binary attachments
312
313 # Get a binary blob (Best Practical logo) 
314
315 # Create a mime entity with an attachment
316
317 use MIME::Entity;
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']);
322
323 # currently in lib/t/autogen
324
325 my $LOGO_FILE = $RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif';
326
327 $entity->attach(Path => $LOGO_FILE,
328                 Type => 'image/gif',
329                 Encoding => 'base64');
330
331 # Create a ticket with a binary attachment
332 $! = 0;
333 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
334
335 $entity->print(\*MAIL);
336
337 close (MAIL);
338
339 #Check the return value
340 is ($? >> 8, 0, "The mail gateway exited normally. yay");
341
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);
349
350 my $file = `cat $LOGO_FILE`;
351 ok ($file, "Read in the logo image");
352
353
354 diag( "for the raw file the content is ". md5_base64($file) );
355
356
357
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;
363 ok($attachment->Id);
364 my $acontent = $attachment->Content;
365
366 diag( "coming from the database, the content is ". md5_base64($acontent) );
367
368 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
369 # Log in as root
370 use Getopt::Long;
371 use LWP::UserAgent;
372
373
374 # Grab the binary attachment via the web ui
375 my $ua      = LWP::UserAgent->new();
376
377 my $full_url = "$url/Ticket/Attachment/".$attachment->TransactionId."/".$attachment->id."/bplogo.gif?&user=root&pass=password";
378 my $r = $ua->get( $full_url);
379
380
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');
383
384
385
386 # }}}
387
388 # {{{ Simple I18N testing
389
390 $! = 0;
391 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
392                                                                          
393 print MAIL <<EOF;
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"
398
399 2 accented lines
400 \303\242\303\252\303\256\303\264\303\273
401 \303\241\303\251\303\255\303\263\303\272
402 bye
403 EOF
404 close (MAIL);
405
406 #Check the return value
407 is ($? >> 8, 0, "The mail gateway exited normally. yay");
408
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);
416
417
418
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.
424
425 $! = 0;
426 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue general --action correspond"), "Opened the mailgate - $!");
427                                                                          
428 print MAIL <<EOF;
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"
433
434 2 accented lines
435 \303\242\303\252\303\256\303\264\303\273
436 \303\241\303\251\303\255\303\263\303\272
437 bye
438 EOF
439 close (MAIL);
440
441 #Check the return value
442 is ($? >> 8, 0, "The mail gateway exited normally. yay");
443
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");
451
452
453
454 $unistring = "\303\241\303\251\303\255\303\263\303\272";
455 Encode::_utf8_on($unistring);
456
457 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
458
459 # }}}
460
461
462 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
463 ok ($val, $msg);
464
465 ##=for later
466
467 SKIP: {
468 skip "Advanced mailgate actions require an unsafe configuration", 47 unless $RT::UnsafeEmailCommands;
469
470 #create new queue to be shure we don't mess with rights
471 use RT::Queue;
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' );
475
476 # {{{ Check take and resolve actions
477
478 # create ticket that is owned by nobody
479 use RT::Ticket;
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' );
484
485 $! = 0;
486 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
487 print MAIL <<EOF;
488 From: root\@localhost
489 Subject: [$RT::rtname \#$id] test
490
491 EOF
492 close (MAIL);
493 is ($? >> 8, 0, "The mail gateway exited normally");
494
495 $tick = RT::Ticket->new($RT::SystemUser);
496 $tick->Load( $id );
497 is( $tick->Id, $id, 'load correct ticket');
498 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
499
500 # check that there is no text transactions writen
501 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
502
503 my $status;
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');
507
508
509
510 $! = 0;
511 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
512 print MAIL <<EOF;
513 From: root\@localhost
514 Subject: [$RT::rtname \#$id] correspondence
515
516 test
517 EOF
518 close (MAIL);
519 is ($? >> 8, 0, "The mail gateway exited normally");
520
521 DBIx::SearchBuilder::Record::Cachable->FlushCache;
522
523 $tick = RT::Ticket->new($RT::SystemUser);
524 $tick->Load( $id );
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' );
533
534 $! = 0;
535 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve --debug"), "Opened the mailgate - $!");
536 print MAIL <<EOF;
537 From: root\@localhost
538 Subject: [$RT::rtname \#$id] test
539
540 EOF
541 close (MAIL);
542 is ($? >> 8, 0, "The mail gateway exited normally");
543
544 DBIx::SearchBuilder::Record::Cachable->FlushCache;
545
546 $tick = RT::Ticket->new($RT::SystemUser);
547 $tick->Load( $id );
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');
551
552 use RT::User;
553 my $user = RT::User->new( $RT::SystemUser );
554 my ($uid) = $user->Create( Name => 'ext-mailgate',
555                            EmailAddress => 'ext-mailgate@localhost',
556                            Privileged => 1,
557                            Password => 'qwe123',
558                          );
559 ok( $uid, 'user created for ext-mailgate tests' );
560 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );
561
562 $tick = RT::Ticket->new($RT::SystemUser);
563 ($id) = $tick->Create( Queue => $qid, Subject => 'test' );
564 ok( $id, 'create new ticket' );
565
566 $! = 0;
567 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
568 print MAIL <<EOF;
569 From: ext-mailgate\@localhost
570 Subject: [example.com \#$id] test
571
572 EOF
573 close (MAIL);
574 is ( $? >> 8, 0, "mailgate exited normally" );
575 DBIx::SearchBuilder::Record::Cachable->FlushCache;
576
577 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
578
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" );
583
584 $! = 0;
585 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
586 print MAIL <<EOF;
587 From: ext-mailgate\@localhost
588 Subject: [example.com \#$id] test
589
590 correspond-take
591 EOF
592 close (MAIL);
593 is ( $? >> 8, 0, "mailgate exited normally" );
594 DBIx::SearchBuilder::Record::Cachable->FlushCache;
595
596 cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
597 is( $tick->Transactions->Count, 3, "one transactions added" );
598
599 $! = 0;
600 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
601 print MAIL <<EOF;
602 From: ext-mailgate\@localhost
603 Subject: [example.com \#$id] test
604
605 correspond-take
606 EOF
607 close (MAIL);
608 is ( $? >> 8, 0, "mailgate exited normally" );
609 DBIx::SearchBuilder::Record::Cachable->FlushCache;
610
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" );
613
614 # revoke ReplyToTicket right
615 use RT::ACE;
616 my $ace = RT::ACE->new($RT::SystemUser);
617 $ace->Load( $ace_id );
618 $ace->Delete;
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 ) {
623         $ace->Delete;
624 }
625
626 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );
627
628
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" );
634
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" );
639
640 $! = 0;
641 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
642 print MAIL <<EOF;
643 From: ext-mailgate\@localhost
644 Subject: [example.com \#$id] test
645
646 take-correspond with reply right granted to owner role
647 EOF
648 close (MAIL);
649 is ( $? >> 8, 0, "mailgate exited normally" );
650 DBIx::SearchBuilder::Record::Cachable->FlushCache;
651
652 $tick->Load( $id );
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" );
656
657
658 # }}}
659 };
660
661
662 1;
663