2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-06-06 10:40:10 -04:00
|
|
|
require 'user_name_suggester'
|
|
|
|
|
|
|
|
describe UserNameSuggester do
|
|
|
|
|
|
|
|
describe 'name heuristics' do
|
|
|
|
it 'is able to guess a decent username from an email' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest('bob@bob.com')).to eq('bob')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.suggest' do
|
2013-08-29 10:15:02 -04:00
|
|
|
before do
|
|
|
|
User.stubs(:username_length).returns(3..15)
|
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
|
|
|
|
it "doesn't raise an error on nil username" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest(nil)).to eq(nil)
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'corrects weird characters' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest("Darth%^Vader")).to eq('Darth_Vader')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2014-03-18 18:02:33 -04:00
|
|
|
it "transliterates some characters" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest("Jørn")).to eq('Jorn')
|
2014-03-18 18:02:33 -04:00
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
it 'adds 1 to an existing username' do
|
|
|
|
user = Fabricate(:user)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest(user.username)).to eq("#{user.username}1")
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds numbers if it's too short" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest('a')).to eq('a11')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has a special case for me and i emails" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest('me@eviltrout.com')).to eq('eviltrout')
|
|
|
|
expect(UserNameSuggester.suggest('i@eviltrout.com')).to eq('eviltrout')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shortens very long suggestions" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest("myreallylongnameisrobinwardesquire")).to eq('myreallylongnam')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "makes room for the digit added if the username is too long" do
|
|
|
|
User.create(username: 'myreallylongnam', email: 'fake@discourse.org')
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest("myreallylongnam")).to eq('myreallylongna1')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2015-08-25 16:33:50 -04:00
|
|
|
it "doesn't suggest reserved usernames" do
|
|
|
|
SiteSetting.reserved_usernames = 'admin|steve|steve1'
|
|
|
|
expect(UserNameSuggester.suggest("admin@hissite.com")).to eq('admin1')
|
|
|
|
expect(UserNameSuggester.suggest("steve")).to eq('steve2')
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
it "removes leading character if it is not alphanumeric" do
|
2016-01-20 09:37:34 -05:00
|
|
|
expect(UserNameSuggester.suggest(".myname")).to eq('myname')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows leading _" do
|
|
|
|
expect(UserNameSuggester.suggest("_myname")).to eq('_myname')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes trailing characters if they are invalid" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest("myname!^$=")).to eq('myname')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2016-01-20 09:37:34 -05:00
|
|
|
it "allows dots in the middle" do
|
|
|
|
expect(UserNameSuggester.suggest("my.name")).to eq('my.name')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "remove leading dots" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest(".myname")).to eq('myname')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "remove trailing dots" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(UserNameSuggester.suggest("myname.")).to eq('myname')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
|
2015-09-11 07:23:26 -04:00
|
|
|
it 'handles usernames with a sequence of 2 or more special chars' do
|
|
|
|
expect(UserNameSuggester.suggest('Darth__Vader')).to eq('Darth_Vader')
|
|
|
|
expect(UserNameSuggester.suggest('Darth_-_Vader')).to eq('Darth_Vader')
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
it 'should handle typical facebook usernames' do
|
2016-01-20 09:37:34 -05:00
|
|
|
expect(UserNameSuggester.suggest('roger.nelson.3344913')).to eq('roger.nelson.33')
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-29 10:15:02 -04:00
|
|
|
end
|