2020-07-10 17:05:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require "email/receiver"
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe GroupSmtpMailer do
|
2021-06-28 06:55:13 +08:00
|
|
|
let(:group) do
|
2020-07-10 17:05:55 +08:00
|
|
|
Fabricate(
|
|
|
|
:group,
|
2020-10-28 05:01:58 +08:00
|
|
|
name: "Testers",
|
|
|
|
title: "Tester",
|
|
|
|
full_name: "Testers Group",
|
|
|
|
smtp_server: "smtp.gmail.com",
|
|
|
|
smtp_port: 587,
|
|
|
|
smtp_ssl: true,
|
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.
### UI
Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.
You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.
If there is an issue with the configuration or credentials a meaningful error message should be shown.
IMAP settings must also be validated when IMAP is enabled, before saving.
When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):
### Database & Backend
This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.
For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.
There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-28 07:28:18 +08:00
|
|
|
smtp_enabled: true,
|
2020-10-28 05:01:58 +08:00
|
|
|
imap_server: "imap.gmail.com",
|
|
|
|
imap_port: 993,
|
|
|
|
imap_ssl: true,
|
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.
### UI
Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.
You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.
If there is an issue with the configuration or credentials a meaningful error message should be shown.
IMAP settings must also be validated when IMAP is enabled, before saving.
When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):
### Database & Backend
This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.
For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.
There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-28 07:28:18 +08:00
|
|
|
imap_enabled: true,
|
2020-10-28 05:01:58 +08:00
|
|
|
email_username: "bugs@gmail.com",
|
|
|
|
email_password: "super$secret$password",
|
|
|
|
)
|
2021-06-28 06:55:13 +08:00
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
let(:user) do
|
2020-07-10 17:05:55 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add_owner(user)
|
|
|
|
user
|
2021-06-28 06:55:13 +08:00
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
let(:email) { <<~EMAIL }
|
2022-04-21 00:05:17 +08: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-05 06:07:50 +08:00
|
|
|
Cc: someotherperson@test.com
|
2022-04-21 00:05:17 +08: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-03-01 03:50:55 +08:00
|
|
|
EMAIL
|
2021-06-28 06:55:13 +08:00
|
|
|
|
|
|
|
let(:receiver) do
|
|
|
|
receiver = Email::Receiver.new(email, destinations: [group], uid_validity: 1, uid: 10_000)
|
|
|
|
receiver.process!
|
|
|
|
receiver
|
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
|
|
|
|
let(:raw) { "hello, how are you doing?" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_smtp = true
|
2020-10-28 05:01:58 +08:00
|
|
|
SiteSetting.enable_imap = true
|
2020-07-10 17:05:55 +08:00
|
|
|
Jobs.run_immediately!
|
2021-06-28 06:55:13 +08: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 17:05:55 +08:00
|
|
|
end
|
|
|
|
|
2023-06-19 21:22:00 +08: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
|
|
|
|
|
2020-07-10 17:05:55 +08:00
|
|
|
it "sends an email as reply" do
|
2020-10-28 05:01:58 +08:00
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
2020-07-10 17:05:55 +08:00
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
|
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
|
|
|
expect(sent_mail.to).to contain_exactly("john@doe.com")
|
2023-01-05 06:07:50 +08:00
|
|
|
expect(sent_mail.cc).to contain_exactly("someotherperson@test.com")
|
2021-06-28 06:55:13 +08:00
|
|
|
expect(sent_mail.reply_to).to eq(nil)
|
2020-07-10 17:05:55 +08:00
|
|
|
expect(sent_mail.subject).to eq("Re: Hello from John")
|
|
|
|
expect(sent_mail.to_s).to include(raw)
|
|
|
|
end
|
|
|
|
|
2022-04-21 00:05:17 +08: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]
|
|
|
|
expect(sent_mail.to_s).to include(
|
|
|
|
"[Testers Group (1)](http://test.localhost/g/Testers), james.bond@gmail.com",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
it "uses the OP incoming email subject for the subject over topic title" do
|
|
|
|
receiver.incoming_email.topic.update(title: "blah")
|
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
|
|
|
expect(sent_mail.subject).to eq("Re: Hello from John")
|
|
|
|
end
|
|
|
|
|
2020-10-28 05:01:58 +08: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
|
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
it "uses the correct IMAP/SMTP reply to address and does not create a post reply key" do
|
2020-10-28 05:01:58 +08:00
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
expect(PostReplyKey.find_by(user_id: user.id, post_id: post.id)).to eq(nil)
|
|
|
|
|
2020-10-28 05:01:58 +08:00
|
|
|
sent_mail = ActionMailer::Base.deliveries[0]
|
2021-06-28 06:55:13 +08:00
|
|
|
expect(sent_mail.reply_to).to eq(nil)
|
|
|
|
expect(sent_mail.from).to contain_exactly("bugs@gmail.com")
|
2020-10-28 05:01:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when IMAP is disabled for the group" do
|
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.
### UI
Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.
You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.
If there is an issue with the configuration or credentials a meaningful error message should be shown.
IMAP settings must also be validated when IMAP is enabled, before saving.
When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):
### Database & Backend
This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.
For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.
There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-28 07:28:18 +08:00
|
|
|
before { group.update(imap_enabled: false) }
|
2020-10-28 05:01:58 +08:00
|
|
|
|
2021-06-28 06:55:13 +08:00
|
|
|
it "does send the email" do
|
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when SMTP is disabled for the group" do
|
|
|
|
before { group.update(smtp_enabled: false) }
|
|
|
|
|
2021-06-07 12:17:35 +08:00
|
|
|
it "does not send the email" do
|
2020-10-28 05:01:58 +08:00
|
|
|
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
|
|
|
|
|
2021-06-07 12:17:35 +08:00
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(0)
|
2020-10-28 05:01:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-10 17:05:55 +08:00
|
|
|
end
|