add validator for the 'reply_by_email_enabled' site setting

This commit is contained in:
Régis Hanol 2016-02-09 23:35:40 +01:00
parent 13983c2f80
commit 8944d62aa6
4 changed files with 59 additions and 1 deletions

View file

@ -1263,6 +1263,8 @@ en:
pop3_polling_username_is_empty: "You must set a 'pop3 polling username' before enabling POP3 polling."
pop3_polling_password_is_empty: "You must set a 'pop3 polling password' before enabling POP3 polling."
pop3_polling_authentication_failed: "POP3 authentication failed. Please verify your pop3 credentials."
reply_by_email_address_is_empty: "You must set a 'reply by email address' before enabling reply by email."
pop3_polling_disabled: "You must first enabled POP3 polling before enabling reply by email."
notification_types:
group_mentioned: "%{group_name} was mentioned in %{link}"

View file

@ -506,7 +506,9 @@ email:
client: true
email_custom_headers: 'Auto-Submitted: auto-generated'
email_subject: '[%{site_name}] %{optional_pm}%{optional_cat}%{topic_title}'
reply_by_email_enabled: false
reply_by_email_enabled:
default: false
validator: "ReplyByEmailEnabledValidator"
reply_by_email_address:
default: ''
validator: "ReplyByEmailAddressValidator"

View file

@ -0,0 +1,23 @@
class ReplyByEmailEnabledValidator
def initialize(opts={})
@opts = opts
end
def valid_value?(val)
# only validate when enabling reply by email
return true if val == "f"
# ensure reply_by_email_address is configured && polling is working
SiteSetting.reply_by_email_address.present? &&
SiteSetting.pop3_polling_enabled?
end
def error_message
if SiteSetting.reply_by_email_address.blank?
I18n.t("site_settings.errors.reply_by_email_address_is_empty")
else
I18n.t("site_settings.errors.pop3_polling_disabled")
end
end
end

View file

@ -0,0 +1,31 @@
require 'rails_helper'
describe ReplyByEmailEnabledValidator do
describe '#valid_value?' do
subject(:validator) { described_class.new }
it "only validates when enabling the setting" do
expect(validator.valid_value?("f")).to eq(true)
end
it "returns false if reply_by_email_address is missing" do
SiteSetting.expects(:reply_by_email_address).returns("")
expect(validator.valid_value?("t")).to eq(false)
end
it "returns false if POP3 polling is disabled" do
SiteSetting.expects(:reply_by_email_address).returns("foo.%{reply_key}+42@bar.com")
SiteSetting.expects(:pop3_polling_enabled).returns(false)
expect(validator.valid_value?("t")).to eq(false)
end
it "returns true when POP3 polling is enabled and the reply_by_email_address is configured" do
SiteSetting.expects(:reply_by_email_address).returns("foo.%{reply_key}+42@bar.com")
SiteSetting.expects(:pop3_polling_enabled).returns(true)
expect(validator.valid_value?("t")).to eq(true)
end
end
end