2020-07-10 04:05:55 -05:00
|
|
|
# frozen_string_literal: true
|
2023-01-09 05:18:21 -06:00
|
|
|
require "email/receiver"
|
2020-07-10 04:05:55 -05:00
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe GroupSmtpMailer do
|
2021-06-27 17:55:13 -05:00
|
|
|
let(:group) do
|
2023-01-09 05:18:21 -06:00
|
|
|
Fabricate(
|
|
|
|
:group,
|
|
|
|
name: "Testers",
|
|
|
|
title: "Tester",
|
|
|
|
full_name: "Testers Group",
|
|
|
|
smtp_server: "smtp.gmail.com",
|
|
|
|
smtp_port: 587,
|
2024-07-17 19:33:14 -05:00
|
|
|
smtp_ssl_mode: Group.smtp_ssl_modes[:starttls],
|
2023-01-09 05:18:21 -06:00
|
|
|
smtp_enabled: true,
|
|
|
|
imap_server: "imap.gmail.com",
|
|
|
|
imap_port: 993,
|
|
|
|
imap_ssl: true,
|
|
|
|
imap_enabled: true,
|
|
|
|
email_username: "bugs@gmail.com",
|
|
|
|
email_password: "super$secret$password",
|
|
|
|
)
|
2021-06-27 17:55:13 -05:00
|
|
|
end
|
2020-07-10 04:05:55 -05:00
|
|
|
|
2021-06-27 17:55:13 -05:00
|
|
|
let(:user) do
|
2020-07-10 04:05:55 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add_owner(user)
|
|
|
|
user
|
2021-06-27 17:55:13 -05:00
|
|
|
end
|
2020-07-10 04:05:55 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
let(:email) { <<~EMAIL }
|
2022-04-20 11:05:17 -05:00
|
|
|
Delivered-To: bugs@gmail.com
|
|
|
|
MIME-Version: 1.0
|
|
|
|
From: John Doe <john@doe.com>
|
|
|
|
Date: Tue, 01 Jan 2019 12:00:00 +0200
|
|
|
|
Message-ID: <a52f67a3d3560f2a35276cda8519b10b595623bcb66912bb92df6651ad5f75be@mail.gmail.com>
|
|
|
|
Subject: Hello from John
|
|
|
|
To: "bugs@gmail.com" <bugs@gmail.com>
|
2023-01-04 16:07:50 -06:00
|
|
|
Cc: someotherperson@test.com
|
2022-04-20 11:05:17 -05:00
|
|
|
Content-Type: text/plain; charset="UTF-8"
|
|
|
|
|
|
|
|
Hello,
|
|
|
|
|
|
|
|
How are you doing?
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
EMAIL
|
2021-06-27 17:55:13 -05:00
|
|
|
|
|
|
|
let(:receiver) do
|
2023-01-09 05:18:21 -06:00
|
|
|
receiver = Email::Receiver.new(email, destinations: [group], uid_validity: 1, uid: 10_000)
|
2021-06-27 17:55:13 -05:00
|
|
|
receiver.process!
|
|
|
|
receiver
|
|
|
|
end
|
2020-07-10 04:05:55 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
let(:raw) { "hello, how are you doing?" }
|
2020-07-10 04:05:55 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_smtp = true
|
2020-10-27 16:01:58 -05:00
|
|
|
SiteSetting.enable_imap = true
|
2020-07-10 04:05:55 -05:00
|
|
|
Jobs.run_immediately!
|
2021-06-27 17:55:13 -05:00
|
|
|
SiteSetting.manual_polling_enabled = true
|
|
|
|
SiteSetting.reply_by_email_address = "test+%{reply_key}@test.com"
|
|
|
|
SiteSetting.reply_by_email_enabled = true
|
2020-07-10 04:05:55 -05:00
|
|
|
end
|
|
|
|
|
2023-06-19 08:22:00 -05:00
|
|
|
it "sends an email for first post when IMAP is disabled" do
|
|
|
|
staged = Fabricate(:staged)
|
|
|
|
group.update(imap_enabled: false)
|
|
|
|
|
|
|
|
PostCreator.create!(
|
|
|
|
user,
|
|
|
|
skip_validations: true,
|
|
|
|
title: "Hello from John",
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: staged.username,
|
|
|
|
target_group_names: group.name,
|
|
|
|
raw: raw,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
|
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
|
|
|
expect(sent_mail.to).to contain_exactly(staged.email)
|
|
|
|
expect(sent_mail.subject).to eq("Hello from John")
|
|
|
|
expect(sent_mail.to_s).to include(raw)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "sends an email as reply" do
|
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
2020-07-10 04:05:55 -05:00
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
|
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(sent_mail.to).to contain_exactly("john@doe.com")
|
|
|
|
expect(sent_mail.cc).to contain_exactly("someotherperson@test.com")
|
2021-06-27 17:55:13 -05:00
|
|
|
expect(sent_mail.reply_to).to eq(nil)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(sent_mail.subject).to eq("Re: Hello from John")
|
2020-07-10 04:05:55 -05:00
|
|
|
expect(sent_mail.to_s).to include(raw)
|
|
|
|
end
|
|
|
|
|
2022-04-20 11:05:17 -05:00
|
|
|
it "includes the participants list in the email" do
|
|
|
|
Fabricate(:staged, email: "james.bond@gmail.com")
|
|
|
|
topic = receiver.incoming_email.topic
|
|
|
|
topic.invite(Discourse.system_user, "james.bond@gmail.com")
|
|
|
|
|
|
|
|
PostCreator.create(user, topic_id: topic.id, raw: raw)
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
|
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(sent_mail.to_s).to include(
|
|
|
|
"[Testers Group (1)](http://test.localhost/g/Testers), james.bond@gmail.com",
|
|
|
|
)
|
2022-04-20 11:05:17 -05:00
|
|
|
end
|
|
|
|
|
2021-06-27 17:55:13 -05:00
|
|
|
it "uses the OP incoming email subject for the subject over topic title" do
|
|
|
|
receiver.incoming_email.topic.update(title: "blah")
|
2023-01-09 05:18:21 -06:00
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
2021-06-27 17:55:13 -05:00
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(sent_mail.subject).to eq("Re: Hello from John")
|
2021-06-27 17:55:13 -05:00
|
|
|
end
|
|
|
|
|
2023-11-27 23:32:59 -06:00
|
|
|
it "configures delivery options for SMTP correctly" do
|
|
|
|
mail = GroupSmtpMailer.send_mail(group, user.email, Fabricate(:post))
|
|
|
|
expect(mail.delivery_method.settings).to eq(
|
|
|
|
{
|
|
|
|
address: "smtp.gmail.com",
|
|
|
|
port: 587,
|
|
|
|
domain: "gmail.com",
|
|
|
|
user_name: "bugs@gmail.com",
|
|
|
|
password: "super$secret$password",
|
|
|
|
authentication: GlobalSetting.smtp_authentication,
|
|
|
|
enable_starttls_auto: true,
|
2024-07-17 19:33:14 -05:00
|
|
|
enable_ssl: false,
|
2023-11-27 23:32:59 -06:00
|
|
|
return_response: true,
|
|
|
|
open_timeout: GlobalSetting.group_smtp_open_timeout,
|
|
|
|
read_timeout: GlobalSetting.group_smtp_read_timeout,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-07-16 01:21:14 -05:00
|
|
|
it "uses the login SMTP authentication method for office365" do
|
|
|
|
group.update!(smtp_server: "smtp.office365.com")
|
|
|
|
mail = GroupSmtpMailer.send_mail(group, user.email, Fabricate(:post))
|
|
|
|
expect(mail.delivery_method.settings[:authentication]).to eq("login")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the login SMTP authentication method for outlook" do
|
|
|
|
group.update!(smtp_server: "smtp-mail.outlook.com")
|
|
|
|
mail = GroupSmtpMailer.send_mail(group, user.email, Fabricate(:post))
|
|
|
|
expect(mail.delivery_method.settings[:authentication]).to eq("login")
|
|
|
|
end
|
|
|
|
|
2020-10-27 16:01:58 -05:00
|
|
|
context "when the site has a reply by email address configured" do
|
|
|
|
before do
|
|
|
|
SiteSetting.manual_polling_enabled = true
|
|
|
|
SiteSetting.reply_by_email_address = "test+%{reply_key}@test.com"
|
|
|
|
SiteSetting.reply_by_email_enabled = true
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "uses the correct IMAP/SMTP reply to address and does not create a post reply key" do
|
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
2020-10-27 16:01:58 -05:00
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
|
2021-06-27 17:55:13 -05:00
|
|
|
expect(PostReplyKey.find_by(user_id: user.id, post_id: post.id)).to eq(nil)
|
|
|
|
|
2020-10-27 16:01:58 -05:00
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
2021-06-27 17:55:13 -05:00
|
|
|
expect(sent_mail.reply_to).to eq(nil)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(sent_mail.from).to contain_exactly("bugs@gmail.com")
|
2020-10-27 16:01:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when IMAP is disabled for the group" do
|
2023-01-09 05:18:21 -06:00
|
|
|
before { group.update(imap_enabled: false) }
|
2020-10-27 16:01:58 -05:00
|
|
|
|
2021-06-27 17:55:13 -05:00
|
|
|
it "does send the email" do
|
2023-01-09 05:18:21 -06:00
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
2021-06-27 17:55:13 -05:00
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when SMTP is disabled for the group" do
|
2023-01-09 05:18:21 -06:00
|
|
|
before { group.update(smtp_enabled: false) }
|
2021-06-27 17:55:13 -05:00
|
|
|
|
2021-06-06 23:17:35 -05:00
|
|
|
it "does not send the email" do
|
2023-01-09 05:18:21 -06:00
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
2020-10-27 16:01:58 -05:00
|
|
|
|
2021-06-06 23:17:35 -05:00
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(0)
|
2020-10-27 16:01:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-10 04:05:55 -05:00
|
|
|
end
|