mirror of
https://github.com/internetee/registry.git
synced 2025-06-11 23:24:48 +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,16 +3,16 @@ module Concerns
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
def email_verification
|
||||
EmailAddressVerification.find_or_create_by(email: self.class.punycode_to_unicode(email),
|
||||
domain: domain(email))
|
||||
@email_verification ||= EmailAddressVerification.find_or_create_by(email: unicode_email,
|
||||
domain: domain(email))
|
||||
end
|
||||
|
||||
def billing_email_verification
|
||||
return unless attribute_names.include?('billing_email')
|
||||
|
||||
EmailAddressVerification.find_or_create_by(email: self.class
|
||||
.punycode_to_unicode(billing_email),
|
||||
domain: domain(billing_email))
|
||||
@billing_email_verification ||= EmailAddressVerification
|
||||
.find_or_create_by(email: unicode_billing_email,
|
||||
domain: domain(billing_email))
|
||||
end
|
||||
|
||||
class_methods do
|
||||
|
@ -45,6 +45,14 @@ module Concerns
|
|||
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)
|
||||
SimpleIDN.to_unicode(self.class.domain(email))
|
||||
end
|
||||
|
@ -53,16 +61,29 @@ module Concerns
|
|||
self.class.punycode_to_unicode(email)
|
||||
end
|
||||
|
||||
def verify_email_mx_smtp(field:, email:)
|
||||
errors.add(field, :invalid) unless email.blank? || Truemail.valid?(email)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
|
|
@ -41,8 +41,7 @@ class EmailAddressVerification < ApplicationRecord
|
|||
end
|
||||
|
||||
def verify
|
||||
media = :mx
|
||||
validation_request = Truemail.validate(email, with: media)
|
||||
validation_request = Truemail.validate(email)
|
||||
|
||||
if validation_request.result.success
|
||||
update(verified_at: Time.zone.now,
|
||||
|
@ -52,6 +51,6 @@ class EmailAddressVerification < ApplicationRecord
|
|||
success: false)
|
||||
end
|
||||
|
||||
validation_request.result.success
|
||||
validation_request.result
|
||||
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
|
||||
setup do
|
||||
@contact = contacts(:john)
|
||||
@old_validation_type = Truemail.configure.default_validation_type
|
||||
end
|
||||
|
||||
teardown do
|
||||
Truemail.configure.default_validation_type = @old_validation_type
|
||||
end
|
||||
|
||||
def test_valid_contact_fixture_is_valid
|
||||
|
@ -61,27 +66,37 @@ class ContactTest < ActiveSupport::TestCase
|
|||
assert contact.invalid?
|
||||
end
|
||||
|
||||
def tests_email_mx_and_smtp
|
||||
Truemail.configure do |config|
|
||||
config.default_validation_type = :smtp
|
||||
end
|
||||
|
||||
def test_email_verification_valid
|
||||
contact = valid_contact
|
||||
contact.email = 'info@internet.ee'
|
||||
assert contact.valid?
|
||||
end
|
||||
|
||||
def test_email_verification_smtp_error
|
||||
Truemail.configure.default_validation_type = :smtp
|
||||
|
||||
contact = valid_contact
|
||||
contact.email = 'somecrude1337joke@internet.ee'
|
||||
assert contact.invalid?
|
||||
assert_equal I18n.t('email.email_smtp_check_error'), contact.errors.messages[:email].first
|
||||
end
|
||||
|
||||
contact.email = 'some@strangesentence@internet.ee'
|
||||
assert contact.invalid?
|
||||
def test_email_verification_mx_error
|
||||
Truemail.configure.default_validation_type = :mx
|
||||
|
||||
contact = valid_contact
|
||||
contact.email = 'somecrude31337joke@somestrange31337domain.ee'
|
||||
assert contact.invalid?
|
||||
assert_equal I18n.t('email.email_mx_check_error'), contact.errors.messages[:email].first
|
||||
end
|
||||
|
||||
Truemail.configure do |config|
|
||||
config.default_validation_type = :regex
|
||||
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
|
||||
|
||||
def test_invalid_without_phone
|
||||
|
|
|
@ -5,11 +5,13 @@ class RegistrarTest < ActiveSupport::TestCase
|
|||
@registrar = registrars(:bestnames)
|
||||
@original_default_language = Setting.default_language
|
||||
@original_days_to_keep_invoices_active = Setting.days_to_keep_invoices_active
|
||||
@old_validation_type = Truemail.configure.default_validation_type
|
||||
end
|
||||
|
||||
teardown do
|
||||
Setting.default_language = @original_default_language
|
||||
Setting.days_to_keep_invoices_active = @original_days_to_keep_invoices_active
|
||||
Truemail.configure.default_validation_type = @old_validation_type
|
||||
end
|
||||
|
||||
def test_valid_registrar_is_valid
|
||||
|
@ -38,27 +40,82 @@ class RegistrarTest < ActiveSupport::TestCase
|
|||
assert registrar.invalid?
|
||||
end
|
||||
|
||||
def tests_email_mx_and_smtp
|
||||
Truemail.configure do |config|
|
||||
config.default_validation_type = :smtp
|
||||
end
|
||||
|
||||
def test_email_verification_valid
|
||||
registrar = valid_registrar
|
||||
registrar.email = 'info@internet.ee'
|
||||
registrar.billing_email = nil
|
||||
|
||||
assert registrar.valid?
|
||||
end
|
||||
|
||||
def test_email_verification_smtp_error
|
||||
Truemail.configure.default_validation_type = :smtp
|
||||
|
||||
registrar = valid_registrar
|
||||
registrar.email = 'somecrude1337joke@internet.ee'
|
||||
assert registrar.invalid?
|
||||
registrar.billing_email = nil
|
||||
|
||||
registrar.email = 'some@strangesentence@internet.ee'
|
||||
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'
|
||||
assert registrar.invalid?
|
||||
registrar.billing_email = nil
|
||||
|
||||
Truemail.configure do |config|
|
||||
config.default_validation_type = :regex
|
||||
end
|
||||
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
|
||||
|
||||
def test_creates_email_verification_in_unicode
|
||||
|
@ -88,29 +145,6 @@ class RegistrarTest < ActiveSupport::TestCase
|
|||
assert registrar.valid?
|
||||
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
|
||||
billing_email = 'billing@registrar.test'
|
||||
registrar = Registrar.new(billing_email: billing_email)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue