2020-10-21 20:49:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Jobs::GroupSmtpEmail do
|
2021-06-27 20:42:06 -04:00
|
|
|
fab!(:topic) { Fabricate(:private_message_topic, title: "Help I need support") }
|
2021-01-05 00:32:04 -05:00
|
|
|
fab!(:post) do
|
2021-06-27 20:42:06 -04:00
|
|
|
Fabricate(:post, topic: topic, raw: "some first post content")
|
2021-06-27 23:19:17 -04:00
|
|
|
Fabricate(:post, topic: topic, raw: "some intermediate content")
|
2021-06-27 20:42:06 -04:00
|
|
|
Fabricate(:post, topic: topic, raw: "this is the second post reply")
|
2021-01-05 00:32:04 -05:00
|
|
|
end
|
2021-06-27 18:55:13 -04:00
|
|
|
fab!(:group) { Fabricate(:smtp_group, name: "support-group", full_name: "Support Group") }
|
2021-01-05 00:32:04 -05:00
|
|
|
fab!(:recipient_user) { Fabricate(:user, email: "test@test.com") }
|
|
|
|
let(:post_id) { post.id }
|
2020-10-21 20:49:08 -04:00
|
|
|
let(:args) do
|
|
|
|
{
|
|
|
|
group_id: group.id,
|
2021-01-05 00:32:04 -05:00
|
|
|
post_id: post_id,
|
2021-06-27 18:55:13 -04:00
|
|
|
email: "test@test.com",
|
|
|
|
cc_emails: %w[otherguy@test.com cormac@lit.com],
|
2020-10-21 20:49:08 -04:00
|
|
|
}
|
|
|
|
end
|
2021-06-27 20:42:06 -04:00
|
|
|
let(:staged1) { Fabricate(:staged, email: "otherguy@test.com") }
|
|
|
|
let(:staged2) { Fabricate(:staged, email: "cormac@lit.com") }
|
|
|
|
let(:normaluser) { Fabricate(:user, email: "justanormalguy@test.com", username: "normaluser") }
|
2020-10-21 20:49:08 -04:00
|
|
|
|
|
|
|
before do
|
2021-06-27 18:55:13 -04:00
|
|
|
SiteSetting.enable_smtp = true
|
2020-10-21 20:49:08 -04:00
|
|
|
SiteSetting.manual_polling_enabled = true
|
2021-06-27 18:55:13 -04:00
|
|
|
SiteSetting.reply_by_email_address = "test+%{reply_key}@test.com"
|
2020-10-21 20:49:08 -04:00
|
|
|
SiteSetting.reply_by_email_enabled = true
|
2021-06-27 20:42:06 -04:00
|
|
|
TopicAllowedGroup.create(group: group, topic: topic)
|
2021-10-19 01:26:22 -04:00
|
|
|
TopicAllowedUser.create(user: recipient_user, topic: topic)
|
2021-06-27 20:42:06 -04:00
|
|
|
TopicAllowedUser.create(user: staged1, topic: topic)
|
|
|
|
TopicAllowedUser.create(user: staged2, topic: topic)
|
|
|
|
TopicAllowedUser.create(user: normaluser, topic: topic)
|
2020-10-21 20:49:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sends an email using the GroupSmtpMailer and Email::Sender" do
|
|
|
|
message = Mail::Message.new(body: "hello", to: "myemail@example.invalid")
|
2023-01-04 17:07:50 -05:00
|
|
|
GroupSmtpMailer
|
|
|
|
.expects(:send_mail)
|
|
|
|
.with(
|
|
|
|
group,
|
|
|
|
"test@test.com",
|
|
|
|
post,
|
|
|
|
cc_addresses: %w[otherguy@test.com cormac@lit.com],
|
|
|
|
bcc_addresses: [],
|
|
|
|
)
|
|
|
|
.returns(message)
|
2021-06-27 18:55:13 -04:00
|
|
|
subject.execute(args)
|
|
|
|
end
|
|
|
|
|
2021-06-27 20:42:06 -04:00
|
|
|
it "includes a 'reply above this line' message" do
|
|
|
|
subject.execute(args)
|
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log.as_mail_message.html_part.to_s).to include(
|
|
|
|
I18n.t("user_notifications.reply_above_line"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include context posts" do
|
|
|
|
subject.execute(args)
|
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log.as_mail_message.text_part.to_s).not_to include(
|
|
|
|
I18n.t("user_notifications.previous_discussion"),
|
|
|
|
)
|
|
|
|
expect(email_log.as_mail_message.text_part.to_s).not_to include("some first post content")
|
|
|
|
end
|
|
|
|
|
2021-06-27 23:19:17 -04:00
|
|
|
it "does not include in reply to post in email but still has the header" do
|
|
|
|
second_post = topic.posts.find_by(post_number: 2)
|
|
|
|
post.update!(reply_to_post_number: 1, reply_to_user: second_post.user)
|
|
|
|
PostReply.create(post: second_post, reply: post)
|
2021-06-27 20:42:06 -04:00
|
|
|
subject.execute(args)
|
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
2022-09-25 19:14:24 -04:00
|
|
|
expect(email_log.raw_headers).to include(
|
|
|
|
"In-Reply-To: <discourse/post/#{second_post.id}@#{Email::Sender.host_for(Discourse.base_url)}>",
|
|
|
|
)
|
2021-06-27 23:19:17 -04:00
|
|
|
expect(email_log.as_mail_message.html_part.to_s).not_to include(
|
|
|
|
I18n.t("user_notifications.in_reply_to"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-10-19 01:26:22 -04:00
|
|
|
it "includes the participants in the correct format (but not the recipient user), and does not have links for the staged users" do
|
2021-06-27 23:19:17 -04:00
|
|
|
subject.execute(args)
|
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
email_text = email_log.as_mail_message.text_part.to_s
|
|
|
|
expect(email_text).to include("Support Group")
|
|
|
|
expect(email_text).to include("otherguy@test.com")
|
|
|
|
expect(email_text).not_to include("[otherguy@test.com]")
|
|
|
|
expect(email_text).to include("cormac@lit.com")
|
|
|
|
expect(email_text).not_to include("[cormac@lit.com]")
|
|
|
|
expect(email_text).to include("normaluser")
|
2021-10-19 01:26:22 -04:00
|
|
|
expect(email_text).not_to include(recipient_user.username)
|
2021-06-27 20:42:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an EmailLog record with the correct details" do
|
|
|
|
subject.execute(args)
|
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log).not_to eq(nil)
|
2022-09-25 19:14:24 -04:00
|
|
|
expect(email_log.message_id).to eq("discourse/post/#{post.id}@test.localhost")
|
2021-06-27 20:42:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an IncomingEmail record with the correct details to avoid double processing IMAP" do
|
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 20:42:06 -04:00
|
|
|
incoming_email =
|
|
|
|
IncomingEmail.find_by(post_id: post.id, topic_id: post.topic_id, user_id: post.user.id)
|
|
|
|
expect(incoming_email).not_to eq(nil)
|
2022-09-25 19:14:24 -04:00
|
|
|
expect(incoming_email.message_id).to eq("discourse/post/#{post.id}@test.localhost")
|
2021-06-27 20:42:06 -04:00
|
|
|
expect(incoming_email.created_via).to eq(IncomingEmail.created_via_types[:group_smtp])
|
|
|
|
expect(incoming_email.to_addresses).to eq("test@test.com")
|
|
|
|
expect(incoming_email.cc_addresses).to eq("otherguy@test.com;cormac@lit.com")
|
|
|
|
expect(incoming_email.subject).to eq("Re: Help I need support")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not create a post reply key, it always replies to the group email_username" do
|
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 20:42:06 -04:00
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
post_reply_key = PostReplyKey.where(user_id: recipient_user, post_id: post.id).first
|
|
|
|
expect(post_reply_key).to eq(nil)
|
2021-07-19 21:56:04 -04:00
|
|
|
expect(email_log.raw_headers).not_to include("Reply-To: Support Group <#{group.email_username}")
|
|
|
|
expect(email_log.raw_headers).to include("From: Support Group <#{group.email_username}")
|
2021-06-27 20:42:06 -04:00
|
|
|
end
|
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
it "creates an EmailLog record with the correct details" do
|
2020-10-21 20:49:08 -04:00
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 18:55:13 -04:00
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log).not_to eq(nil)
|
2022-09-25 19:14:24 -04:00
|
|
|
expect(email_log.message_id).to eq("discourse/post/#{post.id}@test.localhost")
|
2020-10-21 20:49:08 -04:00
|
|
|
end
|
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
it "creates an IncomingEmail record with the correct details to avoid double processing IMAP" do
|
2020-10-21 20:49:08 -04:00
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 18:55:13 -04:00
|
|
|
incoming_email =
|
|
|
|
IncomingEmail.find_by(post_id: post.id, topic_id: post.topic_id, user_id: post.user.id)
|
|
|
|
expect(incoming_email).not_to eq(nil)
|
2022-09-25 19:14:24 -04:00
|
|
|
expect(incoming_email.message_id).to eq("discourse/post/#{post.id}@test.localhost")
|
2021-06-27 18:55:13 -04:00
|
|
|
expect(incoming_email.created_via).to eq(IncomingEmail.created_via_types[:group_smtp])
|
|
|
|
expect(incoming_email.to_addresses).to eq("test@test.com")
|
|
|
|
expect(incoming_email.cc_addresses).to eq("otherguy@test.com;cormac@lit.com")
|
|
|
|
expect(incoming_email.subject).to eq("Re: Help I need support")
|
2020-10-21 20:49:08 -04:00
|
|
|
end
|
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
it "does not create a post reply key, it always replies to the group email_username" do
|
2020-10-21 20:49:08 -04:00
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 18:55:13 -04:00
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
2020-10-27 17:01:58 -04:00
|
|
|
post_reply_key = PostReplyKey.where(user_id: recipient_user, post_id: post.id).first
|
2021-06-27 18:55:13 -04:00
|
|
|
expect(post_reply_key).to eq(nil)
|
2021-07-19 21:56:04 -04:00
|
|
|
expect(email_log.raw).not_to include("Reply-To: Support Group <#{group.email_username}")
|
|
|
|
expect(email_log.raw).to include("From: Support Group <#{group.email_username}")
|
2021-06-27 18:55:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "falls back to the group name if full name is blank" do
|
|
|
|
group.update(full_name: "")
|
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 18:55:13 -04:00
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
2021-07-19 21:56:04 -04:00
|
|
|
expect(email_log.raw_headers).to include("From: support-group <#{group.email_username}")
|
2020-10-21 20:49:08 -04:00
|
|
|
end
|
2021-01-05 00:32:04 -05:00
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
it "has the group_smtp_id and the to_address filled in correctly" do
|
2021-01-05 00:32:04 -05:00
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
expect(ActionMailer::Base.deliveries.last.subject).to eq("Re: Help I need support")
|
2021-06-27 18:55:13 -04:00
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log.to_address).to eq("test@test.com")
|
|
|
|
expect(email_log.smtp_group_id).to eq(group.id)
|
|
|
|
end
|
|
|
|
|
2021-11-15 17:15:11 -05:00
|
|
|
it "drops malformed cc addresses when sending the email" do
|
|
|
|
args2 = args.clone
|
|
|
|
args2[:cc_emails] << "somebadccemail@test.com<mailto:somebadccemail@test.com"
|
|
|
|
subject.execute(args2)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
last_email = ActionMailer::Base.deliveries.last
|
|
|
|
expect(last_email.subject).to eq("Re: Help I need support")
|
|
|
|
expect(last_email.cc).to match_array(%w[otherguy@test.com cormac@lit.com])
|
|
|
|
end
|
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
context "when there are cc_addresses" do
|
|
|
|
it "has the cc_addresses and cc_user_ids filled in correctly" do
|
|
|
|
subject.execute(args)
|
2021-07-05 00:56:32 -04:00
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
2023-01-04 17:07:50 -05:00
|
|
|
sent_mail = ActionMailer::Base.deliveries.last
|
|
|
|
expect(sent_mail.subject).to eq("Re: Help I need support")
|
|
|
|
expect(sent_mail.cc).to eq(%w[otherguy@test.com cormac@lit.com])
|
2021-06-27 18:55:13 -04:00
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log.cc_addresses).to eq("otherguy@test.com;cormac@lit.com")
|
2021-06-27 20:42:06 -04:00
|
|
|
expect(email_log.cc_user_ids).to match_array([staged1.id, staged2.id])
|
2021-06-27 18:55:13 -04:00
|
|
|
end
|
2023-01-04 17:07:50 -05:00
|
|
|
|
|
|
|
it "where cc_addresses match non-staged users, convert to bcc_addresses" do
|
|
|
|
staged2.update!(staged: false, active: true)
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
sent_mail = ActionMailer::Base.deliveries.last
|
|
|
|
expect(sent_mail.subject).to eq("Re: Help I need support")
|
|
|
|
expect(sent_mail.cc).to eq(["otherguy@test.com"])
|
|
|
|
expect(sent_mail.bcc).to eq(["cormac@lit.com"])
|
|
|
|
email_log =
|
|
|
|
EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
|
|
|
expect(email_log.cc_addresses).to eq("otherguy@test.com")
|
|
|
|
expect(email_log.bcc_addresses).to eq("cormac@lit.com")
|
|
|
|
expect(email_log.cc_user_ids).to match_array([staged1.id])
|
|
|
|
end
|
2021-01-05 00:32:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the post in the argument is the OP" do
|
|
|
|
let(:post_id) { post.topic.posts.first.id }
|
2021-07-14 00:23:14 -04:00
|
|
|
|
|
|
|
context "when the group has imap enabled" do
|
|
|
|
before { group.update!(imap_enabled: true) }
|
|
|
|
|
|
|
|
it "aborts and does not send a group SMTP email; the OP is the one that sent the email in the first place" do
|
|
|
|
expect { subject.execute(args) }.not_to(change { EmailLog.count })
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the group does not have imap enabled" do
|
|
|
|
before { group.update!(imap_enabled: false) }
|
|
|
|
|
|
|
|
it "sends the email as expected" do
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(1)
|
|
|
|
end
|
2021-07-05 00:56:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the post is deleted" do
|
|
|
|
it "aborts and adds a skipped email log" do
|
|
|
|
post.trash!
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
expect(
|
|
|
|
SkippedEmailLog.exists?(
|
|
|
|
email_type: "group_smtp",
|
|
|
|
user: recipient_user,
|
|
|
|
post: nil,
|
|
|
|
to_address: recipient_user.email,
|
|
|
|
reason_type: SkippedEmailLog.reason_types[:group_smtp_post_deleted],
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2021-07-05 00:56:32 -04:00
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the topic is deleted" do
|
|
|
|
it "aborts and adds a skipped email log" do
|
|
|
|
post.topic.trash!
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
expect(
|
|
|
|
SkippedEmailLog.exists?(
|
|
|
|
email_type: "group_smtp",
|
|
|
|
user: recipient_user,
|
|
|
|
post: post,
|
|
|
|
to_address: recipient_user.email,
|
|
|
|
reason_type: SkippedEmailLog.reason_types[:group_smtp_topic_deleted],
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2021-07-05 00:56:32 -04:00
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when smtp is not enabled" do
|
|
|
|
it "returns without sending email" do
|
|
|
|
SiteSetting.enable_smtp = false
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when disable_emails is yes" do
|
|
|
|
it "returns without sending email" do
|
|
|
|
SiteSetting.disable_emails = "yes"
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when group is deleted" do
|
2021-07-05 00:56:32 -04:00
|
|
|
it "returns without sending email" do
|
|
|
|
group.destroy
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when smtp is not enabled for the group" do
|
|
|
|
it "returns without sending email" do
|
|
|
|
group.update!(smtp_enabled: false)
|
|
|
|
subject.execute(args)
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
expect(
|
|
|
|
SkippedEmailLog.exists?(
|
|
|
|
email_type: "group_smtp",
|
|
|
|
user: recipient_user,
|
|
|
|
post: post,
|
|
|
|
to_address: recipient_user.email,
|
|
|
|
reason_type: SkippedEmailLog.reason_types[:group_smtp_disabled_for_group],
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2021-07-05 00:56:32 -04:00
|
|
|
).to eq(true)
|
2021-01-05 00:32:04 -05:00
|
|
|
end
|
|
|
|
end
|
2020-10-21 20:49:08 -04:00
|
|
|
end
|