summaryrefslogtreecommitdiff
path: root/spec/initializers/mail_encoding_patch_spec.rb
blob: 41074af3503e1093efaf2e39e17171b902adeab5 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# frozen_string_literal: true

require 'fast_spec_helper'

require 'mail'
require_relative '../../config/initializers/mail_encoding_patch.rb'

describe 'Mail quoted-printable transfer encoding patch and Unicode characters' do
  shared_examples 'email encoding' do |email|
    it 'enclosing in a new object does not change the encoded original' do
      new_email = Mail.new(email)

      expect(new_email.subject).to eq(email.subject)
      expect(new_email.from).to eq(email.from)
      expect(new_email.to).to eq(email.to)
      expect(new_email.content_type).to eq(email.content_type)
      expect(new_email.content_transfer_encoding).to eq(email.content_transfer_encoding)

      expect(new_email.encoded).to eq(email.encoded)
    end
  end

  context 'with a text email' do
    context 'with a body that encodes to exactly 74 characters (final newline)' do
      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
        content_type 'text/plain; charset=UTF-8'
        content_transfer_encoding 'quoted-printable'
        body "-123456789-123456789-123456789-123456789-123456789-123456789-123456789-1\n"
      end

      it_behaves_like 'email encoding', email
    end

    context 'with a body that encodes to exactly 74 characters (no final newline)' do
      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
        content_type 'text/plain; charset=UTF-8'
        content_transfer_encoding 'quoted-printable'
        body "-123456789-123456789-123456789-123456789-123456789-123456789-123456789-12"
      end

      it_behaves_like 'email encoding', email
    end

    context 'with a body that encodes to exactly 75 characters' do
      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
        content_type 'text/plain; charset=UTF-8'
        content_transfer_encoding 'quoted-printable'
        body "-123456789-123456789-123456789-123456789-123456789-123456789-123456789-12\n"
      end

      it_behaves_like 'email encoding', email
    end
  end

  context 'with an html email' do
    context 'with a body that encodes to exactly 74 characters (final newline)' do
      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
        content_type 'text/html; charset=UTF-8'
        content_transfer_encoding 'quoted-printable'
        body "<p>-123456789-123456789-123456789-123456789-123456789-123456789-1234</p>\n"
      end

      it_behaves_like 'email encoding', email
    end

    context 'with a body that encodes to exactly 74 characters (no final newline)' do
      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
        content_type 'text/html; charset=UTF-8'
        content_transfer_encoding 'quoted-printable'
        body "<p>-123456789-123456789-123456789-123456789-123456789-123456789-12345</p>"
      end

      it_behaves_like 'email encoding', email
    end

    context 'with a body that encodes to exactly 75 characters' do
      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
        content_type 'text/html; charset=UTF-8'
        content_transfer_encoding 'quoted-printable'
        body "<p>-123456789-123456789-123456789-123456789-123456789-123456789-12345</p>\n"
      end

      it_behaves_like 'email encoding', email
    end
  end

  context 'a multipart email' do
    email = Mail.new do
      to 'jane.doe@example.com'
      from 'John Dóe <john.doe@example.com>'
      subject 'Encoding tést'
    end

    text_part = Mail::Part.new do
      content_type 'text/plain; charset=UTF-8'
      content_transfer_encoding 'quoted-printable'
      body "\r\n\r\n@john.doe, now known as John Dóe has accepted your invitation to join the Administrator / htmltest project.\r\n\r\nhttp://169.254.169.254:3000/root/htmltest\r\n\r\n-- \r\nYou're receiving this email because of your account on 169.254.169.254.\r\n\r\n\r\n\r\n"
    end

    html_part = Mail::Part.new do
      content_type 'text/html; charset=UTF-8'
      content_transfer_encoding 'quoted-printable'
      body "\r\n\r\n@john.doe, now known as John Dóe has accepted your invitation to join the Administrator / htmltest project.\r\n\r\nhttp://169.254.169.254:3000/root/htmltest\r\n\r\n-- \r\nYou're receiving this email because of your account on 169.254.169.254.\r\n\r\n\r\n\r\n"
    end

    email.text_part = text_part
    email.html_part = html_part

    it_behaves_like 'email encoding', email
  end

  context 'with non UTF-8 charset' do
    email = Mail.new do
      to 'jane.doe@example.com'
      from 'John Dóe <john.doe@example.com>'
      subject 'Encoding tést'
      content_type 'text/plain; charset=windows-1251'
      content_transfer_encoding 'quoted-printable'
      body "This line is very long and will be put in multiple quoted-printable lines. Some Russian character: Д\n\n\n".encode('windows-1251')
    end

    it_behaves_like 'email encoding', email

    it 'can be decoded back' do
      expect(Mail.new(email).body.decoded.dup.force_encoding('windows-1251').encode('utf-8')).to include('Some Russian character: Д')
    end
  end

  context 'with binary content' do
    context 'can be encoded with \'base64\' content-transfer-encoding' do
      image = File.binread('spec/fixtures/rails_sample.jpg')

      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
      end

      part = Mail::Part.new
      part.body = [image].pack('m')
      part.content_type = 'image/jpg'
      part.content_transfer_encoding = 'base64'

      email.parts << part

      it_behaves_like 'email encoding', email

      it 'binary contents are not modified' do
        expect(email.parts.first.decoded).to eq(image)

        # Enclosing in a new Mail object does not corrupt encoded data
        expect(Mail.new(email).parts.first.decoded).to eq(image)
      end
    end

    context 'encoding fails with \'quoted-printable\' content-transfer-encoding' do
      image = File.binread('spec/fixtures/rails_sample.jpg')

      email = Mail.new do
        to 'jane.doe@example.com'
        from 'John Dóe <john.doe@example.com>'
        subject 'Encoding tést'
      end

      part = Mail::Part.new
      part.body = [image].pack('M*')
      part.content_type = 'image/jpg'
      part.content_transfer_encoding = 'quoted-printable'

      email.parts << part

      # The Mail patch in `config/initializers/mail_encoding_patch.rb` fixes
      # encoding of non-binary content. The failure below is expected since we
      # reverted some upstream changes in order to properly support SMIME signatures
      # See https://gitlab.com/gitlab-org/gitlab/issues/197386
      it 'content cannot be decoded back' do
        # Headers are ok
        expect(email.subject).to eq(email.subject)
        expect(email.from).to eq(email.from)
        expect(email.to).to eq(email.to)
        expect(email.content_type).to eq(email.content_type)
        expect(email.content_transfer_encoding).to eq(email.content_transfer_encoding)

        # Content cannot be recovered
        expect(email.parts.first.decoded).not_to eq(image)
      end
    end
  end
end