debian-mirror-gitlab/spec/lib/gitlab/email/reply_parser_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

273 lines
10 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
require "spec_helper"
# Inspired in great part by Discourse's Email::Receiver
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Email::ReplyParser do
2015-09-25 12:07:36 +05:30
describe '#execute' do
2018-11-08 19:23:39 +05:30
def test_parse_body(mail_string, params = {})
2021-02-22 17:27:13 +05:30
described_class.new(Mail::Message.new(mail_string), **params).execute
2015-09-25 12:07:36 +05:30
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
2021-12-11 22:18:48 +05:30
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
2015-09-25 12:07:36 +05:30
it "properly renders plaintext-only email" do
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/plaintext_only.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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 "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
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/paragraphs.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-BODY.strip_heredoc.chomp
2019-02-15 15:39:39 +05:30
Is there any reason the *old* candy can't be kept in silos while the new candy
2015-09-25 12:07:36 +05:30
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
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/html_paragraphs.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-BODY.strip_heredoc.chomp
Awesome!
Pleasure to have you here!
:boom:
BODY
)
end
it "handles newlines" do
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/newlines.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/inline_reply.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/gmail_web.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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
it "properly renders email reply from iOS default mail client" do
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/ios_default.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/android_gmail.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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
2017-09-10 17:25:29 +05:30
expect(test_parse_body(fixture_file("emails/windows_8_metro.eml")))
.to eq(
2015-09-25 12:07:36 +05:30
<<-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
2017-08-17 22:00:37 +05:30
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
2017-09-10 17:25:29 +05:30
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
2018-11-08 19:23:39 +05:30
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
2021-10-27 15:23:28 +05:30
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
2015-09-25 12:07:36 +05:30
end
end