2020-10-21 20:49:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Jobs::GroupSmtpEmail do
|
2021-01-05 00:32:04 -05:00
|
|
|
fab!(:post) do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
Fabricate(:post, topic: topic)
|
|
|
|
Fabricate(:post, topic: topic)
|
|
|
|
end
|
|
|
|
fab!(:group) { Fabricate(:imap_group) }
|
|
|
|
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,
|
2020-10-21 20:49:08 -04:00
|
|
|
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)
|
2020-10-27 17:01:58 -04:00
|
|
|
Email::Sender.expects(:new).with(message, :group_smtp, recipient_user).returns(stub(send: nil))
|
2020-10-21 20:49:08 -04:00
|
|
|
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)
|
2021-01-05 00:32:04 -05:00
|
|
|
expect(incoming.message_id).to eq("topic/#{post.topic_id}/#{post.id}@test.localhost")
|
2021-01-19 22:22:41 -05:00
|
|
|
expect(incoming.created_via).to eq(IncomingEmail.created_via_types[:group_smtp])
|
2020-10-21 20:49:08 -04:00
|
|
|
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)
|
2020-10-27 17:01:58 -04:00
|
|
|
post_reply_key = PostReplyKey.where(user_id: recipient_user, post_id: post.id).first
|
2020-10-21 20:49:08 -04:00
|
|
|
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
|
2021-01-05 00:32:04 -05:00
|
|
|
|
|
|
|
it "has the from_address and the to_addresses and subject filled in correctly" do
|
|
|
|
subject.execute(args)
|
|
|
|
incoming = IncomingEmail.find_by(post_id: post.id, user_id: post.user_id, topic_id: post.topic_id)
|
|
|
|
expect(incoming.to_addresses).to eq("test@test.com")
|
|
|
|
expect(incoming.subject).to include("Re: This is a test topic")
|
|
|
|
expect(incoming.from_address).to eq("discourseteam@ponyexpress.com")
|
|
|
|
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
|
|
|
|
expect { subject.execute(args) }.not_to(change { IncomingEmail.count })
|
|
|
|
end
|
|
|
|
end
|
2020-10-21 20:49:08 -04:00
|
|
|
end
|