Skip to content

A platform for community discussion. Free, open, simple.

FIX: email to a category should not require enabling reply-by-email (#37227)

Michael Browna2b22d182e39
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index 80f124e9dfe..d0c9d7be120 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -210,6 +210,22 @@ module Email
return
end
+ if post = find_related_post(force: true)
+ # the email is a reply to *something*
+ if category = post.topic&.category
+ # it's a topic in a category
+ if category.mailinglist_mirror?
+ # replies to categories that are mailing mirrors should work,
+ # even if reply_by_email is not otherwise enabled
+ elsif !SiteSetting.reply_by_email_enabled
+ # an edge case where reply by email is disabled but the user has replied to the category email
+ # without this, the category email would be looked up and found and then the new post associated
+ # with the existing topic, bypassing the reply_by_email_enabled setting
+ raise ReplyNotAllowedError
+ end
+ end
+ end
+
if post = find_related_post
# Most of the time, it is impossible to **reply** without a reply key, so exit early
if user.blank?
@@ -844,7 +860,7 @@ module Email
return group if group
category = Category.find_by_email(address)
- return category if category && SiteSetting.reply_by_email_enabled?
+ return category if category
end
# reply
diff --git a/spec/fixtures/emails/exchange_html_body.eml b/spec/fixtures/emails/exchange_html_body.eml
index 0177c632592..c855ca73dd4 100644
--- a/spec/fixtures/emails/exchange_html_body.eml
+++ b/spec/fixtures/emails/exchange_html_body.eml
@@ -1,6 +1,6 @@
Return-Path: <[email protected]>
From: Foo Bar <[email protected]>
Date: Fri, 15 Jan 2017 00:12:43 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
diff --git a/spec/fixtures/emails/exchange_html_body_and_reply.eml b/spec/fixtures/emails/exchange_html_body_and_reply.eml
index 00a423a7f92..f504b9913b0 100644
--- a/spec/fixtures/emails/exchange_html_body_and_reply.eml
+++ b/spec/fixtures/emails/exchange_html_body_and_reply.eml
@@ -1,6 +1,6 @@
Return-Path: <[email protected]>
From: Foo Bar <[email protected]>
Date: Fri, 15 Jan 2017 00:12:43 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
diff --git a/spec/fixtures/emails/exchange_html_reply.eml b/spec/fixtures/emails/exchange_html_reply.eml
index 1cb1751834e..5457bbcc563 100644
--- a/spec/fixtures/emails/exchange_html_reply.eml
+++ b/spec/fixtures/emails/exchange_html_reply.eml
@@ -1,6 +1,6 @@
Return-Path: <[email protected]>
From: Foo Bar <[email protected]>
Date: Fri, 15 Jan 2017 00:12:43 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
diff --git a/spec/fixtures/emails/gmail_html_reply.eml b/spec/fixtures/emails/gmail_html_reply.eml
index d03205ae591..5e4e4f50dc0 100644
--- a/spec/fixtures/emails/gmail_html_reply.eml
+++ b/spec/fixtures/emails/gmail_html_reply.eml
@@ -1,6 +1,6 @@
Return-Path: <[email protected]>
From: Foo Bar <[email protected]>
Date: Fri, 15 Jan 2016 00:12:43 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
diff --git a/spec/fixtures/emails/html_reply.eml b/spec/fixtures/emails/html_reply.eml
index 669d46c83a0..20873e9320a 100644
--- a/spec/fixtures/emails/html_reply.eml
+++ b/spec/fixtures/emails/html_reply.eml
@@ -1,6 +1,6 @@
Return-Path: <[email protected]>
From: Foo Bar <[email protected]>
Date: Fri, 15 Jan 2016 00:12:43 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
diff --git a/spec/fixtures/emails/reply_to_category_address.eml b/spec/fixtures/emails/reply_to_category_address.eml
new file mode 100644
index 00000000000..31a11cdd78b
--- /dev/null
+++ b/spec/fixtures/emails/reply_to_category_address.eml
@@ -0,0 +1,8 @@
+Subject: SUBJECT
+In-Reply-To: REFERENCE
+References: REFERENCE
+Date: DATE
+
+That is an excellent idea, I shall implement it immediately.
diff --git a/spec/fixtures/emails/reply_user_matching_alternativereplyaddr.eml b/spec/fixtures/emails/reply_user_matching_alternativereplyaddr.eml
new file mode 100644
index 00000000000..9ac75bdd5c3
--- /dev/null
+++ b/spec/fixtures/emails/reply_user_matching_alternativereplyaddr.eml
@@ -0,0 +1,10 @@
+Return-Path: <[email protected]>
+From: Foo Bar <[email protected]>
+Date: Fri, 15 Jan 2016 00:12:43 +0100
+Message-ID: <[email protected]>
+Mime-Version: 1.0
+Content-Type: text/plain
+Content-Transfer-Encoding: 7bit
+
+Lorem ipsum dolor sit amet, consectetur adipiscing elit.
diff --git a/spec/lib/email/processor_spec.rb b/spec/lib/email/processor_spec.rb
index 6b85d1508d2..8021ab8ffa0 100644
--- a/spec/lib/email/processor_spec.rb
+++ b/spec/lib/email/processor_spec.rb
@@ -3,48 +3,113 @@
require "email/processor"
RSpec.describe Email::Processor do
+ def configure_reply_by_email
+ SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
+ SiteSetting.manual_polling_enabled = true
+ SiteSetting.reply_by_email_enabled = true
+ end
+
after { Discourse.redis.flushdb }
let(:from) { "[email protected]" }
- context "when reply via email is too short" do
- let(:mail) { file_from_fixtures("chinese_reply.eml", "emails").read }
- fab!(:post)
- fab!(:user) { Fabricate(:user, email: "[email protected]", refresh_auto_groups: true) }
-
- fab!(:post_reply_key) do
- Fabricate(
- :post_reply_key,
- user: user,
- post: post,
- reply_key: "4f97315cc828096c9cb34c6f1a0d6fe8",
- )
+ context "with reply_by_email configured" do
+ before { configure_reply_by_email }
+
+ context "when reply via email is too short" do
+ let(:mail) { file_from_fixtures("chinese_reply.eml", "emails").read }
+ fab!(:post)
+ fab!(:user) { Fabricate(:user, email: "[email protected]", refresh_auto_groups: true) }
+
+ fab!(:post_reply_key) do
+ Fabricate(
+ :post_reply_key,
+ user: user,
+ post: post,
+ reply_key: "4f97315cc828096c9cb34c6f1a0d6fe8",
+ )
+ end
+
+ before { SiteSetting.min_post_length = 1000 }
+
+ it "rejects reply and sends an email with custom error message" do
+ processor = Email::Processor.new(mail)
+ processor.process!
+
+ rejection_raw = ActionMailer::Base.deliveries.first.body.raw_source
+
+ count = SiteSetting.min_post_length
+ destination = processor.receiver.mail.to
+ former_title = processor.receiver.mail.subject
+
+ expect(rejection_raw.gsub(/\r/, "")).to eq(
+ I18n.t(
+ "system_messages.email_reject_post_too_short.text_body_template",
+ count: count,
+ destination: destination,
+ former_title: former_title,
+ ).gsub(/\r/, ""),
+ )
+ end
end
+ describe "from reply to email address" do
+ let(:mail) do
+ "Date: Fri, 15 Jan 2016 00:12:43 +0100\nFrom: [email protected]\nTo: [email protected]\nSubject: FOO BAR\n\nFoo foo bar bar?"
+ end
+
+ it "ignores the email" do
+ Email::Receiver
+ .any_instance
+ .stubs(:process_internal)
+ .raises(Email::Receiver::FromReplyByAddressError.new)
- before do
- SiteSetting.email_in = true
- SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
- SiteSetting.min_post_length = 1000
+ expect { Email::Processor.process!(mail) }.not_to change { EmailLog.count }
+ end
end
- it "rejects reply and sends an email with custom error message" do
- processor = Email::Processor.new(mail)
- processor.process!
+ describe "mailinglist mirror" do
+ before { Fabricate(:mailinglist_mirror_category) }
- rejection_raw = ActionMailer::Base.deliveries.first.body.raw_source
+ it "does not send rejection email" do
+ SiteSetting.email_in = true
- count = SiteSetting.min_post_length
- destination = processor.receiver.mail.to
- former_title = processor.receiver.mail.subject
+ Email::Receiver.any_instance.stubs(:process_internal).raises("boom")
- expect(rejection_raw.gsub(/\r/, "")).to eq(
- I18n.t(
- "system_messages.email_reject_post_too_short.text_body_template",
- count: count,
- destination: destination,
- former_title: former_title,
- ).gsub(/\r/, ""),
- )
+ email = <<~EMAIL
+ Subject: Hello world
+ EMAIL
+
+ expect { Email::Processor.process!(email) }.to_not change { EmailLog.count }
+ end
+ end
+
+ describe "when replying to a post that is too old" do
+ fab!(:user) { Fabricate(:user, email: "[email protected]") }
+ fab!(:topic)
+ fab!(:post) { Fabricate(:post, topic: topic, created_at: 3.days.ago) }
+ let(:mail) do
+ file_from_fixtures("old_destination.eml", "emails").read.gsub(":post_id", post.id.to_s)
+ end
+
+ it "rejects the email with the right response" do
+ SiteSetting.disallow_reply_by_email_after_days = 2
+ processor = Email::Processor.new(mail)
+ processor.process!
+
+ rejection_raw = ActionMailer::Base.deliveries.first.body.to_s
+
+ expect(rejection_raw).to eq(
+ I18n.t(
+ "system_messages.email_reject_old_destination.text_body_template",
+ destination: '["[email protected]"]',
+ former_title: "Some Old Post",
+ short_url: "#{Discourse.base_url}/p/#{post.id}",
+ number_of_days: 2,
+ ),
+ )
+ end
end
end
@@ -139,76 +204,6 @@ RSpec.describe Email::Processor do
end
end
- describe "from reply to email address" do
- before do
- SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
- SiteSetting.manual_polling_enabled = true
- SiteSetting.reply_by_email_enabled = true
- end
-
- let(:mail) do
- "Date: Fri, 15 Jan 2016 00:12:43 +0100\nFrom: [email protected]\nTo: [email protected]\nSubject: FOO BAR\n\nFoo foo bar bar?"
- end
-
- it "ignores the email" do
- Email::Receiver
- .any_instance
- .stubs(:process_internal)
- .raises(Email::Receiver::FromReplyByAddressError.new)
-
- expect { Email::Processor.process!(mail) }.not_to change { EmailLog.count }
- end
- end
-
- describe "mailinglist mirror" do
- before do
- SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
- SiteSetting.manual_polling_enabled = true
- SiteSetting.reply_by_email_enabled = true
- SiteSetting.email_in = true
- Fabricate(:mailinglist_mirror_category)
- end
-
- it "does not send rejection email" do
- Email::Receiver.any_instance.stubs(:process_internal).raises("boom")
-
- email = <<~EMAIL
- Subject: Hello world
- EMAIL
-
- expect { Email::Processor.process!(email) }.to_not change { EmailLog.count }
- end
- end
-
- describe "when replying to a post that is too old" do
- fab!(:user) { Fabricate(:user, email: "[email protected]") }
- fab!(:topic)
- fab!(:post) { Fabricate(:post, topic: topic, created_at: 3.days.ago) }
- let(:mail) do
- file_from_fixtures("old_destination.eml", "emails").read.gsub(":post_id", post.id.to_s)
- end
-
- it "rejects the email with the right response" do
- SiteSetting.disallow_reply_by_email_after_days = 2
- processor = Email::Processor.new(mail)
- processor.process!
-
- rejection_raw = ActionMailer::Base.deliveries.first.body.to_s
-
- expect(rejection_raw).to eq(
- I18n.t(
- "system_messages.email_reject_old_destination.text_body_template",
- destination: '["[email protected]"]',
- former_title: "Some Old Post",
- short_url: "#{Discourse.base_url}/p/#{post.id}",
- number_of_days: 2,
- ),
- )
- end
- end
-
describe "when group email recipients exceeds maximum_recipients_per_new_group_email site setting" do
let(:mail) { file_from_fixtures("cc.eml", "emails").read }
diff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rb
index 5948bb6a5c1..5a787d4f6c3 100644
--- a/spec/lib/email/receiver_spec.rb
+++ b/spec/lib/email/receiver_spec.rb
@@ -3,10 +3,10 @@
require "email/receiver"
RSpec.describe Email::Receiver do
- before do
- SiteSetting.email_in = true
+ before { SiteSetting.email_in = true }
+
+ def configure_reply_by_email
SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
- SiteSetting.alternative_reply_by_email_addresses = "alt+%{reply_key}@bar.com"
SiteSetting.manual_polling_enabled = true
SiteSetting.reply_by_email_enabled = true
end
@@ -69,146 +69,10 @@ RSpec.describe Email::Receiver do
expect { process(:silenced_sender) }.to raise_error(Email::Receiver::SilencedUserError)
end
- it "doesn't raise an InactiveUserError when the sender is staged" do
- user = Fabricate(:user, email: "[email protected]", active: false, staged: true)
- post = Fabricate(:post)
-
- Fabricate(
- :post_reply_key,
- user: user,
- post: post,
- reply_key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
- )
-
- expect { process(:staged_sender) }.not_to raise_error
- end
-
it "raises a BadDestinationAddress when destinations aren't matching any of the incoming emails" do
expect { process(:bad_destinations) }.to raise_error(Email::Receiver::BadDestinationAddress)
end
- it "raises an OldDestinationError when notification is too old" do
- SiteSetting.disallow_reply_by_email_after_days = 2
-
- topic = Fabricate(:topic)
- post = Fabricate(:post, topic: topic)
- Fabricate(:user, email: "[email protected]")
-
- mail = email(:old_destination).gsub(":post_id", post.id.to_s)
- expect { Email::Receiver.new(mail).process! }.to raise_error(
- Email::Receiver::BadDestinationAddress,
- )
-
- IncomingEmail.destroy_all
- post.update!(created_at: 3.days.ago)
-
- expect { Email::Receiver.new(mail).process! }.to raise_error(
- Email::Receiver::OldDestinationError,
- )
- expect(IncomingEmail.last.error).to eq("Email::Receiver::OldDestinationError")
-
- SiteSetting.disallow_reply_by_email_after_days = 0
- IncomingEmail.destroy_all
-
- expect { Email::Receiver.new(mail).process! }.to raise_error(
- Email::Receiver::BadDestinationAddress,
- )
- end
-
- describe "bounces" do
- it "raises a BouncerEmailError" do
- expect { process(:bounced_email) }.to raise_error(Email::Receiver::BouncedEmailError)
- expect(IncomingEmail.last.is_bounce).to eq(true)
-
- expect { process(:bounced_email_multiple_status_codes) }.to raise_error(
- Email::Receiver::BouncedEmailError,
- )
- expect(IncomingEmail.last.is_bounce).to eq(true)
- end
-
- describe "creating whisper post in PMs for staged users" do
- let(:email_address) { "[email protected]" }
- fab!(:user1) { Fabricate(:user, refresh_auto_groups: true) }
- let(:user2) { Fabricate(:staged, email: email_address) }
- let(:topic) do
- Fabricate(
- :topic,
- archetype: "private_message",
- category_id: nil,
- user: user1,
- allowed_users: [user1, user2],
- )
- end
- let(:post) { create_post(topic: topic, user: user1) }
-
- before do
- SiteSetting.enable_staged_users = true
- SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
- end
-
- def create_post_reply_key(value)
- Fabricate(:post_reply_key, reply_key: value, user: user2, post: post)
- end
-
- it "when bounce without verp" do
- create_post_reply_key("4f97315cc828096c9cb34c6f1a0d6fe8")
-
- expect { process(:bounced_email) }.to raise_error(Email::Receiver::BouncedEmailError)
- post = Post.last
- expect(post.whisper?).to eq(true)
- expect(post.raw).to eq(
- I18n.t(
- "system_messages.email_bounced",
- email: email_address,
- raw: "Your email bounced",
- ).strip,
- )
- expect(IncomingEmail.last.is_bounce).to eq(true)
- end
-
- context "when bounce with verp" do
- let(:bounce_key) { "14b08c855160d67f2e0c2f8ef36e251e" }
-
- before do
- SiteSetting.reply_by_email_address = "foo+%{reply_key}@discourse.org"
- create_post_reply_key(bounce_key)
- Fabricate(
- :email_log,
- to_address: email_address,
- user: user2,
- bounce_key: bounce_key,
- post: post,
- )
- end
-
- it "creates a post with the bounce error" do
- expect { process(:hard_bounce_via_verp) }.to raise_error(
- Email::Receiver::BouncedEmailError,
- )
- post = Post.last
- expect(post.whisper?).to eq(true)
- expect(post.raw).to eq(
- I18n.t(
- "system_messages.email_bounced",
- email: email_address,
- raw: "Your email bounced",
- ).strip,
- )
- expect(IncomingEmail.last.is_bounce).to eq(true)
- end
-
- it "updates the email log with the bounce error message" do
- expect { process(:hard_bounce_via_verp) }.to raise_error(
- Email::Receiver::BouncedEmailError,
- )
- email_log = EmailLog.find_by(bounce_key: bounce_key)
- expect(email_log.bounced).to eq(true)
- expect(email_log.bounce_error_code).to eq("5.1.1")
- end
- end
- end
- end
-
it "logs a blank error" do
Email::Receiver.any_instance.stubs(:process_internal).raises(RuntimeError, "")
begin
@@ -278,2194 +142,2387 @@ RSpec.describe Email::Receiver do
end
end
- describe "reply" do
- let(:reply_key) { "4f97315cc828096c9cb34c6f1a0d6fe8" }
- fab!(:category)
- fab!(:user) { Fabricate(:user, email: "[email protected]", refresh_auto_groups: true) }
- fab!(:topic) { create_topic(category: category, user: user) }
- fab!(:post) { create_post(topic: topic) }
+ shared_examples "creates topic with forwarded message as quote" do |destination, address|
+ it "creates topic with forwarded message as quote" do
+ expect { process(:forwarded_email_1) }.to change(Topic, :count)
- let!(:post_reply_key) do
- Fabricate(:post_reply_key, reply_key: reply_key, user: user, post: post)
- end
+ topic = Topic.last
+ if destination == :category
+ expect(topic.category).to eq(Category.where(email_in: address).first)
+ else
+ expect(topic.archetype).to eq(Archetype.private_message)
+ expect(topic.allowed_groups).to eq(Group.where(incoming_email: address))
+ end
- let :topic_user do
- TopicUser.find_by(topic_id: topic.id, user_id: user.id)
- end
+ post = Post.last
- it "uses MD5 of 'mail_string' there is no message_id" do
- mail_string = email(:missing_message_id)
- expect { Email::Receiver.new(mail_string).process! }.to change { IncomingEmail.count }
- expect(IncomingEmail.last.message_id).to eq(Digest::MD5.hexdigest(mail_string))
- end
+ expect(post.user.email).to eq("[email protected]")
+ expect(post.raw).to eq(<<~RAW.chomp)
+ @team, can you have a look at this email below?
- it "raises a ReplyUserNotMatchingError when the email address isn't matching the one we sent the notification to" do
- Fabricate(:user, email: "[email protected]")
- expect { process(:reply_user_not_matching) }.to raise_error(
- Email::Receiver::ReplyUserNotMatchingError,
- )
- end
+ [quote]
+ From: Some One &lt;some@one\\.com&gt;
+ To: Ba Bar &lt;ba@bar\\.com&gt;
+ Date: Mon, 1 Dec 2016 00:13:37 \\+0100
+ Subject: Discoursing much?
- it "raises a FromReplyByAddressError when the email is from the reply by email address" do
- expect { process(:from_reply_by_email_address) }.to raise_error(
- Email::Receiver::FromReplyByAddressError,
- )
- end
+ Hello Ba Bar,
- it "accepts reply from secondary email address" do
- Fabricate(:secondary_email, email: "[email protected]", user: user)
+ Discoursing much today?
- expect { process(:reply_user_not_matching) }.to change { topic.posts.count }
+ XoXo
+ [/quote]
+ RAW
+ end
+ end
- post = Post.last
+ describe "new message to a group" do
+ fab!(:group) { Fabricate(:group, incoming_email: "[email protected]|[email protected]") }
- expect(post.raw).to eq("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
+ it "handles encoded display names" do
+ expect { process(:encoded_display_name) }.to change(Topic, :count)
- expect(post.user).to eq(user)
- end
+ topic = Topic.last
+ expect(topic.title).to eq("I need help")
+ expect(topic.private_message?).to eq(true)
+ expect(topic.allowed_groups).to include(group)
- it "raises a ReplyNotAllowedError when user without permissions is replying" do
- Fabricate(:user, email: "[email protected]")
- category.set_permissions(admins: :full)
- category.save
- expect { process(:reply_user_not_matching_but_known) }.to raise_error(
- Email::Receiver::ReplyNotAllowedError,
- )
+ user = topic.user
+ expect(user.staged).to eq(true)
+ expect(user.username).to eq("user1")
+ expect(user.name).to eq("Случайная Имя")
end
- it "raises a TopicNotFoundError when the topic was deleted" do
- topic.update_columns(deleted_at: 1.day.ago)
- expect { process(:reply_user_matching) }.to raise_error(Email::Receiver::TopicNotFoundError)
+ it "handles email with no subject" do
+ expect { process(:no_subject) }.to change(Topic, :count)
+ expect(Topic.last.title).to eq("This topic needs a title")
end
- context "with a closed topic" do
- before { topic.update_columns(closed: true) }
+ context "with reply_by_email configured" do
+ before { configure_reply_by_email }
+ it "invites everyone in the chain but emails configured as 'incoming' (via reply, group or category)" do
+ expect { process(:cc) }.to change(Topic, :count)
- it "raises a TopicClosedError when the topic was closed" do
- expect { process(:reply_user_matching) }.to raise_error(Email::Receiver::TopicClosedError)
- end
+ topic = Topic.last
- it "Can watch topics via the watch command" do
- # TODO support other locales as well, the tricky thing is that these string live in
- # client.yml not on server yml so it is a bit tricky to find
+ emails = topic.allowed_users.joins(:user_emails).pluck(:"user_emails.email")
+ expect(emails).to contain_exactly("[email protected]", "[email protected]", "[email protected]")
- topic.update_columns(closed: true)
- process(:watch)
- expect(topic_user.notification_level).to eq(NotificationLevels.topic_levels[:watching])
+ expect(topic.topic_users.count).to eq(3)
end
- it "Can mute topics via the mute command" do
- process(:mute)
- expect(topic_user.notification_level).to eq(NotificationLevels.topic_levels[:muted])
+ it "invites users with a secondary email in the chain" do
+ user1 =
+ Fabricate(
+ :user,
+ trust_level: TrustLevel[2],
+ user_emails: [
+ Fabricate.build(:secondary_email, email: "[email protected]"),
+ Fabricate.build(:secondary_email, email: "[email protected]"),
+ ],
+ refresh_auto_groups: true,
+ )
+
+ user2 =
+ Fabricate(
+ :user,
+ trust_level: TrustLevel[2],
+ user_emails: [
+ Fabricate.build(:secondary_email, email: "[email protected]"),
+ Fabricate.build(:secondary_email, email: "[email protected]"),
+ ],
+ refresh_auto_groups: true,
+ )
+
+ expect { process(:cc) }.to change(Topic, :count)
+ expect(Topic.last.allowed_users).to contain_exactly(user1, user2)
end
- it "can track a topic via the track command" do
- process(:track)
- expect(topic_user.notification_level).to eq(NotificationLevels.topic_levels[:tracking])
+ it "cap the number of staged users created per email" do
+ SiteSetting.maximum_staged_users_per_email = 1
+ expect { process(:cc) }.to change(Topic, :count).by(1).and change(User, :count).by(1)
+ expect(Topic.last.ordered_posts[-1].post_type).to eq(Post.types[:moderator_action])
end
- end
- it "raises an InvalidPost when there was an error while creating the post" do
- expect { process(:too_small) }.to raise_error(Email::Receiver::TooShortPost)
- end
+ it "cap the number of staged users existing per email" do
+ Fabricate(:user, email: "[email protected]", staged: true) # from
+ Fabricate(:user, email: "[email protected]", staged: true) # to
- it "raises an InvalidPost when there are too may mentions" do
- SiteSetting.max_mentions_per_post = 1
- Fabricate(:user, username: "user1")
- Fabricate(:user, username: "user2")
- expect { process(:too_many_mentions) }.to raise_error(Email::Receiver::InvalidPost)
- end
+ SiteSetting.maximum_staged_users_per_email = 1
+ expect { process(:cc) }.to change(Topic, :count).and not_change(User, :count)
+ expect(Topic.last.ordered_posts[-1].post_type).to eq(Post.types[:moderator_action])
+ end
+
+ it "rejects messages with too many recipients" do
+ SiteSetting.maximum_recipients_per_new_group_email = 3
+ expect { process(:cc) }.to raise_error(Email::Receiver::TooManyRecipientsError)
+ end
- it "raises an InvalidPostAction when they aren't allowed to like a post" do
- topic.update_columns(archived: true)
- expect { process(:like) }.to raise_error(Email::Receiver::InvalidPostAction)
+ it "uses the incoming_email message-id as the new post's outbound_message_id" do
+ expect { process(:cc) }.to change(Topic, :count)
+ message_id = IncomingEmail.last.message_id
+ expect(Topic.last.first_post.outbound_message_id).to eq(message_id)
+ end
end
- it "creates a new reply post" do
- handler_calls = 0
- handler = proc { |_| handler_calls += 1 }
+ describe "reply-to header" do
+ before { SiteSetting.block_auto_generated_emails = false }
- DiscourseEvent.on(:topic_created, &handler)
+ it "extracts address and uses it for comparison" do
+ expect { process(:reply_to_whitespaces) }.to change(Topic, :count).by(1)
+ incoming = IncomingEmail.find_by(message_id: "[email protected]")
+ expect(incoming.from_address).to eq("[email protected]")
+ expect(User.last.email).to eq("[email protected]")
+ end
- expect { process(:text_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This is a text reply :)\n\nEmail parsing should not break because of a UTF-8 character: ’",
- )
- expect(topic.posts.last.via_email).to eq(true)
- expect(topic.posts.last.cooked).not_to match(/<br/)
+ it "handles emails where there is a Reply-To address, using that instead of the from address, if X-Original-From is present" do
+ expect { process(:reply_to_different_to_from) }.to change(Topic, :count).by(1)
+ incoming = IncomingEmail.find_by(message_id: "[email protected]")
+ expect(incoming.from_address).to eq("[email protected]")
+ expect(User.last.email).to eq("[email protected]")
+ end
- expect { process(:html_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is a **HTML** reply ;)")
+ it "allows for quotes around the display name of the Reply-To address" do
+ expect { process(:reply_to_different_to_from_quoted_display_name) }.to change(
+ Topic,
+ :count,
+ ).by(1)
+ incoming = IncomingEmail.find_by(message_id: "[email protected]")
+ expect(incoming.from_address).to eq("[email protected]")
+ expect(User.last.email).to eq("[email protected]")
+ end
- DiscourseEvent.off(:topic_created, &handler)
- expect(handler_calls).to eq(0)
- end
+ it "does not use the reply-to address if an X-Original-From header is not present" do
+ expect { process(:reply_to_different_to_from_no_x_original) }.to change(Topic, :count).by(1)
+ incoming = IncomingEmail.find_by(message_id: "[email protected]")
+ expect(incoming.from_address).to eq("[email protected]")
+ expect(User.last.email).to eq("[email protected]")
+ end
- it "stores the created_via source against the incoming email" do
- process(:text_reply, source: :handle_mail)
- expect(IncomingEmail.last.created_via).to eq(IncomingEmail.created_via_types[:handle_mail])
- process(:text_and_html_reply, source: :pop3_poll)
- expect(IncomingEmail.last.created_via).to eq(IncomingEmail.created_via_types[:pop3_poll])
+ it "does not use the reply-to address if the X-Original-From header is different from the reply-to address" do
+ expect { process(:reply_to_different_to_from_x_original_different) }.to change(
+ Topic,
+ :count,
+ ).by(1)
+ incoming = IncomingEmail.find_by(message_id: "[email protected]")
+ expect(incoming.from_address).to eq("[email protected]")
+ expect(User.last.email).to eq("[email protected]")
+ end
end
- it "stores the message_id of the incoming email against the post as outbound_message_id" do
- expect { process(:text_reply, source: :handle_mail) }.to change(Post, :count)
- message_id = IncomingEmail.last.message_id
- expect(Post.last.outbound_message_id).to eq(message_id)
- end
+ describe "when 'find_related_post_with_key' is disabled" do
+ before { SiteSetting.find_related_post_with_key = false }
- it "automatically elides gmail quotes" do
- SiteSetting.always_show_trimmed_content = true
- expect { process(:gmail_html_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq <<~MD.strip
- This is a **GMAIL** reply ;)
+ it "associates email replies using both 'In-Reply-To' and 'References' headers" do
+ expect { process(:email_reply_1) }.to change(Topic, :count).by(1) &
+ change(Post, :count).by(3) & change(User, :count).by(3)
- <details class='elided'>
- <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+ topic = Topic.last
+ users = User.last(3)
+ ordered_posts = topic.ordered_posts
+ expect(ordered_posts.size).to eq(3)
- This is the *elided* part!
+ expect(ordered_posts.first.raw).to eq("This is email reply **1**.")
- </details>
- MD
- end
+ ordered_posts[1..-1].each do |post|
+ expect(post.action_code).to eq("invited_user")
+ expect(post.user.email).to eq("[email protected]")
- it "correctly extracts body from exchange emails" do
- SiteSetting.always_show_trimmed_content = true
- expect { process(:exchange_html_body) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is the **body** of the email.")
- end
+ expect(users.map(&:username)).to include(post.custom_fields["action_code_who"])
+ end
- it "correctly extracts reply from exchange emails" do
- SiteSetting.always_show_trimmed_content = true
- expect { process(:exchange_html_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is the **body !!** of the email.")
- end
+ expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1)
+ expect { process(:email_reply_3) }.to change { topic.posts.count }.by(1)
+ ordered_posts[1..-1].each(&:trash!)
+ expect { process(:email_reply_4) }.to change { topic.posts.count }.by(1)
+ end
- it "correctly extracts body & reply from exchange emails" do
- SiteSetting.always_show_trimmed_content = true
- expect { process(:exchange_html_body_and_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq <<~MD.strip
- This is the **body** of the email.
+ describe "replying with various message-id formats using In-Reply-To header" do
+ let!(:topic) do
+ process(:email_reply_1)
+ Topic.last
+ end
+ let!(:post) { Fabricate(:post, topic: topic) }
- <details class='elided'>
- <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+ def process_mail_with_message_id(message_id)
+ mail_string = <<~EMAIL
+ Return-Path: <[email protected]>
+ From: Two <[email protected]>
+ Subject: RE: Testing email threading
+ Date: Fri, 15 Jan 2016 00:12:43 +0100
+ Message-ID: <[email protected]>
+ In-Reply-To: <#{message_id}>
+ Mime-Version: 1.0
+ Content-Type: text/plain
+ Content-Transfer-Encoding: 7bit
- This is the *reply*!
+ This is email reply testing with Message-ID formats.
+ EMAIL
+ Email::Receiver.new(mail_string).process!
+ end
- </details>
- MD
+ it "posts a reply using a message-id in the format discourse/post/POST_ID@HOST" do
+ expect {
+ process_mail_with_message_id("discourse/post/#{post.id}@test.localhost")
+ }.to change { Post.count }.by(1)
+ expect(topic.reload.posts.last.raw).to include(
+ "This is email reply testing with Message-ID formats",
+ )
+ end
+ end
end
- it "doesn't process email with same message-id more than once" do
- expect do
- process(:text_reply)
- process(:text_reply)
- end.to change { topic.posts.count }.by(1)
- end
+ it "supports any kind of attachments when 'allow_all_attachments_for_group_messages' is enabled" do
+ SiteSetting.allow_all_attachments_for_group_messages = true
+ expect { process(:attached_rb_file) }.to change(Topic, :count)
- it "handles different encodings correctly" do
- expect { process(:hebrew_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("שלום! מה שלומך היום?")
+ post = Topic.last.first_post
+ upload = post.uploads.first
- expect { process(:chinese_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("您好! 你今天好吗?")
-
- expect { process(:reply_with_weird_encoding) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is a reply with a weird encoding.")
-
- expect { process(:reply_with_8bit_encoding) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("hab vergessen kritische zeichen einzufügen:\näöüÄÖÜß")
+ expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
end
- it "prefers text over html when site setting is disabled" do
- SiteSetting.incoming_email_prefer_html = false
- expect { process(:text_and_html_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is the *text* part.")
+ it "reenables user's PM email notifications when user emails new topic to group" do
+ user = Fabricate(:user, email: "[email protected]")
+ user.user_option.update_columns(email_messages_level: UserOption.email_level_types[:never])
+ expect { process(:group_existing_user) }.to change(Topic, :count)
+ user.reload
+ expect(user.user_option.email_messages_level).to eq(UserOption.email_level_types[:always])
end
- it "prefers html over text when site setting is enabled" do
- SiteSetting.incoming_email_prefer_html = true
- expect { process(:text_and_html_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is the **html** part.")
- end
+ context "with forwarded emails behaviour set to create replies" do
+ before do
+ Fabricate(:group, incoming_email: "[email protected]")
+ SiteSetting.forwarded_emails_behaviour = "create_replies"
+ end
- it "uses text when prefer_html site setting is enabled but no html is available" do
- SiteSetting.incoming_email_prefer_html = true
- expect { process(:text_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This is a text reply :)\n\nEmail parsing should not break because of a UTF-8 character: ’",
- )
- end
+ it "handles forwarded emails" do
+ expect { process(:forwarded_email_1) }.to change(Topic, :count)
- it "removes the 'on <date>, <contact> wrote' quoting line" do
- expect { process(:on_date_contact_wrote) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is the actual reply.")
- end
+ forwarded_post, last_post = *Post.last(2)
- it "removes the 'Previous Replies' marker" do
- expect { process(:previous_replies) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This will not include the previous discussion that is present in this email.",
- )
- end
+ expect(forwarded_post.user.email).to eq("[email protected]")
+ expect(last_post.user.email).to eq("[email protected]")
- it "removes the translated 'Previous Replies' marker" do
- expect { process(:previous_replies_de) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This will not include the previous discussion that is present in this email.",
- )
- end
+ expect(forwarded_post.raw).to match(/XoXo/)
+ expect(last_post.raw).to match(/can you have a look at this email below/)
- it "removes the 'type reply above' marker" do
- expect { process(:reply_above) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This will not include the previous discussion that is present in this email.",
- )
- end
+ expect(last_post.post_type).to eq(Post.types[:regular])
+ end
- it "removes the translated 'Previous Replies' marker" do
- expect { process(:reply_above_de) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This will not include the previous discussion that is present in this email.",
- )
- end
+ it "handles weirdly forwarded emails" do
+ group.add(Fabricate(:user, email: "[email protected]"))
+ group.save
- it "handles multiple paragraphs" do
- expect { process(:paragraphs) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "Do you like liquorice?\n\nI really like them. One could even say that I am *addicted* to liquorice. And if\nyou can mix it up with some anise, then I'm in heaven ;)",
- )
- end
+ SiteSetting.forwarded_emails_behaviour = "create_replies"
+ expect { process(:forwarded_email_2) }.to change(Topic, :count)
- it "raises a NoSenderDetectedError when the From header can't be parsed" do
- expect { process(:invalid_from_1) }.to raise_error(Email::Receiver::NoSenderDetectedError)
- end
+ forwarded_post, last_post = *Post.last(2)
- it "raises a NoSenderDetectedError when the From header doesn't contain an email address" do
- expect { process(:invalid_from_2) }.to raise_error(Email::Receiver::NoSenderDetectedError)
- end
+ expect(forwarded_post.user.email).to eq("[email protected]")
+ expect(last_post.user.email).to eq("[email protected]")
- it "doesn't raise an AutoGeneratedEmailError due to an X-Auto-Response-Suppress header" do
- expect { process(:quirks_exchange_xars) }.to change { topic.posts.count }
- end
+ expect(forwarded_post.raw).to match(/XoXo/)
+ expect(last_post.raw).to match(/can you have a look at this email below/)
- it "doesn't raise an AutoGeneratedEmailError when the mail is auto generated but is allowlisted" do
- SiteSetting.auto_generated_allowlist = "[email protected]|[email protected]"
- expect { process(:auto_generated_allowlisted) }.to change { topic.posts.count }
- end
+ expect(last_post.post_type).to eq(Post.types[:whisper])
+ end
- it "doesn't raise an AutoGeneratedEmailError when block_auto_generated_emails is disabled" do
- SiteSetting.block_auto_generated_emails = false
- expect { process(:auto_generated_unblocked) }.to change { topic.posts.count }
- end
+ # Who thought this was a good idea?!
+ it "doesn't blow up with localized email headers" do
+ expect { process(:forwarded_email_3) }.to change(Topic, :count)
+ end
- it "allows staged users to reply to a restricted category" do
- user.update_columns(staged: true)
+ it "adds a small action post to explain who forwarded the email when the sender didn't write anything" do
+ expect { process(:forwarded_email_4) }.to change(Topic, :count)
- category.email_in = "[email protected]"
- category.email_in_allow_strangers = true
- category.set_permissions(Group[:trust_level_4] => :full)
- category.save!
+ forwarded_post, last_post = *Post.last(2)
- expect { process(:staged_reply_restricted) }.to change { topic.posts.count }
- end
+ expect(forwarded_post.user.email).to eq("[email protected]")
+ expect(forwarded_post.raw).to match(/XoXo/)
- it "posts a reply to the topic when the post was deleted" do
- post.update_columns(deleted_at: 1.day.ago)
- expect { process(:reply_user_matching) }.to change { topic.posts.count }
- expect(topic.ordered_posts.last.reply_to_post_number).to be_nil
+ expect(last_post.user.email).to eq("[email protected]")
+ expect(last_post.post_type).to eq(Post.types[:small_action])
+ expect(last_post.action_code).to eq("forwarded")
+ end
end
- describe "Unsubscribing via email" do
- let(:last_email) { ActionMailer::Base.deliveries.last }
+ context "with forwarded emails behaviour set to quote" do
+ before { SiteSetting.forwarded_emails_behaviour = "quote" }
- describe "unsubscribe_subject.eml" do
- it "sends an email asking the user to confirm the unsubscription" do
- expect { process("unsubscribe_subject") }.to change {
- ActionMailer::Base.deliveries.count
- }.by(1)
- expect(last_email.to.length).to eq 1
- expect(last_email.from.length).to eq 1
- expect(last_email.from).to include "noreply@#{Discourse.current_hostname}"
- expect(last_email.to).to include "[email protected]"
- expect(last_email.subject).to eq I18n.t(:"unsubscribe_mailer.subject_template").gsub(
- "%{site_title}",
- SiteSetting.title,
- )
- end
+ include_examples "creates topic with forwarded message as quote",
+ :group,
+ end
- it "does nothing unless unsubscribe_via_email is turned on" do
- SiteSetting.unsubscribe_via_email = false
- before_deliveries = ActionMailer::Base.deliveries.count
- expect { process("unsubscribe_subject") }.to raise_error {
- Email::Receiver::BadDestinationAddress
- }
- expect(before_deliveries).to eq ActionMailer::Base.deliveries.count
- end
+ context "when a reply is sent to a group's email_username" do
+ let!(:topic) do
+ group.update(email_username: "[email protected]")
+ process(:email_reply_1)
+ Topic.last
end
- describe "unsubscribe_body.eml" do
- it "sends an email asking the user to confirm the unsubscription" do
- expect { process("unsubscribe_body") }.to change {
- ActionMailer::Base.deliveries.count
- }.by(1)
- expect(last_email.to.length).to eq 1
- expect(last_email.from.length).to eq 1
- expect(last_email.from).to include "noreply@#{Discourse.current_hostname}"
- expect(last_email.to).to include "[email protected]"
- expect(last_email.subject).to eq I18n.t(:"unsubscribe_mailer.subject_template").gsub(
- "%{site_title}",
- SiteSetting.title,
- )
- end
-
- it "does nothing unless unsubscribe_via_email is turned on" do
- SiteSetting.unsubscribe_via_email = false
- before_deliveries = ActionMailer::Base.deliveries.count
- expect { process("unsubscribe_body") }.to raise_error { Email::Receiver::InvalidPost }
- expect(before_deliveries).to eq ActionMailer::Base.deliveries.count
- end
+ it "does not invite the group email_username as a staged user" do
+ process(:email_reply_to_group_email_username)
+ expect(User.find_by_email("[email protected]")).to eq(nil)
end
- it "raises an UnsubscribeNotAllowed and does not send an unsubscribe email" do
- before_deliveries = ActionMailer::Base.deliveries.count
- expect { process(:unsubscribe_new_user) }.to raise_error {
- Email::Receiver::UnsubscribeNotAllowed
- }
- expect(before_deliveries).to eq ActionMailer::Base.deliveries.count
+ it "creates the reply when the sender and referenced message id are known" do
+ expect { process(:email_reply_to_group_email_username) }.to change { topic.posts.count }.by(
+ 1,
+ ).and not_change { Topic.count }
end
end
- it "handles inline reply" do
- expect { process(:inline_reply) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("And this is *my* reply :+1:")
- end
+ context "when a group forwards an email to its inbox" do
+ before do
+ group.update!(
+ email_username: "[email protected]",
+ smtp_server: "smtp.test.com",
+ smtp_port: 587,
+ smtp_ssl_mode: Group.smtp_ssl_modes[:starttls],
+ smtp_enabled: true,
+ )
+ end
- it "retrieves the first part of multiple replies" do
- expect { process(:inline_mixed_replies) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "> WAT <https://bar.com/users/wat> November 28\n>\n> This is the previous post.\n\nAnd this is *my* reply :+1:\n\n> This is another post.\n\nAnd this is **another** reply.",
- )
- end
+ it "does not use the team's address as the from_address; it uses the original sender address" do
+ process(:forwarded_by_group_to_inbox)
+ topic = Topic.last
+ expect(topic.incoming_email.first.to_addresses).to include("[email protected]")
+ expect(topic.incoming_email.first.from_address).to eq("[email protected]")
+ end
- it "strips mobile/webmail signatures" do
- expect { process(:iphone_signature) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is not the signature you're looking for.")
- end
+ context "with forwarded emails behaviour set to create replies" do
+ before { SiteSetting.forwarded_emails_behaviour = "create_replies" }
- it "strips 'original message' context" do
- expect { process(:original_message) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is a reply :)")
- end
+ it "does not use the team's address as the from_address; it uses the original sender address" do
+ process(:forwarded_by_group_to_inbox)
+ topic = Topic.last
+ expect(topic.incoming_email.first.to_addresses).to include("[email protected]")
+ expect(topic.incoming_email.first.from_address).to eq("[email protected]")
+ end
- it "add the 'elided' part of the original message only for private messages" do
- topic.update_columns(category_id: nil, archetype: Archetype.private_message)
- topic.allowed_users << user
- topic.save
+ it "does not say the email was forwarded by the original sender, it says the email is forwarded by the group" do
+ expect { process(:forwarded_by_group_to_inbox) }.to change {
+ User.where(staged: true).count
+ }.by(4)
+ topic = Topic.last
+ forwarded_small_post = topic.ordered_posts.last
+ expect(forwarded_small_post.action_code).to eq("forwarded")
+ expect(forwarded_small_post.user).to eq(User.find_by_email("[email protected]"))
+ end
- expect { process(:original_message) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This is a reply :)\n\n<details class='elided'>\n<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>\n\n---Original Message---\nThis part should not be included\n\n</details>",
- )
- end
+ it "keeps track of the cc addresses of the forwarded email and creates staged users for them" do
+ expect { process(:forwarded_by_group_to_inbox) }.to change {
+ User.where(staged: true).count
+ }.by(4)
+ topic = Topic.last
+ cc_user1 = User.find_by_email("[email protected]")
+ cc_user2 = User.find_by_email("[email protected]")
+ fred_user = User.find_by_email("[email protected]")
+ team_user = User.find_by_email("[email protected]")
+ expect(topic.incoming_email.first.cc_addresses).to eq("[email protected];[email protected]")
+ expect(topic.topic_allowed_users.pluck(:user_id)).to match_array(
+ [fred_user.id, team_user.id, cc_user1.id, cc_user2.id],
+ )
+ end
- it "doesn't include the 'elided' part of the original message when always_show_trimmed_content is disabled" do
- SiteSetting.always_show_trimmed_content = false
- expect { process(:original_message) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq("This is a reply :)")
- end
+ it "keeps track of the cc addresses of the final forwarded email as well" do
+ expect { process(:forwarded_by_group_to_inbox_double_cc) }.to change {
+ User.where(staged: true).count
+ }.by(5)
+ topic = Topic.last
+ cc_user1 = User.find_by_email("[email protected]")
+ cc_user2 = User.find_by_email("[email protected]")
+ fred_user = User.find_by_email("[email protected]")
+ team_user = User.find_by_email("[email protected]")
+ someother_user = User.find_by_email("[email protected]")
+ expect(topic.incoming_email.first.cc_addresses).to eq(
+ )
+ expect(topic.topic_allowed_users.pluck(:user_id)).to match_array(
+ [fred_user.id, team_user.id, someother_user.id, cc_user1.id, cc_user2.id],
+ )
+ end
- it "adds the 'elided' part of the original message for public replies when always_show_trimmed_content is enabled" do
- SiteSetting.always_show_trimmed_content = true
- expect { process(:original_message) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This is a reply :)\n\n<details class='elided'>\n<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>\n\n---Original Message---\nThis part should not be included\n\n</details>",
- )
- end
+ context "when staged user for the team email already exists" do
+ let!(:staged_team_user) do
+ User.create!(
+ email: "[email protected]",
+ username: UserNameSuggester.suggest("[email protected]"),
+ name: "team teamson",
+ staged: true,
+ )
+ end
- it "doesn't trim the message when trim_incoming_emails is disabled" do
- SiteSetting.trim_incoming_emails = false
- expect { process(:original_message) }.to change { topic.posts.count }
- expect(topic.posts.last.raw).to eq(
- "This is a reply :)\n\n---Original Message---\nThis part should not be included",
- )
+ it "uses that and does not create another staged user" do
+ expect { process(:forwarded_by_group_to_inbox) }.to change {
+ User.where(staged: true).count
+ }.by(3)
+ topic = Topic.last
+ forwarded_small_post = topic.ordered_posts.last
+ expect(forwarded_small_post.action_code).to eq("forwarded")
+ expect(forwarded_small_post.user).to eq(staged_team_user)
+ end
+ end
+ end
end
- it "supports attached images in TEXT part" do
- SiteSetting.incoming_email_prefer_html = false
-
- expect { process(:no_body_with_image) }.to change { topic.posts.count }
-
- post = topic.posts.last
- upload = post.uploads.first
-
- expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
+ context "when emailing a group by email_username and following reply flow" do
+ let!(:original_inbound_email_topic) do
+ group.update!(
+ email_username: "[email protected]",
+ smtp_server: "smtp.test.com",
+ smtp_port: 587,
+ smtp_ssl_mode: Group.smtp_ssl_modes[:starttls],
+ smtp_enabled: true,
+ )
+ process(:email_to_group_email_username_1)
+ Topic.last
+ end
+ fab!(:user_in_group) do
+ u = Fabricate(:user)
+ Fabricate(:group_user, user: u, group: group)
+ u
+ end
- expect { process(:inline_image) }.to change { topic.posts.count }
+ before do
+ NotificationEmailer.enable
+ SiteSetting.disallow_reply_by_email_after_days = 10_000
+ Jobs.run_immediately!
+ end
- post = topic.posts.last
- upload = post.uploads.first
+ def reply_as_group_user
+ group_post =
+ PostCreator.create(
+ user_in_group,
+ raw: "Thanks for your request. Please try to restart.",
+ topic_id: original_inbound_email_topic.id,
+ )
+ email_log = EmailLog.last
+ [email_log, group_post]
+ end
- expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
- end
+ it "the inbound processed email creates an incoming email and topic record correctly, and adds the group to the topic" do
+ incoming = IncomingEmail.find_by(topic: original_inbound_email_topic)
+ user = User.find_by_email("[email protected]")
+ expect(original_inbound_email_topic.topic_allowed_users.first.user_id).to eq(user.id)
+ expect(original_inbound_email_topic.topic_allowed_groups.first.group_id).to eq(group.id)
+ expect(incoming.to_addresses).to eq("[email protected]")
+ expect(incoming.from_address).to eq("[email protected]")
+ expect(incoming.message_id).to eq("[email protected]")
+ end
- it "supports attached images in HTML part" do
- SiteSetting.incoming_email_prefer_html = true
+ it "creates an EmailLog when someone from the group replies, and does not create an IncomingEmail record for the reply" do
+ email_log, group_post = reply_as_group_user
+ expect(email_log.message_id).to eq("discourse/post/#{group_post.id}@test.localhost")
+ expect(email_log.to_address).to eq("[email protected]")
+ expect(email_log.email_type).to eq("user_private_message")
+ expect(email_log.post_id).to eq(group_post.id)
+ expect(IncomingEmail.exists?(post_id: group_post.id)).to eq(false)
+ end
- expect { process(:inline_image) }.to change { topic.posts.count }
+ it "processes a reply from the OP user to the group SMTP username, linking the reply_to_post_number correctly by matching in_reply_to to the email log" do
+ email_log, group_post = reply_as_group_user
- post = topic.posts.last
- upload = post.uploads.last
-
- expect(post.raw).to eq(<<~MD.chomp)
- **Before**
+ reply_email = email(:email_to_group_email_username_2)
+ reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
+ expect { Email::Receiver.new(reply_email).process! }.to not_change {
+ Topic.count
+ }.and change { Post.count }.by(1)
- <img src="#{upload.short_url}" alt="内嵌图片 1">
+ reply_post = Post.last
+ expect(reply_post.reply_to_user).to eq(user_in_group)
+ expect(reply_post.reply_to_post_number).to eq(group_post.post_number)
+ end
- *After*
- MD
- end
+ it "handles multiple message IDs in the in_reply_to header by only using the first one" do
+ email_log, group_post = reply_as_group_user
- it "gracefully handles malformed images in HTML part" do
- expect { process(:inline_image_2) }.to change { topic.posts.count }
+ reply_email = email(:email_to_group_email_username_3)
+ reply_email.gsub!(
+ "MESSAGE_ID_REPLY_TO",
+ "<#{email_log.message_id}> <test/message/[email protected]>",
+ )
+ expect { Email::Receiver.new(reply_email).process! }.to not_change {
+ Topic.count
+ }.and change { Post.count }.by(1)
- post = topic.posts.last
- upload = post.uploads.last
+ reply_post = Post.last
+ expect(reply_post.reply_to_user).to eq(user_in_group)
+ expect(reply_post.reply_to_post_number).to eq(group_post.post_number)
+ end
- expect(post.raw).to eq(<<~MD.chomp)
- [image:#{"0" * 5000}
+ it "processes the reply from the user as a brand new topic if they have replied from a different address (e.g. auto forward) and allow_unknown_sender_topic_replies is disabled" do
+ email_log, _group_post = reply_as_group_user
- [details="#{I18n.t("emails.incoming.attachments")}"]
+ reply_email = email(:email_to_group_email_username_2_as_unknown_sender)
+ reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
+ expect { Email::Receiver.new(reply_email).process! }.to change { Topic.count }.by(
+ 1,
+ ).and change { Post.count }.by(1)
- #{UploadMarkdown.new(upload).to_markdown}
+ reply_post = Post.last
+ expect(reply_post.topic_id).not_to eq(original_inbound_email_topic.id)
+ end
- [/details]
- MD
- end
+ it "processes the reply from the user as a reply if they have replied from a different address (e.g. auto forward) and allow_unknown_sender_topic_replies is enabled" do
+ group.update!(allow_unknown_sender_topic_replies: true)
+ email_log, _group_post = reply_as_group_user
- it "supports attached images in signature" do
- SiteSetting.incoming_email_prefer_html = true
- SiteSetting.always_show_trimmed_content = true
+ reply_email = email(:email_to_group_email_username_2_as_unknown_sender)
+ reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
+ expect { Email::Receiver.new(reply_email).process! }.to not_change {
+ Topic.count
+ }.and change { Post.count }.by(1)
- expect { process(:body_with_image) }.to change { topic.posts.count }
+ reply_post = Post.last
+ expect(reply_post.topic_id).to eq(original_inbound_email_topic.id)
+ end
- post = topic.posts.last
- upload = post.uploads.last
+ it "creates a new topic with a reference back to the original if replying to a too old topic" do
+ SiteSetting.disallow_reply_by_email_after_days = 2
+ email_log, group_post = reply_as_group_user
- expect(post.raw).to eq(<<~MD.chomp)
- This is a **GMAIL** reply ;)
+ group_post.update(created_at: 10.days.ago)
+ group_post.topic.update(created_at: 10.days.ago)
- <details class='elided'>
- <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+ reply_email = email(:email_to_group_email_username_2)
+ reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
+ expect { Email::Receiver.new(reply_email).process! }.to change { Topic.count }.by(
+ 1,
+ ).and change { Post.count }.by(1)
- <img src="#{upload.short_url}" width="300" height="200">
+ reply_post = Post.last
+ new_topic = Topic.last
- </details>
- MD
+ expect(reply_post.topic).to eq(new_topic)
+ expect(reply_post.raw).to include(
+ I18n.t(
+ "emails.incoming.continuing_old_discussion",
+ url: group_post.topic.url,
+ title: group_post.topic.title,
+ count: SiteSetting.disallow_reply_by_email_after_days,
+ ),
+ )
+ end
end
- it "supports attachments" do
- SiteSetting.authorized_extensions = "txt|jpg"
- expect { process(:attached_txt_file) }.to change { topic.posts.count }
- post = topic.posts.last
- upload = post.uploads.first
-
- expect(post.raw).to eq(<<~MD.chomp)
- Please find some text file attached.
-
- [details="#{I18n.t("emails.incoming.attachments")}"]
+ context "when message sent to a group has no key and find_related_post_with_key is enabled" do
+ let!(:topic) do
+ process(:email_reply_1)
+ Topic.last
+ end
- #{UploadMarkdown.new(upload).to_markdown}
+ it "creates a reply when the sender and referenced message id are known" do
+ expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1).and not_change {
+ Topic.count
+ }
+ end
- [/details]
- MD
+ it "creates a new topic when the sender is not known and the group does not allow unknown senders to reply to topics" do
+ IncomingEmail.where(message_id: "[email protected]").update(cc_addresses: "[email protected]")
+ group.update(allow_unknown_sender_topic_replies: false)
+ expect { process(:email_reply_2) }.to not_change { topic.posts.count }.and change {
+ Topic.count
+ }.by(1)
+ end
- expect { process(:apple_mail_attachment) }.to change { topic.posts.count }
- post = topic.posts.last
- upload = post.uploads.first
+ it "creates a new topic when the referenced message id is not known" do
+ IncomingEmail.where(message_id: "[email protected]").update(message_id: "[email protected]")
+ expect { process(:email_reply_2) }.to not_change { topic.posts.count }.and change {
+ Topic.count
+ }.by(1)
+ end
- expect(post.raw).to eq(<<~MD.chomp)
- Picture below.
+ it "includes the sender on the topic when the message id is known, the sender is not known, and the group allows unknown senders to reply to topics" do
+ IncomingEmail.where(message_id: "[email protected]").update(cc_addresses: "[email protected]")
+ group.update(allow_unknown_sender_topic_replies: true)
+ expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1).and not_change {
+ Topic.count
+ }
+ end
- <img apple-inline="yes" id="06C04C58-783E-4753-9B6B-D57403903060" src="#{upload.short_url}" class="">
+ context "when the sender is not in the topic allowed users" do
+ before do
+ user = User.find_by_email("[email protected]")
+ topic.topic_allowed_users.find_by(user: user).destroy
+ end
- Picture above.
- MD
+ it "adds them to the topic at the same time" do
+ IncomingEmail.where(message_id: "[email protected]").update(cc_addresses: "[email protected]")
+ group.update(allow_unknown_sender_topic_replies: true)
+ expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1).and not_change {
+ Topic.count
+ }
+ end
+ end
end
+ end
- it "tries not to repeat duplicate attachments" do
- SiteSetting.authorized_extensions = "jpg"
- SiteSetting.always_show_trimmed_content = true
+ describe "new topic in a category" do
+ fab!(:category) do
+ Fabricate(
+ :category,
+ email_in_allow_strangers: false,
+ )
+ end
- expect { process(:logo_1) }.to change { Upload.count }.by(1)
- logo = Upload.last
- expect(topic.posts.last.raw).to include logo.short_url
+ it "raises a StrangersNotAllowedError when 'email_in_allow_strangers' is disabled" do
+ expect { process(:new_user) }.to raise_error(Email::Receiver::StrangersNotAllowedError)
+ end
- expect { process(:logo_2) }.not_to change { Upload.count }
- expect(topic.posts.last.raw).to include logo.short_url
+ it "raises an InsufficientTrustLevelError when user's trust level isn't enough" do
+ Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])
+ SiteSetting.email_in_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
+ expect { process(:existing_user) }.to raise_error(
+ Email::Receiver::InsufficientTrustLevelError,
+ )
end
- it "works with removed attachments" do
- SiteSetting.authorized_extensions = "jpg"
+ it "works" do
+ handler_calls = 0
+ handler =
+ proc do |topic|
+ expect(topic.incoming_email_addresses).to contain_exactly(
+ )
+ handler_calls += 1
+ end
- expect { process(:removed_attachments) }.to change { topic.posts.count }
- expect(topic.posts.last.uploads).to be_empty
- end
+ DiscourseEvent.on(:topic_created, &handler)
- it "supports eml attachments" do
- SiteSetting.authorized_extensions = "eml"
- expect { process(:attached_eml_file) }.to change { topic.posts.count }
- post = topic.posts.last
- upload = post.uploads.first
+ user =
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
+ )
+ group = Fabricate(:group)
- expect(post.raw).to eq(<<~MD.chomp)
- Please find the eml file attached.
+ group.add(user)
+ group.save
- [details="#{I18n.t("emails.incoming.attachments")}"]
+ category.set_permissions(group => :create_post)
+ category.save!
- #{UploadMarkdown.new(upload).to_markdown}
+ # raises an InvalidAccess when the user doesn't have the privileges to create a topic
+ expect { process(:existing_user) }.to raise_error(Discourse::InvalidAccess)
- [/details]
- MD
- end
+ category.update_columns(email_in_allow_strangers: true)
- it "can decode attachments" do
- SiteSetting.authorized_extensions = "pdf"
- Fabricate(:group, incoming_email: "[email protected]")
+ # allows new user to create a topic
+ expect { process(:new_user) }.to change(Topic, :count)
- process(:encoded_filename)
- expect(Upload.last.original_filename).to eq("This is a test.pdf")
+ DiscourseEvent.off(:topic_created, &handler)
+ expect(handler_calls).to eq(1)
end
- context "when attachment is rejected" do
- it "sends out the warning email" do
- expect { process(:attached_txt_file) }.to change { EmailLog.count }.by(1)
- expect(EmailLog.last.email_type).to eq("email_reject_attachment")
- expect(topic.posts.last.uploads.size).to eq 0
- end
+ it "creates visible topic for ham" do
+ SiteSetting.email_in_spam_header = "none"
- it "doesn't send out the warning email if sender is staged user" do
- user.update_columns(staged: true)
- expect { process(:attached_txt_file) }.not_to change { EmailLog.count }
- expect(topic.posts.last.uploads.size).to eq 0
- end
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
+ )
+ expect { process(:existing_user) }.to change { Topic.count }.by(1) # Topic created
- it "creates the post with attachment missing message" do
- missing_attachment_regex =
- Regexp.escape(I18n.t("emails.incoming.missing_attachment", filename: "text.txt"))
- expect { process(:attached_txt_file) }.to change { topic.posts.count }
- post = topic.posts.last
- expect(post.raw).to match(/#{missing_attachment_regex}/)
- expect(post.uploads.size).to eq 0
- end
+ topic = Topic.last
+ expect(topic.visible).to eq(true)
+
+ post = Post.last
+ expect(post.hidden).to eq(false)
+ expect(post.hidden_at).to eq(nil)
+ expect(post.hidden_reason_id).to eq(nil)
end
- it "supports emails with just an attachment" do
- SiteSetting.authorized_extensions = "pdf"
- expect { process(:attached_pdf_file) }.to change { topic.posts.count }
- post = topic.posts.last
- upload = post.uploads.last
+ it "creates hidden topic for X-Spam-Flag" do
+ SiteSetting.email_in_spam_header = "X-Spam-Flag"
- expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
+ user =
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
+ )
+ expect { process(:spam_x_spam_flag) }.to change { ReviewableQueuedPost.count }.by(1)
+ expect(user.reload.silenced?).to be(true)
end
- it "supports liking via email" do
- expect { process(:like) }.to change(PostAction, :count)
+ it "creates hidden topic for X-Spam-Status" do
+ SiteSetting.email_in_spam_header = "X-Spam-Status"
+
+ user =
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
+ )
+ expect { process(:spam_x_spam_status) }.to change { ReviewableQueuedPost.count }.by(1)
+ expect(user.reload.silenced?).to be(true)
end
- it "ensures posts aren't dated in the future" do
- # PostCreator doesn't provide sub-second accuracy for created_at
- now = freeze_time Time.zone.now.round
+ it "creates hidden topic for X-SES-Spam-Verdict" do
+ SiteSetting.email_in_spam_header = "X-SES-Spam-Verdict"
- expect { process(:from_the_future) }.to change { topic.posts.count }
- expect(topic.posts.last.created_at).to eq_time(now)
+ user =
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
+ )
+ expect { process(:spam_x_ses_spam_verdict) }.to change { ReviewableQueuedPost.count }.by(1)
+ expect(user.reload.silenced?).to be(true)
end
- it "accepts emails with wrong reply key if the system knows about the forwarded email" do
- Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
- Fabricate(
- :incoming_email,
- raw: <<~RAW,
- Return-Path: <[email protected]>
- From: Alice <[email protected]>
- Subject: Hello world
- Date: Fri, 15 Jan 2016 00:12:43 +0100
- Message-ID: <[email protected]>
- Mime-Version: 1.0
- Content-Type: text/plain; charset=UTF-8
- Content-Transfer-Encoding: quoted-printable
+ it "creates hidden topic for failed Authentication-Results header" do
+ SiteSetting.email_in_authserv_id = "example.com"
- This post was created by email.
- RAW
- from_address: "[email protected]",
- topic: topic,
- post: post,
- user: user,
+ user =
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
+ )
+ expect { process(:dmarc_fail) }.to change { ReviewableQueuedPost.count }.by(1)
+ expect(user.reload.silenced?).to be(false)
+ end
+
+ it "adds the 'elided' part of the original message when always_show_trimmed_content is enabled" do
+ SiteSetting.always_show_trimmed_content = true
+
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ trust_level: TrustLevel[2],
+ refresh_auto_groups: true,
)
+ expect { process(:forwarded_email_to_category) }.to change { Topic.count }.by(1) # Topic created
- expect { process(:reply_user_not_matching_but_known) }.to change { topic.posts.count }
+ new_post, = Post.last
+ expect(new_post.raw).to include(
+ "Hi everyone, can you have a look at the email below?",
+ "<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>",
+ "Discoursing much today?",
+ )
end
- it "re-enables user's PM email notifications when user replies to a private topic" do
- topic.update_columns(category_id: nil, archetype: Archetype.private_message)
- topic.allowed_users << user
- topic.save
+ it "works when approving is enabled" do
+ SiteSetting.approve_unless_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
- user.user_option.update_columns(email_messages_level: UserOption.email_level_types[:never])
- expect { process(:reply_user_matching) }.to change { topic.posts.count }
- user.reload
- expect(user.user_option.email_messages_level).to eq(UserOption.email_level_types[:always])
- end
- end
+ Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])
+ Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[4])
- shared_examples "creates topic with forwarded message as quote" do |destination, address|
- it "creates topic with forwarded message as quote" do
- expect { process(:forwarded_email_1) }.to change(Topic, :count)
+ category.set_permissions(Group[:trust_level_4] => :full)
+ category.save!
- topic = Topic.last
- if destination == :category
- expect(topic.category).to eq(Category.where(email_in: address).first)
- else
- expect(topic.archetype).to eq(Archetype.private_message)
- expect(topic.allowed_groups).to eq(Group.where(incoming_email: address))
- end
+ expect { process(:tl3_user) }.to raise_error(Email::Receiver::InvalidPost)
+ expect { process(:tl4_user) }.to change(Topic, :count)
+ end
- post = Post.last
+ it "ignores by case-insensitive title" do
+ SiteSetting.ignore_by_title = "foo"
+ expect { process(:ignored) }.to_not change(Topic, :count)
+ end
- expect(post.user.email).to eq("[email protected]")
- expect(post.raw).to eq(<<~RAW.chomp)
- @team, can you have a look at this email below?
+ it "associates email from a secondary address with user" do
+ user =
+ Fabricate(
+ :user,
+ trust_level: TrustLevel[2],
+ user_emails: [Fabricate.build(:secondary_email, email: "[email protected]")],
+ refresh_auto_groups: true,
+ )
- [quote]
- From: Some One &lt;some@one\\.com&gt;
- To: Ba Bar &lt;ba@bar\\.com&gt;
- Date: Mon, 1 Dec 2016 00:13:37 \\+0100
- Subject: Discoursing much?
+ expect { process(:existing_user) }.to change(Topic, :count).by(1)
- Hello Ba Bar,
+ topic = Topic.last
- Discoursing much today?
+ expect(topic.posts.last.raw).to eq("Hey, this is a topic from an existing user ;)")
- XoXo
- [/quote]
- RAW
+ expect(topic.user).to eq(user)
end
end
- describe "new message to a group" do
- fab!(:group) { Fabricate(:group, incoming_email: "[email protected]|[email protected]") }
-
- it "handles encoded display names" do
- expect { process(:encoded_display_name) }.to change(Topic, :count)
-
- topic = Topic.last
- expect(topic.title).to eq("I need help")
- expect(topic.private_message?).to eq(true)
- expect(topic.allowed_groups).to include(group)
+ describe "reply to a category email address" do
+ fab!(:category) { Fabricate(:category, email_in: "[email protected]") }
+ fab!(:user1) do
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ refresh_auto_groups: true,
+ trust_level: TrustLevel[2],
+ )
+ end
+ fab!(:user2) do
+ Fabricate(
+ :user,
+ email: "[email protected]",
+ refresh_auto_groups: true,
+ trust_level: TrustLevel[2],
+ )
+ end
+ fab!(:topic) { create_topic(category: category, user: user1) }
+ let(:post) { create_post(topic: topic, user: user1) }
+ let(:reply_email) do
+ email(:reply_to_category_address)
+ .gsub("SUBJECT", "Re: #{topic.title}")
+ .gsub("REFERENCE", "<discourse/post/#{post.id}@#{Discourse.current_hostname}>")
+ .gsub("DATE", Time.now.to_s)
+ end
- user = topic.user
- expect(user.staged).to eq(true)
- expect(user.username).to eq("user1")
- expect(user.name).to eq("Случайная Имя")
+ it "works when reply_by_email is enabled" do
+ configure_reply_by_email
+ expect { Email::Receiver.new(reply_email).process! }.to change { topic.posts.count }
end
- it "handles email with no subject" do
- expect { process(:no_subject) }.to change(Topic, :count)
- expect(Topic.last.title).to eq("This topic needs a title")
+ it "works when reply_by_email is enabled and the category mirrors a mailing list" do
+ configure_reply_by_email
+ category.update!(mailinglist_mirror: true)
+ expect { Email::Receiver.new(reply_email).process! }.to change { topic.posts.count }
end
- it "invites everyone in the chain but emails configured as 'incoming' (via reply, group or category)" do
- expect { process(:cc) }.to change(Topic, :count)
+ it "works when the category mirrors a mailing list" do
+ category.update!(mailinglist_mirror: true)
+ expect { Email::Receiver.new(reply_email).process! }.to change { topic.posts.count }
+ end
- topic = Topic.last
+ it "does not create a reply" do
+ expect { Email::Receiver.new(reply_email).process! }.to raise_error(
+ Email::Receiver::ReplyNotAllowedError,
+ )
+ end
+ end
- emails = topic.allowed_users.joins(:user_emails).pluck(:"user_emails.email")
- expect(emails).to contain_exactly("[email protected]", "[email protected]", "[email protected]")
+ describe "new topic in a category that allows strangers" do
+ fab!(:category) do
+ Fabricate(
+ :category,
+ email_in_allow_strangers: true,
+ )
+ end
- expect(topic.topic_users.count).to eq(3)
+ it "lets an email in from a stranger" do
+ expect { process(:new_user) }.to change(Topic, :count)
end
- it "invites users with a secondary email in the chain" do
- user1 =
- Fabricate(
- :user,
- trust_level: TrustLevel[2],
- user_emails: [
- Fabricate.build(:secondary_email, email: "[email protected]"),
- Fabricate.build(:secondary_email, email: "[email protected]"),
- ],
- refresh_auto_groups: true,
- )
+ it "raises an UserNotFoundError if enable_staged_users is false " do
+ SiteSetting.enable_staged_users = false
+ expect { process(:new_user) }.to raise_error(Email::Receiver::UserNotFoundError)
+ end
- user2 =
- Fabricate(
- :user,
- trust_level: TrustLevel[2],
- user_emails: [
- Fabricate.build(:secondary_email, email: "[email protected]"),
- Fabricate.build(:secondary_email, email: "[email protected]"),
- ],
- refresh_auto_groups: true,
- )
+ it "uses system user if enable_staged_users is false and fallback_to_system_user_for_category_emails is true" do
+ SiteSetting.enable_staged_users = false
+ SiteSetting.email_in_allow_system_user_fallback = true
+ expect { process(:new_user) }.to change(Topic, :count)
+ expect(Topic.last.user).to eq(Discourse.system_user)
+ end
- expect { process(:cc) }.to change(Topic, :count)
- expect(Topic.last.allowed_users).to contain_exactly(user1, user2)
+ it "lets an email in from a high-TL user" do
+ Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[4])
+ expect { process(:tl4_user) }.to change(Topic, :count)
end
- it "cap the number of staged users created per email" do
- SiteSetting.maximum_staged_users_per_email = 1
- expect { process(:cc) }.to change(Topic, :count).by(1).and change(User, :count).by(1)
- expect(Topic.last.ordered_posts[-1].post_type).to eq(Post.types[:moderator_action])
+ it "fails on email from a low-TL user" do
+ SiteSetting.email_in_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
+ Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])
+ expect { process(:tl3_user) }.to raise_error(Email::Receiver::InsufficientTrustLevelError)
end
+ end
- it "cap the number of staged users existing per email" do
- Fabricate(:user, email: "[email protected]", staged: true) # from
- Fabricate(:user, email: "[email protected]", staged: true) # to
+ describe "#reply_by_email_address_regex" do
+ before do
+ SiteSetting.reply_by_email_address = ""
+ SiteSetting.alternative_reply_by_email_addresses = ""
+ end
- SiteSetting.maximum_staged_users_per_email = 1
- expect { process(:cc) }.to change(Topic, :count).and not_change(User, :count)
- expect(Topic.last.ordered_posts[-1].post_type).to eq(Post.types[:moderator_action])
+ it "it matches nothing if there is not reply_by_email_address" do
+ expect(Email::Receiver.reply_by_email_address_regex).to eq(/$a/)
end
- it "rejects messages with too many recipients" do
- SiteSetting.maximum_recipients_per_new_group_email = 3
- expect { process(:cc) }.to raise_error(Email::Receiver::TooManyRecipientsError)
+ it "uses 'reply_by_email_address' site setting" do
+ SiteSetting.reply_by_email_address = "foo+%{reply_key}@bar.com"
+ expect(Email::Receiver.reply_by_email_address_regex).to eq(/foo\+?(\h{32})?@bar\.com/)
end
- it "uses the incoming_email message-id as the new post's outbound_message_id" do
- expect { process(:cc) }.to change(Topic, :count)
- message_id = IncomingEmail.last.message_id
- expect(Topic.last.first_post.outbound_message_id).to eq(message_id)
+ it "uses 'alternative_reply_by_email_addresses' site setting" do
+ SiteSetting.alternative_reply_by_email_addresses = "alt.foo+%{reply_key}@bar.com"
+ expect(Email::Receiver.reply_by_email_address_regex).to eq(/alt\.foo\+?(\h{32})?@bar\.com/)
end
- describe "reply-to header" do
- before { SiteSetting.block_auto_generated_emails = false }
+ it "combines both 'reply_by_email' settings" do
+ SiteSetting.reply_by_email_address = "foo+%{reply_key}@bar.com"
+ SiteSetting.alternative_reply_by_email_addresses = "alt.foo+%{reply_key}@bar.com"
+ expect(Email::Receiver.reply_by_email_address_regex).to eq(
+ /foo\+?(\h{32})?@bar\.com|alt\.foo\+?(\h{32})?@bar\.com/,
+ )
+ end
+ end
- it "extracts address and uses it for comparison" do
- expect { process(:reply_to_whitespaces) }.to change(Topic, :count).by(1)
- incoming = IncomingEmail.find_by(message_id: "[email protected]")
- expect(incoming.from_address).to eq("[email protected]")
- expect(User.last.email).to eq("[email protected]")
- end
+ describe "#check_address" do
+ context "when e-mail is associated with a reply key" do
+ before { SiteSetting.reply_by_email_address = "foo+%{reply_key}@bar.com" }
- it "handles emails where there is a Reply-To address, using that instead of the from address, if X-Original-From is present" do
- expect { process(:reply_to_different_to_from) }.to change(Topic, :count).by(1)
- incoming = IncomingEmail.find_by(message_id: "[email protected]")
- expect(incoming.from_address).to eq("[email protected]")
- expect(User.last.email).to eq("[email protected]")
+ it "returns nil when the key is invalid" do
+ expect(Email::Receiver.check_address("[email protected]")).to be_nil
+ expect(
+ Email::Receiver.check_address("[email protected]"),
+ ).to be_nil
end
- it "allows for quotes around the display name of the Reply-To address" do
- expect { process(:reply_to_different_to_from_quoted_display_name) }.to change(
- Topic,
- :count,
- ).by(1)
- incoming = IncomingEmail.find_by(message_id: "[email protected]")
- expect(incoming.from_address).to eq("[email protected]")
- expect(User.last.email).to eq("[email protected]")
- end
+ context "with a valid reply" do
+ it "returns the destination when the key is valid" do
+ post_reply_key = Fabricate(:post_reply_key, reply_key: "4f97315cc828096c9cb34c6f1a0d6fe8")
- it "does not use the reply-to address if an X-Original-From header is not present" do
- expect { process(:reply_to_different_to_from_no_x_original) }.to change(Topic, :count).by(1)
- incoming = IncomingEmail.find_by(message_id: "[email protected]")
- expect(incoming.from_address).to eq("[email protected]")
- expect(User.last.email).to eq("[email protected]")
- end
+ dest = Email::Receiver.check_address("[email protected]")
- it "does not use the reply-to address if the X-Original-From header is different from the reply-to address" do
- expect { process(:reply_to_different_to_from_x_original_different) }.to change(
- Topic,
- :count,
- ).by(1)
- incoming = IncomingEmail.find_by(message_id: "[email protected]")
- expect(incoming.from_address).to eq("[email protected]")
- expect(User.last.email).to eq("[email protected]")
+ expect(dest).to eq(post_reply_key)
+ end
end
end
- describe "when 'find_related_post_with_key' is disabled" do
- before { SiteSetting.find_related_post_with_key = false }
+ context "when address is associated with a category" do
+ fab!(:category) { Fabricate(:category, email_in: "[email protected]") }
- it "associates email replies using both 'In-Reply-To' and 'References' headers" do
- expect { process(:email_reply_1) }.to change(Topic, :count).by(1) &
- change(Post, :count).by(3) & change(User, :count).by(3)
+ it "returns the destination category" do
+ dest = described_class.check_address("[email protected]")
- topic = Topic.last
- users = User.last(3)
- ordered_posts = topic.ordered_posts
- expect(ordered_posts.size).to eq(3)
+ expect(dest).to eq(category)
+ end
+ end
+ end
- expect(ordered_posts.first.raw).to eq("This is email reply **1**.")
+ describe "staged users" do
+ before { SiteSetting.enable_staged_users = true }
- ordered_posts[1..-1].each do |post|
- expect(post.action_code).to eq("invited_user")
- expect(post.user.email).to eq("[email protected]")
+ shared_examples "does not create staged users" do |email_name, expected_exception|
+ it "does not create staged users" do
+ staged_user_count = User.where(staged: true).count
+ User.expects(:create).never
+ User.expects(:create!).never
- expect(users.map(&:username)).to include(post.custom_fields["action_code_who"])
+ if expected_exception
+ expect { process(email_name) }.to raise_error(expected_exception)
+ else
+ process(email_name)
end
- expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1)
- expect { process(:email_reply_3) }.to change { topic.posts.count }.by(1)
- ordered_posts[1..-1].each(&:trash!)
- expect { process(:email_reply_4) }.to change { topic.posts.count }.by(1)
+ expect(User.where(staged: true).count).to eq(staged_user_count)
end
+ end
- describe "replying with various message-id formats using In-Reply-To header" do
- let!(:topic) do
- process(:email_reply_1)
- Topic.last
- end
- let!(:post) { Fabricate(:post, topic: topic) }
+ shared_examples "cleans up staged users" do |email_name, expected_exception|
+ it "cleans up staged users" do
+ staged_user_count = User.where(staged: true).count
+ expect { process(email_name) }.to raise_error(expected_exception)
+ expect(User.where(staged: true).count).to eq(staged_user_count)
+ end
+ end
- def process_mail_with_message_id(message_id)
- mail_string = <<~EMAIL
- Return-Path: <[email protected]>
- From: Two <[email protected]>
- Subject: RE: Testing email threading
- Date: Fri, 15 Jan 2016 00:12:43 +0100
- Message-ID: <[email protected]>
- In-Reply-To: <#{message_id}>
- Mime-Version: 1.0
- Content-Type: text/plain
- Content-Transfer-Encoding: 7bit
+ context "when email address is screened" do
+ before { ScreenedEmail.expects(:should_block?).with("[email protected]").returns(true) }
- This is email reply testing with Message-ID formats.
- EMAIL
- Email::Receiver.new(mail_string).process!
- end
+ include_examples "does not create staged users",
+ :screened_email,
+ Email::Receiver::ScreenedEmailError
+ end
- it "posts a reply using a message-id in the format discourse/post/POST_ID@HOST" do
- expect {
- process_mail_with_message_id("discourse/post/#{post.id}@test.localhost")
- }.to change { Post.count }.by(1)
- expect(topic.reload.posts.last.raw).to include(
- "This is email reply testing with Message-ID formats",
- )
- end
- end
+ context "when the mail is auto generated" do
+ include_examples "does not create staged users",
+ :auto_generated_header,
+ Email::Receiver::AutoGeneratedEmailError
end
- it "supports any kind of attachments when 'allow_all_attachments_for_group_messages' is enabled" do
- SiteSetting.allow_all_attachments_for_group_messages = true
- expect { process(:attached_rb_file) }.to change(Topic, :count)
+ context "when email is a bounced email" do
+ include_examples "does not create staged users",
+ :bounced_email,
+ Email::Receiver::BouncedEmailError
+ end
- post = Topic.last.first_post
- upload = post.uploads.first
+ context "when the body is blank" do
+ include_examples "does not create staged users",
+ :no_body,
+ Email::Receiver::NoBodyDetectedError
+ end
- expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
+ context "when unsubscribe via email is not allowed" do
+ include_examples "does not create staged users",
+ :unsubscribe_new_user,
+ Email::Receiver::UnsubscribeNotAllowed
end
- it "reenables user's PM email notifications when user emails new topic to group" do
- user = Fabricate(:user, email: "[email protected]")
- user.user_option.update_columns(email_messages_level: UserOption.email_level_types[:never])
- expect { process(:group_existing_user) }.to change(Topic, :count)
- user.reload
- expect(user.user_option.email_messages_level).to eq(UserOption.email_level_types[:always])
+ context "when From email address is not on allowlist" do
+ before do
+ SiteSetting.allowed_email_domains = "example.com|bar.com"
+ Fabricate(:group, incoming_email: "[email protected]")
+ end
+
+ include_examples "does not create staged users",
+ :blocklist_allowlist_email,
+ Email::Receiver::EmailNotAllowed
end
- context "with forwarded emails behaviour set to create replies" do
+ context "when From email address is on blocklist" do
before do
+ SiteSetting.blocked_email_domains = "email.com|mail.com"
Fabricate(:group, incoming_email: "[email protected]")
- SiteSetting.forwarded_emails_behaviour = "create_replies"
end
- it "handles forwarded emails" do
- expect { process(:forwarded_email_1) }.to change(Topic, :count)
+ include_examples "does not create staged users",
+ :blocklist_allowlist_email,
+ Email::Receiver::EmailNotAllowed
+ end
- forwarded_post, last_post = *Post.last(2)
+ context "with blocklist and allowlist for To and Cc" do
+ before { Fabricate(:group, incoming_email: "[email protected]") }
- expect(forwarded_post.user.email).to eq("[email protected]")
- expect(last_post.user.email).to eq("[email protected]")
+ it "does not create staged users for email addresses not on allowlist" do
+ SiteSetting.allowed_email_domains = "mail.com|example.com"
+ process(:blocklist_allowlist_email)
- expect(forwarded_post.raw).to match(/XoXo/)
- expect(last_post.raw).to match(/can you have a look at this email below/)
+ expect(User.find_by_email("[email protected]")).to be_nil
+ expect(User.find_by_email("[email protected]")).to be_nil
+ expect(User.find_by_email("[email protected]")).to be_present
+ end
- expect(last_post.post_type).to eq(Post.types[:regular])
+ it "does not create staged users for email addresses on blocklist" do
+ SiteSetting.blocked_email_domains = "email.com|foo.com"
+ process(:blocklist_allowlist_email)
+
+ expect(User.find_by_email("[email protected]")).to be_nil
+ expect(User.find_by_email("[email protected]")).to be_nil
+ expect(User.find_by_email("[email protected]")).to be_present
end
+ end
- it "handles weirdly forwarded emails" do
- group.add(Fabricate(:user, email: "[email protected]"))
- group.save
+ context "when destinations aren't matching any of the incoming emails" do
+ include_examples "does not create staged users",
+ :bad_destinations,
+ Email::Receiver::BadDestinationAddress
+ end
- SiteSetting.forwarded_emails_behaviour = "create_replies"
- expect { process(:forwarded_email_2) }.to change(Topic, :count)
+ context "when email is sent to category" do
+ context "when email is sent by a new user and category does not allow strangers" do
+ fab!(:category) do
+ Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: false)
+ end
- forwarded_post, last_post = *Post.last(2)
+ include_examples "does not create staged users",
+ :new_user,
+ Email::Receiver::StrangersNotAllowedError
+ end
- expect(forwarded_post.user.email).to eq("[email protected]")
- expect(last_post.user.email).to eq("[email protected]")
+ context "when email has no date" do
+ fab!(:category) do
+ Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: true)
+ end
- expect(forwarded_post.raw).to match(/XoXo/)
- expect(last_post.raw).to match(/can you have a look at this email below/)
+ it "includes the translated string in the error" do
+ expect { process(:no_date) }.to raise_error(Email::Receiver::InvalidPost).with_message(
+ I18n.t("system_messages.email_reject_invalid_post_specified.date_invalid"),
+ )
+ end
- expect(last_post.post_type).to eq(Post.types[:whisper])
+ include_examples "does not create staged users", :no_date, Email::Receiver::InvalidPost
end
- # Who thought this was a good idea?!
- it "doesn't blow up with localized email headers" do
- expect { process(:forwarded_email_3) }.to change(Topic, :count)
+ context "with forwarded emails behaviour set to quote" do
+ before { SiteSetting.forwarded_emails_behaviour = "quote" }
+
+ context "with a category which allows strangers" do
+ fab!(:category) do
+ Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: true)
+ end
+ include_examples "creates topic with forwarded message as quote",
+ :category,
+ end
+
+ context "with a category which doesn't allow strangers" do
+ fab!(:category) do
+ Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: false)
+ end
+ include_examples "cleans up staged users",
+ :forwarded_email_1,
+ Email::Receiver::StrangersNotAllowedError
+ end
end
+ end
- it "adds a small action post to explain who forwarded the email when the sender didn't write anything" do
- expect { process(:forwarded_email_4) }.to change(Topic, :count)
+ context "when email is a reply" do
+ before { configure_reply_by_email }
- forwarded_post, last_post = *Post.last(2)
+ let(:reply_key) { "4f97315cc828096c9cb34c6f1a0d6fe8" }
+ fab!(:category)
+ fab!(:user) { Fabricate(:user, email: "[email protected]", refresh_auto_groups: true) }
+ fab!(:user2) { Fabricate(:user, email: "[email protected]") }
+ fab!(:topic) { create_topic(category: category, user: user) }
+ fab!(:post) { create_post(topic: topic, user: user) }
- expect(forwarded_post.user.email).to eq("[email protected]")
- expect(forwarded_post.raw).to match(/XoXo/)
+ let!(:post_reply_key) do
+ Fabricate(:post_reply_key, reply_key: reply_key, user: user, post: post)
+ end
- expect(last_post.user.email).to eq("[email protected]")
- expect(last_post.post_type).to eq(Post.types[:small_action])
- expect(last_post.action_code).to eq("forwarded")
+ context "when the email address isn't matching the one we sent the notification to" do
+ include_examples "does not create staged users",
+ :reply_user_not_matching,
+ Email::Receiver::ReplyUserNotMatchingError
end
- end
- context "with forwarded emails behaviour set to quote" do
- before { SiteSetting.forwarded_emails_behaviour = "quote" }
+ context "with forwarded emails behaviour set to create replies" do
+ before { SiteSetting.forwarded_emails_behaviour = "create_replies" }
- include_examples "creates topic with forwarded message as quote",
- :group,
+ context "when a reply contains a forwarded email" do
+ include_examples "does not create staged users", :reply_and_forwarded
+ end
+
+ context "with forwarded email to category that doesn't allow strangers" do
+ before { category.update!(email_in: "[email protected]", email_in_allow_strangers: false) }
+
+ include_examples "cleans up staged users",
+ :forwarded_email_1,
+ Email::Receiver::StrangersNotAllowedError
+ end
+ end
end
- context "when a reply is sent to a group's email_username" do
+ context "when replying without key is allowed" do
+ fab!(:group) { Fabricate(:group, incoming_email: "[email protected]") }
let!(:topic) do
- group.update(email_username: "[email protected]")
+ configure_reply_by_email
+ SiteSetting.find_related_post_with_key = false
process(:email_reply_1)
Topic.last
end
- it "does not invite the group email_username as a staged user" do
- process(:email_reply_to_group_email_username)
- expect(User.find_by_email("[email protected]")).to eq(nil)
- end
+ context "when the topic was deleted" do
+ before { topic.update_columns(deleted_at: 1.day.ago) }
- it "creates the reply when the sender and referenced message id are known" do
- expect { process(:email_reply_to_group_email_username) }.to change { topic.posts.count }.by(
- 1,
- ).and not_change { Topic.count }
+ include_examples "cleans up staged users",
+ :email_reply_staged,
+ Email::Receiver::TopicNotFoundError
end
- end
- context "when a group forwards an email to its inbox" do
- before do
- group.update!(
- email_username: "[email protected]",
- smtp_server: "smtp.test.com",
- smtp_port: 587,
- smtp_ssl_mode: Group.smtp_ssl_modes[:starttls],
- smtp_enabled: true,
- )
+ context "when the topic was closed" do
+ before { topic.update_columns(closed: true) }
+
+ include_examples "cleans up staged users",
+ :email_reply_staged,
+ Email::Receiver::TopicClosedError
end
- it "does not use the team's address as the from_address; it uses the original sender address" do
- process(:forwarded_by_group_to_inbox)
- topic = Topic.last
- expect(topic.incoming_email.first.to_addresses).to include("[email protected]")
- expect(topic.incoming_email.first.from_address).to eq("[email protected]")
+ context "when they aren't allowed to like a post" do
+ before { topic.update_columns(archived: true) }
+
+ include_examples "cleans up staged users",
+ :email_reply_like,
+ Email::Receiver::InvalidPostAction
end
+ end
- context "with forwarded emails behaviour set to create replies" do
- before { SiteSetting.forwarded_emails_behaviour = "create_replies" }
+ it "does not remove the incoming email record when staged users are deleted" do
+ expect { process(:bad_destinations) }.to change { IncomingEmail.count }.and raise_error(
+ Email::Receiver::BadDestinationAddress,
+ )
+ expect(IncomingEmail.last.message_id).to eq("[email protected]")
+ end
+ end
- it "does not use the team's address as the from_address; it uses the original sender address" do
- process(:forwarded_by_group_to_inbox)
- topic = Topic.last
- expect(topic.incoming_email.first.to_addresses).to include("[email protected]")
- expect(topic.incoming_email.first.from_address).to eq("[email protected]")
- end
+ describe "mailman mirror" do
+ fab!(:category, :mailinglist_mirror_category)
- it "does not say the email was forwarded by the original sender, it says the email is forwarded by the group" do
- expect { process(:forwarded_by_group_to_inbox) }.to change {
- User.where(staged: true).count
- }.by(4)
- topic = Topic.last
- forwarded_small_post = topic.ordered_posts.last
- expect(forwarded_small_post.action_code).to eq("forwarded")
- expect(forwarded_small_post.user).to eq(User.find_by_email("[email protected]"))
- end
+ it "uses 'from' email address" do
+ expect { process(:mailman_1) }.to change { Topic.count }
+ user = Topic.last.user
+ expect(user.email).to eq("[email protected]")
+ expect(user.name).to eq("Some One")
+ end
- it "keeps track of the cc addresses of the forwarded email and creates staged users for them" do
- expect { process(:forwarded_by_group_to_inbox) }.to change {
- User.where(staged: true).count
- }.by(4)
- topic = Topic.last
- cc_user1 = User.find_by_email("[email protected]")
- cc_user2 = User.find_by_email("[email protected]")
- fred_user = User.find_by_email("[email protected]")
- team_user = User.find_by_email("[email protected]")
- expect(topic.incoming_email.first.cc_addresses).to eq("[email protected];[email protected]")
- expect(topic.topic_allowed_users.pluck(:user_id)).to match_array(
- [fred_user.id, team_user.id, cc_user1.id, cc_user2.id],
- )
- end
+ it "uses 'reply-to' email address" do
+ expect { process(:mailman_2) }.to change { Topic.count }
+ user = Topic.last.user
+ expect(user.email).to eq("[email protected]")
+ expect(user.name).to eq("Some")
+ end
- it "keeps track of the cc addresses of the final forwarded email as well" do
- expect { process(:forwarded_by_group_to_inbox_double_cc) }.to change {
- User.where(staged: true).count
- }.by(5)
- topic = Topic.last
- cc_user1 = User.find_by_email("[email protected]")
- cc_user2 = User.find_by_email("[email protected]")
- fred_user = User.find_by_email("[email protected]")
- team_user = User.find_by_email("[email protected]")
- someother_user = User.find_by_email("[email protected]")
- expect(topic.incoming_email.first.cc_addresses).to eq(
- )
- expect(topic.topic_allowed_users.pluck(:user_id)).to match_array(
- [fred_user.id, team_user.id, someother_user.id, cc_user1.id, cc_user2.id],
- )
- end
+ it "uses 'x-mailfrom' email address and name from CC" do
+ expect { process(:mailman_3) }.to change { Topic.count }
+ user = Topic.last.user
+ expect(user.email).to eq("[email protected]")
+ expect(user.name).to eq("Some One")
+ end
- context "when staged user for the team email already exists" do
- let!(:staged_team_user) do
- User.create!(
- email: "[email protected]",
- username: UserNameSuggester.suggest("[email protected]"),
- name: "team teamson",
- staged: true,
- )
- end
+ it "uses 'x-original-from' email address" do
+ expect { process(:mailman_4) }.to change { Topic.count }
+ user = Topic.last.user
+ expect(user.email).to eq("[email protected]")
+ expect(user.name).to eq("Some")
+ end
+ end
- it "uses that and does not create another staged user" do
- expect { process(:forwarded_by_group_to_inbox) }.to change {
- User.where(staged: true).count
- }.by(3)
- topic = Topic.last
- forwarded_small_post = topic.ordered_posts.last
- expect(forwarded_small_post.action_code).to eq("forwarded")
- expect(forwarded_small_post.user).to eq(staged_team_user)
- end
- end
- end
+ describe "mailing list mirror" do
+ fab!(:category, :mailinglist_mirror_category)
+
+ before { SiteSetting.block_auto_generated_emails = true }
+
+ it "should allow creating topic even when email is autogenerated" do
+ expect { process(:mailinglist) }.to change { Topic.count }
+ expect(IncomingEmail.last.is_auto_generated).to eq(false)
end
- context "when emailing a group by email_username and following reply flow" do
- let!(:original_inbound_email_topic) do
- group.update!(
- email_username: "[email protected]",
- smtp_server: "smtp.test.com",
- smtp_port: 587,
- smtp_ssl_mode: Group.smtp_ssl_modes[:starttls],
- smtp_enabled: true,
- )
- process(:email_to_group_email_username_1)
- Topic.last
- end
- fab!(:user_in_group) do
- u = Fabricate(:user)
- Fabricate(:group_user, user: u, group: group)
- u
- end
+ it "should allow replying without reply key" do
+ process(:mailinglist)
+ topic = Topic.last
+
+ expect { process(:mailinglist_reply) }.to change { topic.posts.count }
+ end
+
+ it "should skip validations for staged users" do
+ Fabricate(:user, email: "[email protected]", staged: true)
+ expect { process(:mailinglist_short_message) }.to change { Topic.count }
+ end
+ it "should skip validations for regular users" do
+ Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
+ expect { process(:mailinglist_short_message) }.to change { Topic.count }
+ end
+
+ context "with read-only category" do
before do
- NotificationEmailer.enable
- SiteSetting.disallow_reply_by_email_after_days = 10_000
- Jobs.run_immediately!
- end
+ category.set_permissions(everyone: :readonly)
+ category.save!
- def reply_as_group_user
- group_post =
- PostCreator.create(
- user_in_group,
- raw: "Thanks for your request. Please try to restart.",
- topic_id: original_inbound_email_topic.id,
- )
- email_log = EmailLog.last
- [email_log, group_post]
+ Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
+ Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
end
- it "the inbound processed email creates an incoming email and topic record correctly, and adds the group to the topic" do
- incoming = IncomingEmail.find_by(topic: original_inbound_email_topic)
- user = User.find_by_email("[email protected]")
- expect(original_inbound_email_topic.topic_allowed_users.first.user_id).to eq(user.id)
- expect(original_inbound_email_topic.topic_allowed_groups.first.group_id).to eq(group.id)
- expect(incoming.to_addresses).to eq("[email protected]")
- expect(incoming.from_address).to eq("[email protected]")
- expect(incoming.message_id).to eq("[email protected]")
+ it "should allow creating topic within read-only category" do
+ expect { process(:mailinglist) }.to change { Topic.count }
end
- it "creates an EmailLog when someone from the group replies, and does not create an IncomingEmail record for the reply" do
- email_log, group_post = reply_as_group_user
- expect(email_log.message_id).to eq("discourse/post/#{group_post.id}@test.localhost")
- expect(email_log.to_address).to eq("[email protected]")
- expect(email_log.email_type).to eq("user_private_message")
- expect(email_log.post_id).to eq(group_post.id)
- expect(IncomingEmail.exists?(post_id: group_post.id)).to eq(false)
+ it "should allow replying within read-only category" do
+ process(:mailinglist)
+ topic = Topic.last
+
+ expect { process(:mailinglist_reply) }.to change { topic.posts.count }
end
+ end
- it "processes a reply from the OP user to the group SMTP username, linking the reply_to_post_number correctly by matching in_reply_to to the email log" do
- email_log, group_post = reply_as_group_user
+ it "ignores unsubscribe email" do
+ SiteSetting.unsubscribe_via_email = true
+ Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
- reply_email = email(:email_to_group_email_username_2)
- reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
- expect { Email::Receiver.new(reply_email).process! }.to not_change {
- Topic.count
- }.and change { Post.count }.by(1)
+ expect { process("mailinglist_unsubscribe") }.to_not change {
+ ActionMailer::Base.deliveries.count
+ }
+ end
- reply_post = Post.last
- expect(reply_post.reply_to_user).to eq(user_in_group)
- expect(reply_post.reply_to_post_number).to eq(group_post.post_number)
- end
+ it "ignores dmarc fails" do
+ expect { process("mailinglist_dmarc_fail") }.to change { Topic.count }
- it "handles multiple message IDs in the in_reply_to header by only using the first one" do
- email_log, group_post = reply_as_group_user
+ post = Topic.last.first_post
+ expect(post.hidden).to eq(false)
+ expect(post.hidden_reason_id).to be_nil
+ end
+ end
- reply_email = email(:email_to_group_email_username_3)
- reply_email.gsub!(
- "MESSAGE_ID_REPLY_TO",
- "<#{email_log.message_id}> <test/message/[email protected]>",
- )
- expect { Email::Receiver.new(reply_email).process! }.to not_change {
- Topic.count
- }.and change { Post.count }.by(1)
+ it "tries to fix unparsable email addresses in To and CC headers" do
+ expect { process(:unparsable_email_addresses) }.to raise_error(
+ Email::Receiver::BadDestinationAddress,
+ )
- reply_post = Post.last
- expect(reply_post.reply_to_user).to eq(user_in_group)
- expect(reply_post.reply_to_post_number).to eq(group_post.post_number)
- end
+ email = IncomingEmail.last
+ expect(email.to_addresses).to eq("[email protected]")
+ expect(email.cc_addresses).to eq("[email protected];[email protected]")
+ end
- it "processes the reply from the user as a brand new topic if they have replied from a different address (e.g. auto forward) and allow_unknown_sender_topic_replies is disabled" do
- email_log, _group_post = reply_as_group_user
+ describe "#select_body" do
+ let(:email) { <<~EMAIL }
+ MIME-Version: 1.0
+ Date: Tue, 01 Jan 2019 00:00:00 +0300
+ Subject: An email with whitespaces
+ From: Foo <[email protected]>
+ Content-Type: text/plain; charset="UTF-8"
- reply_email = email(:email_to_group_email_username_2_as_unknown_sender)
- reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
- expect { Email::Receiver.new(reply_email).process! }.to change { Topic.count }.by(
- 1,
- ).and change { Post.count }.by(1)
+ This is a line that will be stripped
+ This is another line that will be stripped
- reply_post = Post.last
- expect(reply_post.topic_id).not_to eq(original_inbound_email_topic.id)
- end
+ This is a line that will not be touched.
+ This is another line that will not be touched.
- it "processes the reply from the user as a reply if they have replied from a different address (e.g. auto forward) and allow_unknown_sender_topic_replies is enabled" do
- group.update!(allow_unknown_sender_topic_replies: true)
- email_log, _group_post = reply_as_group_user
+ * list
- reply_email = email(:email_to_group_email_username_2_as_unknown_sender)
- reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
- expect { Email::Receiver.new(reply_email).process! }.to not_change {
- Topic.count
- }.and change { Post.count }.by(1)
+ * sub-list
- reply_post = Post.last
- expect(reply_post.topic_id).to eq(original_inbound_email_topic.id)
- end
+ - list
- it "creates a new topic with a reference back to the original if replying to a too old topic" do
- SiteSetting.disallow_reply_by_email_after_days = 2
- email_log, group_post = reply_as_group_user
+ - sub-list
- group_post.update(created_at: 10.days.ago)
- group_post.topic.update(created_at: 10.days.ago)
+ + list
- reply_email = email(:email_to_group_email_username_2)
- reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
- expect { Email::Receiver.new(reply_email).process! }.to change { Topic.count }.by(
- 1,
- ).and change { Post.count }.by(1)
+ + sub-list
- reply_post = Post.last
- new_topic = Topic.last
+ [code]
+ 1.upto(10).each do |i|
+ puts i
+ end
- expect(reply_post.topic).to eq(new_topic)
- expect(reply_post.raw).to include(
- I18n.t(
- "emails.incoming.continuing_old_discussion",
- url: group_post.topic.url,
- title: group_post.topic.title,
- count: SiteSetting.disallow_reply_by_email_after_days,
- ),
- )
- end
- end
+ ```
+ # comment
+ [/code]
- context "when message sent to a group has no key and find_related_post_with_key is enabled" do
- let!(:topic) do
- process(:email_reply_1)
- Topic.last
- end
+ This is going to be stripped too.
- it "creates a reply when the sender and referenced message id are known" do
- expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1).and not_change {
- Topic.count
- }
- end
+ ```
+ 1.upto(10).each do |i|
+ puts i
+ end
- it "creates a new topic when the sender is not known and the group does not allow unknown senders to reply to topics" do
- IncomingEmail.where(message_id: "[email protected]").update(cc_addresses: "[email protected]")
- group.update(allow_unknown_sender_topic_replies: false)
- expect { process(:email_reply_2) }.to not_change { topic.posts.count }.and change {
- Topic.count
- }.by(1)
- end
+ [/code]
+ # comment
+ ```
- it "creates a new topic when the referenced message id is not known" do
- IncomingEmail.where(message_id: "[email protected]").update(message_id: "[email protected]")
- expect { process(:email_reply_2) }.to not_change { topic.posts.count }.and change {
- Topic.count
- }.by(1)
- end
+ This is going to be stripped too.
- it "includes the sender on the topic when the message id is known, the sender is not known, and the group allows unknown senders to reply to topics" do
- IncomingEmail.where(message_id: "[email protected]").update(cc_addresses: "[email protected]")
- group.update(allow_unknown_sender_topic_replies: true)
- expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1).and not_change {
- Topic.count
- }
- end
+ Bye!
+ EMAIL
- context "when the sender is not in the topic allowed users" do
- before do
- user = User.find_by_email("[email protected]")
- topic.topic_allowed_users.find_by(user: user).destroy
- end
+ let(:stripped_text) { <<~MD }
+ This is a line that will be stripped
+ This is another line that will be stripped
- it "adds them to the topic at the same time" do
- IncomingEmail.where(message_id: "[email protected]").update(cc_addresses: "[email protected]")
- group.update(allow_unknown_sender_topic_replies: true)
- expect { process(:email_reply_2) }.to change { topic.posts.count }.by(1).and not_change {
- Topic.count
- }
- end
- end
- end
- end
+ This is a line that will not be touched.
+ This is another line that will not be touched.
- describe "new topic in a category" do
- fab!(:category) do
- Fabricate(
- :category,
- email_in_allow_strangers: false,
- )
- end
+ * list
- it "raises a StrangersNotAllowedError when 'email_in_allow_strangers' is disabled" do
- expect { process(:new_user) }.to raise_error(Email::Receiver::StrangersNotAllowedError)
- end
+ * sub-list
- it "raises an InsufficientTrustLevelError when user's trust level isn't enough" do
- Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])
- SiteSetting.email_in_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
- expect { process(:existing_user) }.to raise_error(
- Email::Receiver::InsufficientTrustLevelError,
- )
- end
+ - list
- it "works" do
- handler_calls = 0
- handler =
- proc do |topic|
- expect(topic.incoming_email_addresses).to contain_exactly(
- )
- handler_calls += 1
+ - sub-list
+
+ + list
+
+ + sub-list
+
+ [code]
+ 1.upto(10).each do |i|
+ puts i
end
- DiscourseEvent.on(:topic_created, &handler)
+ ```
+ # comment
+ [/code]
- user =
- Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
- )
- group = Fabricate(:group)
+ This is going to be stripped too.
- group.add(user)
- group.save
+ ```
+ 1.upto(10).each do |i|
+ puts i
+ end
- category.set_permissions(group => :create_post)
- category.save!
+ [/code]
+ # comment
+ ```
- # raises an InvalidAccess when the user doesn't have the privileges to create a topic
- expect { process(:existing_user) }.to raise_error(Discourse::InvalidAccess)
+ This is going to be stripped too.
- category.update_columns(email_in_allow_strangers: true)
+ Bye!
+ MD
- # allows new user to create a topic
- expect { process(:new_user) }.to change(Topic, :count)
+ it "strips lines if strip_incoming_email_lines is enabled" do
+ SiteSetting.strip_incoming_email_lines = true
- DiscourseEvent.off(:topic_created, &handler)
- expect(handler_calls).to eq(1)
+ receiver = Email::Receiver.new(email)
+ text, _elided, _format = receiver.select_body
+ expect(text).to eq(stripped_text)
end
- it "creates visible topic for ham" do
- SiteSetting.email_in_spam_header = "none"
+ it "works with empty mail body" do
+ SiteSetting.strip_incoming_email_lines = true
- Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
- )
- expect { process(:existing_user) }.to change { Topic.count }.by(1) # Topic created
+ email = <<~EMAIL
+ Date: Tue, 01 Jan 2019 00:00:00 +0300
+ Subject: An email with whitespaces
+ From: Foo <[email protected]>
+ Content-Type: text/plain; charset="UTF-8"
- topic = Topic.last
- expect(topic.visible).to eq(true)
+ --
+ my signature
- post = Post.last
- expect(post.hidden).to eq(false)
- expect(post.hidden_at).to eq(nil)
- expect(post.hidden_reason_id).to eq(nil)
+ EMAIL
+
+ receiver = Email::Receiver.new(email)
+ text, _elided, _format = receiver.select_body
+ expect(text).to be_blank
end
- it "creates hidden topic for X-Spam-Flag" do
- SiteSetting.email_in_spam_header = "X-Spam-Flag"
+ it "strip unsubscribe links" do
+ keep_relative = "/email/unsubscribe/#{SecureRandom.hex(32)}"
+ keep_other_instance = "http://other.discourse.org/email/unsubscribe/#{SecureRandom.hex(32)}"
+ strip_in_text = "#{Discourse.base_url}/email/unsubscribe/#{SecureRandom.hex(32)}"
+ strip_in_elided = "#{Discourse.base_url}/email/unsubscribe/#{SecureRandom.hex(32)}"
- user =
- Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
- )
- expect { process(:spam_x_spam_flag) }.to change { ReviewableQueuedPost.count }.by(1)
- expect(user.reload.silenced?).to be(true)
- end
+ email = <<~EMAIL
+ Date: Fri, 10 Jan 2024 13:25:42 +0100
+ Subject: Will this be stripped?
+ From: Foo <[email protected]>
+ Content-Type: text/plain; charset="UTF-8"
- it "creates hidden topic for X-Spam-Status" do
- SiteSetting.email_in_spam_header = "X-Spam-Status"
+ This is a line that will not be touched.
- user =
- Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
- )
- expect { process(:spam_x_spam_status) }.to change { ReviewableQueuedPost.count }.by(1)
- expect(user.reload.silenced?).to be(true)
- end
+ This is a [relative](#{keep_relative}) link.
- it "creates hidden topic for X-SES-Spam-Verdict" do
- SiteSetting.email_in_spam_header = "X-SES-Spam-Verdict"
+ This one is from <a href="#{keep_other_instance}">another instance</a>
- user =
- Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
- )
- expect { process(:spam_x_ses_spam_verdict) }.to change { ReviewableQueuedPost.count }.by(1)
- expect(user.reload.silenced?).to be(true)
- end
+ Here's my unsubscribe link: #{strip_in_text}
- it "creates hidden topic for failed Authentication-Results header" do
- SiteSetting.email_in_authserv_id = "example.com"
+ XoXo
- user =
- Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
- )
- expect { process(:dmarc_fail) }.to change { ReviewableQueuedPost.count }.by(1)
- expect(user.reload.silenced?).to be(false)
- end
+ ---
- it "adds the 'elided' part of the original message when always_show_trimmed_content is enabled" do
- SiteSetting.always_show_trimmed_content = true
+ To unsubscribe from these emails, [click here](#{strip_in_elided}).
+ EMAIL
+
+ text, elided, _ = Email::Receiver.new(email).select_body
+
+ expect(text).to_not include(strip_in_text)
+ expect(text).to include(keep_relative, keep_other_instance)
+ expect(elided).to_not include(strip_in_elided)
+ end
+ end
+ describe "replying to digest" do
+ fab!(:user)
+ fab!(:digest_message_id) { "7402d8ae-1c6e-44bc-9948-48e007839bcc@localhost" }
+ fab!(:email_log) do
Fabricate(
- :user,
- email: "[email protected]",
- trust_level: TrustLevel[2],
- refresh_auto_groups: true,
+ :email_log,
+ user: user,
+ email_type: "digest",
+ to_address: user.email,
+ message_id: digest_message_id,
)
- expect { process(:forwarded_email_to_category) }.to change { Topic.count }.by(1) # Topic created
+ end
+ let(:email) { <<~EMAIL }
+ MIME-Version: 1.0
+ Date: Tue, 01 Jan 2019 00:00:00 +0300
+ From: someone <#{user.email}>
+ To: Discourse <#{SiteSetting.notification_email}>
+ Message-ID: <CANtGPwC3ZmWSxnnEuJHfosbtc9d0-ZV02b_7KuyircDt4peDC2@mail.gmail.com>
+ In-Reply-To: <#{digest_message_id}>
+ Subject: Re: [Discourse] Summary
+ References: <#{digest_message_id}>
+ Content-Type: text/plain; charset="UTF-8"
- new_post, = Post.last
- expect(new_post.raw).to include(
- "Hi everyone, can you have a look at the email below?",
- "<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>",
- "Discoursing much today?",
+ hello there! I like the digest!
+
+ EMAIL
+
+ before { Jobs.run_immediately! }
+
+ it "returns a ReplyToDigestError" do
+ expect { Email::Receiver.new(email).process! }.to raise_error(
+ Email::Receiver::ReplyToDigestError,
)
end
+ end
- it "works when approving is enabled" do
- SiteSetting.approve_unless_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
+ describe "find_related_post" do
+ let(:user) { Fabricate(:user) }
+ let(:group) { Fabricate(:group, users: [user]) }
- Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])
- Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[4])
+ let(:email_1) { <<~EMAIL }
+ MIME-Version: 1.0
+ Date: Wed, 01 Jan 2019 12:00:00 +0200
+ Message-ID: <[email protected]>
+ Subject: Lorem ipsum dolor sit amet
+ From: Dan Ungureanu <[email protected]>
+ Content-Type: text/plain; charset="UTF-8"
- category.set_permissions(Group[:trust_level_4] => :full)
- category.save!
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
+ semper, erat tempor sodales commodo, mi diam tempus lorem, in vehicula
+ leo libero quis lacus. Nullam justo nunc, sagittis nec metus placerat,
+ auctor condimentum neque. Sed risus purus, fermentum eget purus
+ porttitor, finibus efficitur orci. Integer tempus mi nec odio
+ elementum pulvinar. Pellentesque sed fringilla nulla, ac mollis quam.
+ Vivamus semper lacinia scelerisque. Cras urna magna, porttitor nec
+ libero quis, congue viverra sapien. Nulla sodales ac tellus a
+ suscipit.
+ EMAIL
- expect { process(:tl3_user) }.to raise_error(Email::Receiver::InvalidPost)
- expect { process(:tl4_user) }.to change(Topic, :count)
+ let(:post_2) do
+ incoming_email =
+ IncomingEmail.find_by(message_id: "[email protected]")
+
+ PostCreator.create(
+ user,
+ raw:
+ "Vestibulum rutrum tortor vitae arcu varius, non vestibulum ipsum tempor. Integer nibh libero, dignissim eu velit vel, interdum posuere mi. Aliquam erat volutpat. Pellentesque id nulla ultricies, eleifend ipsum non, fringilla purus. Aliquam pretium dolor lobortis urna volutpat, vel consectetur arcu porta. In non erat quis nibh gravida pharetra consequat vel risus. Aliquam rutrum consectetur est ac posuere. Praesent mattis nunc risus, a molestie lectus accumsan porta.",
+ topic_id: incoming_email.topic_id,
+ )
end
- it "ignores by case-insensitive title" do
- SiteSetting.ignore_by_title = "foo"
- expect { process(:ignored) }.to_not change(Topic, :count)
+ let(:email_3) { <<~EMAIL }
+ MIME-Version: 1.0
+ Date: Wed, 01 Jan 2019 12:00:00 +0200
+ References: <[email protected]> <topic/#{post_2.topic_id}/#{post_2.id}@test.localhost>
+ In-Reply-To: <topic/#{post_2.topic_id}/#{post_2.id}@test.localhost>
+ Message-ID: <[email protected]>
+ Subject: Re: Lorem ipsum dolor sit amet
+ From: Dan Ungureanu <[email protected]>
+ Content-Type: text/plain; charset="UTF-8"
+
+ Integer mattis suscipit facilisis. Ut ullamcorper libero at faucibus
+ sodales. Ut suscipit elit ac dui porta consequat. Suspendisse potenti.
+ Nam ut accumsan dui, eget commodo sapien. Etiam ultrices elementum
+ cursus. Vivamus et diam et orci lobortis porttitor. Aliquam
+ scelerisque ex a imperdiet ornare. Donec interdum laoreet posuere.
+ Nulla sagittis, velit id posuere sollicitudin, elit nunc laoreet
+ libero, vitae aliquet tortor eros at est. Donec vitae massa vehicula,
+ aliquet libero non, porttitor ipsum. Phasellus pellentesque sodales
+ lacus eu sagittis. Aliquam ut condimentum nisi. Nulla in placerat
+ felis. Sed pellentesque, massa auctor venenatis gravida, risus lorem
+ iaculis mi, at hendrerit nisi turpis sit amet metus. Nulla egestas
+ ante eget nisi luctus consectetur.
+ EMAIL
+
+ def receive(email_string)
+ Email::Receiver.new(email_string, destinations: [group]).process!
end
- it "associates email from a secondary address with user" do
- user =
- Fabricate(
- :user,
- trust_level: TrustLevel[2],
- user_emails: [Fabricate.build(:secondary_email, email: "[email protected]")],
- refresh_auto_groups: true,
- )
+ it "makes all posts in same topic" do
+ expect { receive(email_1) }.to change { Topic.count }.by(1).and change {
+ Post.where(post_type: Post.types[:regular]).count
+ }.by(1)
+ expect { post_2 }.to not_change { Topic.count }.and change {
+ Post.where(post_type: Post.types[:regular]).count
+ }.by(1)
+ expect { receive(email_3) }.to not_change { Topic.count }.and change {
+ Post.where(post_type: Post.types[:regular]).count
+ }.by(1)
+ end
+ end
- expect { process(:existing_user) }.to change(Topic, :count).by(1)
+ it "fixes valid addresses in embedded emails" do
+ Fabricate(:group, incoming_email: "[email protected]")
+ process(:long_embedded_email_headers)
+ incoming_email = IncomingEmail.find_by(message_id: "[email protected]")
+ expect(incoming_email.cc_addresses).to include("[email protected]")
+ expect(incoming_email.cc_addresses).to include("[email protected]")
+ end
- topic = Topic.last
+ context "with reply_by_email configured" do
+ before { configure_reply_by_email }
- expect(topic.posts.last.raw).to eq("Hey, this is a topic from an existing user ;)")
+ it "raises an OldDestinationError when notification is too old" do
+ SiteSetting.disallow_reply_by_email_after_days = 2
- expect(topic.user).to eq(user)
- end
- end
+ topic = Fabricate(:topic)
+ post = Fabricate(:post, topic: topic)
+ Fabricate(:user, email: "[email protected]")
- describe "new topic in a category that allows strangers" do
- fab!(:category) do
- Fabricate(
- :category,
- email_in_allow_strangers: true,
+ mail = email(:old_destination).gsub(":post_id", post.id.to_s)
+ expect { Email::Receiver.new(mail).process! }.to raise_error(
+ Email::Receiver::BadDestinationAddress,
)
- end
-
- it "lets an email in from a stranger" do
- expect { process(:new_user) }.to change(Topic, :count)
- end
-
- it "raises an UserNotFoundError if enable_staged_users is false " do
- SiteSetting.enable_staged_users = false
- expect { process(:new_user) }.to raise_error(Email::Receiver::UserNotFoundError)
- end
- it "uses system user if enable_staged_users is false and fallback_to_system_user_for_category_emails is true" do
- SiteSetting.enable_staged_users = false
- SiteSetting.email_in_allow_system_user_fallback = true
- expect { process(:new_user) }.to change(Topic, :count)
- expect(Topic.last.user).to eq(Discourse.system_user)
- end
+ IncomingEmail.destroy_all
+ post.update!(created_at: 3.days.ago)
- it "lets an email in from a high-TL user" do
- Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[4])
- expect { process(:tl4_user) }.to change(Topic, :count)
- end
+ expect { Email::Receiver.new(mail).process! }.to raise_error(
+ Email::Receiver::OldDestinationError,
+ )
+ expect(IncomingEmail.last.error).to eq("Email::Receiver::OldDestinationError")
- it "fails on email from a low-TL user" do
- SiteSetting.email_in_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
- Fabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])
- expect { process(:tl3_user) }.to raise_error(Email::Receiver::InsufficientTrustLevelError)
- end
- end
+ SiteSetting.disallow_reply_by_email_after_days = 0
+ IncomingEmail.destroy_all
- describe "#reply_by_email_address_regex" do
- before do
- SiteSetting.reply_by_email_address = ""
- SiteSetting.alternative_reply_by_email_addresses = ""
+ expect { Email::Receiver.new(mail).process! }.to raise_error(
+ Email::Receiver::BadDestinationAddress,
+ )
end
- it "it matches nothing if there is not reply_by_email_address" do
- expect(Email::Receiver.reply_by_email_address_regex).to eq(/$a/)
- end
+ it "doesn't raise an InactiveUserError when the sender is staged" do
+ user = Fabricate(:user, email: "[email protected]", active: false, staged: true)
+ post = Fabricate(:post)
- it "uses 'reply_by_email_address' site setting" do
- SiteSetting.reply_by_email_address = "foo+%{reply_key}@bar.com"
- expect(Email::Receiver.reply_by_email_address_regex).to eq(/foo\+?(\h{32})?@bar\.com/)
- end
+ Fabricate(
+ :post_reply_key,
+ user: user,
+ post: post,
+ reply_key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ )
- it "uses 'alternative_reply_by_email_addresses' site setting" do
- SiteSetting.alternative_reply_by_email_addresses = "alt.foo+%{reply_key}@bar.com"
- expect(Email::Receiver.reply_by_email_address_regex).to eq(/alt\.foo\+?(\h{32})?@bar\.com/)
+ expect { process(:staged_sender) }.not_to raise_error
end
- it "combines both 'reply_by_email' settings" do
- SiteSetting.reply_by_email_address = "foo+%{reply_key}@bar.com"
- SiteSetting.alternative_reply_by_email_addresses = "alt.foo+%{reply_key}@bar.com"
- expect(Email::Receiver.reply_by_email_address_regex).to eq(
- /foo\+?(\h{32})?@bar\.com|alt\.foo\+?(\h{32})?@bar\.com/,
+ it "raises a FromReplyByAddressError when the email is from the reply by email address" do
+ expect { process(:from_reply_by_email_address) }.to raise_error(
+ Email::Receiver::FromReplyByAddressError,
)
end
- end
- describe "#check_address" do
- context "when e-mail is associated with a reply key" do
- before { SiteSetting.reply_by_email_address = "foo+%{reply_key}@bar.com" }
+ # bounce handling depends on reply_by_email being enabled
+ describe "bounces" do
+ it "raises a BouncerEmailError" do
+ expect { process(:bounced_email) }.to raise_error(Email::Receiver::BouncedEmailError)
+ expect(IncomingEmail.last.is_bounce).to eq(true)
- it "returns nil when the key is invalid" do
- expect(Email::Receiver.check_address("[email protected]")).to be_nil
- expect(
- Email::Receiver.check_address("[email protected]"),
- ).to be_nil
+ expect { process(:bounced_email_multiple_status_codes) }.to raise_error(
+ Email::Receiver::BouncedEmailError,
+ )
+ expect(IncomingEmail.last.is_bounce).to eq(true)
end
- context "with a valid reply" do
- it "returns the destination when the key is valid" do
- post_reply_key = Fabricate(:post_reply_key, reply_key: "4f97315cc828096c9cb34c6f1a0d6fe8")
-
- dest = Email::Receiver.check_address("[email protected]")
-
- expect(dest).to eq(post_reply_key)
+ describe "creating whisper post in PMs for staged users" do
+ let(:email_address) { "[email protected]" }
+ fab!(:user1) { Fabricate(:user, refresh_auto_groups: true) }
+ let(:user2) { Fabricate(:staged, email: email_address) }
+ let(:topic) do
+ Fabricate(
+ :topic,
+ archetype: "private_message",
+ category_id: nil,
+ user: user1,
+ allowed_users: [user1, user2],
+ )
end
- end
- end
+ let(:post) { create_post(topic: topic, user: user1) }
- context "when address is associated with a category" do
- fab!(:category) { Fabricate(:category, email_in: "[email protected]") }
+ before do
+ SiteSetting.enable_staged_users = true
+ SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
+ end
- context "when replying by email is enabled" do
- it "returns the destination category" do
- SiteSetting.reply_by_email_enabled = true
+ def create_post_reply_key(value)
+ Fabricate(:post_reply_key, reply_key: value, user: user2, post: post)
+ end
- dest = described_class.check_address("[email protected]")
+ it "when bounce without verp" do
+ create_post_reply_key("4f97315cc828096c9cb34c6f1a0d6fe8")
- expect(dest).to eq(category)
+ expect { process(:bounced_email) }.to raise_error(Email::Receiver::BouncedEmailError)
+ post = Post.last
+ expect(post.whisper?).to eq(true)
+ expect(post.raw).to eq(
+ I18n.t(
+ "system_messages.email_bounced",
+ email: email_address,
+ raw: "Your email bounced",
+ ).strip,
+ )
+ expect(IncomingEmail.last.is_bounce).to eq(true)
end
- end
- context "when replying by email is disabled" do
- it "returns nil" do
- SiteSetting.reply_by_email_enabled = false
+ context "when bounce with verp" do
+ let(:bounce_key) { "14b08c855160d67f2e0c2f8ef36e251e" }
+
+ before do
+ SiteSetting.reply_by_email_address = "foo+%{reply_key}@discourse.org"
+ create_post_reply_key(bounce_key)
+ Fabricate(
+ :email_log,
+ to_address: email_address,
+ user: user2,
+ bounce_key: bounce_key,
+ post: post,
+ )
+ end
- dest = described_class.check_address("[email protected]")
+ it "creates a post with the bounce error" do
+ expect { process(:hard_bounce_via_verp) }.to raise_error(
+ Email::Receiver::BouncedEmailError,
+ )
+ post = Post.last
+ expect(post.whisper?).to eq(true)
+ expect(post.raw).to eq(
+ I18n.t(
+ "system_messages.email_bounced",
+ email: email_address,
+ raw: "Your email bounced",
+ ).strip,
+ )
+ expect(IncomingEmail.last.is_bounce).to eq(true)
+ end
- expect(dest).to be_nil
+ it "updates the email log with the bounce error message" do
+ expect { process(:hard_bounce_via_verp) }.to raise_error(
+ Email::Receiver::BouncedEmailError,
+ )
+ email_log = EmailLog.find_by(bounce_key: bounce_key)
+ expect(email_log.bounced).to eq(true)
+ expect(email_log.bounce_error_code).to eq("5.1.1")
+ end
end
end
end
- end
-
- describe "staged users" do
- before { SiteSetting.enable_staged_users = true }
-
- shared_examples "does not create staged users" do |email_name, expected_exception|
- it "does not create staged users" do
- staged_user_count = User.where(staged: true).count
- User.expects(:create).never
- User.expects(:create!).never
- if expected_exception
- expect { process(email_name) }.to raise_error(expected_exception)
- else
- process(email_name)
- end
+ describe "reply" do
+ let(:reply_key) { "4f97315cc828096c9cb34c6f1a0d6fe8" }
+ fab!(:category)
+ fab!(:user) { Fabricate(:user, email: "[email protected]", refresh_auto_groups: true) }
+ fab!(:topic) { create_topic(category: category, user: user) }
+ fab!(:post) { create_post(topic: topic) }
- expect(User.where(staged: true).count).to eq(staged_user_count)
+ let!(:post_reply_key) do
+ Fabricate(:post_reply_key, reply_key: reply_key, user: user, post: post)
end
- end
- shared_examples "cleans up staged users" do |email_name, expected_exception|
- it "cleans up staged users" do
- staged_user_count = User.where(staged: true).count
- expect { process(email_name) }.to raise_error(expected_exception)
- expect(User.where(staged: true).count).to eq(staged_user_count)
+ let :topic_user do
+ TopicUser.find_by(topic_id: topic.id, user_id: user.id)
end
- end
-
- context "when email address is screened" do
- before { ScreenedEmail.expects(:should_block?).with("[email protected]").returns(true) }
-
- include_examples "does not create staged users",
- :screened_email,
- Email::Receiver::ScreenedEmailError
- end
- context "when the mail is auto generated" do
- include_examples "does not create staged users",
- :auto_generated_header,
- Email::Receiver::AutoGeneratedEmailError
- end
+ it "accepts reply from secondary email address" do
+ Fabricate(:secondary_email, email: "[email protected]", user: user)
- context "when email is a bounced email" do
- include_examples "does not create staged users",
- :bounced_email,
- Email::Receiver::BouncedEmailError
- end
+ expect { process(:reply_user_not_matching) }.to change { topic.posts.count }
- context "when the body is blank" do
- include_examples "does not create staged users",
- :no_body,
- Email::Receiver::NoBodyDetectedError
- end
+ post = Post.last
- context "when unsubscribe via email is not allowed" do
- include_examples "does not create staged users",
- :unsubscribe_new_user,
- Email::Receiver::UnsubscribeNotAllowed
- end
+ expect(post.raw).to eq("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
- context "when From email address is not on allowlist" do
- before do
- SiteSetting.allowed_email_domains = "example.com|bar.com"
- Fabricate(:group, incoming_email: "[email protected]")
+ expect(post.user).to eq(user)
end
- include_examples "does not create staged users",
- :blocklist_allowlist_email,
- Email::Receiver::EmailNotAllowed
- end
-
- context "when From email address is on blocklist" do
- before do
- SiteSetting.blocked_email_domains = "email.com|mail.com"
- Fabricate(:group, incoming_email: "[email protected]")
+ it "uses MD5 of 'mail_string' there is no message_id" do
+ mail_string = email(:missing_message_id)
+ expect { Email::Receiver.new(mail_string).process! }.to change { IncomingEmail.count }
+ expect(IncomingEmail.last.message_id).to eq(Digest::MD5.hexdigest(mail_string))
end
- include_examples "does not create staged users",
- :blocklist_allowlist_email,
- Email::Receiver::EmailNotAllowed
- end
-
- context "with blocklist and allowlist for To and Cc" do
- before { Fabricate(:group, incoming_email: "[email protected]") }
-
- it "does not create staged users for email addresses not on allowlist" do
- SiteSetting.allowed_email_domains = "mail.com|example.com"
- process(:blocklist_allowlist_email)
-
- expect(User.find_by_email("[email protected]")).to be_nil
- expect(User.find_by_email("[email protected]")).to be_nil
- expect(User.find_by_email("[email protected]")).to be_present
+ it "raises a ReplyUserNotMatchingError when the email address isn't matching the one we sent the notification to" do
+ Fabricate(:user, email: "[email protected]")
+ expect { process(:reply_user_not_matching) }.to raise_error(
+ Email::Receiver::ReplyUserNotMatchingError,
+ )
end
- it "does not create staged users for email addresses on blocklist" do
- SiteSetting.blocked_email_domains = "email.com|foo.com"
- process(:blocklist_allowlist_email)
-
- expect(User.find_by_email("[email protected]")).to be_nil
- expect(User.find_by_email("[email protected]")).to be_nil
- expect(User.find_by_email("[email protected]")).to be_present
+ it "raises a ReplyNotAllowedError when user without permissions is replying" do
+ Fabricate(:user, email: "[email protected]")
+ category.set_permissions(admins: :full)
+ category.save
+ expect { process(:reply_user_not_matching_but_known) }.to raise_error(
+ Email::Receiver::ReplyNotAllowedError,
+ )
+ end
+ it "works" do
+ expect { process(:reply_user_matching) }.to change { topic.posts.count }
end
- end
-
- context "when destinations aren't matching any of the incoming emails" do
- include_examples "does not create staged users",
- :bad_destinations,
- Email::Receiver::BadDestinationAddress
- end
- context "when email is sent to category" do
- context "when email is sent by a new user and category does not allow strangers" do
- fab!(:category) do
- Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: false)
- end
+ it "works when sent to an alternative reply address" do
+ SiteSetting.alternative_reply_by_email_addresses = "alt+%{reply_key}@bar.com"
+ expect { process(:reply_user_matching_alternativereplyaddr) }.to change {
+ topic.posts.count
+ }
+ end
- include_examples "does not create staged users",
- :new_user,
- Email::Receiver::StrangersNotAllowedError
+ it "raises a TopicNotFoundError when the topic was deleted" do
+ topic.update_columns(deleted_at: 1.day.ago)
+ expect { process(:reply_user_matching) }.to raise_error(Email::Receiver::TopicNotFoundError)
end
- context "when email has no date" do
- fab!(:category) do
- Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: true)
- end
+ context "with a closed topic" do
+ before { topic.update_columns(closed: true) }
- it "includes the translated string in the error" do
- expect { process(:no_date) }.to raise_error(Email::Receiver::InvalidPost).with_message(
- I18n.t("system_messages.email_reject_invalid_post_specified.date_invalid"),
- )
+ it "raises a TopicClosedError when the topic was closed" do
+ expect { process(:reply_user_matching) }.to raise_error(Email::Receiver::TopicClosedError)
end
- include_examples "does not create staged users", :no_date, Email::Receiver::InvalidPost
- end
+ it "Can watch topics via the watch command" do
+ # TODO support other locales as well, the tricky thing is that these string live in
+ # client.yml not on server yml so it is a bit tricky to find
- context "with forwarded emails behaviour set to quote" do
- before { SiteSetting.forwarded_emails_behaviour = "quote" }
+ topic.update_columns(closed: true)
+ process(:watch)
+ expect(topic_user.notification_level).to eq(NotificationLevels.topic_levels[:watching])
+ end
- context "with a category which allows strangers" do
- fab!(:category) do
- Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: true)
- end
- include_examples "creates topic with forwarded message as quote",
- :category,
+ it "Can mute topics via the mute command" do
+ process(:mute)
+ expect(topic_user.notification_level).to eq(NotificationLevels.topic_levels[:muted])
end
- context "with a category which doesn't allow strangers" do
- fab!(:category) do
- Fabricate(:category, email_in: "[email protected]", email_in_allow_strangers: false)
- end
- include_examples "cleans up staged users",
- :forwarded_email_1,
- Email::Receiver::StrangersNotAllowedError
+ it "can track a topic via the track command" do
+ process(:track)
+ expect(topic_user.notification_level).to eq(NotificationLevels.topic_levels[:tracking])
end
end
- end
- context "when email is a reply" do
- let(:reply_key) { "4f97315cc828096c9cb34c6f1a0d6fe8" }
- fab!(:category)
- fab!(:user) { Fabricate(:user, email: "[email protected]", refresh_auto_groups: true) }
- fab!(:user2) { Fabricate(:user, email: "[email protected]") }
- fab!(:topic) { create_topic(category: category, user: user) }
- fab!(:post) { create_post(topic: topic, user: user) }
+ it "raises an InvalidPost when there was an error while creating the post" do
+ expect { process(:too_small) }.to raise_error(Email::Receiver::TooShortPost)
+ end
- let!(:post_reply_key) do
- Fabricate(:post_reply_key, reply_key: reply_key, user: user, post: post)
+ it "raises an InvalidPost when there are too may mentions" do
+ SiteSetting.max_mentions_per_post = 1
+ Fabricate(:user, username: "user1")
+ Fabricate(:user, username: "user2")
+ expect { process(:too_many_mentions) }.to raise_error(Email::Receiver::InvalidPost)
end
- context "when the email address isn't matching the one we sent the notification to" do
- include_examples "does not create staged users",
- :reply_user_not_matching,
- Email::Receiver::ReplyUserNotMatchingError
+ it "raises an InvalidPostAction when they aren't allowed to like a post" do
+ topic.update_columns(archived: true)
+ expect { process(:like) }.to raise_error(Email::Receiver::InvalidPostAction)
end
- context "with forwarded emails behaviour set to create replies" do
- before { SiteSetting.forwarded_emails_behaviour = "create_replies" }
+ it "creates a new reply post" do
+ handler_calls = 0
+ handler = proc { |_| handler_calls += 1 }
- context "when a reply contains a forwarded email" do
- include_examples "does not create staged users", :reply_and_forwarded
- end
+ DiscourseEvent.on(:topic_created, &handler)
- context "with forwarded email to category that doesn't allow strangers" do
- before { category.update!(email_in: "[email protected]", email_in_allow_strangers: false) }
+ expect { process(:text_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This is a text reply :)\n\nEmail parsing should not break because of a UTF-8 character: ’",
+ )
+ expect(topic.posts.last.via_email).to eq(true)
+ expect(topic.posts.last.cooked).not_to match(/<br/)
- include_examples "cleans up staged users",
- :forwarded_email_1,
- Email::Receiver::StrangersNotAllowedError
- end
+ expect { process(:html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is a **HTML** reply ;)")
+
+ DiscourseEvent.off(:topic_created, &handler)
+ expect(handler_calls).to eq(0)
end
- end
- context "when replying without key is allowed" do
- fab!(:group) { Fabricate(:group, incoming_email: "[email protected]") }
- let!(:topic) do
- SiteSetting.find_related_post_with_key = false
- process(:email_reply_1)
- Topic.last
+ it "stores the created_via source against the incoming email" do
+ process(:text_reply, source: :handle_mail)
+ expect(IncomingEmail.last.created_via).to eq(IncomingEmail.created_via_types[:handle_mail])
+ process(:text_and_html_reply, source: :pop3_poll)
+ expect(IncomingEmail.last.created_via).to eq(IncomingEmail.created_via_types[:pop3_poll])
end
- context "when the topic was deleted" do
- before { topic.update_columns(deleted_at: 1.day.ago) }
+ it "stores the message_id of the incoming email against the post as outbound_message_id" do
+ expect { process(:text_reply, source: :handle_mail) }.to change(Post, :count)
+ message_id = IncomingEmail.last.message_id
+ expect(Post.last.outbound_message_id).to eq(message_id)
+ end
- include_examples "cleans up staged users",
- :email_reply_staged,
- Email::Receiver::TopicNotFoundError
+ it "automatically elides gmail quotes" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:gmail_html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq <<~MD.strip
+ This is a **GMAIL** reply ;)
+
+ <details class='elided'>
+ <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+
+ This is the *elided* part!
+
+ </details>
+ MD
end
- context "when the topic was closed" do
- before { topic.update_columns(closed: true) }
+ it "correctly extracts body from exchange emails" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:exchange_html_body) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the **body** of the email.")
+ end
- include_examples "cleans up staged users",
- :email_reply_staged,
- Email::Receiver::TopicClosedError
+ it "correctly extracts reply from exchange emails" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:exchange_html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the **body !!** of the email.")
end
- context "when they aren't allowed to like a post" do
- before { topic.update_columns(archived: true) }
+ it "correctly extracts body & reply from exchange emails" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:exchange_html_body_and_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq <<~MD.strip
+ This is the **body** of the email.
- include_examples "cleans up staged users",
- :email_reply_like,
- Email::Receiver::InvalidPostAction
+ <details class='elided'>
+ <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
+
+ This is the *reply*!
+
+ </details>
+ MD
end
- end
- it "does not remove the incoming email record when staged users are deleted" do
- expect { process(:bad_destinations) }.to change { IncomingEmail.count }.and raise_error(
- Email::Receiver::BadDestinationAddress,
- )
- expect(IncomingEmail.last.message_id).to eq("[email protected]")
- end
- end
+ it "doesn't process email with same message-id more than once" do
+ expect do
+ process(:text_reply)
+ process(:text_reply)
+ end.to change { topic.posts.count }.by(1)
+ end
- describe "mailman mirror" do
- fab!(:category, :mailinglist_mirror_category)
+ it "handles different encodings correctly" do
+ expect { process(:hebrew_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("שלום! מה שלומך היום?")
- it "uses 'from' email address" do
- expect { process(:mailman_1) }.to change { Topic.count }
- user = Topic.last.user
- expect(user.email).to eq("[email protected]")
- expect(user.name).to eq("Some One")
- end
+ expect { process(:chinese_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("您好! 你今天好吗?")
- it "uses 'reply-to' email address" do
- expect { process(:mailman_2) }.to change { Topic.count }
- user = Topic.last.user
- expect(user.email).to eq("[email protected]")
- expect(user.name).to eq("Some")
- end
+ expect { process(:reply_with_weird_encoding) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is a reply with a weird encoding.")
- it "uses 'x-mailfrom' email address and name from CC" do
- expect { process(:mailman_3) }.to change { Topic.count }
- user = Topic.last.user
- expect(user.email).to eq("[email protected]")
- expect(user.name).to eq("Some One")
- end
+ expect { process(:reply_with_8bit_encoding) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("hab vergessen kritische zeichen einzufügen:\näöüÄÖÜß")
+ end
- it "uses 'x-original-from' email address" do
- expect { process(:mailman_4) }.to change { Topic.count }
- user = Topic.last.user
- expect(user.email).to eq("[email protected]")
- expect(user.name).to eq("Some")
- end
- end
+ it "prefers text over html when site setting is disabled" do
+ SiteSetting.incoming_email_prefer_html = false
+ expect { process(:text_and_html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the *text* part.")
+ end
- describe "mailing list mirror" do
- fab!(:category, :mailinglist_mirror_category)
+ it "prefers html over text when site setting is enabled" do
+ SiteSetting.incoming_email_prefer_html = true
+ expect { process(:text_and_html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the **html** part.")
+ end
- before { SiteSetting.block_auto_generated_emails = true }
+ it "uses text when prefer_html site setting is enabled but no html is available" do
+ SiteSetting.incoming_email_prefer_html = true
+ expect { process(:text_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This is a text reply :)\n\nEmail parsing should not break because of a UTF-8 character: ’",
+ )
+ end
- it "should allow creating topic even when email is autogenerated" do
- expect { process(:mailinglist) }.to change { Topic.count }
- expect(IncomingEmail.last.is_auto_generated).to eq(false)
- end
+ it "removes the 'on <date>, <contact> wrote' quoting line" do
+ expect { process(:on_date_contact_wrote) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is the actual reply.")
+ end
- it "should allow replying without reply key" do
- process(:mailinglist)
- topic = Topic.last
+ it "removes the 'Previous Replies' marker" do
+ expect { process(:previous_replies) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This will not include the previous discussion that is present in this email.",
+ )
+ end
- expect { process(:mailinglist_reply) }.to change { topic.posts.count }
- end
+ it "removes the translated 'Previous Replies' marker" do
+ expect { process(:previous_replies_de) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This will not include the previous discussion that is present in this email.",
+ )
+ end
- it "should skip validations for staged users" do
- Fabricate(:user, email: "[email protected]", staged: true)
- expect { process(:mailinglist_short_message) }.to change { Topic.count }
- end
+ it "removes the 'type reply above' marker" do
+ expect { process(:reply_above) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This will not include the previous discussion that is present in this email.",
+ )
+ end
- it "should skip validations for regular users" do
- Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
- expect { process(:mailinglist_short_message) }.to change { Topic.count }
- end
+ it "removes the translated 'Previous Replies' marker" do
+ expect { process(:reply_above_de) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This will not include the previous discussion that is present in this email.",
+ )
+ end
- context "with read-only category" do
- before do
- category.set_permissions(everyone: :readonly)
- category.save!
+ it "handles multiple paragraphs" do
+ expect { process(:paragraphs) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "Do you like liquorice?\n\nI really like them. One could even say that I am *addicted* to liquorice. And if\nyou can mix it up with some anise, then I'm in heaven ;)",
+ )
+ end
- Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
- Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
+ it "raises a NoSenderDetectedError when the From header can't be parsed" do
+ expect { process(:invalid_from_1) }.to raise_error(Email::Receiver::NoSenderDetectedError)
end
- it "should allow creating topic within read-only category" do
- expect { process(:mailinglist) }.to change { Topic.count }
+ it "raises a NoSenderDetectedError when the From header doesn't contain an email address" do
+ expect { process(:invalid_from_2) }.to raise_error(Email::Receiver::NoSenderDetectedError)
end
- it "should allow replying within read-only category" do
- process(:mailinglist)
- topic = Topic.last
+ it "doesn't raise an AutoGeneratedEmailError due to an X-Auto-Response-Suppress header" do
+ expect { process(:quirks_exchange_xars) }.to change { topic.posts.count }
+ end
- expect { process(:mailinglist_reply) }.to change { topic.posts.count }
+ it "doesn't raise an AutoGeneratedEmailError when the mail is auto generated but is allowlisted" do
+ SiteSetting.auto_generated_allowlist = "[email protected]|[email protected]"
+ expect { process(:auto_generated_allowlisted) }.to change { topic.posts.count }
end
- end
- it "ignores unsubscribe email" do
- SiteSetting.unsubscribe_via_email = true
- Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
+ it "doesn't raise an AutoGeneratedEmailError when block_auto_generated_emails is disabled" do
+ SiteSetting.block_auto_generated_emails = false
+ expect { process(:auto_generated_unblocked) }.to change { topic.posts.count }
+ end
- expect { process("mailinglist_unsubscribe") }.to_not change {
- ActionMailer::Base.deliveries.count
- }
- end
+ it "allows staged users to reply to a restricted category" do
+ user.update_columns(staged: true)
- it "ignores dmarc fails" do
- expect { process("mailinglist_dmarc_fail") }.to change { Topic.count }
+ category.email_in = "[email protected]"
+ category.email_in_allow_strangers = true
+ category.set_permissions(Group[:trust_level_4] => :full)
+ category.save!
- post = Topic.last.first_post
- expect(post.hidden).to eq(false)
- expect(post.hidden_reason_id).to be_nil
- end
- end
+ expect { process(:staged_reply_restricted) }.to change { topic.posts.count }
+ end
- it "tries to fix unparsable email addresses in To and CC headers" do
- expect { process(:unparsable_email_addresses) }.to raise_error(
- Email::Receiver::BadDestinationAddress,
- )
+ it "posts a reply to the topic when the post was deleted" do
+ post.update_columns(deleted_at: 1.day.ago)
+ expect { process(:reply_user_matching) }.to change { topic.posts.count }
+ expect(topic.ordered_posts.last.reply_to_post_number).to be_nil
+ end
- email = IncomingEmail.last
- expect(email.to_addresses).to eq("[email protected]")
- expect(email.cc_addresses).to eq("[email protected];[email protected]")
- end
+ describe "Unsubscribing via email" do
+ let(:last_email) { ActionMailer::Base.deliveries.last }
- describe "#select_body" do
- let(:email) { <<~EMAIL }
- MIME-Version: 1.0
- Date: Tue, 01 Jan 2019 00:00:00 +0300
- Subject: An email with whitespaces
- From: Foo <[email protected]>
- Content-Type: text/plain; charset="UTF-8"
+ describe "unsubscribe_subject.eml" do
+ it "sends an email asking the user to confirm the unsubscription" do
+ expect { process("unsubscribe_subject") }.to change {
+ ActionMailer::Base.deliveries.count
+ }.by(1)
+ expect(last_email.to.length).to eq 1
+ expect(last_email.from.length).to eq 1
+ expect(last_email.from).to include "noreply@#{Discourse.current_hostname}"
+ expect(last_email.to).to include "[email protected]"
+ expect(last_email.subject).to eq I18n.t(:"unsubscribe_mailer.subject_template").gsub(
+ "%{site_title}",
+ SiteSetting.title,
+ )
+ end
- This is a line that will be stripped
- This is another line that will be stripped
+ it "does nothing unless unsubscribe_via_email is turned on" do
+ SiteSetting.unsubscribe_via_email = false
+ before_deliveries = ActionMailer::Base.deliveries.count
+ expect { process("unsubscribe_subject") }.to raise_error {
+ Email::Receiver::BadDestinationAddress
+ }
+ expect(before_deliveries).to eq ActionMailer::Base.deliveries.count
+ end
+ end
- This is a line that will not be touched.
- This is another line that will not be touched.
+ describe "unsubscribe_body.eml" do
+ it "sends an email asking the user to confirm the unsubscription" do
+ expect { process("unsubscribe_body") }.to change {
+ ActionMailer::Base.deliveries.count
+ }.by(1)
+ expect(last_email.to.length).to eq 1
+ expect(last_email.from.length).to eq 1
+ expect(last_email.from).to include "noreply@#{Discourse.current_hostname}"
+ expect(last_email.to).to include "[email protected]"
+ expect(last_email.subject).to eq I18n.t(:"unsubscribe_mailer.subject_template").gsub(
+ "%{site_title}",
+ SiteSetting.title,
+ )
+ end
- * list
+ it "does nothing unless unsubscribe_via_email is turned on" do
+ SiteSetting.unsubscribe_via_email = false
+ before_deliveries = ActionMailer::Base.deliveries.count
+ expect { process("unsubscribe_body") }.to raise_error { Email::Receiver::InvalidPost }
+ expect(before_deliveries).to eq ActionMailer::Base.deliveries.count
+ end
+ end
- * sub-list
+ it "raises an UnsubscribeNotAllowed and does not send an unsubscribe email" do
+ before_deliveries = ActionMailer::Base.deliveries.count
+ expect { process(:unsubscribe_new_user) }.to raise_error {
+ Email::Receiver::UnsubscribeNotAllowed
+ }
+ expect(before_deliveries).to eq ActionMailer::Base.deliveries.count
+ end
+ end
- - list
+ it "handles inline reply" do
+ expect { process(:inline_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("And this is *my* reply :+1:")
+ end
- - sub-list
+ it "retrieves the first part of multiple replies" do
+ expect { process(:inline_mixed_replies) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "> WAT <https://bar.com/users/wat> November 28\n>\n> This is the previous post.\n\nAnd this is *my* reply :+1:\n\n> This is another post.\n\nAnd this is **another** reply.",
+ )
+ end
- + list
+ it "strips mobile/webmail signatures" do
+ expect { process(:iphone_signature) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is not the signature you're looking for.")
+ end
- + sub-list
+ it "strips 'original message' context" do
+ expect { process(:original_message) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is a reply :)")
+ end
- [code]
- 1.upto(10).each do |i|
- puts i
- end
+ it "add the 'elided' part of the original message only for private messages" do
+ topic.update_columns(category_id: nil, archetype: Archetype.private_message)
+ topic.allowed_users << user
+ topic.save
- ```
- # comment
- [/code]
+ expect { process(:original_message) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This is a reply :)\n\n<details class='elided'>\n<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>\n\n---Original Message---\nThis part should not be included\n\n</details>",
+ )
+ end
- This is going to be stripped too.
+ it "doesn't include the 'elided' part of the original message when always_show_trimmed_content is disabled" do
+ SiteSetting.always_show_trimmed_content = false
+ expect { process(:original_message) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is a reply :)")
+ end
- ```
- 1.upto(10).each do |i|
- puts i
- end
+ it "adds the 'elided' part of the original message for public replies when always_show_trimmed_content is enabled" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:original_message) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This is a reply :)\n\n<details class='elided'>\n<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>\n\n---Original Message---\nThis part should not be included\n\n</details>",
+ )
+ end
+
+ it "doesn't trim the message when trim_incoming_emails is disabled" do
+ SiteSetting.trim_incoming_emails = false
+ expect { process(:original_message) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq(
+ "This is a reply :)\n\n---Original Message---\nThis part should not be included",
+ )
+ end
+
+ it "supports attached images in TEXT part" do
+ SiteSetting.incoming_email_prefer_html = false
+
+ expect { process(:no_body_with_image) }.to change { topic.posts.count }
+
+ post = topic.posts.last
+ upload = post.uploads.first
+
+ expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
+
+ expect { process(:inline_image) }.to change { topic.posts.count }
+
+ post = topic.posts.last
+ upload = post.uploads.first
+
+ expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
+ end
+
+ it "supports attached images in HTML part" do
+ SiteSetting.incoming_email_prefer_html = true
- [/code]
- # comment
- ```
+ expect { process(:inline_image) }.to change { topic.posts.count }
- This is going to be stripped too.
+ post = topic.posts.last
+ upload = post.uploads.last
- Bye!
- EMAIL
+ expect(post.raw).to eq(<<~MD.chomp)
+ **Before**
- let(:stripped_text) { <<~MD }
- This is a line that will be stripped
- This is another line that will be stripped
+ <img src="#{upload.short_url}" alt="内嵌图片 1">
- This is a line that will not be touched.
- This is another line that will not be touched.
+ *After*
+ MD
+ end
- * list
+ it "gracefully handles malformed images in HTML part" do
+ expect { process(:inline_image_2) }.to change { topic.posts.count }
- * sub-list
+ post = topic.posts.last
+ upload = post.uploads.last
- - list
+ expect(post.raw).to eq(<<~MD.chomp)
+ [image:#{"0" * 5000}
- - sub-list
+ [details="#{I18n.t("emails.incoming.attachments")}"]
- + list
+ #{UploadMarkdown.new(upload).to_markdown}
- + sub-list
+ [/details]
+ MD
+ end
- [code]
- 1.upto(10).each do |i|
- puts i
- end
+ it "supports attached images in signature" do
+ SiteSetting.incoming_email_prefer_html = true
+ SiteSetting.always_show_trimmed_content = true
- ```
- # comment
- [/code]
+ expect { process(:body_with_image) }.to change { topic.posts.count }
- This is going to be stripped too.
+ post = topic.posts.last
+ upload = post.uploads.last
- ```
- 1.upto(10).each do |i|
- puts i
- end
+ expect(post.raw).to eq(<<~MD.chomp)
+ This is a **GMAIL** reply ;)
- [/code]
- # comment
- ```
+ <details class='elided'>
+ <summary title='Show trimmed content'>&#183;&#183;&#183;</summary>
- This is going to be stripped too.
+ <img src="#{upload.short_url}" width="300" height="200">
- Bye!
+ </details>
MD
+ end
- it "strips lines if strip_incoming_email_lines is enabled" do
- SiteSetting.strip_incoming_email_lines = true
+ it "supports attachments" do
+ SiteSetting.authorized_extensions = "txt|jpg"
+ expect { process(:attached_txt_file) }.to change { topic.posts.count }
+ post = topic.posts.last
+ upload = post.uploads.first
- receiver = Email::Receiver.new(email)
- text, _elided, _format = receiver.select_body
- expect(text).to eq(stripped_text)
- end
+ expect(post.raw).to eq(<<~MD.chomp)
+ Please find some text file attached.
- it "works with empty mail body" do
- SiteSetting.strip_incoming_email_lines = true
+ [details="#{I18n.t("emails.incoming.attachments")}"]
- email = <<~EMAIL
- Date: Tue, 01 Jan 2019 00:00:00 +0300
- Subject: An email with whitespaces
- From: Foo <[email protected]>
- Content-Type: text/plain; charset="UTF-8"
+ #{UploadMarkdown.new(upload).to_markdown}
- --
- my signature
+ [/details]
+ MD
- EMAIL
+ expect { process(:apple_mail_attachment) }.to change { topic.posts.count }
+ post = topic.posts.last
+ upload = post.uploads.first
- receiver = Email::Receiver.new(email)
- text, _elided, _format = receiver.select_body
- expect(text).to be_blank
- end
+ expect(post.raw).to eq(<<~MD.chomp)
+ Picture below.
- it "strip unsubscribe links" do
- keep_relative = "/email/unsubscribe/#{SecureRandom.hex(32)}"
- keep_other_instance = "http://other.discourse.org/email/unsubscribe/#{SecureRandom.hex(32)}"
- strip_in_text = "#{Discourse.base_url}/email/unsubscribe/#{SecureRandom.hex(32)}"
- strip_in_elided = "#{Discourse.base_url}/email/unsubscribe/#{SecureRandom.hex(32)}"
+ <img apple-inline="yes" id="06C04C58-783E-4753-9B6B-D57403903060" src="#{upload.short_url}" class="">
- email = <<~EMAIL
- Date: Fri, 10 Jan 2024 13:25:42 +0100
- Subject: Will this be stripped?
- From: Foo <[email protected]>
- Content-Type: text/plain; charset="UTF-8"
+ Picture above.
+ MD
+ end
- This is a line that will not be touched.
+ it "tries not to repeat duplicate attachments" do
+ SiteSetting.authorized_extensions = "jpg"
+ SiteSetting.always_show_trimmed_content = true
- This is a [relative](#{keep_relative}) link.
+ expect { process(:logo_1) }.to change { Upload.count }.by(1)
+ logo = Upload.last
+ expect(topic.posts.last.raw).to include logo.short_url
- This one is from <a href="#{keep_other_instance}">another instance</a>
+ expect { process(:logo_2) }.not_to change { Upload.count }
+ expect(topic.posts.last.raw).to include logo.short_url
+ end
- Here's my unsubscribe link: #{strip_in_text}
+ it "works with removed attachments" do
+ SiteSetting.authorized_extensions = "jpg"
- XoXo
+ expect { process(:removed_attachments) }.to change { topic.posts.count }
+ expect(topic.posts.last.uploads).to be_empty
+ end
- ---
+ it "supports eml attachments" do
+ SiteSetting.authorized_extensions = "eml"
+ expect { process(:attached_eml_file) }.to change { topic.posts.count }
+ post = topic.posts.last
+ upload = post.uploads.first
- To unsubscribe from these emails, [click here](#{strip_in_elided}).
- EMAIL
+ expect(post.raw).to eq(<<~MD.chomp)
+ Please find the eml file attached.
- text, elided, _ = Email::Receiver.new(email).select_body
+ [details="#{I18n.t("emails.incoming.attachments")}"]
- expect(text).to_not include(strip_in_text)
- expect(text).to include(keep_relative, keep_other_instance)
- expect(elided).to_not include(strip_in_elided)
- end
- end
+ #{UploadMarkdown.new(upload).to_markdown}
- describe "replying to digest" do
- fab!(:user)
- fab!(:digest_message_id) { "7402d8ae-1c6e-44bc-9948-48e007839bcc@localhost" }
- fab!(:email_log) do
- Fabricate(
- :email_log,
- user: user,
- email_type: "digest",
- to_address: user.email,
- message_id: digest_message_id,
- )
- end
- let(:email) { <<~EMAIL }
- MIME-Version: 1.0
- Date: Tue, 01 Jan 2019 00:00:00 +0300
- From: someone <#{user.email}>
- To: Discourse <#{SiteSetting.notification_email}>
- Message-ID: <CANtGPwC3ZmWSxnnEuJHfosbtc9d0-ZV02b_7KuyircDt4peDC2@mail.gmail.com>
- In-Reply-To: <#{digest_message_id}>
- Subject: Re: [Discourse] Summary
- References: <#{digest_message_id}>
- Content-Type: text/plain; charset="UTF-8"
+ [/details]
+ MD
+ end
- hello there! I like the digest!
+ it "can decode attachments" do
+ SiteSetting.authorized_extensions = "pdf"
+ Fabricate(:group, incoming_email: "[email protected]")
- EMAIL
+ process(:encoded_filename)
+ expect(Upload.last.original_filename).to eq("This is a test.pdf")
+ end
- before { Jobs.run_immediately! }
+ context "when attachment is rejected" do
+ it "sends out the warning email" do
+ expect { process(:attached_txt_file) }.to change { EmailLog.count }.by(1)
+ expect(EmailLog.last.email_type).to eq("email_reject_attachment")
+ expect(topic.posts.last.uploads.size).to eq 0
+ end
- it "returns a ReplyToDigestError" do
- expect { Email::Receiver.new(email).process! }.to raise_error(
- Email::Receiver::ReplyToDigestError,
- )
- end
- end
+ it "doesn't send out the warning email if sender is staged user" do
+ user.update_columns(staged: true)
+ expect { process(:attached_txt_file) }.not_to change { EmailLog.count }
+ expect(topic.posts.last.uploads.size).to eq 0
+ end
- describe "find_related_post" do
- let(:user) { Fabricate(:user) }
- let(:group) { Fabricate(:group, users: [user]) }
+ it "creates the post with attachment missing message" do
+ missing_attachment_regex =
+ Regexp.escape(I18n.t("emails.incoming.missing_attachment", filename: "text.txt"))
+ expect { process(:attached_txt_file) }.to change { topic.posts.count }
+ post = topic.posts.last
+ expect(post.raw).to match(/#{missing_attachment_regex}/)
+ expect(post.uploads.size).to eq 0
+ end
+ end
- let(:email_1) { <<~EMAIL }
- MIME-Version: 1.0
- Date: Wed, 01 Jan 2019 12:00:00 +0200
- Message-ID: <[email protected]>
- Subject: Lorem ipsum dolor sit amet
- From: Dan Ungureanu <[email protected]>
- Content-Type: text/plain; charset="UTF-8"
+ it "supports emails with just an attachment" do
+ SiteSetting.authorized_extensions = "pdf"
+ expect { process(:attached_pdf_file) }.to change { topic.posts.count }
+ post = topic.posts.last
+ upload = post.uploads.last
- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
- semper, erat tempor sodales commodo, mi diam tempus lorem, in vehicula
- leo libero quis lacus. Nullam justo nunc, sagittis nec metus placerat,
- auctor condimentum neque. Sed risus purus, fermentum eget purus
- porttitor, finibus efficitur orci. Integer tempus mi nec odio
- elementum pulvinar. Pellentesque sed fringilla nulla, ac mollis quam.
- Vivamus semper lacinia scelerisque. Cras urna magna, porttitor nec
- libero quis, congue viverra sapien. Nulla sodales ac tellus a
- suscipit.
- EMAIL
+ expect(post.raw).to include UploadMarkdown.new(upload).to_markdown
+ end
- let(:post_2) do
- incoming_email =
- IncomingEmail.find_by(message_id: "[email protected]")
+ it "supports liking via email" do
+ expect { process(:like) }.to change(PostAction, :count)
+ end
- PostCreator.create(
- user,
- raw:
- "Vestibulum rutrum tortor vitae arcu varius, non vestibulum ipsum tempor. Integer nibh libero, dignissim eu velit vel, interdum posuere mi. Aliquam erat volutpat. Pellentesque id nulla ultricies, eleifend ipsum non, fringilla purus. Aliquam pretium dolor lobortis urna volutpat, vel consectetur arcu porta. In non erat quis nibh gravida pharetra consequat vel risus. Aliquam rutrum consectetur est ac posuere. Praesent mattis nunc risus, a molestie lectus accumsan porta.",
- topic_id: incoming_email.topic_id,
- )
- end
+ it "ensures posts aren't dated in the future" do
+ # PostCreator doesn't provide sub-second accuracy for created_at
+ now = freeze_time Time.zone.now.round
- let(:email_3) { <<~EMAIL }
- MIME-Version: 1.0
- Date: Wed, 01 Jan 2019 12:00:00 +0200
- References: <[email protected]> <topic/#{post_2.topic_id}/#{post_2.id}@test.localhost>
- In-Reply-To: <topic/#{post_2.topic_id}/#{post_2.id}@test.localhost>
- Message-ID: <[email protected]>
- Subject: Re: Lorem ipsum dolor sit amet
- From: Dan Ungureanu <[email protected]>
- Content-Type: text/plain; charset="UTF-8"
+ expect { process(:from_the_future) }.to change { topic.posts.count }
+ expect(topic.posts.last.created_at).to eq_time(now)
+ end
- Integer mattis suscipit facilisis. Ut ullamcorper libero at faucibus
- sodales. Ut suscipit elit ac dui porta consequat. Suspendisse potenti.
- Nam ut accumsan dui, eget commodo sapien. Etiam ultrices elementum
- cursus. Vivamus et diam et orci lobortis porttitor. Aliquam
- scelerisque ex a imperdiet ornare. Donec interdum laoreet posuere.
- Nulla sagittis, velit id posuere sollicitudin, elit nunc laoreet
- libero, vitae aliquet tortor eros at est. Donec vitae massa vehicula,
- aliquet libero non, porttitor ipsum. Phasellus pellentesque sodales
- lacus eu sagittis. Aliquam ut condimentum nisi. Nulla in placerat
- felis. Sed pellentesque, massa auctor venenatis gravida, risus lorem
- iaculis mi, at hendrerit nisi turpis sit amet metus. Nulla egestas
- ante eget nisi luctus consectetur.
- EMAIL
+ it "accepts emails with wrong reply key if the system knows about the forwarded email" do
+ Fabricate(:user, email: "[email protected]", refresh_auto_groups: true)
+ Fabricate(
+ :incoming_email,
+ raw: <<~RAW,
+ Return-Path: <[email protected]>
+ From: Alice <[email protected]>
+ Subject: Hello world
+ Date: Fri, 15 Jan 2016 00:12:43 +0100
+ Message-ID: <[email protected]>
+ Mime-Version: 1.0
+ Content-Type: text/plain; charset=UTF-8
+ Content-Transfer-Encoding: quoted-printable
- def receive(email_string)
- Email::Receiver.new(email_string, destinations: [group]).process!
- end
+ This post was created by email.
+ RAW
+ from_address: "[email protected]",
+ topic: topic,
+ post: post,
+ user: user,
+ )
- it "makes all posts in same topic" do
- expect { receive(email_1) }.to change { Topic.count }.by(1).and change {
- Post.where(post_type: Post.types[:regular]).count
- }.by(1)
- expect { post_2 }.to not_change { Topic.count }.and change {
- Post.where(post_type: Post.types[:regular]).count
- }.by(1)
- expect { receive(email_3) }.to not_change { Topic.count }.and change {
- Post.where(post_type: Post.types[:regular]).count
- }.by(1)
- end
- end
+ expect { process(:reply_user_not_matching_but_known) }.to change { topic.posts.count }
+ end
- it "fixes valid addresses in embedded emails" do
- Fabricate(:group, incoming_email: "[email protected]")
- process(:long_embedded_email_headers)
- incoming_email = IncomingEmail.find_by(message_id: "[email protected]")
- expect(incoming_email.cc_addresses).to include("[email protected]")
- expect(incoming_email.cc_addresses).to include("[email protected]")
+ it "re-enables user's PM email notifications when user replies to a private topic" do
+ topic.update_columns(category_id: nil, archetype: Archetype.private_message)
+ topic.allowed_users << user
+ topic.save
+
+ user.user_option.update_columns(email_messages_level: UserOption.email_level_types[:never])
+ expect { process(:reply_user_matching) }.to change { topic.posts.count }
+ user.reload
+ expect(user.user_option.email_messages_level).to eq(UserOption.email_level_types[:always])
+ end
+ end
end
end