Skip to content

Commit 61a64cc

Browse files
aurelien-reeveshiyuki2578
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 80ade97 commit 61a64cc

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
@@ -473,6 +473,7 @@ def emojis
473473

474474
before_create :generate_keys
475475
before_validation :prepare_contents, if: :local?
476+
before_validation :prepare_username, on: :create
476477
before_destroy :clean_feed_manager
477478

478479
private
@@ -482,6 +483,10 @@ def prepare_contents
482483
note&.strip!
483484
end
484485

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

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)