mirror of
https://github.com/internetee/registry.git
synced 2025-06-13 16:14:47 +02:00
Add verification creation on email validation, add tests
This commit is contained in:
parent
b0332a7abd
commit
b446cff3fd
6 changed files with 137 additions and 58 deletions
|
@ -3,15 +3,15 @@ module Concerns
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
def email_verification
|
def email_verification
|
||||||
EmailAddressVerification.find_or_create_by(email: self.class.punycode_to_unicode(email),
|
@email_verification ||= EmailAddressVerification.find_or_create_by(email: unicode_email,
|
||||||
domain: domain(email))
|
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: self.class
|
@billing_email_verification ||= EmailAddressVerification
|
||||||
.punycode_to_unicode(billing_email),
|
.find_or_create_by(email: unicode_billing_email,
|
||||||
domain: domain(billing_email))
|
domain: domain(billing_email))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -45,6 +45,14 @@ module Concerns
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unicode_billing_email
|
||||||
|
self.class.punycode_to_unicode(billing_email)
|
||||||
|
end
|
||||||
|
|
||||||
|
def unicode_email
|
||||||
|
self.class.punycode_to_unicode(email)
|
||||||
|
end
|
||||||
|
|
||||||
def domain(email)
|
def domain(email)
|
||||||
SimpleIDN.to_unicode(self.class.domain(email))
|
SimpleIDN.to_unicode(self.class.domain(email))
|
||||||
end
|
end
|
||||||
|
@ -53,16 +61,29 @@ module Concerns
|
||||||
self.class.punycode_to_unicode(email)
|
self.class.punycode_to_unicode(email)
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_email_mx_smtp(field:, email:)
|
|
||||||
errors.add(field, :invalid) unless email.blank? || Truemail.valid?(email)
|
|
||||||
end
|
|
||||||
|
|
||||||
def correct_email_format
|
def correct_email_format
|
||||||
verify_email_mx_smtp(field: :email, email: email)
|
return if email.blank?
|
||||||
|
|
||||||
|
result = email_verification.verify
|
||||||
|
process_result(result: result, field: :email)
|
||||||
end
|
end
|
||||||
|
|
||||||
def correct_billing_email_format
|
def correct_billing_email_format
|
||||||
verify_email_mx_smtp(field: :billing_email, email: billing_email)
|
return if email.blank?
|
||||||
|
|
||||||
|
result = billing_email_verification.verify
|
||||||
|
process_result(result: result, field: :billing_email)
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_result(result:, field:)
|
||||||
|
case result[:errors].keys.first
|
||||||
|
when :smtp
|
||||||
|
errors.add(field, I18n.t('email.email_smtp_check_error'))
|
||||||
|
when :mx
|
||||||
|
errors.add(field, I18n.t('email.email_mx_check_error'))
|
||||||
|
when :regex
|
||||||
|
errors.add(field, I18n.t('email.email_regex_check_error'))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,8 +41,7 @@ class EmailAddressVerification < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify
|
def verify
|
||||||
media = :mx
|
validation_request = Truemail.validate(email)
|
||||||
validation_request = Truemail.validate(email, with: media)
|
|
||||||
|
|
||||||
if validation_request.result.success
|
if validation_request.result.success
|
||||||
update(verified_at: Time.zone.now,
|
update(verified_at: Time.zone.now,
|
||||||
|
@ -52,6 +51,6 @@ class EmailAddressVerification < ApplicationRecord
|
||||||
success: false)
|
success: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
validation_request.result.success
|
validation_request.result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
5
config/locales/admin/email_verifable.en.yml
Normal file
5
config/locales/admin/email_verifable.en.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
en:
|
||||||
|
email:
|
||||||
|
email_smtp_check_error: SMTP check error
|
||||||
|
email_mx_check_error: Mail domain not found
|
||||||
|
email_regex_check_error: Invalid format
|
5
config/locales/admin/email_verifable.et.yml
Normal file
5
config/locales/admin/email_verifable.et.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
et:
|
||||||
|
email:
|
||||||
|
email_smtp_check_error: Eposti aadressi ei leitud (SMTP viga)
|
||||||
|
email_mx_check_error: Eposti aadressi domeeni ei leitud
|
||||||
|
email_regex_check_error: Eposti aadress on vigane
|
|
@ -3,6 +3,11 @@ require 'test_helper'
|
||||||
class ContactTest < ActiveSupport::TestCase
|
class ContactTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
@contact = contacts(:john)
|
@contact = contacts(:john)
|
||||||
|
@old_validation_type = Truemail.configure.default_validation_type
|
||||||
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
Truemail.configure.default_validation_type = @old_validation_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_valid_contact_fixture_is_valid
|
def test_valid_contact_fixture_is_valid
|
||||||
|
@ -61,27 +66,37 @@ class ContactTest < ActiveSupport::TestCase
|
||||||
assert contact.invalid?
|
assert contact.invalid?
|
||||||
end
|
end
|
||||||
|
|
||||||
def tests_email_mx_and_smtp
|
def test_email_verification_valid
|
||||||
Truemail.configure do |config|
|
|
||||||
config.default_validation_type = :smtp
|
|
||||||
end
|
|
||||||
|
|
||||||
contact = valid_contact
|
contact = valid_contact
|
||||||
contact.email = 'info@internet.ee'
|
contact.email = 'info@internet.ee'
|
||||||
assert contact.valid?
|
assert contact.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_email_verification_smtp_error
|
||||||
|
Truemail.configure.default_validation_type = :smtp
|
||||||
|
|
||||||
|
contact = valid_contact
|
||||||
contact.email = 'somecrude1337joke@internet.ee'
|
contact.email = 'somecrude1337joke@internet.ee'
|
||||||
assert contact.invalid?
|
assert contact.invalid?
|
||||||
|
assert_equal I18n.t('email.email_smtp_check_error'), contact.errors.messages[:email].first
|
||||||
|
end
|
||||||
|
|
||||||
contact.email = 'some@strangesentence@internet.ee'
|
def test_email_verification_mx_error
|
||||||
assert contact.invalid?
|
Truemail.configure.default_validation_type = :mx
|
||||||
|
|
||||||
|
contact = valid_contact
|
||||||
contact.email = 'somecrude31337joke@somestrange31337domain.ee'
|
contact.email = 'somecrude31337joke@somestrange31337domain.ee'
|
||||||
assert contact.invalid?
|
assert contact.invalid?
|
||||||
|
assert_equal I18n.t('email.email_mx_check_error'), contact.errors.messages[:email].first
|
||||||
Truemail.configure do |config|
|
|
||||||
config.default_validation_type = :regex
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_email_verification_regex_error
|
||||||
|
Truemail.configure.default_validation_type = :regex
|
||||||
|
|
||||||
|
contact = valid_contact
|
||||||
|
contact.email = 'some@strangesentence@internet.ee'
|
||||||
|
assert contact.invalid?
|
||||||
|
assert_equal I18n.t('email.email_regex_check_error'), contact.errors.messages[:email].first
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_without_phone
|
def test_invalid_without_phone
|
||||||
|
|
|
@ -5,11 +5,13 @@ class RegistrarTest < ActiveSupport::TestCase
|
||||||
@registrar = registrars(:bestnames)
|
@registrar = registrars(:bestnames)
|
||||||
@original_default_language = Setting.default_language
|
@original_default_language = Setting.default_language
|
||||||
@original_days_to_keep_invoices_active = Setting.days_to_keep_invoices_active
|
@original_days_to_keep_invoices_active = Setting.days_to_keep_invoices_active
|
||||||
|
@old_validation_type = Truemail.configure.default_validation_type
|
||||||
end
|
end
|
||||||
|
|
||||||
teardown do
|
teardown do
|
||||||
Setting.default_language = @original_default_language
|
Setting.default_language = @original_default_language
|
||||||
Setting.days_to_keep_invoices_active = @original_days_to_keep_invoices_active
|
Setting.days_to_keep_invoices_active = @original_days_to_keep_invoices_active
|
||||||
|
Truemail.configure.default_validation_type = @old_validation_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_valid_registrar_is_valid
|
def test_valid_registrar_is_valid
|
||||||
|
@ -38,27 +40,82 @@ class RegistrarTest < ActiveSupport::TestCase
|
||||||
assert registrar.invalid?
|
assert registrar.invalid?
|
||||||
end
|
end
|
||||||
|
|
||||||
def tests_email_mx_and_smtp
|
def test_email_verification_valid
|
||||||
Truemail.configure do |config|
|
|
||||||
config.default_validation_type = :smtp
|
|
||||||
end
|
|
||||||
|
|
||||||
registrar = valid_registrar
|
registrar = valid_registrar
|
||||||
registrar.email = 'info@internet.ee'
|
registrar.email = 'info@internet.ee'
|
||||||
|
registrar.billing_email = nil
|
||||||
|
|
||||||
assert registrar.valid?
|
assert registrar.valid?
|
||||||
|
|
||||||
registrar.email = 'somecrude1337joke@internet.ee'
|
|
||||||
assert registrar.invalid?
|
|
||||||
|
|
||||||
registrar.email = 'some@strangesentence@internet.ee'
|
|
||||||
assert registrar.invalid?
|
|
||||||
|
|
||||||
registrar.email = 'somecrude31337joke@somestrange31337domain.ee'
|
|
||||||
assert registrar.invalid?
|
|
||||||
|
|
||||||
Truemail.configure do |config|
|
|
||||||
config.default_validation_type = :regex
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_email_verification_smtp_error
|
||||||
|
Truemail.configure.default_validation_type = :smtp
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.email = 'somecrude1337joke@internet.ee'
|
||||||
|
registrar.billing_email = nil
|
||||||
|
|
||||||
|
assert registrar.invalid?
|
||||||
|
assert_equal I18n.t('email.email_smtp_check_error'), registrar.errors.messages[:email].first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_email_verification_mx_error
|
||||||
|
Truemail.configure.default_validation_type = :mx
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.email = 'somecrude31337joke@somestrange31337domain.ee'
|
||||||
|
registrar.billing_email = nil
|
||||||
|
|
||||||
|
assert registrar.invalid?
|
||||||
|
assert_equal I18n.t('email.email_mx_check_error'), registrar.errors.messages[:email].first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_email_verification_regex_error
|
||||||
|
Truemail.configure.default_validation_type = :regex
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.email = 'some@strangesentence@internet.ee'
|
||||||
|
registrar.billing_email = nil
|
||||||
|
|
||||||
|
assert registrar.invalid?
|
||||||
|
assert_equal I18n.t('email.email_regex_check_error'), registrar.errors.messages[:email].first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_billing_email_verification_valid
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.billing_email = 'info@internet.ee'
|
||||||
|
|
||||||
|
assert registrar.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_billing_email_verification_smtp_error
|
||||||
|
Truemail.configure.default_validation_type = :smtp
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.billing_email = 'somecrude1337joke@internet.ee'
|
||||||
|
|
||||||
|
assert registrar.invalid?
|
||||||
|
assert_equal I18n.t('email.email_smtp_check_error'), registrar.errors.messages[:billing_email].first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_billing_email_verification_mx_error
|
||||||
|
Truemail.configure.default_validation_type = :mx
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.billing_email = 'somecrude31337joke@somestrange31337domain.ee'
|
||||||
|
|
||||||
|
assert registrar.invalid?
|
||||||
|
assert_equal I18n.t('email.email_mx_check_error'), registrar.errors.messages[:billing_email].first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_billing_email_verification_regex_error
|
||||||
|
Truemail.configure.default_validation_type = :regex
|
||||||
|
|
||||||
|
registrar = valid_registrar
|
||||||
|
registrar.billing_email = 'some@strangesentence@internet.ee'
|
||||||
|
|
||||||
|
assert registrar.invalid?
|
||||||
|
assert_equal I18n.t('email.email_regex_check_error'), registrar.errors.messages[:billing_email].first
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_creates_email_verification_in_unicode
|
def test_creates_email_verification_in_unicode
|
||||||
|
@ -88,29 +145,6 @@ class RegistrarTest < ActiveSupport::TestCase
|
||||||
assert registrar.valid?
|
assert registrar.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
def tests_email_mx_and_smtp
|
|
||||||
Truemail.configure do |config|
|
|
||||||
config.default_validation_type = :smtp
|
|
||||||
end
|
|
||||||
|
|
||||||
registrar = valid_registrar
|
|
||||||
registrar.billing_email = 'info@internet.ee'
|
|
||||||
assert registrar.valid?
|
|
||||||
|
|
||||||
registrar.billing_email = 'somecrude1337joke@internet.ee'
|
|
||||||
assert registrar.invalid?
|
|
||||||
|
|
||||||
registrar.billing_email = 'непонятное@словосочетание@internet.ee'
|
|
||||||
assert registrar.invalid?
|
|
||||||
|
|
||||||
registrar.billing_email = 'somecrude31337joke@somestrange31337domain.ee'
|
|
||||||
assert registrar.invalid?
|
|
||||||
|
|
||||||
Truemail.configure do |config|
|
|
||||||
config.default_validation_type = :regex
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_returns_billing_email_when_provided
|
def test_returns_billing_email_when_provided
|
||||||
billing_email = 'billing@registrar.test'
|
billing_email = 'billing@registrar.test'
|
||||||
registrar = Registrar.new(billing_email: billing_email)
|
registrar = Registrar.new(billing_email: billing_email)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue