Normalize current_username on account redirect form (#38262)

This commit is contained in:
Matt Jankowski 2026-03-18 05:33:00 -04:00 committed by GitHub
parent 5e7f221cab
commit b497bb2908
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 7 deletions

View File

@ -2,15 +2,20 @@
class Form::Redirect
include ActiveModel::Model
include ActiveModel::Attributes
include ActiveModel::Attributes::Normalization
attr_accessor :account, :target_account, :current_password,
:current_username
attr_accessor :account, :target_account, :current_password
attr_reader :acct
attribute :acct, :string
attribute :current_username, :string
validates :acct, presence: true, domain: { acct: true }
validate :validate_target_account
normalizes :acct, with: ->(value) { value.to_s.strip.gsub(/\A@/, '') }, apply_to_nil: true
normalizes :current_username, with: ->(value) { value.strip.delete_prefix('@') }
def valid_with_challenge?(current_user)
if current_user.encrypted_password.present?
errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
@ -24,10 +29,6 @@ class Form::Redirect
valid?
end
def acct=(val)
@acct = val.to_s.strip.gsub(/\A@/, '')
end
private
def set_target_account

View File

@ -29,4 +29,11 @@ RSpec.describe Form::Redirect do
end
end
end
describe 'Normalizations' do
it { is_expected.to normalize(:acct).from(nil).to('') }
it { is_expected.to normalize(:acct).from(' @username ').to('username') }
it { is_expected.to normalize(:current_username).from(' @username ').to('username') }
end
end

View File

@ -32,6 +32,19 @@ RSpec.describe 'Settings Migration Redirects' do
.to have_content(I18n.t('migrations.cancelled_msg'))
end
context 'when user has blank encrypted password' do
before { user.update! encrypted_password: '' }
it 'saves a redirect via username confirmation' do
visit new_settings_migration_redirect_path
fill_in 'form_redirect_acct', with: 'new@example.host'
fill_in 'form_redirect_current_username', with: " @#{user.account.username} "
expect { click_on I18n.t('migrations.set_redirect') }
.to(change { user.reload.account.moved_to_account_id }.from(nil))
end
end
private
def stub_resolver