From ddbd1d5ab8ae423d333f1ef446e050f58d12753b Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 4 Oct 2017 15:08:51 -0400 Subject: [PATCH] allow regex options on username site settings --- lib/validators/regex_setting_validation.rb | 17 +++++++++++++++ lib/validators/string_setting_validator.rb | 13 +++++------- lib/validators/username_setting_validator.rb | 12 +++++++++-- .../username_setting_validator_spec.rb | 21 +++++++++++++++++++ 4 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 lib/validators/regex_setting_validation.rb diff --git a/lib/validators/regex_setting_validation.rb b/lib/validators/regex_setting_validation.rb new file mode 100644 index 00000000000..cd0df29f49c --- /dev/null +++ b/lib/validators/regex_setting_validation.rb @@ -0,0 +1,17 @@ +module RegexSettingValidation + + def initialize_regex_opts(opts = {}) + @regex = Regexp.new(opts[:regex]) if opts[:regex] + @regex_error = opts[:regex_error] || 'site_settings.errors.regex_mismatch' + end + + def regex_match?(val) + if @regex && !(val =~ @regex) + @regex_fail = true + return false + end + + true + end + +end diff --git a/lib/validators/string_setting_validator.rb b/lib/validators/string_setting_validator.rb index b7ffc2dcf3e..e2dda644587 100644 --- a/lib/validators/string_setting_validator.rb +++ b/lib/validators/string_setting_validator.rb @@ -1,8 +1,10 @@ class StringSettingValidator + + include RegexSettingValidation + def initialize(opts = {}) @opts = opts - @regex = Regexp.new(opts[:regex]) if opts[:regex] - @regex_error = opts[:regex_error] || 'site_settings.errors.regex_mismatch' + initialize_regex_opts(opts) end def valid_value?(val) @@ -13,12 +15,7 @@ class StringSettingValidator return false end - if @regex && !(val =~ @regex) - @regex_fail = true - return false - end - - true + regex_match?(val) end def error_message diff --git a/lib/validators/username_setting_validator.rb b/lib/validators/username_setting_validator.rb index d9aa18ad5fc..52ae5abd050 100644 --- a/lib/validators/username_setting_validator.rb +++ b/lib/validators/username_setting_validator.rb @@ -1,13 +1,21 @@ class UsernameSettingValidator + + include RegexSettingValidation + def initialize(opts = {}) @opts = opts + initialize_regex_opts(opts) end def valid_value?(val) - !val.present? || User.where(username: val).exists? + !val.present? || (User.where(username: val).exists? && regex_match?(val)) end def error_message - I18n.t('site_settings.errors.invalid_username') + if @regex_fail + I18n.t(@regex_error) + else + I18n.t('site_settings.errors.invalid_username') + end end end diff --git a/spec/components/validators/username_setting_validator_spec.rb b/spec/components/validators/username_setting_validator_spec.rb index 8e06e4fd832..8b302bba350 100644 --- a/spec/components/validators/username_setting_validator_spec.rb +++ b/spec/components/validators/username_setting_validator_spec.rb @@ -17,5 +17,26 @@ describe UsernameSettingValidator do it "returns false if value does not match a user's username" do expect(validator.valid_value?('no way')).to eq(false) end + + context "regex support" do + let!(:darthvader) { Fabricate(:user, username: 'darthvader') } + let!(:luke) { Fabricate(:user, username: 'luke') } + + it "returns false if regex doesn't match" do + v = described_class.new(regex: 'darth') + expect(v.valid_value?('luke')).to eq(false) + expect(v.valid_value?('vader')).to eq(false) + end + + it "returns true if regex matches" do + v = described_class.new(regex: 'darth') + expect(v.valid_value?('darthvader')).to eq(true) + end + + it "returns false if regex matches but username doesn't match a user" do + v = described_class.new(regex: 'darth') + expect(v.valid_value?('darthmaul')).to eq(false) + end + end end end