gateway.t 27.7 KB
Newer Older
Jesse Vincent's avatar
Jesse Vincent committed
1
use strict;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
2
3
use warnings;

4

Kevin Falcone's avatar
Kevin Falcone committed
5
use RT::Test config => 'Set( $UnsafeEmailCommands, 1);', tests => 228, actual_server => 1;
6
my ($baseurl, $m) = RT::Test->started_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
7
8

use RT::Tickets;
9

Ruslan Zakirov's avatar
Ruslan Zakirov committed
10
use MIME::Entity;
11
use Digest::MD5 qw(md5_base64);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
12
use LWP::UserAgent;
13

Ruslan Zakirov's avatar
Ruslan Zakirov committed
14
# TODO: --extension queue
Ruslan Zakirov's avatar
Ruslan Zakirov committed
15

Ruslan Zakirov's avatar
Ruslan Zakirov committed
16
my $url = $m->rt_base_url;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
17

18
diag "Make sure that when we call the mailgate without URL, it fails";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
19
20
21
22
23
24
25
26
{
    my $text = <<EOF;
From: root\@localhost
To: rt\@@{[RT->Config->Get('rtname')]}
Subject: This is a test of new ticket creation

Foob!
EOF
27
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => undef);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
28
29
    is ($status >> 8, 1, "The mail gateway exited with a failure");
    ok (!$id, "No ticket id") or diag "by mistake ticket #$id";
Shawn M Moore's avatar
Shawn M Moore committed
30
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
31
32
}

33
diag "Make sure that when we call the mailgate with wrong URL, it tempfails";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
34
35
{
    my $text = <<EOF;
36
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
37
To: rt\@@{[RT->Config->Get('rtname')]}
38
39
40
41
Subject: This is a test of new ticket creation

Foob!
EOF
42
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, url => 'http://this.test.for.non-connection.is.expected.to.generate.an.error');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
43
44
    is ($status >> 8, 75, "The mail gateway exited with a failure");
    ok (!$id, "No ticket id");
Shawn M Moore's avatar
Shawn M Moore committed
45
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
46
47
48
}

my $everyone_group;
49
diag "revoke rights tests depend on";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
50
{
51
    $everyone_group = RT::Group->new( RT->SystemUser );
Ruslan Zakirov's avatar
Ruslan Zakirov committed
52
53
54
55
56
57
58
59
    $everyone_group->LoadSystemInternalGroup( 'Everyone' );
    ok ($everyone_group->Id, "Found group 'everyone'");

    foreach( qw(CreateTicket ReplyToTicket CommentOnTicket) ) {
        $everyone_group->PrincipalObj->RevokeRight(Right => $_);
    }
}

60
diag "Test new ticket creation by root who is privileged and superuser";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
61
62
{
    my $text = <<EOF;
63
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
64
To: rt\@@{[RT->Config->Get('rtname')]}
65
66
67
68
69
70
Subject: This is a test of new ticket creation

Blah!
Foob!
EOF

71
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
72
73
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "Created ticket");
74

Ruslan Zakirov's avatar
Ruslan Zakirov committed
75
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
76
77
    isa_ok ($tick, 'RT::Ticket');
    is ($tick->Id, $id, "correct ticket id");
CL Sung's avatar
CL Sung committed
78
    is ($tick->Subject , 'This is a test of new ticket creation', "Created the ticket");
Shawn M Moore's avatar
Shawn M Moore committed
79
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
80
}
81

82
diag "Test the 'X-RT-Mail-Extension' field in the header of a ticket";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
83
84
85
86
87
88
89
90
91
{
    my $text = <<EOF;
From: root\@localhost
To: rt\@@{[RT->Config->Get('rtname')]}
Subject: This is a test of the X-RT-Mail-Extension field
Blah!
Foob!
EOF
    local $ENV{'EXTENSION'} = "bad value with\nnewlines\n";
92
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
93
94
95
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "Created ticket #$id");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
96
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
97
98
    isa_ok ($tick, 'RT::Ticket');
    is ($tick->Id, $id, "correct ticket id");
99
    is ($tick->Subject, 'This is a test of the X-RT-Mail-Extension field', "Created the ticket");
