This commit was generated by cvs2svn to compensate for changes in r4407,
[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 => 57;
56 use RT;
57 RT::LoadConfig();
58 RT::Init();
59 use RT::I18N;
60 # Make sure that when we call the mailgate wrong, it tempfails
61
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 - $@");
63 print MAIL <<EOF;
64 From: root\@localhost
65 To: rt\@example.com
66 Subject: This is a test of new ticket creation
67
68 Foob!
69 EOF
70 close (MAIL);
71
72 # Check the return value
73 is ( $? >> 8, 75, "The error message above is expected The mail gateway exited with a failure. yay");
74
75
76 # {{{ Test new ticket creation by root who is privileged and superuser
77
78 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
79 print MAIL <<EOF;
80 From: root\@localhost
81 To: rt\@example.com
82 Subject: This is a test of new ticket creation
83
84 Blah!
85 Foob!
86 EOF
87 close (MAIL);
88
89 #Check the return value
90 is ($? >> 8, 0, "The mail gateway exited normally. yay");
91
92 use RT::Tickets;
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");
100
101 # }}}
102
103
104 # {{{This is a test of new ticket creation as an unknown user
105
106 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
107 print MAIL <<EOF;
108 From: doesnotexist\@example.com
109 To: rt\@example.com
110 Subject: This is a test of new ticket creation as an unknown user
111
112 Blah!
113 Foob!
114 EOF
115 close (MAIL);
116 #Check the return value
117 is ($? >> 8, 0, "The mail gateway exited normally. yay");
118
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@example.com');
127 ok( $u->Id == 0, " user does not exist and was not created by failed ticket submission");
128
129
130 # }}}
131
132 # {{{ now everybody can create tickets.  can a random unkown user create tickets?
133
134 my $g = RT::Group->new($RT::SystemUser);
135 $g->LoadSystemInternalGroup('Everyone');
136 ok( $g->Id, "Found 'everybody'");
137
138 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
139 ok ($val, "Granted everybody the right to create tickets - $msg");
140
141
142 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
143 print MAIL <<EOF;
144 From: doesnotexist\@example.com
145 To: rt\@example.com
146 Subject: This is a test of new ticket creation as an unknown user
147
148 Blah!
149 Foob!
150 EOF
151 close (MAIL);
152 #Check the return value
153 is ($? >> 8, 0, "The mail gateway exited normally. yay");
154
155
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@example.com');
164 ok( $u->Id != 0, " user does not exist and was created by ticket submission");
165
166 # }}}
167
168
169 # {{{  can another random reply to a ticket without being granted privs? answer should be no.
170
171
172 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
173 #ok ($val, "Granted everybody the right to create tickets - $msg");
174
175 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
176 print MAIL <<EOF;
177 From: doesnotexist-2\@example.com
178 To: rt\@example.com
179 Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user
180
181 Blah!  (Should not work.)
182 Foob!
183 EOF
184 close (MAIL);
185 #Check the return value
186 is ($? >> 8, 0, "The mail gateway exited normally. yay");
187
188 $u = RT::User->new($RT::SystemUser);
189 $u->Load('doesnotexist-2@example.com');
190 ok( $u->Id == 0, " user does not exist and was not created by ticket correspondence submission");
191 # }}}
192
193
194 # {{{  can another random reply to a ticket after being granted privs? answer should be yes
195
196
197 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
198 ok ($val, "Granted everybody the right to reply to  tickets - $msg");
199
200 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
201 print MAIL <<EOF;
202 From: doesnotexist-2\@example.com
203 To: rt\@example.com
204 Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user
205
206 Blah!
207 Foob!
208 EOF
209 close (MAIL);
210 #Check the return value
211 is ($? >> 8, 0, "The mail gateway exited normally. yay");
212
213
214 $u = RT::User->new($RT::SystemUser);
215 $u->Load('doesnotexist-2@example.com');
216 ok( $u->Id != 0, " user exists and was created by ticket correspondence submission");
217
218 # }}}
219
220 # {{{  can another random comment on a ticket without being granted privs? answer should be no.
221
222
223 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
224 #ok ($val, "Granted everybody the right to create tickets - $msg");
225
226 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action comment"), "Opened the mailgate - $@");
227 print MAIL <<EOF;
228 From: doesnotexist-3\@example.com
229 To: rt\@example.com
230 Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user
231
232 Blah!  (Should not work.)
233 Foob!
234 EOF
235 close (MAIL);
236
237 #Check the return value
238 is ($? >> 8, 0, "The mail gateway exited normally. yay");
239
240 $u = RT::User->new($RT::SystemUser);
241 $u->Load('doesnotexist-3@example.com');
242 ok( $u->Id == 0, " user does not exist and was not created by ticket comment submission");
243
244 # }}}
245 # {{{  can another random reply to a ticket after being granted privs? answer should be yes
246
247
248 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
249 ok ($val, "Granted everybody the right to reply to  tickets - $msg");
250
251 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action comment"), "Opened the mailgate - $@");
252 print MAIL <<EOF;
253 From: doesnotexist-3\@example.com
254 To: rt\@example.com
255 Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user
256
257 Blah!
258 Foob!
259 EOF
260 close (MAIL);
261
262 #Check the return value
263 is ($? >> 8, 0, "The mail gateway exited normally. yay");
264
265 $u = RT::User->new($RT::SystemUser);
266 $u->Load('doesnotexist-3@example.com');
267 ok( $u->Id != 0, " user exists and was created by ticket comment submission");
268
269 # }}}
270
271 # {{{ Testing preservation of binary attachments
272
273 # Get a binary blob (Best Practical logo) 
274
275 # Create a mime entity with an attachment
276
277 use MIME::Entity;
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']);
282
283 # currently in lib/t/autogen
284
285 my $LOGO_FILE = $RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif';
286
287 $entity->attach(Path => $LOGO_FILE,
288                 Type => 'image/gif',
289                 Encoding => 'base64');
290
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 - $@");
293
294 $entity->print(\*MAIL);
295
296 close (MAIL);
297
298 #Check the return value
299 is ($? >> 8, 0, "The mail gateway exited normally. yay");
300
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);
308
309 my $file = `cat $LOGO_FILE`;
310 ok ($file, "Read in the logo image");
311
312
313         use Digest::MD5;
314 warn "for the raw file the content is ".Digest::MD5::md5_base64($file);
315
316
317
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;
323 ok($attachment->Id);
324 my $acontent = $attachment->Content;
325
326         warn "coming from the  database, the content is ".Digest::MD5::md5_base64($acontent);
327
328 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
329 # Log in as root
330 use Getopt::Long;
331 use LWP::UserAgent;
332
333
334 # Grab the binary attachment via the web ui
335 my $ua      = LWP::UserAgent->new();
336
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);
339
340
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');
343
344
345
346 # }}}
347
348 # {{{ Simple I18N testing
349
350 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
351                                                                          
352 print MAIL <<EOF;
353 From: root\@localhost
354 To: rtemail\@example.com
355 Subject: This is a test of I18N ticket creation
356 Content-Type: text/plain; charset="utf-8"
357
358 2 accented lines
359 \303\242\303\252\303\256\303\264\303\273
360 \303\241\303\251\303\255\303\263\303\272
361 bye
362 EOF
363 close (MAIL);
364
365 #Check the return value
366 is ($? >> 8, 0, "The mail gateway exited normally. yay");
367
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);
375
376
377
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.
383
384 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@");
385                                                                          
386 print MAIL <<EOF;
387 From: root\@localhost
388 To: rtemail\@example.com
389 Subject: This is a test of I18N ticket creation
390 Content-Type: text/plain; charset="utf-8"
391
392 2 accented lines
393 \303\242\303\252\303\256\303\264\303\273
394 \303\241\303\251\303\255\303\263\303\272
395 bye
396 EOF
397 close (MAIL);
398
399 #Check the return value
400 is ($? >> 8, 0, "The mail gateway exited normally. yay");
401
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");
409
410
411
412 $unistring = "\303\241\303\251\303\255\303\263\303\272";
413 Encode::_utf8_on($unistring);
414
415 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
416
417 # }}}
418
419
420 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
421 ok ($val, $msg);
422
423 =for later
424
425 TODO: {
426
427 # {{{ Check take and resolve actions
428
429 # create ticket that is owned by nobody
430 use RT::Ticket;
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' );
435
436 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action take"), "Opened the mailgate - $@");
437 print MAIL <<EOF;
438 From: root\@localhost
439 Subject: [example.com \#$id] test
440
441 EOF
442 close (MAIL);
443 is ($? >> 8, 0, "The mail gateway exited normally");
444
445 $tick = RT::Ticket->new($RT::SystemUser);
446 $tick->Load( $id );
447 is( $tick->Id, $id, 'load correct ticket');
448 is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
449
450 # check that there is no text transactions writen
451 is( $tick->Transactions->Count, 2, 'no superfluous transactions');
452
453 my $status = '';
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');
457
458
459
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 - $@");
462 print MAIL <<EOF;
463 From: root\@localhost
464 Subject: [example.com \#$id] correspondence
465
466 test
467 EOF
468 close (MAIL);
469 is ($? >> 8, 0, "The mail gateway exited normally");
470
471 $tick = RT::Ticket->new($RT::SystemUser);
472 $tick->Load( $id );
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, "[example.com \#$id] correspondence", 'successfuly add correspond within take via email' );
478 # +1 because of auto open
479 is( $tick->Transactions->Count, 6, 'no superfluous transactions');
480
481 ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action resolve"), "Opened the mailgate - $@");
482 print MAIL <<EOF;
483 From: root\@localhost
484 Subject: [example.com \#$id] test
485
486 EOF
487 close (MAIL);
488 is ($? >> 8, 0, "The mail gateway exited normally");
489
490 DBIx::SearchBuilder::Record::Cachable->FlushCache;
491
492 $tick = RT::Ticket->new($RT::SystemUser);
493 $tick->Load( $id );
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');
497
498 };
499
500 =cut
501
502 # }}}
503
504 1;