2020-10-22 08:49:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Jobs::GroupSmtpEmail do
|
2021-01-05 13:32:04 +08:00
|
|
|
fab!(:post) do
|
2021-06-28 06:55:13 +08:00
|
|
|
topic = Fabricate(:topic, title: "Help I need support")
|
2021-01-05 13:32:04 +08:00
|
|
|
Fabricate(:post, topic: topic)
|
|
|
|
Fabricate(:post, topic: topic)
|
|
|
|
end
|
2021-06-28 06:55:13 +08:00
|
|
|
fab!(:group) { Fabricate(:smtp_group, name: "support-group", full_name: "Support Group") }
|
2021-01-05 13:32:04 +08:00
|
|
|
fab!(:recipient_user) { Fabricate(:user, email: "test@test.com") }
|
|
|
|
let(:post_id) { post.id }
|
2020-10-22 08:49:08 +08:00
|
|
|
let(:args) do
|
|
|
|
{
|
|
|
|
group_id: group.id,
|
2021-01-05 13:32:04 +08:00
|
|
|
post_id: post_id,
|
2021-06-28 06:55:13 +08:00
|
|
|
email: "test@test.com",
|
|
|
|
cc_emails: ["otherguy@test.com", "cormac@lit.com"]
|
2020-10-22 08:49:08 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2021-06-28 06:55:13 +08:00
|
|
|
SiteSetting.enable_smtp = true
|
2020-10-22 08:49:08 +08:00
|
|
|
SiteSetting.manual_polling_enabled = true
|
2021-06-28 06:55:13 +08:00
|
|
|
SiteSetting.reply_by_email_address = "test+%{reply_key}@test.com"
|
2020-10-22 08:49:08 +08:00
|
|
|
SiteSetting.reply_by_email_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends an email using the GroupSmtpMailer and Email::Sender" do
|
|
|
|
message = Mail::Message.new(body: "hello", to: "myemail@example.invalid")
|
2021-06-28 06:55:13 +08:00
|
|
|
GroupSmtpMailer.expects(:send_mail).with(group, "test@test.com", post, ["otherguy@test.com", "cormac@lit.com"]).returns(message)
|
|
|
|
subject.execute(args)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates an EmailLog record with the correct details" do
|
2020-10-22 08:49:08 +08:00
|
|
|
subject.execute(args)
|
2021-06-28 06:55:13 +08: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)
|
|
|
|
expect(email_log.message_id).to eq("topic/#{post.topic_id}/#{post.id}@test.localhost")
|
2020-10-22 08:49:08 +08:00
|
|
|
end
|
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
it "creates an IncomingEmail record with the correct details to avoid double processing IMAP" do
|
2020-10-22 08:49:08 +08:00
|
|
|
subject.execute(args)
|
2021-06-28 06:55:13 +08: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)
|
|
|
|
expect(incoming_email.message_id).to eq("topic/#{post.topic_id}/#{post.id}@test.localhost")
|
|
|
|
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-22 08:49:08 +08:00
|
|
|
end
|
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
it "does not create a post reply key, it always replies to the group email_username" do
|
2020-10-22 08:49:08 +08:00
|
|
|
subject.execute(args)
|
2021-06-28 06:55:13 +08:00
|
|
|
email_log = EmailLog.find_by(post_id: post.id, topic_id: post.topic_id, user_id: recipient_user.id)
|
2020-10-28 05:01:58 +08:00
|
|
|
post_reply_key = PostReplyKey.where(user_id: recipient_user, post_id: post.id).first
|
2021-06-28 06:55:13 +08:00
|
|
|
expect(post_reply_key).to eq(nil)
|
|
|
|
expect(email_log.raw).not_to include("Reply-To: Support Group via Discourse <#{group.email_username}")
|
|
|
|
expect(email_log.raw).to include("From: Support Group via Discourse <#{group.email_username}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "falls back to the group name if full name is blank" do
|
|
|
|
group.update(full_name: "")
|
|
|
|
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.raw).to include("From: support-group via Discourse <#{group.email_username}")
|
2020-10-22 08:49:08 +08:00
|
|
|
end
|
2021-01-05 13:32:04 +08:00
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
it "has the group_smtp_id and the to_address filled in correctly" do
|
2021-01-05 13:32:04 +08:00
|
|
|
subject.execute(args)
|
2021-06-28 06:55:13 +08: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
|
|
|
|
|
|
|
|
context "when there are cc_addresses" do
|
|
|
|
let!(:cormac_user) { Fabricate(:user, email: "cormac@lit.com") }
|
|
|
|
|
|
|
|
it "has the cc_addresses and cc_user_ids filled in correctly" 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.cc_addresses).to eq("otherguy@test.com;cormac@lit.com")
|
|
|
|
expect(email_log.cc_user_ids).to eq([cormac_user.id])
|
|
|
|
end
|
2021-01-05 13:32:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the post in the argument is the OP" do
|
|
|
|
let(:post_id) { post.topic.posts.first.id }
|
|
|
|
it "aborts and does not send a group SMTP email; the OP is the one that sent the email in the first place" do
|
2021-06-28 06:55:13 +08:00
|
|
|
expect { subject.execute(args) }.not_to(change { EmailLog.count })
|
2021-01-05 13:32:04 +08:00
|
|
|
end
|
|
|
|
end
|
2020-10-22 08:49:08 +08:00
|
|
|
end
|