Ruslan Zakirov's avatar
Ruslan Zakirov committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

    my $transactions = $tick->Transactions;
    $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
    $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
    my $txn = $transactions->First;
    isa_ok ($txn, 'RT::Transaction');
    is ($txn->Type, 'Create', "correct type");

    my $attachment = $txn->Attachments->First;
    isa_ok ($attachment, 'RT::Attachment');
    # XXX: We eat all newlines in header, that's not what RFC's suggesting
    is (
        $attachment->GetHeader('X-RT-Mail-Extension'),
        "bad value with newlines",
        'header is in place, without trailing newline char'
    );
Shawn M Moore's avatar
Shawn M Moore committed
116
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
117
118
}

119
diag "Make sure that not standard --extension is passed";
120
121
122
123
124
125
126
127
{
    my $text = <<EOF;
From: root\@localhost
To: rt\@@{[RT->Config->Get('rtname')]}
Subject: This is a test of new ticket creation

Foob!
EOF
128
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
129
130
131
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "Created ticket #$id");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
132
    my $tick = RT::Test->last_ticket;
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    isa_ok ($tick, 'RT::Ticket');
    is ($tick->Id, $id, "correct ticket id");

    my $transactions = $tick->Transactions;
    $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
    $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
    my $txn = $transactions->First;
    isa_ok ($txn, 'RT::Transaction');
    is ($txn->Type, 'Create', "correct type");

    my $attachment = $txn->Attachments->First;
    isa_ok ($attachment, 'RT::Attachment');
    is (
        $attachment->GetHeader('X-RT-Mail-Extension'),
        'some-extension-arg',
        'header is in place'
    );
Shawn M Moore's avatar
Shawn M Moore committed
150
    $m->no_warnings_ok;
151
152
}

153
diag "Test new ticket creation without --action argument";
154
155
{
    my $text = <<EOF;
156
157
158
159
160
161
162
From: root\@localhost
To: rt\@$RT::rtname
Subject: using mailgate without --action arg

Blah!
Foob!
EOF
163
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'some-extension-arg' );
164
165
166
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "Created ticket #$id");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
167
    my $tick = RT::Test->last_ticket;
168
169
170
    isa_ok ($tick, 'RT::Ticket');
    is ($tick->Id, $id, "correct ticket id");
    is ($tick->Subject, 'using mailgate without --action arg', "using mailgate without --action arg");
Shawn M Moore's avatar
Shawn M Moore committed
171
    $m->no_warnings_ok;
172
173
}

174
diag "This is a test of new ticket creation as an unknown user";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
175
176
{
    my $text = <<EOF;
Jesse Vincent's avatar
Jesse Vincent committed
177
178
From: doesnotexist\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
179
180
181
182
183
Subject: This is a test of new ticket creation as an unknown user

Blah!
Foob!
EOF
184
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
185
186
187
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok (!$id, "no ticket created");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
188
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
189
190
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ".$tick->Id);
CL Sung's avatar
CL Sung committed
191
    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");
Ruslan Zakirov's avatar
Ruslan Zakirov committed
192

193
    my $u = RT::User->new(RT->SystemUser);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
194
195
    $u->Load("doesnotexist\@@{[RT->Config->Get('rtname')]}");
    ok( !$u->Id, "user does not exist and was not created by failed ticket submission");
Shawn M Moore's avatar
Shawn M Moore committed
196
197

    $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email/);
198
    $m->next_warning_like(qr/RT could not load a valid user/);
Shawn M Moore's avatar
Shawn M Moore committed
199
200
201
202
    TODO: {
        local $TODO = "we're a bit noisy for this warning case";
        $m->no_leftover_warnings_ok;
    }
Ruslan Zakirov's avatar
Ruslan Zakirov committed
203
204
}

205
diag "grant everybody with CreateTicket right";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
206
{
207
208
209
210
211
    ok( RT::Test->set_rights(
        { Principal => $everyone_group->PrincipalObj,
          Right => [qw(CreateTicket)],
        },
    ), "Granted everybody the right to create tickets");
Ruslan Zakirov's avatar
Ruslan Zakirov committed
212
213
214
}

