Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rfc822-attachment.t « mail « t - github.com/bestpractical/rt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f66584f7370495e6b421cc2df9706435acff90ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use strict;
use warnings;

use RT::Test tests => undef;

use MIME::Entity;

diag "simple rfc822 attachment";
{

    my $top = MIME::Entity->build(
        From    => 'root@localhost',
        To      => 'rt@localhost',
        Subject => 'this is top',
        Data    => ['top mail'],
    );

    my $rfc822 = MIME::Entity->build(
        From    => 'foo@localhost',
        To      => 'bar@localhost',
        Subject => 'rfc822',
        Data    => ['rfc822 attachment 测试'],
        Charset => 'UTF-8',
        'X-Brokenness' => 'high',
    );

    $top->attach(
        Data => $rfc822->stringify,
        Type => 'message/rfc822',
        Charset => 'UTF-8',
    );

    for my $mime ( $top, contents_as_mime($top) ) {
        diag "testing mail";
        is $mime->parts, 2, 'two mime parts';

        eval { $mime->as_string };
        ok( !$@, 'stringifying mime does not die' );

        like $mime->head->get('Subject'), qr/this is top/, 'top subject';
        like $mime->head->get('From'), qr/root\@localhost/, 'top From';
        like $mime->parts(0)->bodyhandle->as_string, qr/top mail/, 'content of top';
        
        my $attach = $mime->parts(1);
        my $body   = $attach->bodyhandle->as_string;

        like $attach->head->mime_type, qr/message\/rfc822/, 'attach of type message/rfc822';
        like $body, qr/rfc822 attachment 测试/, 'attach content';

        headers_like(
            $attach,
            Subject         => 'rfc822',
            From            => 'foo@localhost',
            'X-Brokenness'  => 'high',
        );
    }
}

diag "multipart rfc822 attachment";
{

    my $top = MIME::Entity->build(
        From    => 'root@localhost',
        To      => 'rt@localhost',
        Subject => 'this is top',
        Data    => ['top mail'],
    );

    my $rfc822 = MIME::Entity->build(
        From    => 'foo@localhost',
        To      => 'bar@localhost',
        Subject => 'rfc822',
        Data    => ['rfc822 attachment 测试附件'],
        Charset => 'UTF-8',
        'X-Brokenness' => 'high',
    );

    $rfc822->attach(
        Data => ['<b>attachment of rfc822 attachment 测试</b>'],
        Type => 'text/html',
        Charset  => 'UTF-8',
    );

    $top->attach(
        Data => $rfc822->stringify,
        Type => 'message/rfc822',
        Charset => 'UTF-8',
    );

    for my $mime ( $top, contents_as_mime($top) ) {
        diag "testing mail";
        is $mime->parts, 2, 'two mime parts';
        eval { $mime->as_string };
        ok( !$@, 'stringifying mime does not die' );

        like $mime->head->get('Subject'), qr/this is top/, 'top subject';
        like $mime->head->get('From'), qr/root\@localhost/, 'top From';
        like $mime->parts(0)->bodyhandle->as_string, qr/top mail/, 'content of top';
        
        my $attach = $mime->parts(1);
        my $body   = $attach->bodyhandle->as_string;

        like $attach->head->mime_type, qr/message\/rfc822/, 'attach of type message/rfc822';
        like $body, qr/rfc822 attachment 测试附件/, 'attach content';

        like $body, qr/attachment of rfc822 attachment 测试/, 'attach content';

        headers_like(
            $attach,
            Subject         => 'rfc822',
            From            => 'foo@localhost',
            'X-Brokenness'  => 'high',
            'Content-Type'  => 'text/plain',
            'Content-type'  => 'text/html',
        );
    }
}

sub content_as_mime {
    my $entity = shift;
    my ( $status, $id ) = RT::Test->send_via_mailgate($entity);
    is( $status >> 8, 0, "The mail gateway exited normally" );
    ok( $id, "created ticket" );
    # We can't simply use Txn->ContentAsMIME since that is wrapped in a
    # message/rfc822 entity
    return RT::Test->last_ticket->Transactions->First->Attachments->First->ContentAsMIME(Children => 1);
}

sub contents_as_mime {
    my $entity = shift;
    RT->Config->Set( 'TreatAttachedEmailAsFiles', 1 );
    my @contents = content_as_mime($entity);
    RT->Config->Set( 'TreatAttachedEmailAsFiles', 0 );
    push @contents, content_as_mime($entity);
    return @contents;
}

sub headers_like {
    my $attach = shift;
    my %header = (@_);
    my $body   = $attach->bodyhandle->as_string;
    for my $name (keys %header) {
        if (lc $name eq 'content-type') {
            like $attach->head->get($name), qr/message\/rfc822/, "attach $name message/rfc822, not from a subpart";
        } else {
            is $attach->head->get($name), undef, "attach $name not in part header";
        }
        like $body, qr/$name: $header{$name}/i, "attach $name in part body";
    }
}

done_testing;