Skip to content

Commit

Permalink
Squish username before validation (mastodon#10239)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
aurelien-reeves authored and hiyuki2578 committed Oct 2, 2019
1 parent 80ade97 commit 61a64cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def emojis

before_create :generate_keys
before_validation :prepare_contents, if: :local?
before_validation :prepare_username, on: :create
before_destroy :clean_feed_manager

private
Expand All @@ -482,6 +483,10 @@ def prepare_contents
note&.strip!
end

def prepare_username
username&.squish!
end

def generate_keys
return unless local? && !Rails.env.test?

Expand Down
5 changes: 5 additions & 0 deletions spec/models/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@
expect(account).to model_have_error_on_field(:username)
end

it 'squishes the username before validation' do
account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ")
expect(account.username).to eq 'bob'
end

context 'when is local' do
it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
account_1 = Fabricate(:account, username: 'the_doctor')
Expand Down

0 comments on commit 61a64cc

Please sign in to comment.