my $ticket_id;
215
diag "now everybody can create tickets. can a random unkown user create tickets?";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
216
217
{
    my $text = <<EOF;
Jesse Vincent's avatar
Jesse Vincent committed
218
219
From: doesnotexist\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
220
221
222
223
224
Subject: This is a test of new ticket creation as an unknown user

Blah!
Foob!
EOF
225
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
226
227
228
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "ticket created");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
229
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
230
231
232
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ".$tick->Id);
    is ($tick->Id, $id, "correct ticket id");
CL Sung's avatar
CL Sung committed
233
    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");
Ruslan Zakirov's avatar
Ruslan Zakirov committed
234

235
    my $u = RT::User->new( RT->SystemUser );
Ruslan Zakirov's avatar
Ruslan Zakirov committed
236
237
238
    $u->Load( "doesnotexist\@@{[RT->Config->Get('rtname')]}" );
    ok ($u->Id, "user does not exist and was created by ticket submission");
    $ticket_id = $id;
Shawn M Moore's avatar
Shawn M Moore committed
239
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
240
241
}

242
diag "can another random reply to a ticket without being granted privs? answer should be no.";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
243
244
{
    my $text = <<EOF;
Jesse Vincent's avatar
Jesse Vincent committed
245
246
From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
Ruslan Zakirov's avatar
Ruslan Zakirov committed
247
Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
248

249
Blah!  (Should not work.)
250
251
Foob!
EOF
252
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
253
254
255
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok (!$id, "no way to reply to the ticket");

256
    my $u = RT::User->new(RT->SystemUser);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
257
258
    $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
    ok( !$u->Id, " user does not exist and was not created by ticket correspondence submission");
Shawn M Moore's avatar
Shawn M Moore committed
259
260
261
262
263
    $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email \(doesnotexist-2\@example\.com\)/);
    TODO: {
        local $TODO = "we're a bit noisy for this warning case";
        $m->no_leftover_warnings_ok;
    }
Ruslan Zakirov's avatar
Ruslan Zakirov committed
264
265
}

266
diag "grant everyone 'ReplyToTicket' right";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
267
{
268
269
270
271
272
    ok( RT::Test->set_rights(
        { Principal => $everyone_group->PrincipalObj,
          Right => [qw(CreateTicket ReplyToTicket)],
        },
    ), "Granted everybody the right to reply to tickets" );
Ruslan Zakirov's avatar
Ruslan Zakirov committed
273
274
}

275
diag "can another random reply to a ticket after being granted privs? answer should be yes";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
276
277
{
    my $text = <<EOF;
Jesse Vincent's avatar
Jesse Vincent committed
278
279
From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
Ruslan Zakirov's avatar
Ruslan Zakirov committed
280
Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a reply as an unknown user
281
282
283
284

Blah!
Foob!
EOF
285
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
286
287
    is ($status >> 8, 0, "The mail gateway exited normally");
    is ($id, $ticket_id, "replied to the ticket");
288

289
    my $u = RT::User->new(RT->SystemUser);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
290
291
    $u->Load('doesnotexist-2@'.RT->Config->Get('rtname'));
    ok ($u->Id, "user exists and was created by ticket correspondence submission");
Shawn M Moore's avatar
Shawn M Moore committed
292
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
293
}
294

295
diag "add a reply to the ticket using '--extension ticket' feature";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
296
297
298
299
300
301
302
303
304
305
{
    my $text = <<EOF;
From: doesnotexist-2\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
Subject: This is a test of a reply as an unknown user

Blah!
Foob!
EOF
    local $ENV{'EXTENSION'} = $ticket_id;
306
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'ticket');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
307
308
309
    is ($status >> 8, 0, "The mail gateway exited normally");
    is ($id, $ticket_id, "replied to the ticket");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
310
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ".$tick->Id);
    is ($tick->Id, $id, "correct ticket id");

    my $transactions = $tick->Transactions;
    $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
    $transactions->Limit( FIELD => 'Type', OPERATOR => '!=', VALUE => 'EmailRecord');
    my $txn = $transactions->First;
    isa_ok ($txn, 'RT::Transaction');
    is ($txn->Type, 'Correspond', "correct type");

    my $attachment = $txn->Attachments->First;
    isa_ok ($attachment, 'RT::Attachment');
    is ($attachment->GetHeader('X-RT-Mail-Extension'), $id, 'header is in place');
