mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 20:55:44 +02:00
Add support of punycode domains in emails
This commit is contained in:
parent
56ac816dd1
commit
b4369bdcd0
6 changed files with 77 additions and 21 deletions
|
@ -3,21 +3,54 @@ module Concerns
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
def email_verification
|
def email_verification
|
||||||
EmailAddressVerification.find_or_create_by(email: email.downcase,
|
EmailAddressVerification.find_or_create_by(email: self.class.punycode_to_unicode(email),
|
||||||
domain: domain(email.downcase))
|
domain: domain(email))
|
||||||
end
|
end
|
||||||
|
|
||||||
def billing_email_verification
|
def billing_email_verification
|
||||||
return unless attribute_names.include?('billing_email')
|
return unless attribute_names.include?('billing_email')
|
||||||
|
|
||||||
EmailAddressVerification.find_or_create_by(email: billing_email.downcase,
|
EmailAddressVerification.find_or_create_by(email: self.class
|
||||||
domain: domain(email.downcase))
|
.punycode_to_unicode(billing_email),
|
||||||
|
domain: domain(billing_email))
|
||||||
|
end
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def domain(email)
|
||||||
|
Mail::Address.new(email).domain&.downcase || 'not_found'
|
||||||
|
rescue Mail::Field::IncompleteParseError
|
||||||
|
'not_found'
|
||||||
|
end
|
||||||
|
|
||||||
|
def local(email)
|
||||||
|
Mail::Address.new(email).local&.downcase || email
|
||||||
|
rescue Mail::Field::IncompleteParseError
|
||||||
|
email
|
||||||
|
end
|
||||||
|
|
||||||
|
def punycode_to_unicode(email)
|
||||||
|
return email if domain(email) == 'not_found'
|
||||||
|
|
||||||
|
local = local(email)
|
||||||
|
domain = SimpleIDN.to_unicode(domain(email))
|
||||||
|
"#{local}@#{domain}"&.downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
def unicode_to_punycode(email)
|
||||||
|
return email if domain(email) == 'not_found'
|
||||||
|
|
||||||
|
local = local(email)
|
||||||
|
domain = SimpleIDN.to_ascii(domain(email))
|
||||||
|
"#{local}@#{domain}"&.downcase
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def domain(email)
|
def domain(email)
|
||||||
Mail::Address.new(email).domain || 'not_found'
|
SimpleIDN.to_unicode(self.class.domain(email))
|
||||||
rescue Mail::Field::IncompleteParseError
|
end
|
||||||
'not_found'
|
|
||||||
|
def punycode_to_unicode(email)
|
||||||
|
self.class.punycode_to_unicode(email)
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_email_mx_smtp(field:, email:)
|
def verify_email_mx_smtp(field:, email:)
|
||||||
|
|
|
@ -39,8 +39,7 @@ class EmailAddressVerification < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify
|
def verify
|
||||||
# media = success ? :mx : :smtp
|
media = :mx
|
||||||
media = :regex
|
|
||||||
validation_request = Truemail.validate(email, with: media)
|
validation_request = Truemail.validate(email, with: media)
|
||||||
|
|
||||||
if validation_request.result.success
|
if validation_request.result.success
|
||||||
|
|
|
@ -30,8 +30,10 @@ Truemail.configure do |config|
|
||||||
# Available validation types: :regex, :mx, :smtp
|
# Available validation types: :regex, :mx, :smtp
|
||||||
if Rails.env.production?
|
if Rails.env.production?
|
||||||
config.default_validation_type = :smtp
|
config.default_validation_type = :smtp
|
||||||
else
|
elsif Rails.env.test?
|
||||||
config.default_validation_type = :regex
|
config.default_validation_type = :regex
|
||||||
|
else
|
||||||
|
config.default_validation_type = :mx
|
||||||
end
|
end
|
||||||
|
|
||||||
# Optional parameter. You can predefine which type of validation will be used for domains.
|
# Optional parameter. You can predefine which type of validation will be used for domains.
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
class FillEmailVerifications < ActiveRecord::Migration[6.0]
|
class FillEmailVerifications < ActiveRecord::Migration[6.0]
|
||||||
def up
|
include Concerns::EmailVerifable
|
||||||
registrar_billing_emails = Registrar.pluck(:billing_email).uniq.reject(&:blank?).map(&:downcase)
|
|
||||||
registrar_emails = Registrar.pluck(:email).uniq.reject(&:blank?).map(&:downcase)
|
|
||||||
contact_emails = Contact.pluck(:email).uniq.reject(&:blank?).map(&:downcase)
|
|
||||||
|
|
||||||
emails = (contact_emails + registrar_emails + registrar_billing_emails).uniq
|
def up
|
||||||
|
registrar_billing_emails = Registrar.pluck(:billing_email).uniq.reject(&:blank?)
|
||||||
|
registrar_emails = Registrar.pluck(:email).uniq.reject(&:blank?)
|
||||||
|
contact_emails = Contact.pluck(:email).uniq.reject(&:blank?)
|
||||||
|
|
||||||
|
emails = (contact_emails + registrar_emails + registrar_billing_emails)
|
||||||
|
emails = emails.map{ |email| punycode_to_unicode(email) }.uniq
|
||||||
|
|
||||||
result = emails.map do |email|
|
result = emails.map do |email|
|
||||||
{ email: email, domain: domain(email) }
|
{ email: email, domain: domain(email) }
|
||||||
|
@ -15,10 +18,4 @@ class FillEmailVerifications < ActiveRecord::Migration[6.0]
|
||||||
def down
|
def down
|
||||||
EmailAddressVerification.delete_all
|
EmailAddressVerification.delete_all
|
||||||
end
|
end
|
||||||
|
|
||||||
def domain(email)
|
|
||||||
Mail::Address.new(email).domain || 'not_found'
|
|
||||||
rescue Mail::Field::IncompleteParseError
|
|
||||||
'not_found'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -270,6 +270,16 @@ class ContactTest < ActiveSupport::TestCase
|
||||||
assert_equal domain.whois_record.try(:json).try(:[], 'registrant'), @contact.name
|
assert_equal domain.whois_record.try(:json).try(:[], 'registrant'), @contact.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_creates_email_verification_in_unicode
|
||||||
|
unicode_email = 'suur@äri.ee'
|
||||||
|
punycode_email = Contact.unicode_to_punycode(unicode_email)
|
||||||
|
|
||||||
|
@contact.email = punycode_email
|
||||||
|
@contact.save
|
||||||
|
|
||||||
|
assert_equal @contact.email_verification.email, unicode_email
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
|
def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
|
||||||
|
|
|
@ -61,6 +61,21 @@ class RegistrarTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_creates_email_verification_in_unicode
|
||||||
|
unicode_email = 'suur@äri.ee'
|
||||||
|
punycode_email = Registrar.unicode_to_punycode(unicode_email)
|
||||||
|
unicode_billing_email = 'billing@äri.ee'
|
||||||
|
punycode_billing_email = Registrar.unicode_to_punycode(unicode_billing_email)
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.email = punycode_email
|
||||||
|
registrar.billing_email = punycode_billing_email
|
||||||
|
registrar.save
|
||||||
|
|
||||||
|
assert_equal registrar.email_verification.email, unicode_email
|
||||||
|
assert_equal registrar.billing_email_verification.email, unicode_billing_email
|
||||||
|
end
|
||||||
|
|
||||||
def test_invalid_without_accounting_customer_code
|
def test_invalid_without_accounting_customer_code
|
||||||
registrar = valid_registrar
|
registrar = valid_registrar
|
||||||
registrar.accounting_customer_code = ''
|
registrar.accounting_customer_code = ''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue