Fix blank user's name (#19723)

This commit is contained in:
Jerin K C 2023-07-21 19:25:41 +05:30 committed by GitHub
parent 1f202c6a9a
commit 6c2c1660db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 2 deletions

View file

@ -131,7 +131,7 @@ class User < ApplicationRecord
validates :following_orgs_count, presence: true
validates :following_tags_count, presence: true
validates :following_users_count, presence: true
validates :name, length: { in: 1..100 }
validates :name, length: { in: 1..100 }, presence: true
validates :password, length: { in: 8..100 }, allow_nil: true
validates :payment_pointer, format: PAYMENT_POINTER_REGEXP, allow_blank: true
validates :rating_votes_count, presence: true

View file

@ -1,7 +1,7 @@
<div class="crayons-card crayons-card--secondary crayons-notice crayons-notice--danger" data-testid="account-errors-panel" role="alert">
<div class="crayons-card__header">
<h3 class="crayons-card__headline">
<%= pluralize(errors.size, "error") %> <%= title_suffix %>:
<%= title_suffix %>
</h3>
</div>
<div class="crayons-card__body">

View file

@ -0,0 +1,9 @@
module DataUpdateScripts
class BackfillUserBlankName
def run
User.where(" TRIM(name)='' ").each do |user|
user.update_column(:name, user.username)
end
end
end
end

View file

@ -0,0 +1,33 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20230718102644_backfill_user_blank_name.rb",
)
describe DataUpdateScripts::BackfillUserBlankName do
let(:invalid_user_with_blank_name) { make_user_for(" ", "user_name") }
let(:valid_user_name_with_trailing_spaces) { create(:user, name: " Name ") }
let(:valid_user) { create(:user) }
def make_user_for(name, username)
build(:user, name: name, username: username).tap do |user|
user.save(validate: false)
end
end
it "replaces the blank name with the username" do
username = invalid_user_with_blank_name.username
expect { described_class.new.run }
.to change { invalid_user_with_blank_name.reload.name }
.from(" ")
.to(username)
end
it "does not modify the name if it has trailing whitespaces" do
expect { described_class.new.run }.not_to change(:valid_user_name_with_trailing_spaces, :name)
end
it "does not modify the name if it is valid" do
expect { described_class.new.run }.not_to change(:valid_user, :name)
end
end

View file

@ -193,6 +193,8 @@ RSpec.describe User do
it { is_expected.to validate_length_of(:password).is_at_most(100).is_at_least(8) }
it { is_expected.to validate_length_of(:username).is_at_most(30).is_at_least(2) }
it { is_expected.not_to allow_value(" ").for(:name) }
it { is_expected.to validate_presence_of(:articles_count) }
it { is_expected.to validate_presence_of(:badge_achievements_count) }
it { is_expected.to validate_presence_of(:blocked_by_count) }