Shawn M Moore's avatar
Shawn M Moore committed
325
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
326
}
327

328
diag "can another random comment on a ticket without being granted privs? answer should be no";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
329
330
{
    my $text = <<EOF;
Jesse Vincent's avatar
Jesse Vincent committed
331
332
From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
Ruslan Zakirov's avatar
Ruslan Zakirov committed
333
Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
334

335
Blah!  (Should not work.)
336
337
Foob!
EOF
338
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
339
340
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok (!$id, "no way to comment on the ticket");
341

342
    my $u = RT::User->new(RT->SystemUser);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
343
344
    $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
    ok( !$u->Id, " user does not exist and was not created by ticket comment submission");
Shawn M Moore's avatar
Shawn M Moore committed
345
346
347
348
349
    $m->next_warning_like(qr/RT's configuration does not allow\s+for the creation of a new user for this email \(doesnotexist-3\@example\.com\)/);
    TODO: {
        local $TODO = "we're a bit noisy for this warning case";
        $m->no_leftover_warnings_ok;
    }
Ruslan Zakirov's avatar
Ruslan Zakirov committed
350
}
351
352


353
diag "grant everyone 'CommentOnTicket' right";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
354
{
355
356
357
358
359
    ok( RT::Test->set_rights(
        { Principal => $everyone_group->PrincipalObj,
          Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
        },
    ), "Granted everybody the right to comment on tickets");
Ruslan Zakirov's avatar
Ruslan Zakirov committed
360
}
361

362
diag "can another random reply to a ticket after being granted privs? answer should be yes";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
363
364
{
    my $text = <<EOF;
Jesse Vincent's avatar
Jesse Vincent committed
365
366
From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
Ruslan Zakirov's avatar
Ruslan Zakirov committed
367
Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment as an unknown user
368
369
370
371

Blah!
Foob!
EOF
372
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, action => 'comment');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
373
374
375
    is ($status >> 8, 0, "The mail gateway exited normally");
    is ($id, $ticket_id, "replied to the ticket");

376
    my $u = RT::User->new(RT->SystemUser);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
377
378
    $u->Load('doesnotexist-3@'.RT->Config->Get('rtname'));
    ok ($u->Id, " user exists and was created by ticket comment submission");
Shawn M Moore's avatar
Shawn M Moore committed
379
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
380
381
}

382
diag "add comment to the ticket using '--extension action' feature";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
383
384
385
386
387
388
389
390
391
392
{
    my $text = <<EOF;
From: doesnotexist-3\@@{[RT->Config->Get('rtname')]}
To: rt\@@{[RT->Config->Get('rtname')]}
Subject: [@{[RT->Config->Get('rtname')]} #$ticket_id] This is a test of a comment via '--extension action'

Blah!
Foob!
EOF
    local $ENV{'EXTENSION'} = 'comment';
393
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text, extension => 'action');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
394
395
396
    is ($status >> 8, 0, "The mail gateway exited normally");
    is ($id, $ticket_id, "added comment to the ticket");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
397
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
398
399
400
401
402
403
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ".$tick->Id);
    is ($tick->Id, $id, "correct ticket id");

    my $transactions = $tick->Transactions;
    $transactions->OrderByCols({ FIELD => 'id', ORDER => 'DESC' });
Ruslan Zakirov's avatar
Ruslan Zakirov committed
404
405
406
407
408
409
    $transactions->Limit(
        FIELD => 'Type',
        OPERATOR => 'NOT ENDSWITH',
        VALUE => 'EmailRecord',
        ENTRYAGGREGATOR => 'AND',
    );
Ruslan Zakirov's avatar
Ruslan Zakirov committed
410
411
412
413
414
415
416
    my $txn = $transactions->First;
    isa_ok ($txn, 'RT::Transaction');
    is ($txn->Type, 'Comment', "correct type");

    my $attachment = $txn->Attachments->First;
    isa_ok ($attachment, 'RT::Attachment');
    is ($attachment->GetHeader('X-RT-Mail-Extension'), 'comment', 'header is in place');
Shawn M Moore's avatar
Shawn M Moore committed
417
    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
418
419
}

