summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/email/reply_parser_spec.rb
blob: e4c68dbba921efe207932245ecece5fd7b1bd09e (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# frozen_string_literal: true

require "spec_helper"

# Inspired in great part by Discourse's Email::Receiver
RSpec.describe Gitlab::Email::ReplyParser do
  describe '#execute' do
    def test_parse_body(mail_string, params = {})
      described_class.new(Mail::Message.new(mail_string), **params).execute
    end

    it "returns an empty string if the message is blank" do
      expect(test_parse_body("")).to eq("")
    end

    it "returns an empty string if the message is not an email" do
      expect(test_parse_body("asdf" * 30)).to eq("")
    end

    it "returns an empty string if there is no reply content" do
      expect(test_parse_body(fixture_file("emails/no_content_reply.eml"))).to eq("")
    end

    context 'when allow_only_quotes is true' do
      it "returns quoted text from email" do
        text = test_parse_body(fixture_file("emails/no_content_reply.eml"), allow_only_quotes: true)

        expect(text).to eq(
          <<-BODY.strip_heredoc.chomp
            >
            >
            >
            > eviltrout posted in 'Adventure Time Sux' on Discourse Meta:
            >
            > ---
            > hey guys everyone knows adventure time sucks!
            >
            > ---
            > Please visit this link to respond: http://localhost:3000/t/adventure-time-sux/1234/3
            >
            > To unsubscribe from these emails, visit your [user preferences](http://localhost:3000/user_preferences).
            >
          BODY
        )
      end
    end

    it "properly renders plaintext-only email" do
      expect(test_parse_body(fixture_file("emails/plaintext_only.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            ### reply from default mail client in Windows 8.1 Metro


            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.


            This is a **bold** word in Markdown


            This is a link http://example.com
          BODY
        )
    end

    it "properly renders html-only email with table and blockquote" do
      expect(test_parse_body(fixture_file("emails/html_table_and_blockquote.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            Company	Contact	Country
            Alfreds Futterkiste	Maria Anders	Germany
            Centro comercial Moctezuma	Francisco Chang	Mexico
            Words can be like X-rays, if you use them properly—they’ll go through anything. You read and you’re pierced.
          BODY
        )
    end

    it "supports a Dutch reply" do
      expect(test_parse_body(fixture_file("emails/dutch.eml"))).to eq("Dit is een antwoord in het Nederlands.")
    end

    it "removes an 'on date wrote' quoting line" do
      expect(test_parse_body(fixture_file("emails/on_wrote.eml"))).to eq("Sure, all you need to do is frobnicate the foobar and you'll be all set!")
    end

    it "handles multiple paragraphs" do
      expect(test_parse_body(fixture_file("emails/paragraphs.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            Is there any reason the *old* candy can't be kept in silos while the new candy
            is imported into *new* silos?

            The thing about candy is it stays delicious for a long time -- we can just keep
            it there without worrying about it too much, imo.

            Thanks for listening.
          BODY
        )
    end

    it "handles multiple paragraphs when parsing html" do
      expect(test_parse_body(fixture_file("emails/html_paragraphs.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            Awesome!

            Pleasure to have you here!

            :boom:
          BODY
        )
    end

    it "handles newlines" do
      expect(test_parse_body(fixture_file("emails/newlines.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            This is my reply.
            It is my best reply.
            It will also be my *only* reply.
          BODY
        )
    end

    it "handles inline reply" do
      expect(test_parse_body(fixture_file("emails/inline_reply.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            >     techAPJ <https://meta.discourse.org/users/techapj>
            > November 28
            >
            > Test reply.
            >
            > First paragraph.
            >
            > Second paragraph.
            >
            > To respond, reply to this email or visit
            > https://meta.discourse.org/t/testing-default-email-replies/22638/3 in
            > your browser.
            >  ------------------------------
            > Previous Replies    codinghorror
            > <https://meta.discourse.org/users/codinghorror>
            > November 28
            >
            > We're testing the latest GitHub email processing library which we are
            > integrating now.
            >
            > https://github.com/github/email_reply_parser
            >
            > Go ahead and reply to this topic and I'll reply from various email clients
            > for testing.
            >   ------------------------------
            >
            > To respond, reply to this email or visit
            > https://meta.discourse.org/t/testing-default-email-replies/22638/3 in
            > your browser.
            >
            > To unsubscribe from these emails, visit your user preferences
            > <https://meta.discourse.org/my/preferences>.
            >

            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
            the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
            fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
            the lazy dog. The quick brown fox jumps over the lazy dog.
          BODY
        )
    end

    it "properly renders email reply from gmail web client" do
      expect(test_parse_body(fixture_file("emails/gmail_web.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            ### This is a reply from standard GMail in Google Chrome.

            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
            the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
            fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
            the lazy dog. The quick brown fox jumps over the lazy dog.

            Here's some **bold** text in Markdown.

            Here's a link http://example.com
          BODY
        )
    end

    context 'properly renders email reply from gmail web client' do
      context 'when feature flag is enabled' do
        it do
          expect(test_parse_body(fixture_file("emails/html_only.eml")))
          .to eq(
            <<-BODY.strip_heredoc.chomp
              ### This is a reply from standard GMail in Google Chrome.

              The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

              Here's some **bold** text, **strong** text and *italic* in Markdown.

              Here's a link http://example.com

              Here's an img ![Miro](http://img.png)<details>
              <summary>
              One</summary>
              Some details</details>

              <details>
              <summary>
              Two</summary>
              Some details</details>

              Test reply.

              First paragraph.

              Second paragraph.
            BODY
          )
        end
      end

      context 'when feature flag is disabled' do
        before do
          stub_feature_flags(service_desk_html_to_text_email_handler: false)
        end

        it do
          expect(test_parse_body(fixture_file("emails/html_only.eml")))
            .to eq(
              <<-BODY.strip_heredoc.chomp
                ### This is a reply from standard GMail in Google Chrome.

                The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

                Here's some **bold** text, strong text and italic in Markdown.

                Here's a link http://example.com

                Here's an img [Miro]One Some details Two Some details

                Test reply.

                First paragraph.

                Second paragraph.
              BODY
            )
        end
      end
    end

    it "properly renders email reply from iOS default mail client" do
      expect(test_parse_body(fixture_file("emails/ios_default.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            ### this is a reply from iOS default mail

            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

            Here's some **bold** markdown text.

            Here's a link http://example.com
          BODY
        )
    end

    it "properly renders email reply from Android 5 gmail client" do
      expect(test_parse_body(fixture_file("emails/android_gmail.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            ### this is a reply from Android 5 gmail

            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
            the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
            fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
            The quick brown fox jumps over the lazy dog.

            This is **bold** in Markdown.

            This is a link to http://example.com
          BODY
        )
    end

    it "properly renders email reply from Windows 8.1 Metro default mail client" do
      expect(test_parse_body(fixture_file("emails/windows_8_metro.eml")))
        .to eq(
          <<-BODY.strip_heredoc.chomp
            ### reply from default mail client in Windows 8.1 Metro


            The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.


            This is a **bold** word in Markdown


            This is a link http://example.com
          BODY
        )
    end

    it "properly renders email reply from MS Outlook client" do
      expect(test_parse_body(fixture_file("emails/outlook.eml"))).to eq("Microsoft Outlook 2010")
    end

    it "properly renders html-only email from MS Outlook" do
      expect(test_parse_body(fixture_file("emails/outlook_html.eml"))).to eq("Microsoft Outlook 2010")
    end

    it "does not wrap links with no href in unnecessary brackets" do
      expect(test_parse_body(fixture_file("emails/html_empty_link.eml"))).to eq("no brackets!")
    end

    it "does not trim reply if trim_reply option is false" do
      expect(test_parse_body(fixture_file("emails/valid_new_issue_with_quote.eml"), { trim_reply: false }))
        .to eq(
          <<-BODY.strip_heredoc.chomp
          The reply by email functionality should be extended to allow creating a new issue by email.
          even when the email is forwarded to the project which may include lines that begin with ">"

          there should be a quote below this line:

          > this is a quote
          BODY
        )
    end

    it "appends trimmed reply when when append_reply option is true" do
      body = <<-BODY.strip_heredoc.chomp
      The reply by email functionality should be extended to allow creating a new issue by email.
      even when the email is forwarded to the project which may include lines that begin with ">"

      there should be a quote below this line:
      BODY

      reply = <<-BODY.strip_heredoc.chomp
      > this is a quote
      BODY

      expect(test_parse_body(fixture_file("emails/valid_new_issue_with_quote.eml"), { append_reply: true }))
        .to contain_exactly(body, reply)
    end

    context 'non-UTF-8 content' do
      let(:charset) { '; charset=Shift_JIS' }
      let(:raw_content) do
        <<-BODY.strip_heredoc.chomp
          From: Jake the Dog <alan@adventuretime.ooo>
          To: incoming+email-test-project_id-issue-@appmail.adventuretime.ooo
          Message-ID: <CAH_Wr+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
          Subject: The message subject! @all
          Content-Type: text/plain#{charset}
          Content-Transfer-Encoding: 8bit

          こんにちは。 この世界は素晴らしいです。
        BODY
      end

      # Strip encoding to simulate the case when Ruby fallback to ASCII-8bit
      # when it meets an unknown encoding
      let(:encoded_content) { raw_content.encode("Shift_JIS").bytes.pack("c*") }

      it "parses body under UTF-8 encoding" do
        expect(test_parse_body(encoded_content))
          .to eq(<<-BODY.strip_heredoc.chomp)
            こんにちは。 この世界は素晴らしいです。
          BODY
      end

      # This test would raise an exception if encoding is not handled properly
      # Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/364329
      context 'charset is absent and reply trimming is disabled' do
        let(:charset) { '' }

        it "parses body under UTF-8 encoding" do
          expect(test_parse_body(encoded_content, { trim_reply: false }))
            .to eq(<<-BODY.strip_heredoc.chomp)
              こんにちは。 この世界は素晴らしいです。
            BODY
        end
      end

      context 'multipart email' do
        let(:raw_content) do
          <<-BODY.strip_heredoc.chomp
            From: Jake the Dog <alan@adventuretime.ooo>
            To: incoming+email-test-project_id-issue-@appmail.adventuretime.ooo
            Message-ID: <CAH_Wr+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
            Subject: The message subject! @all
            Content-Type: multipart/alternative;
              boundary=Apple-Mail-B41C7F8E-3639-49B0-A5D5-440E125A7105
            Content-Transfer-Encoding: 7bbit

            --Apple-Mail-B41C7F8E-3639-49B0-A5D5-440E125A7105
            Content-Type: text/plain
            Content-Transfer-Encodng: 7bit

            こんにちは。 この世界は素晴らしいです。
          BODY
        end

        it "parses body under UTF-8 encoding" do
          expect(test_parse_body(encoded_content, { trim_reply: false }))
            .to eq(<<-BODY.strip_heredoc.chomp)
              こんにちは。 この世界は素晴らしいです。
            BODY
        end
      end
    end
  end
end