From: Andy Allan Date: Thu, 18 Jan 2024 10:49:52 +0000 (+0000) Subject: Merge pull request #4485 from tomhughes/drop-lower-index X-Git-Tag: live~986 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/c9a86866bb6f2d0b41604078a0f05aadc5633f46?hp=9db635a992ec6464c40ac69eadeac78dff60ccf9 Merge pull request #4485 from tomhughes/drop-lower-index Drop lowercase index on display names --- diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0608b699b..9fc132014 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -50,7 +50,7 @@ jobs: rubygems: 3.4.10 bundler-cache: true - name: Cache node modules - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: node_modules key: yarn-${{ env.os }}-${{ hashFiles('yarn.lock') }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f2fa6763a..bfe456076 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,7 +26,7 @@ jobs: rubygems: 3.4.10 bundler-cache: true - name: Cache node modules - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: node_modules key: yarn-ubuntu-${{ matrix.ubuntu }}-${{ hashFiles('yarn.lock') }} diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8a196f52e..9874aa379 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -66,7 +66,7 @@ Metrics/BlockNesting: # Offense count: 26 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 299 + Max: 305 # Offense count: 59 # Configuration parameters: AllowedMethods, AllowedPatterns. diff --git a/app/models/user.rb b/app/models/user.rb index 8ed6d4465..7faf748cd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -99,6 +99,7 @@ class User < ApplicationRecord validates :display_name, :if => proc { |u| u.display_name_changed? }, :characters => { :url_safe => true }, :whitespace => { :leading => false, :trailing => false } + validate :display_name_cannot_be_user_id_with_other_id, :if => proc { |u| u.display_name_changed? } validates :email, :presence => true, :confirmation => true, :characters => true validates :email, :if => proc { |u| u.email_changed? }, :uniqueness => { :case_sensitive => false } @@ -123,6 +124,12 @@ class User < ApplicationRecord before_save :update_tile after_save :spam_check + def display_name_cannot_be_user_id_with_other_id + display_name&.match(/^user_(\d+)$/i) do |m| + errors.add :display_name, I18n.t("activerecord.errors.messages.display_name_is_user_n") unless m[1].to_i == id + end + end + def to_param display_name end diff --git a/config/locales/en.yml b/config/locales/en.yml index 653e363a6..ca22de9bd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -40,6 +40,7 @@ en: messages: invalid_email_address: does not appear to be a valid e-mail address email_address_not_routable: is not routable + display_name_is_user_n: can't be user_n unless n is your user id models: user_mute: attributes: diff --git a/test/models/user_test.rb b/test/models/user_test.rb index d21512f2a..42949504f 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -94,6 +94,36 @@ class UserTest < ActiveSupport::TestCase end end + def test_display_name_user_id_new + existing_user = create(:user) + user = build(:user) + + user.display_name = "user_#{existing_user.id}" + assert_not user.valid?, "user_ name is valid for existing user id when it shouldn't be" + + user.display_name = "user_#{existing_user.id + 1}" + assert_not user.valid?, "user_ name is valid for new user id when it shouldn't be" + end + + def test_display_name_user_id_rename + existing_user = create(:user) + user = create(:user) + + user.display_name = "user_#{existing_user.id}" + assert_not user.valid?, "user_ name is valid for existing user id when it shouldn't be" + + user.display_name = "user_#{user.id}" + assert_predicate user, :valid?, "user_ name is invalid for own id, when it should be" + end + + def test_display_name_user_id_unchanged_is_valid + user = build(:user, :display_name => "user_0") + user.save(:validate => false) + user.reload + + assert_predicate user, :valid?, "user_0 display_name is invalid but it hasn't been changed" + end + def test_friends_with alice = create(:user, :active) bob = create(:user, :active)