420
diag "Testing preservation of binary attachments";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
421
422
{
    # Get a binary blob (Best Practical logo) 
423
    my $LOGO_FILE = $RT::StaticPath .'/images/bpslogo.png';
Ruslan Zakirov's avatar
Ruslan Zakirov committed
424
425
426
427
428
429
430
431
432
433
434

    # Create a mime entity with an attachment
    my $entity = MIME::Entity->build(
        From    => 'root@localhost',
        To      => 'rt@localhost',
        Subject => 'binary attachment test',
        Data    => ['This is a test of a binary attachment'],
    );

    $entity->attach(
        Path     => $LOGO_FILE,
435
        Type     => 'image/png',
Ruslan Zakirov's avatar
Ruslan Zakirov committed
436
437
438
        Encoding => 'base64',
    );
    # Create a ticket with a binary attachment
439
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($entity);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
440
441
442
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "created ticket");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
443
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
444
445
446
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ".$tick->Id);
    is ($tick->Id, $id, "correct ticket id");
CL Sung's avatar
CL Sung committed
447
    is ($tick->Subject , 'binary attachment test', "Created the ticket - ".$tick->Id);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
448
449
450

    my $file = `cat $LOGO_FILE`;
    ok ($file, "Read in the logo image");
451
    diag "for the raw file the md5 hex is ". Digest::MD5::md5_hex($file);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
452
453

    # Verify that the binary attachment is valid in the database
454
    my $attachments = RT::Attachments->new(RT->SystemUser);
455
    $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/png');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
456
457
458
459
460
461
462
463
    my $txn_alias = $attachments->Join(
        ALIAS1 => 'main',
        FIELD1 => 'TransactionId',
        TABLE2 => 'Transactions',
        FIELD2 => 'id',
    );
    $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectType', VALUE => 'RT::Ticket' );
    $attachments->Limit( ALIAS => $txn_alias, FIELD => 'ObjectId', VALUE => $id );
464
    is ($attachments->Count, 1, 'Found only one png attached to the ticket');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
465
466
467
468
    my $attachment = $attachments->First;
    ok ($attachment->Id, 'loaded attachment object');
    my $acontent = $attachment->Content;

469
    diag "coming from the database, md5 hex is ".Digest::MD5::md5_hex($acontent);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
470
471
472
473
474
    is ($acontent, $file, 'The attachment isn\'t screwed up in the database.');

    # Grab the binary attachment via the web ui
    my $ua = new LWP::UserAgent;
    my $full_url = "$url/Ticket/Attachment/". $attachment->TransactionId
475
        ."/". $attachment->id. "/bpslogo.png?&user=root&pass=password";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
476
477
478
479
    my $r = $ua->get( $full_url );

    # Verify that the downloaded attachment is the same as what we uploaded.
    is ($file, $r->content, 'The attachment isn\'t screwed up in download');
Shawn M Moore's avatar
Shawn M Moore committed
480
481

    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
482
483
}

484
diag "Simple I18N testing";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
485
486
{
    my $text = <<EOF;
487
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
488
To: rtemail\@@{[RT->Config->Get('rtname')]}
489
490
491
492
493
494
495
496
Subject: This is a test of I18N ticket creation
Content-Type: text/plain; charset="utf-8"

2 accented lines
\303\242\303\252\303\256\303\264\303\273
\303\241\303\251\303\255\303\263\303\272
bye
EOF
497
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
498
499
500
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "created ticket");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
501
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
502
503
504
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ". $tick->Id);
    is ($tick->Id, $id, "correct ticket");
CL Sung's avatar
CL Sung committed
505
    is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket - ". $tick->Subject);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
506

507
    my $unistring = Encode::decode("UTF-8","\303\241\303\251\303\255\303\263\303\272");
Ruslan Zakirov's avatar
Ruslan Zakirov committed
508
509
510
511
512
513
514
515
516
    is (
        $tick->Transactions->First->Content,
        $tick->Transactions->First->Attachments->First->Content,
        "Content is ". $tick->Transactions->First->Attachments->First->Content
    );
    ok (
        $tick->Transactions->First->Content =~ /$unistring/i,
        $tick->Id." appears to be unicode ". $tick->Transactions->First->Attachments->First->Id
    );
Shawn M Moore's avatar
Shawn M Moore committed
517
518

    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
519
520
}

