mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 17:33:40 +08:00
632942e697
There is a site setting reply_by_email_enabled which when combined with reply_by_email_address creates a Reply-To header in emails in the format "test+%{reply_key}@test.com" along with a PostReplyKey record, so when replying Discourse knows where to route the reply.
However this conflicts with the IMAP implementation. Since we are sending the email for a group via SMTP and from their actual email account, we want all replys to go to that email account as well so the IMAP sync job can pick them up and put them in the correct place. So if the group has IMAP enabled and configured, then the reply-to header will be correct.
This PR also makes a further fix to 64b0b50
by using the correct recipient user for the PostReplyKey record. If the post user is used we encounter this error:
if destination.user_id != user.id && !forwarded_reply_key?(destination, user)
raise ReplyUserNotMatchingError, "post_reply_key.user_id => #{destination.user_id.inspect}, user.id => #{user.id.inspect}"
end
This is because the user above is found from the from_address, but the destination which is the PostReplyKey is made by the post.user, which will be different people.
46 lines
1.7 KiB
Ruby
46 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
RSpec.describe Jobs::GroupSmtpEmail do
|
|
let(:post) { Fabricate(:post) }
|
|
let(:group) { Fabricate(:imap_group) }
|
|
let!(:recipient_user) { Fabricate(:user, email: "test@test.com") }
|
|
let(:args) do
|
|
{
|
|
group_id: group.id,
|
|
post_id: post.id,
|
|
email: "test@test.com"
|
|
}
|
|
end
|
|
|
|
before do
|
|
SiteSetting.reply_by_email_address = "test+%{reply_key}@incoming.com"
|
|
SiteSetting.manual_polling_enabled = true
|
|
SiteSetting.reply_by_email_enabled = true
|
|
SiteSetting.enable_smtp = true
|
|
end
|
|
|
|
it "sends an email using the GroupSmtpMailer and Email::Sender" do
|
|
message = Mail::Message.new(body: "hello", to: "myemail@example.invalid")
|
|
GroupSmtpMailer.expects(:send_mail).with(group, "test@test.com", post).returns(message)
|
|
Email::Sender.expects(:new).with(message, :group_smtp, recipient_user).returns(stub(send: nil))
|
|
subject.execute(args)
|
|
end
|
|
|
|
it "creates an IncomingEmail record to avoid double processing via IMAP" do
|
|
subject.execute(args)
|
|
incoming = IncomingEmail.find_by(post_id: post.id, user_id: post.user_id, topic_id: post.topic_id)
|
|
expect(incoming).not_to eq(nil)
|
|
expect(incoming.message_id).to eq("topic/#{post.topic_id}@test.localhost")
|
|
end
|
|
|
|
it "creates a PostReplyKey and correctly uses it for the email reply_key substitution" do
|
|
subject.execute(args)
|
|
incoming = IncomingEmail.find_by(post_id: post.id, user_id: post.user_id, topic_id: post.topic_id)
|
|
post_reply_key = PostReplyKey.where(user_id: recipient_user, post_id: post.id).first
|
|
expect(post_reply_key).not_to eq(nil)
|
|
expect(incoming.raw).to include("Reply-To: Discourse <test+#{post_reply_key.reply_key}@incoming.com>")
|
|
end
|
|
end
|