Normalize current_username on account redirect form (#38262)
This commit is contained in:
parent
5e7f221cab
commit
b497bb2908
@ -2,15 +2,20 @@
|
|||||||
|
|
||||||
class Form::Redirect
|
class Form::Redirect
|
||||||
include ActiveModel::Model
|
include ActiveModel::Model
|
||||||
|
include ActiveModel::Attributes
|
||||||
|
include ActiveModel::Attributes::Normalization
|
||||||
|
|
||||||
attr_accessor :account, :target_account, :current_password,
|
attr_accessor :account, :target_account, :current_password
|
||||||
:current_username
|
|
||||||
|
|
||||||
attr_reader :acct
|
attribute :acct, :string
|
||||||
|
attribute :current_username, :string
|
||||||
|
|
||||||
validates :acct, presence: true, domain: { acct: true }
|
validates :acct, presence: true, domain: { acct: true }
|
||||||
validate :validate_target_account
|
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)
|
def valid_with_challenge?(current_user)
|
||||||
if current_user.encrypted_password.present?
|
if current_user.encrypted_password.present?
|
||||||
errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
|
errors.add(:current_password, :invalid) unless current_user.valid_password?(current_password)
|
||||||
@ -24,10 +29,6 @@ class Form::Redirect
|
|||||||
valid?
|
valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
def acct=(val)
|
|
||||||
@acct = val.to_s.strip.gsub(/\A@/, '')
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_target_account
|
def set_target_account
|
||||||
|
|||||||
@ -29,4 +29,11 @@ RSpec.describe Form::Redirect do
|
|||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@ -32,6 +32,19 @@ RSpec.describe 'Settings Migration Redirects' do
|
|||||||
.to have_content(I18n.t('migrations.cancelled_msg'))
|
.to have_content(I18n.t('migrations.cancelled_msg'))
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def stub_resolver
|
def stub_resolver
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user