521
diag "supposedly I18N fails on the second message sent in.";
Ruslan Zakirov's avatar
Ruslan Zakirov committed
522
523
{
    my $text = <<EOF;
524
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
525
To: rtemail\@@{[RT->Config->Get('rtname')]}
526
527
528
529
530
531
532
533
Subject: This is a test of I18N ticket creation
Content-Type: text/plain; charset="utf-8"

2 accented lines
\303\242\303\252\303\256\303\264\303\273
\303\241\303\251\303\255\303\263\303\272
bye
EOF
534
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
Ruslan Zakirov's avatar
Ruslan Zakirov committed
535
536
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "created ticket");
537

Ruslan Zakirov's avatar
Ruslan Zakirov committed
538
    my $tick = RT::Test->last_ticket;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
539
540
541
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ". $tick->Id);
    is ($tick->Id, $id, "correct ticket");
CL Sung's avatar
CL Sung committed
542
    is ($tick->Subject , 'This is a test of I18N ticket creation', "Created the ticket");
543

544
    my $unistring = Encode::decode("UTF-8","\303\241\303\251\303\255\303\263\303\272");
545

Ruslan Zakirov's avatar
Ruslan Zakirov committed
546
547
548
549
    ok (
        $tick->Transactions->First->Content =~ $unistring,
        "It appears to be unicode - ". $tick->Transactions->First->Content
    );
Shawn M Moore's avatar
Shawn M Moore committed
550
551

    $m->no_warnings_ok;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
552
}
553

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
diag "make sure we check that UTF-8 is really UTF-8";
{
    my $text = <<EOF;
From: root\@localhost
To: rtemail\@@{[RT->Config->Get('rtname')]}
Subject: This is test wrong utf-8 chars
Content-Type: text/plain; charset="utf-8"

utf-8: informaci\303\263n confidencial
latin1: informaci\363n confidencial

bye
EOF
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "created ticket");

    my $tick = RT::Test->last_ticket;
    is ($tick->Id, $id, "correct ticket");

574
    my $content = Encode::encode("UTF-8",$tick->Transactions->First->Content);
575
576
577
578
579
580
581

    like $content, qr{informaci\303\263n confidencial};
    like $content, qr{informaci\357\277\275n confidencial};

    $m->no_warnings_ok;
}

582
diag "check that mailgate doesn't suffer from empty Reply-To:";
583
584
585
586
587
588
589
590
591
592
{
    my $text = <<EOF;
From: root\@localhost
Reply-To: 
To: rtemail\@@{[RT->Config->Get('rtname')]}
Subject: test
Content-Type: text/plain; charset="utf-8"

test
EOF
593
    my ($status, $id) = RT::Test->send_via_mailgate_and_http($text);
594
595
596
    is ($status >> 8, 0, "The mail gateway exited normally");
    ok ($id, "created ticket");

Ruslan Zakirov's avatar
Ruslan Zakirov committed
597
    my $tick = RT::Test->last_ticket;
598
599
600
601
602
    isa_ok ($tick, 'RT::Ticket');
    ok ($tick->Id, "found ticket ". $tick->Id);
    is ($tick->Id, $id, "correct ticket");

    like $tick->RequestorAddresses, qr/root\@localhost/, 'correct requestor';
Shawn M Moore's avatar
Shawn M Moore committed
603
604

    $m->no_warnings_ok;
605
606
}

607

Ruslan Zakirov's avatar
Ruslan Zakirov committed
608
my ($val,$msg) = $everyone_group->PrincipalObj->RevokeRight(Right => 'CreateTicket');
609
610
ok ($val, $msg);

