2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2015-03-06 16:44:54 -05:00
|
|
|
|
|
|
|
describe UsernameChanger do
|
|
|
|
|
|
|
|
describe '#change' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'success' do
|
|
|
|
let(:new_username) { "#{user.username}1234" }
|
|
|
|
|
|
|
|
before do
|
2017-02-27 15:02:00 -05:00
|
|
|
@result = UsernameChanger.change(user, new_username)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(@result).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should change the username' do
|
|
|
|
user.reload
|
|
|
|
expect(user.username).to eq(new_username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should change the username_lower' do
|
|
|
|
user.reload
|
|
|
|
expect(user.username_lower).to eq(new_username.downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'failure' do
|
|
|
|
let(:wrong_username) { "" }
|
|
|
|
let(:username_before_change) { user.username }
|
|
|
|
let(:username_lower_before_change) { user.username_lower }
|
|
|
|
|
|
|
|
before do
|
2017-02-27 15:02:00 -05:00
|
|
|
@result = UsernameChanger.change(user, wrong_username)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(@result).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not change the username' do
|
|
|
|
user.reload
|
|
|
|
expect(user.username).to eq(username_before_change)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not change the username_lower' do
|
|
|
|
user.reload
|
|
|
|
expect(user.username_lower).to eq(username_lower_before_change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'change the case of my username' do
|
|
|
|
let!(:myself) { Fabricate(:user, username: 'hansolo') }
|
|
|
|
|
|
|
|
it 'should return true' do
|
2017-02-27 15:02:00 -05:00
|
|
|
expect(UsernameChanger.change(myself, "HanSolo")).to eq(true)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should change the username' do
|
2017-02-27 15:02:00 -05:00
|
|
|
UsernameChanger.change(myself, "HanSolo")
|
2015-03-06 16:44:54 -05:00
|
|
|
expect(myself.reload.username).to eq('HanSolo')
|
|
|
|
end
|
2017-02-23 00:48:57 -05:00
|
|
|
|
|
|
|
it "logs the action" do
|
2017-02-27 15:02:00 -05:00
|
|
|
expect { UsernameChanger.change(myself, "HanSolo", myself) }.to change { UserHistory.count }.by(1)
|
|
|
|
expect { UsernameChanger.change(myself, "HanSolo", myself) }.to change { UserHistory.count }.by(0) # make sure it does not log a dupe
|
2017-02-23 00:48:57 -05:00
|
|
|
end
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'allow custom minimum username length from site settings' do
|
|
|
|
before do
|
|
|
|
@custom_min = 2
|
|
|
|
SiteSetting.min_username_length = @custom_min
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow a shorter username than default' do
|
2017-02-27 15:02:00 -05:00
|
|
|
result = UsernameChanger.change(user, 'a' * @custom_min)
|
2015-03-06 16:44:54 -05:00
|
|
|
expect(result).not_to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not allow a shorter username than limit' do
|
2017-02-27 15:02:00 -05:00
|
|
|
result = UsernameChanger.change(user, 'a' * (@custom_min - 1))
|
2015-03-06 16:44:54 -05:00
|
|
|
expect(result).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not allow a longer username than limit' do
|
2017-02-27 15:02:00 -05:00
|
|
|
result = UsernameChanger.change(user, 'a' * (User.username_length.end + 1))
|
2015-03-06 16:44:54 -05:00
|
|
|
expect(result).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|