Skip to content

Commit fa46419

Browse files
aurelien-reevesGargron
authored andcommitted
Squish username before validation (mastodon#10239)
* Squish username before validation (mastodon#10101) Fix mastodon#10101 * Move before_validation hook to a private method Also add Unicode wite-spaces to the spec to support the use of squish over strip.
1 parent 63b6efa commit fa46419

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

app/models/account.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ def emojis
472472

473473
before_create :generate_keys
474474
before_validation :prepare_contents, if: :local?
475+
before_validation :prepare_username, on: :create
475476
before_destroy :clean_feed_manager
476477

477478
private
@@ -481,6 +482,10 @@ def prepare_contents
481482
note&.strip!
482483
end
483484

485+
def prepare_username
486+
username&.squish!
487+
end
488+
484489
def generate_keys
485490
return unless local? && !Rails.env.test?
486491

spec/models/account_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@
558558
expect(account).to model_have_error_on_field(:username)
559559
end
560560

561+
it 'squishes the username before validation' do
562+
account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ")
563+
expect(account.username).to eq 'bob'
564+
end
565+
561566
context 'when is local' do
562567
it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
563568
account_1 = Fabricate(:account, username: 'the_doctor')

0 commit comments

Comments
 (0)