Allow setting nil value in Settings::Upsert (#14228)

This commit is contained in:
Mac Siri 2021-07-20 10:57:09 -04:00 committed by GitHub
parent c4185c1339
commit b06b446176
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View file

@ -8,13 +8,13 @@ module Settings
# the cache, or call Settings::Smtp.clear_cache
cache_prefix { "v1" }
field :address, type: :string, default: ApplicationConfig["SMTP_ADDRESS"]
field :authentication, type: :string, default: ApplicationConfig["SMTP_AUTHENTICATION"],
field :address, type: :string, default: ApplicationConfig["SMTP_ADDRESS"].presence
field :authentication, type: :string, default: ApplicationConfig["SMTP_AUTHENTICATION"].presence,
validates: { inclusion: %w[plain login cram_md5] }
field :domain, type: :string, default: ApplicationConfig["SMTP_DOMAIN"]
field :password, type: :string, default: ApplicationConfig["SMTP_PASSWORD"]
field :domain, type: :string, default: ApplicationConfig["SMTP_DOMAIN"].presence
field :password, type: :string, default: ApplicationConfig["SMTP_PASSWORD"].presence
field :port, type: :integer, default: ApplicationConfig["SMTP_PORT"].presence || 25
field :user_name, type: :string, default: ApplicationConfig["SMTP_USER_NAME"]
field :user_name, type: :string, default: ApplicationConfig["SMTP_USER_NAME"].presence
class << self
def settings

View file

@ -32,6 +32,8 @@ module Settings
settings_class.public_send("#{key}=", value.to_h)
elsif value.present?
settings_class.public_send("#{key}=", value.strip)
elsif value.blank?
settings_class.public_send("#{key}=", nil)
end
rescue ActiveRecord::RecordInvalid => e
@errors << e.message

View file

@ -3,6 +3,8 @@ class ValidDomainCsvValidator < ActiveModel::EachValidator
VALID_DOMAIN = /^[a-zA-Z0-9]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.freeze
def validate_each(record, attribute, value)
return unless value
return if value.all? { |domain| domain.match?(VALID_DOMAIN) }
record.errors.add(attribute, options[:message] || DEFAULT_MESSAGE)

View file

@ -793,6 +793,32 @@ RSpec.describe "/admin/customization/config", type: :request do
end
end
describe "SMTP configs" do
after { Settings::SMTP.clear_cache }
it "updates appropriate SMTP configs" do
expected_handle = { "address" => "smtp.example.com", "port" => "1234" }
post admin_settings_smtp_settings_path, params: {
settings_smtp: expected_handle,
confirmation: confirmation_message
}
expect(Settings::SMTP.address).to eq("smtp.example.com")
expect(Settings::SMTP.port).to eq(1234)
end
it "unsets appropriate SMTP config, and apply default value if applicable" do
Settings::SMTP.address = "smtp.example.com"
Settings::SMTP.port = 12_345
expected_handle = { "address" => "", "port" => "" }
post admin_settings_smtp_settings_path, params: {
settings_smtp: expected_handle,
confirmation: confirmation_message
}
expect(Settings::SMTP.address).to eq(nil)
expect(Settings::SMTP.port).to eq(25)
end
end
describe "Sponsors" do
it "updates the sponsor_headline" do
headline = "basic"