0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/spec/mailers/group_smtp_mailer_spec.rb
David Battersby 1450851538
FIX: allow custom email sender name via translation overrides (#40674)
The translation key for email_from was mistakenly removed in #36974 as
it was thought to be redundant since we were dropping the "via site
name" text from notification emails. However we should still allow sites
to override this.

This change restores the translation key for `email_from` and adds a new
`email_from_group` to allow overriding these values.

Both keys also whitelist `site_name` for interpolation, despite omitting
it from the default translation.
2026-06-09 14:03:03 +04:00

165 lines
5.4 KiB
Ruby
Vendored

# frozen_string_literal: true
require "email/receiver"
RSpec.describe GroupSmtpMailer do
let(:group) do
Fabricate(
:group,
name: "Testers",
title: "Tester",
full_name: "Testers Group",
smtp_server: "smtp.gmail.com",
smtp_port: 587,
smtp_ssl_mode: Group.smtp_ssl_modes[:starttls],
smtp_enabled: true,
email_username: "bugs@gmail.com",
email_password: "super$secret$password",
)
end
let(:user) do
user = Fabricate(:user)
group.add_owner(user)
user
end
let(:email) { <<~EMAIL }
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>
Cc: someotherperson@test.com
Content-Type: text/plain; charset="UTF-8"
Hello,
How are you doing?
EMAIL
let(:receiver) do
receiver = Email::Receiver.new(email, destinations: [group], uid_validity: 1, uid: 10_000)
receiver.process!
receiver
end
let(:raw) { "hello, how are you doing?" }
before do
SiteSetting.enable_smtp = true
Jobs.run_immediately!
SiteSetting.manual_polling_enabled = true
SiteSetting.reply_by_email_address = "test+%{reply_key}@test.com"
SiteSetting.reply_by_email_enabled = true
end
it "sends an email as reply" do
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
expect(ActionMailer::Base.deliveries.size).to eq(1)
sent_mail = ActionMailer::Base.deliveries[0]
expect(sent_mail.to).to contain_exactly("john@doe.com")
expect(sent_mail.cc).to contain_exactly("someotherperson@test.com")
expect(sent_mail.reply_to).to eq(nil)
expect(sent_mail.subject).to eq("Re: Hello from John")
expect(sent_mail.to_s).to include(raw)
end
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
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
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,
enable_ssl: false,
return_response: true,
open_timeout: GlobalSetting.group_smtp_open_timeout.to_f,
read_timeout: GlobalSetting.group_smtp_read_timeout.to_f,
},
)
end
it "lets admins add the site name to the sender name via the email_from_group override" do
TranslationOverride.upsert!(
SiteSetting.default_locale,
"email_from_group",
"%{group_name} via %{site_name}",
)
mail = GroupSmtpMailer.send_mail(group, user.email, Fabricate(:post))
expect(mail[:from].display_names).to eql(["Testers Group via #{Email.site_title}"])
end
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
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
it "uses the correct 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)
expect(ActionMailer::Base.deliveries.size).to eq(1)
expect(PostReplyKey.find_by(user_id: user.id, post_id: post.id)).to eq(nil)
sent_mail = ActionMailer::Base.deliveries[0]
expect(sent_mail.reply_to).to eq(nil)
expect(sent_mail.from).to contain_exactly("bugs@gmail.com")
end
context "when SMTP is disabled for the group" do
before { group.update(smtp_enabled: false) }
it "does not send the email" do
post = PostCreator.create(user, topic_id: receiver.incoming_email.topic.id, raw: raw)
expect(ActionMailer::Base.deliveries.size).to eq(0)
end
end
end
end