Skip to content

Commit 41bb91f

Browse files
rtuckerhiyuki2578
authored andcommitted
Don't delete periods when validating username uniqueness (mastodon#11392) (mastodon#11400)
* Check to make sure usernames with '.' cannot be created * Add test for instance actor account name conflicts This makes sure that migration 20190715164535_add_instance_actor won't fail if there's already an account that is named the same as the domain (minus the .) * Put the test into the correct context... * Add another test to split this into two validations * Don't delete periods when validating username uniqueness (mastodon#11392) The 20190715164535_add_instance_actor migration fails if there's already a username similar to the domain name, e.g. if you are 'vulpine.club' and have a user named 'vulpineclub', validation fails. Upon further review, usernames with periods are dropped by the regular expression in the Account class, so we don't need to worry about it here. Fixes mastodon#11392
1 parent 5dae356 commit 41bb91f

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

app/validators/unique_username_validator.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# frozen_string_literal: true
22

3+
# See also: USERNAME_RE in the Account class
4+
35
class UniqueUsernameValidator < ActiveModel::Validator
46
def validate(account)
57
return if account.username.nil?
68

7-
normalized_username = account.username.downcase.delete('.')
9+
normalized_username = account.username.downcase
810

911
scope = Account.where(domain: nil).where('lower(username) = ?', normalized_username)
1012
scope = scope.where.not(id: account.id) if account.persisted?

spec/models/account_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,29 @@
583583
expect(account.valid?).to be true
584584
end
585585

586+
it 'is valid if we are creating an instance actor account with a period' do
587+
account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
588+
expect(account.valid?).to be true
589+
end
590+
591+
it 'is valid if we are creating a possibly-conflicting instance actor account' do
592+
account_1 = Fabricate(:account, username: 'examplecom')
593+
account_2 = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
594+
expect(account_2.valid?).to be true
595+
end
596+
586597
it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
587598
account = Fabricate.build(:account, username: 'the-doctor')
588599
account.valid?
589600
expect(account).to model_have_error_on_field(:username)
590601
end
591602

603+
it 'is invalid if the username contains a period' do
604+
account = Fabricate.build(:account, username: 'the.doctor')
605+
account.valid?
606+
expect(account).to model_have_error_on_field(:username)
607+
end
608+
592609
it 'is invalid if the username is longer then 128 characters' do
593610
account = Fabricate.build(:account, username: Faker::Lorem.characters(129))
594611
account.valid?

0 commit comments

Comments
 (0)