0
0
Fork 0

Add coverage for ExistingUsernameValidator (#25592)

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Matt Jankowski 2023-10-16 09:41:23 -04:00 committed by GitHub
parent f8afa0f614
commit f5bc1f20e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 14 deletions

View file

@ -2,25 +2,40 @@
class ExistingUsernameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?
@value = value
return if @value.blank?
usernames_and_domains = value.split(',').filter_map do |str|
username, domain = str.strip.gsub(/\A@/, '').split('@', 2)
if options[:multiple]
record.errors.add(attribute, not_found_multiple_message) if usernames_with_no_accounts.any?
elsif usernames_with_no_accounts.any? || usernames_and_domains.size > 1
record.errors.add(attribute, not_found_message)
end
end
private
def usernames_and_domains
@value.split(',').filter_map do |string|
username, domain = string.strip.gsub(/\A@/, '').split('@', 2)
domain = nil if TagManager.instance.local_domain?(domain)
next if username.blank?
[str, username, domain]
end
usernames_with_no_accounts = usernames_and_domains.filter_map do |(str, username, domain)|
str unless Account.find_remote(username, domain)
end
if options[:multiple]
record.errors.add(attribute, I18n.t('existing_username_validator.not_found_multiple', usernames: usernames_with_no_accounts.join(', '))) if usernames_with_no_accounts.any?
elsif usernames_with_no_accounts.any? || usernames_and_domains.size > 1
record.errors.add(attribute, I18n.t('existing_username_validator.not_found'))
[string, username, domain]
end
end
def usernames_with_no_accounts
usernames_and_domains.filter_map do |(string, username, domain)|
string unless Account.find_remote(username, domain)
end
end
def not_found_multiple_message
I18n.t('existing_username_validator.not_found_multiple', usernames: usernames_with_no_accounts.join(', '))
end
def not_found_message
I18n.t('existing_username_validator.not_found')
end
end