611
SKIP: {
612
613
skip "Advanced mailgate actions require an unsafe configuration", 47
    unless RT->Config->Get('UnsafeEmailCommands');
Ruslan Zakirov's avatar
Ruslan Zakirov committed
614

615
# create new queue to be shure we don't mess with rights
616
use RT::Queue;
617
my $queue = RT::Queue->new(RT->SystemUser);
618
619
my ($qid) = $queue->Create( Name => 'ext-mailgate');
ok( $qid, 'queue created for ext-mailgate tests' );
620

621

622
623
# create ticket that is owned by nobody
use RT::Ticket;
624
my $tick = RT::Ticket->new(RT->SystemUser);
625
my ($id) = $tick->Create( Queue => 'ext-mailgate', Subject => 'test');
626
ok( $id, 'new ticket created' );
627
is( $tick->Owner, RT->Nobody->Id, 'owner of the new ticket is nobody' );
628

629
$! = 0;
630
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
631
632
print MAIL <<EOF;
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
633
Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
634
635
636
637
638

EOF
close (MAIL);
is ($? >> 8, 0, "The mail gateway exited normally");

639
640
DBIx::SearchBuilder::Record::Cachable->FlushCache;

641
$tick = RT::Ticket->new(RT->SystemUser);
642
643
644
645
$tick->Load( $id );
is( $tick->Id, $id, 'load correct ticket');
is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');

646
647
# check that there is no text transactions writen (create + 2 for take)
is( $tick->Transactions->Count, 3, 'no superfluous transactions');
648

649
my $status;
650
($status, $msg) = $tick->SetOwner( RT->Nobody->Id, 'Force' );
651
ok( $status, 'successfuly changed owner: '. ($msg||'') );
652
is( $tick->Owner, RT->Nobody->Id, 'set owner back to nobody');
653

Shawn M Moore's avatar
Shawn M Moore committed
654
655
$m->no_warnings_ok;

656

657
$! = 0;
658
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $@");
659
660
print MAIL <<EOF;
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
661
Subject: [@{[RT->Config->Get('rtname')]} \#$id] correspondence
662

663
664
665
666
667
test
EOF
close (MAIL);
is ($? >> 8, 0, "The mail gateway exited normally");

668
669
DBIx::SearchBuilder::Record::Cachable->FlushCache;

670
$tick = RT::Ticket->new(RT->SystemUser);
671
$tick->Load( $id );
672
is( $tick->Id, $id, "load correct ticket #$id");
673
674
675
is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email');
my $txns = $tick->Transactions;
$txns->Limit( FIELD => 'Type', VALUE => 'Correspond');
676
$txns->OrderBy( FIELD => 'id', ORDER => 'DESC' );
677
678
# +2 from owner to nobody, +1 because of auto open, +2 from take, +1 from correspond
is( $tick->Transactions->Count, 9, 'no superfluous transactions');
679
is( $txns->First->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' );
680

Shawn M Moore's avatar
Shawn M Moore committed
681
682
683
$m->no_warnings_ok;


684
$! = 0;
685
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action resolve"), "Opened the mailgate - $!");
686
687
print MAIL <<EOF;
From: root\@localhost
Jesse Vincent's avatar
Jesse Vincent committed
688
Subject: [@{[RT->Config->Get('rtname')]} \#$id] test
689
690
691
692
693
694
695

EOF
close (MAIL);
is ($? >> 8, 0, "The mail gateway exited normally");

DBIx::SearchBuilder::Record::Cachable->FlushCache;

696
$tick = RT::Ticket->new(RT->SystemUser);
697
698
699
$tick->Load( $id );
is( $tick->Id, $id, 'load correct ticket');
is( $tick->Status, 'resolved', 'successfuly resolved ticket via email');
700
701
# +1 from resolve
is( $tick->Transactions->Count, 10, 'no superfluous transactions');
702

703
use RT::User;
704
my $user = RT::User->new( RT->SystemUser );
705
my ($uid) = $user->Create( Name => 'ext-mailgate',
706
707
708
709
                           EmailAddress => 'ext-mailgate@localhost',
                           Privileged => 1,
                           Password => 'qwe123',
                         );
710
711
712
ok( $uid, 'user created for ext-mailgate tests' );
ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "User can't own ticket" );

713
$tick = RT::Ticket->new(RT->SystemUser);
714
715
716
($id) = $tick->Create( Queue => $qid, Subject => 'test' );
ok( $id, 'create new ticket' );

717
718
my $rtname = RT->Config->Get('rtname');

Shawn M Moore's avatar
Shawn M Moore committed
719
720
$m->no_warnings_ok;

721
$! = 0;
722
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take"), "Opened the mailgate - $!");
723
724
print MAIL <<EOF;
From: ext-mailgate\@localhost
725
Subject: [$rtname \#$id] test
726
727
728
729
730
731
732
733
734
735
736
737
738

EOF
close (MAIL);
is ( $? >> 8, 0, "mailgate exited normally" );
DBIx::SearchBuilder::Record::Cachable->FlushCache;

cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );

($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'ReplyToTicket' );
ok( $status, "successfuly granted right: $msg" );
my $ace_id = $status;
ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can reply to ticket" );

739
$m->next_warning_like(qr/That user may not own tickets in that queue/);
Shawn M Moore's avatar
Shawn M Moore committed
740
741
742
$m->next_warning_like(qr/Could not record email: Ticket not taken/);
$m->no_leftover_warnings_ok;

743
$! = 0;
744
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action correspond-take"), "Opened the mailgate - $!");
745
746
print MAIL <<EOF;
From: ext-mailgate\@localhost
747
Subject: [$rtname \#$id] test
748
749
750
751
752
753
754
755
756
757

correspond-take
EOF
close (MAIL);
is ( $? >> 8, 0, "mailgate exited normally" );
DBIx::SearchBuilder::Record::Cachable->FlushCache;

cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
is( $tick->Transactions->Count, 3, "one transactions added" );

758
$m->next_warning_like(qr/That user may not own tickets in that queue/);
Shawn M Moore's avatar
Shawn M Moore committed
759
760
761
$m->next_warning_like(qr/Could not record email: Ticket not taken/);
$m->no_leftover_warnings_ok;

762
$! = 0;
763
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
764
765
print MAIL <<EOF;
From: ext-mailgate\@localhost
766
Subject: [$rtname \#$id] test
767
768
769
770
771
772
773
774
775
776

correspond-take
EOF
close (MAIL);
is ( $? >> 8, 0, "mailgate exited normally" );
DBIx::SearchBuilder::Record::Cachable->FlushCache;

cmp_ok( $tick->Owner, '!=', $user->id, "we didn't change owner" );
is( $tick->Transactions->Count, 3, "no transactions added, user can't take ticket first" );

777
$m->next_warning_like(qr/That user may not own tickets in that queue/);
Shawn M Moore's avatar
Shawn M Moore committed
778
779
780
$m->next_warning_like(qr/Could not record email: Ticket not taken/);
$m->no_leftover_warnings_ok;

781
782
# revoke ReplyToTicket right
use RT::ACE;
783
my $ace = RT::ACE->new(RT->SystemUser);
784
785
$ace->Load( $ace_id );
$ace->Delete;
786
my $acl = RT::ACL->new(RT->SystemUser);
787
788
789
$acl->Limit( FIELD => 'RightName', VALUE => 'ReplyToTicket' );
$acl->LimitToObject( $RT::System );
while( my $ace = $acl->Next ) {
790
    $ace->Delete;
791
792
793
794
795
}

ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "User can't reply to ticket any more" );


796
797
my $group = $queue->RoleGroup( 'Owner' );
ok( $group->Id, "load queue owners role group" );
798
$ace = RT::ACE->new( RT->SystemUser );
799
800
801
802
803
804
805
806
($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
ok( $ace_id, "Granted queue owners role group with ReplyToTicket right" );

($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
ok( $status, "successfuly granted right: $msg" );
($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'TakeTicket' );
ok( $status, "successfuly granted right: $msg" );

807
$! = 0;
808
ok(open(MAIL, '|-', "$RT::BinPath/rt-mailgate --url $url --queue ext-mailgate --action take-correspond"), "Opened the mailgate - $!");
809
810
print MAIL <<EOF;
From: ext-mailgate\@localhost
811
Subject: [$rtname \#$id] test
812
813
814
815
816
817
818
819
820
821

take-correspond with reply right granted to owner role
EOF
close (MAIL);
is ( $? >> 8, 0, "mailgate exited normally" );
DBIx::SearchBuilder::Record::Cachable->FlushCache;

$tick->Load( $id );
is( $tick->Owner, $user->id, "we changed owner" );
ok( $user->HasRight( Right => 'ReplyToTicket', Object => $tick ), "owner can reply to ticket" );
822
823
# +2 from take, +1 from correspond
is( $tick->Transactions->Count, 6, "transactions added" );
824

Shawn M Moore's avatar
Shawn M Moore committed
825
$m->no_warnings_ok;
826

827
828
};