From ec2930712dde7e7d380e62e0b73d0bd0c4e12c0a Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Mon, 4 Apr 2022 15:15:32 -0600 Subject: [PATCH] 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 --- app/models/user.rb | 2 +- spec/models/user_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 5d69ccede9..ac0ce5c7ea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5e4b26de5b..383656c34c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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