From 8944d62aa69eb3be11e1ef6b80216dc02e6d9edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 9 Feb 2016 23:35:40 +0100 Subject: [PATCH] add validator for the 'reply_by_email_enabled' site setting --- config/locales/server.en.yml | 2 ++ config/site_settings.yml | 4 ++- .../reply_by_email_enabled_validator.rb | 23 ++++++++++++++ .../reply_by_email_enabled_validator_spec.rb | 31 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lib/validators/reply_by_email_enabled_validator.rb create mode 100644 spec/components/validators/reply_by_email_enabled_validator_spec.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 0c70dfc9d..71be1163d 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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}" diff --git a/config/site_settings.yml b/config/site_settings.yml index 37176146f..6937a8de3 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -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" diff --git a/lib/validators/reply_by_email_enabled_validator.rb b/lib/validators/reply_by_email_enabled_validator.rb new file mode 100644 index 000000000..063dbe822 --- /dev/null +++ b/lib/validators/reply_by_email_enabled_validator.rb @@ -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 diff --git a/spec/components/validators/reply_by_email_enabled_validator_spec.rb b/spec/components/validators/reply_by_email_enabled_validator_spec.rb new file mode 100644 index 000000000..7596afef9 --- /dev/null +++ b/spec/components/validators/reply_by_email_enabled_validator_spec.rb @@ -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