Prefer custom SMTP config over Sendgrid (#16216)

This commit is contained in:
Mac Siri 2022-01-24 14:11:50 -05:00 committed by GitHub
parent a39850b05e
commit 3f2653a14d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 11 deletions

View file

@ -25,7 +25,7 @@ class ForemInstance
end
def self.smtp_enabled?
(Settings::SMTP.user_name.present? && Settings::SMTP.password.present?) || ENV["SENDGRID_API_KEY"].present?
Settings::SMTP.provided_minimum_settings? || ENV["SENDGRID_API_KEY"].present?
end
def self.invitation_only?

View file

@ -14,14 +14,24 @@ module Settings
class << self
def settings
return sendgrid_settings if ENV["SENDGRID_API_KEY"].present?
if provided_minimum_settings?
custom_provider_settings
else
fallback_sendgrid_settings
end
end
keys.index_with { |k| public_send(k) }.symbolize_keys
def provided_minimum_settings?
address.present? && user_name.present? && password.present?
end
private
def sendgrid_settings
def custom_provider_settings
to_h
end
def fallback_sendgrid_settings
{
address: "smtp.sendgrid.net",
port: 587,

View file

@ -23,6 +23,7 @@ RSpec.describe ApplicationMailer, type: :mailer do
end
it "sets proper SMTP credential during callback" do
Settings::SMTP.address = "smtp.google.com"
Settings::SMTP.user_name = Faker::Internet.username
Settings::SMTP.password = Faker::Internet.password
email.deliver_now

View file

@ -82,7 +82,8 @@ RSpec.describe ForemInstance, type: :model do
expect(described_class.smtp_enabled?).to be(false)
end
it "returns true if user_name and password are present" do
it "returns true if provided_minimum_settings?" do
allow(Settings::SMTP).to receive(:address).and_return("address")
allow(Settings::SMTP).to receive(:user_name).and_return("something")
allow(Settings::SMTP).to receive(:password).and_return("something")

View file

@ -1,16 +1,18 @@
require "rails_helper"
RSpec.describe Settings::SMTP do
let(:key) { "something" }
before { ENV["SENDGRID_API_KEY"] = key }
after do
described_class.clear_cache
ENV["SENDGRID_API_KEY"] = nil
end
describe "::settings" do
it "use default sendgrid config if SENDGRID_API_KEY is available" do
key = "something"
it "use falback sendgrid settings if address is not provided" do
domain = "test.com"
allow(ApplicationConfig).to receive(:[]).with("SENDGRID_API_KEY").and_return(key)
ENV["SENDGRID_API_KEY"] = "something"
allow(Settings::General).to receive(:app_domain).and_return(domain)
expect(described_class.settings).to eq({
@ -21,10 +23,9 @@ RSpec.describe Settings::SMTP do
password: key,
domain: domain
})
ENV["SENDGRID_API_KEY"] = nil
end
it "uses Settings::SMTP config if SENDGRID_API_KEY is not available" do
it "uses Settings::SMTP config if address is provided" do
described_class.address = "smtp.google.com"
described_class.port = 25
described_class.authentication = "plain"
@ -42,4 +43,20 @@ RSpec.describe Settings::SMTP do
})
end
end
describe "::provided_minimum_settings?" do
it "returns true if addess, user_name, and password are provided" do
described_class.address = "smtp.google.com"
described_class.user_name = "username"
described_class.password = "password"
expect(described_class.provided_minimum_settings?).to be true
end
it "returns false if one of addess, user_name, or password is missing" do
described_class.address = "smtp.google.com"
expect(described_class.provided_minimum_settings?).to be false
end
end
end

View file

@ -12,6 +12,7 @@ seeder = Seeder.new
Settings::UserExperience.public = true
Settings::General.waiting_on_first_user = false
Settings::Authentication.allow_email_password_registration = true
Settings::SMTP.address = "smtp.website.com"
Settings::SMTP.user_name = "username"
Settings::SMTP.password = "password"