diff --git a/lib/user_name_suggester.rb b/lib/user_name_suggester.rb index 57602d990aa..f39565941f0 100644 --- a/lib/user_name_suggester.rb +++ b/lib/user_name_suggester.rb @@ -1,4 +1,5 @@ module UserNameSuggester + GENERIC_NAMES = ['i', 'me', 'info', 'support', 'admin', 'webmaster', 'hello', 'mail', 'office', 'contact', 'team'] def self.suggest(name, allow_username = nil) return unless name.present? @@ -11,7 +12,7 @@ module UserNameSuggester # When 'walter@white.com' take 'walter' name = Regexp.last_match[1] # When 'me@eviltrout.com' take 'eviltrout' - name = Regexp.last_match[2] if ['i', 'me', 'info', 'support', 'admin', 'webmaster', 'hello', 'mail', 'office', 'contact', 'team'].include?(name) + name = Regexp.last_match[2] if GENERIC_NAMES.include?(name) end name end diff --git a/spec/components/user_name_suggester_spec.rb b/spec/components/user_name_suggester_spec.rb index f6fde98996a..9d02951cbf3 100644 --- a/spec/components/user_name_suggester_spec.rb +++ b/spec/components/user_name_suggester_spec.rb @@ -50,11 +50,17 @@ describe UserNameSuggester do end it "doesn't suggest reserved usernames" do - SiteSetting.reserved_usernames = 'admin|steve|steve1' - expect(UserNameSuggester.suggest("admin@hissite.com")).to eq('admin1') + SiteSetting.reserved_usernames = 'myadmin|steve|steve1' + expect(UserNameSuggester.suggest("myadmin@hissite.com")).to eq('myadmin1') expect(UserNameSuggester.suggest("steve")).to eq('steve2') end + it "doesn't suggest generic usernames" do + UserNameSuggester::GENERIC_NAMES.each do |name| + expect(UserNameSuggester.suggest("#{name}@apple.org")).to eq('apple') + end + end + it "removes leading character if it is not alphanumeric" do expect(UserNameSuggester.suggest(".myname")).to eq('myname') end