FIX: 500 error when creating a user with an integer username (#16370)

Via the API it is possible to create a user with an integer username. So
123 instead of "123". This causes the following 500 error:

```
NoMethodError (undefined method `unicode_normalize' for 1:Integer)
app/models/user.rb:276:in `normalize_username'
```

See: https://meta.discourse.org/t/222281
This commit is contained in:
Blake Erickson 2022-04-04 15:15:32 -06:00 committed by GitHub
parent 1da4b9eeb3
commit ec2930712d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -273,7 +273,7 @@ class User < ActiveRecord::Base
end
def self.normalize_username(username)
username.unicode_normalize.downcase if username.present?
username.to_s.unicode_normalize.downcase if username.present?
end
def self.username_available?(username, email = nil, allow_reserved_username: false)

View File

@ -50,6 +50,12 @@ describe User do
expect(user.errors.full_messages.first)
.to include(user_error_message(:username, :same_as_password))
end
describe 'when a username is an integer' do
it 'is converted to a string on normalization' do
expect(User.normalize_username(123)).to eq("123") # This is possible via the API
end
end
end
describe 'name' do