BigW Consortium Gitlab

reply_parser_spec.rb 7.99 KB
Newer Older
1 2 3
require "spec_helper"

# Inspired in great part by Discourse's Email::Receiver
Douwe Maan committed
4
describe Gitlab::Email::ReplyParser, lib: true do
5
  describe '#execute' do
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    def test_parse_body(mail_string)
      described_class.new(Mail::Message.new(mail_string)).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

Douwe Maan committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    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
        )
38 39 40 41 42 43 44 45 46 47 48 49
    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"))).
Douwe Maan committed
50 51 52 53
        to eq(
          <<-BODY.strip_heredoc.chomp
            Is there any reason the *old* candy can't be be kept in silos while the new candy
            is imported into *new* silos?
54

Douwe Maan committed
55 56
            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.
57

Douwe Maan committed
58 59 60
            Thanks for listening.
          BODY
        )
61 62 63 64
    end

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

Douwe Maan committed
69
            Pleasure to have you here!
70

Douwe Maan committed
71 72 73
            :boom:
          BODY
        )
74 75 76 77
    end

    it "handles newlines" do
      expect(test_parse_body(fixture_file("emails/newlines.eml"))).
Douwe Maan committed
78 79 80 81 82 83 84
        to eq(
          <<-BODY.strip_heredoc.chomp
            This is my reply.
            It is my best reply.
            It will also be my *only* reply.
          BODY
        )
85 86 87 88
    end

    it "handles inline reply" do
      expect(test_parse_body(fixture_file("emails/inline_reply.eml"))).
Douwe Maan committed
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
        to eq(
          <<-BODY.strip_heredoc.chomp
            On Wed, Oct 8, 2014 at 11:12 AM, techAPJ <info@unconfigured.discourse.org> wrote:

            >     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
        )
134 135 136 137
    end

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

Douwe Maan committed
142 143 144 145 146
            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.
147

Douwe Maan committed
148
            Here's some **bold** text in Markdown.
149

Douwe Maan committed
150 151 152
            Here's a link http://example.com
          BODY
        )
153 154 155 156
    end

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

Douwe Maan committed
161
            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.
162

Douwe Maan committed
163
            Here's some **bold** markdown text.
164

Douwe Maan committed
165 166 167
            Here's a link http://example.com
          BODY
        )
168 169 170 171
    end

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

Douwe Maan committed
176 177 178 179
            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.
180

Douwe Maan committed
181
            This is **bold** in Markdown.
182

Douwe Maan committed
183 184 185
            This is a link to http://example.com
          BODY
        )
186 187 188 189
    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"))).
Douwe Maan committed
190 191 192
        to eq(
          <<-BODY.strip_heredoc.chomp
            ### reply from default mail client in Windows 8.1 Metro
193 194


Douwe Maan committed
195
            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.
196 197


Douwe Maan committed
198
            This is a **bold** word in Markdown
199 200


Douwe Maan committed
201 202 203
            This is a link http://example.com
          BODY
        )
204 205 206 207 208 209 210
    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
  end
end