Skip to content

Commit 2e49833

Browse files
authored
Merge pull request mastodon#108 from CrossGate-Pawoo/cherry_pick_fix_query
Search account domain in lowercase (mastodon#13016)
2 parents 79a4fe8 + 207a818 commit 2e49833

5 files changed

Lines changed: 56 additions & 17 deletions

File tree

app/models/account.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ class Account < ApplicationRecord
7171
has_one :user, inverse_of: :account
7272

7373
validates :username, presence: true
74+
validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? }
7475

7576
# Remote user validations
76-
validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: -> { !local? && will_save_change_to_username? }
7777

7878
# Local user validations
7979
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? }
80-
validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? }
8180
validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
8281
validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
8382
validates :note, length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }

app/models/concerns/account_finder_concern.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,15 @@ def scoped_accounts
4444
end
4545

4646
def with_usernames
47-
Account.where.not(username: '')
47+
Account.where.not(Account.arel_table[:username].lower.eq '')
4848
end
4949

5050
def matching_username
5151
Account.where(Account.arel_table[:username].lower.eq username.to_s.downcase)
5252
end
5353

5454
def matching_domain
55-
if domain.nil?
56-
Account.where(domain: nil)
57-
else
58-
Account.where(Account.arel_table[:domain].lower.eq domain.to_s.downcase)
59-
end
55+
Account.where(Account.arel_table[:domain].lower.eq(domain.nil? ? nil : domain.to_s.downcase))
6056
end
6157
end
6258
end

app/validators/unique_username_validator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ class UniqueUsernameValidator < ActiveModel::Validator
44
def validate(account)
55
return if account.username.nil?
66

7-
normalized_username = account.username.downcase.delete('.')
7+
normalized_username = account.username.downcase
8+
normalized_domain = account.domain&.downcase
89

9-
scope = Account.where(domain: nil).where('lower(username) = ?', normalized_username)
10+
scope = Account.where(Account.arel_table[:username].lower.eq normalized_username).where(Account.arel_table[:domain].lower.eq normalized_domain)
1011
scope = scope.where.not(id: account.id) if account.persisted?
1112

1213
account.errors.add(:username, :taken) if scope.exists?

spec/models/account_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,18 +676,18 @@
676676
end
677677

678678
context 'when is remote' do
679-
it 'is invalid if the username is not unique in case-sensitive comparison among accounts in the same normalized domain' do
679+
it 'is invalid if the username is same among accounts in the same normalized domain' do
680680
Fabricate(:account, domain: 'にゃん', username: 'username')
681681
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username')
682682
account.valid?
683683
expect(account).to model_have_error_on_field(:username)
684684
end
685685

686-
it 'is valid even if the username is unique only in case-sensitive comparison among accounts in the same normalized domain' do
686+
it 'is invalid if the username is not unique in case-insensitive comparison among accounts in the same normalized domain' do
687687
Fabricate(:account, domain: 'にゃん', username: 'username')
688688
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
689689
account.valid?
690-
expect(account).not_to model_have_error_on_field(:username)
690+
expect(account).to model_have_error_on_field(:username)
691691
end
692692

693693
it 'is valid even if the username doesn\'t only contains letters, numbers and underscores' do

spec/validators/unique_username_validator_spec.rb

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,43 @@
44

55
describe UniqueUsernameValidator do
66
describe '#validate' do
7+
context 'when local account' do
8+
it 'does not add errors if username is nil' do
9+
account = double(username: nil, domain: nil, persisted?: false, errors: double(add: nil))
10+
subject.validate(account)
11+
expect(account.errors).to_not have_received(:add)
12+
end
13+
14+
it 'does not add errors when existing one is subject itself' do
15+
account = Fabricate(:account, username: 'abcdef')
16+
expect(account).to be_valid
17+
end
18+
19+
it 'adds an error when the username is already used with ignoring cases' do
20+
Fabricate(:account, username: 'ABCdef')
21+
account = double(username: 'abcDEF', domain: nil, persisted?: false, errors: double(add: nil))
22+
subject.validate(account)
23+
expect(account.errors).to have_received(:add)
24+
end
25+
26+
it 'does not add errors when same username remote account exists' do
27+
Fabricate(:account, username: 'abcdef', domain: 'example.com')
28+
account = double(username: 'abcdef', domain: nil, persisted?: false, errors: double(add: nil))
29+
subject.validate(account)
30+
expect(account.errors).to_not have_received(:add)
31+
end
32+
end
33+
end
34+
35+
context 'when remote account' do
736
it 'does not add errors if username is nil' do
8-
account = double(username: nil, persisted?: false, errors: double(add: nil))
37+
account = double(username: nil, domain: 'example.com', persisted?: false, errors: double(add: nil))
938
subject.validate(account)
1039
expect(account.errors).to_not have_received(:add)
1140
end
1241

1342
it 'does not add errors when existing one is subject itself' do
14-
account = Fabricate(:account, username: 'abcdef')
43+
account = Fabricate(:account, username: 'abcdef', domain: 'example.com')
1544
expect(account).to be_valid
1645
end
1746

@@ -24,10 +53,24 @@
2453
end
2554

2655
it 'adds an error when the username is already used with ignoring cases' do
27-
Fabricate(:account, username: 'ABCdef')
28-
account = double(username: 'abcDEF', persisted?: false, errors: double(add: nil))
56+
Fabricate(:account, username: 'ABCdef', domain: 'example.com')
57+
account = double(username: 'abcDEF', domain: 'example.com', persisted?: false, errors: double(add: nil))
58+
subject.validate(account)
59+
expect(account.errors).to have_received(:add)
60+
end
61+
62+
it 'adds an error when the domain is already used with ignoring cases' do
63+
Fabricate(:account, username: 'ABCdef', domain: 'example.com')
64+
account = double(username: 'ABCdef', domain: 'EXAMPLE.COM', persisted?: false, errors: double(add: nil))
2965
subject.validate(account)
3066
expect(account.errors).to have_received(:add)
3167
end
68+
69+
it 'does not add errors when account with the same username and another domain exists' do
70+
Fabricate(:account, username: 'abcdef', domain: 'example.com')
71+
account = double(username: 'abcdef', domain: 'example2.com', persisted?: false, errors: double(add: nil))
72+
subject.validate(account)
73+
expect(account.errors).to_not have_received(:add)
74+
end
3275
end
3376
end

0 commit comments

Comments
 (0)