Maintenance: Remove while loop user check login.

This commit is contained in:
Rolf Schmidt 2021-09-22 14:39:10 +02:00 committed by Thorsten Eckel
parent 57b9ac91f3
commit 7dbd1c1b15
2 changed files with 40 additions and 9 deletions

View file

@ -926,17 +926,16 @@ try to find correct name
end
# check if login already exists
self.login = login.downcase.strip
check = true
while check
base_login = login.downcase.strip
alternatives = [nil] + Array(1..20) + [ SecureRandom.uuid ]
alternatives.each do |suffix|
self.login = "#{base_login}#{suffix}"
exists = User.find_by(login: login)
if exists && exists.id != id
self.login = "#{login}#{rand(999)}" # rubocop:disable Zammad/ForbidRand
else
check = false
return true if !exists || exists.id == id
end
end
true
raise Exceptions::UnprocessableEntity, "Invalid user login generation for login #{login}!"
end
def check_mail_delivery_failed

View file

@ -573,6 +573,38 @@ RSpec.describe User, type: :model do
end
end
end
describe '#check_login' do
let(:agent) { create(:agent) }
it 'does use the origin login' do
new_agent = create(:agent)
expect(new_agent.login).not_to end_with('1')
end
it 'does number up agent logins (1)' do
new_agent = create(:agent, login: agent.login)
expect(new_agent.login).to eq("#{agent.login}1")
end
it 'does number up agent logins (5)' do
new_agent = create(:agent, login: agent.login)
4.times do
new_agent = create(:agent, login: agent.login)
end
expect(new_agent.login).to eq("#{agent.login}5")
end
it 'does backup with uuid in cases of many duplicates' do
new_agent = create(:agent, login: agent.login)
20.times do
new_agent = create(:agent, login: agent.login)
end
expect(new_agent.login.sub!(agent.login, '')).to be_a_uuid
end
end
end
describe 'Attributes:' do