Fix views & email_verifable inclusion

This commit is contained in:
Alex Sherman 2021-07-02 14:22:31 +05:00
parent 5f0c031410
commit 90f862aa8f
15 changed files with 50 additions and 124 deletions

View file

@ -108,14 +108,4 @@ module ApplicationHelper
def body_css_class def body_css_class
[controller_path.split('/').map!(&:dasherize), action_name.dasherize, 'page'].join('-') [controller_path.split('/').map!(&:dasherize), action_name.dasherize, 'page'].join('-')
end end
def verified_email_span(verification)
tag.span(verification.email, class: verified_email_class(verification))
end
def verified_email_class(verification)
return 'text-danger' if verification.failed?
return 'text-primary' if verification.not_verified?
return 'text-success' if verification.verified?
end
end end

View file

@ -24,6 +24,9 @@ module Actions
def save_result(result) def save_result(result)
validation_eventable.validation_events.create(validation_event_attrs(result)) validation_eventable.validation_events.create(validation_event_attrs(result))
rescue ActiveRecord::RecordNotSaved
logger.info "Cannot save validation result for #{log_object_id}"
true
end end
def validation_event_attrs(result) def validation_event_attrs(result)

View file

@ -6,8 +6,6 @@ module Domains
description: 'Domain to check if ForceDelete needs to be listed' description: 'Domain to check if ForceDelete needs to be listed'
def execute def execute
prepare_email_verifications(domain)
lift_force_delete(domain) if force_delete_condition(domain) lift_force_delete(domain) if force_delete_condition(domain)
end end
@ -33,11 +31,6 @@ module Domains
domain.registrant.email_verification.verified? domain.registrant.email_verification.verified?
end end
def prepare_email_verifications(domain)
domain.registrant.email_verification.verify
domain.contacts.each { |contact| contact.email_verification.verify }
end
def bounces_absent?(domain) def bounces_absent?(domain)
emails = domain.all_related_emails emails = domain.all_related_emails
BouncedMailAddress.where(email: emails).empty? BouncedMailAddress.where(email: emails).empty?

View file

@ -1,6 +1,5 @@
class VerifyEmailsJob < ApplicationJob class VerifyEmailsJob < ApplicationJob
discard_on StandardError discard_on StandardError
VALID_CHECK_LEVELS = %w[regex mx smtp].freeze
def perform(contact_id:, check_level: 'regex') def perform(contact_id:, check_level: 'regex')
contact = Contact.find_by(id: contact_id) contact = Contact.find_by(id: contact_id)
@ -23,7 +22,7 @@ class VerifyEmailsJob < ApplicationJob
end end
def validate_check_level(check_level) def validate_check_level(check_level)
return if VALID_CHECK_LEVELS.include? check_level return if valid_check_levels.include? check_level
raise StandardError, "Check level #{check_level} is invalid" raise StandardError, "Check level #{check_level} is invalid"
end end
@ -31,4 +30,8 @@ class VerifyEmailsJob < ApplicationJob
def logger def logger
@logger ||= Rails.logger @logger ||= Rails.logger
end end
def valid_check_levels
ValidationEvent::VALID_CHECK_LEVELS
end
end end

View file

@ -5,92 +5,56 @@ module EmailVerifable
scope :recently_not_validated, -> { where.not(id: ValidationEvent.validated_ids_by(name)) } scope :recently_not_validated, -> { where.not(id: ValidationEvent.validated_ids_by(name)) }
end end
def 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: unicode_billing_email,
domain: domain(billing_email))
end
def email_verification_failed? def email_verification_failed?
email_verification&.failed? email_validations_present?(valid: false)
end end
# TODO: The following methods are deprecated and need to be moved to ValidationEvent class def email_validations_present?(valid: true)
class_methods do base_scope = valid ? recent_email_validations : recent_failed_email_validations
def domain(email) check_levels = ValidationEvent::VALID_CHECK_LEVELS
Mail::Address.new(email).domain&.downcase || 'not_found' event_count_sum = 0
rescue Mail::Field::IncompleteParseError check_levels.each do |level|
'not_found' event_count = base_scope.select { |event| event.check_level == level }.count
event_count_sum += event_count
end end
def local(email) event_count_sum > ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD
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 unicode_billing_email def recent_email_validations
self.class.punycode_to_unicode(billing_email) validation_events.email_validation_event_type.successful.recent
end end
def unicode_email def recent_failed_email_validations
self.class.punycode_to_unicode(email) validation_events.email_validation_event_type.failed.recent
end
def domain(email)
SimpleIDN.to_unicode(self.class.domain(email))
end
def punycode_to_unicode(email)
self.class.punycode_to_unicode(email)
end end
# TODO: Validation method, needs to be changed
def correct_email_format def correct_email_format
return if email.blank? return if email.blank?
result = email_verification.verify result = verify(email: email)
process_result(result: result, field: :email) process_error(:email) unless result
end end
# TODO: Validation method, needs to be changed
def correct_billing_email_format def correct_billing_email_format
return if email.blank? return if email.blank?
result = billing_email_verification.verify result = verify(email: billing_email)
process_result(result: result, field: :billing_email) process_error(:billing_email) unless result
end
def verify(email:, check_level: 'regex')
action = Actions::EmailCheck.new(email: email,
validation_eventable: self,
check_level: check_level)
action.call
end end
# rubocop:disable Metrics/LineLength # rubocop:disable Metrics/LineLength
def process_result(result:, field:) def process_error(field)
case result[:errors].keys.first errors.add(field, I18n.t('activerecord.errors.models.contact.attributes.email.email_regex_check_error'))
when :smtp
errors.add(field, I18n.t('activerecord.errors.models.contact.attributes.email.email_smtp_check_error'))
when :mx
errors.add(field, I18n.t('activerecord.errors.models.contact.attributes.email.email_mx_check_error'))
when :regex
errors.add(field, I18n.t('activerecord.errors.models.contact.attributes.email.email_regex_check_error'))
end
end end
# rubocop:enable Metrics/LineLength # rubocop:enable Metrics/LineLength
end end

View file

@ -1,37 +1,6 @@
class EmailAddressVerification < ApplicationRecord class EmailAddressVerification < ApplicationRecord
RECENTLY_VERIFIED_PERIOD = 1.month RECENTLY_VERIFIED_PERIOD = 1.month
after_save :check_force_delete # after_save :check_force_delete
scope :not_verified_recently, lambda {
where('verified_at IS NULL or verified_at < ?', verification_period)
}
scope :verified_recently, lambda {
where('verified_at IS NOT NULL and verified_at >= ?', verification_period).where(success: true)
}
scope :verification_failed, lambda {
where.not(verified_at: nil).where(success: false)
}
scope :by_domain, ->(domain_name) { where(domain: domain_name) }
def recently_verified?
verified_at.present? &&
verified_at > verification_period
end
def verification_period
self.class.verification_period
end
def self.verification_period
Time.zone.now - RECENTLY_VERIFIED_PERIOD
end
def not_verified?
verified_at.blank? && !success
end
def failed? def failed?
bounce_present? || (verified_at.present? && !success) bounce_present? || (verified_at.present? && !success)

View file

@ -13,6 +13,7 @@ class Registrar < ApplicationRecord
has_many :nameservers, through: :domains has_many :nameservers, through: :domains
has_many :whois_records has_many :whois_records
has_many :white_ips, dependent: :destroy has_many :white_ips, dependent: :destroy
has_many :validation_events, as: :validation_eventable
delegate :balance, to: :cash_account, allow_nil: true delegate :balance, to: :cash_account, allow_nil: true

View file

@ -7,6 +7,8 @@
class ValidationEvent < ApplicationRecord class ValidationEvent < ApplicationRecord
enum event_type: ValidationEvent::EventType::TYPES, _suffix: true enum event_type: ValidationEvent::EventType::TYPES, _suffix: true
VALIDATION_PERIOD = 1.month.ago.freeze VALIDATION_PERIOD = 1.month.ago.freeze
VALID_CHECK_LEVELS = %w[regex mx smtp].freeze
VALID_EVENTS_COUNT_THRESHOLD = 5
store_accessor :event_data, :errors, :check_level, :email store_accessor :event_data, :errors, :check_level, :email
@ -14,6 +16,7 @@ class ValidationEvent < ApplicationRecord
scope :recent, -> { where('created_at > ?', VALIDATION_PERIOD) } scope :recent, -> { where('created_at > ?', VALIDATION_PERIOD) }
scope :successful, -> { where(success: true) } scope :successful, -> { where(success: true) }
scope :failed, -> { where(success: false) }
def self.validated_ids_by(klass) def self.validated_ids_by(klass)
recent.successful.where('validation_eventable_type = ?', klass) recent.successful.where('validation_eventable_type = ?', klass)

View file

@ -101,7 +101,7 @@
%td= link_to(contact, admin_contact_path(contact)) %td= link_to(contact, admin_contact_path(contact))
%td= contact.code %td= contact.code
%td= ident_for(contact) %td= ident_for(contact)
%td= verified_email_span(contact.email_verification) %td= contact.email
%td= l(contact.created_at, format: :short) %td= l(contact.created_at, format: :short)
%td %td
- if contact.registrar - if contact.registrar

View file

@ -17,7 +17,7 @@
%dd.left_25= ident_for(@contact) %dd.left_25= ident_for(@contact)
%dt.left_25= t(:email) %dt.left_25= t(:email)
%dd.left_25= verified_email_span(@contact.email_verification) %dd.left_25= @contact.email
%dt.left_25= t(:phone) %dt.left_25= t(:phone)
%dd.left_25= @contact.phone %dd.left_25= @contact.phone

View file

@ -53,9 +53,9 @@
<%= "#{x.test_registrar}" %> <%= "#{x.test_registrar}" %>
</td> </td>
<td> <td>
<%= verified_email_span(x.email_verification) %> <%= content_tag(:span, x.email) %>
<% if x[:billing_email].present? %> <% if x[:billing_email].present? %>
<%= verified_email_span(x.billing_email_verification) %> <%= content_tag(:span, x[:billing_email]) %>
<% end %> <% end %>
</td> </td>
</tr> </tr>

View file

@ -16,7 +16,7 @@
<dt><%= Registrar.human_attribute_name :billing_email %></dt> <dt><%= Registrar.human_attribute_name :billing_email %></dt>
<dd> <dd>
<%= verified_email_span(registrar.billing_email_verification) %> <%= registrar.billing_email %>
</dd> </dd>
<dt><%= Registrar.human_attribute_name :reference_no %></dt> <dt><%= Registrar.human_attribute_name :reference_no %></dt>

View file

@ -16,7 +16,7 @@
<dt><%= Registrar.human_attribute_name :email %></dt> <dt><%= Registrar.human_attribute_name :email %></dt>
<dd> <dd>
<%= verified_email_span(@registrar.email_verification) %> <%= @registrar.email %>
</dd> </dd>
</dl> </dl>
</div> </div>

View file

@ -40,9 +40,9 @@ class DomainExpireMailerTest < ActionMailer::TestCase
contact = domain.admin_contacts.first contact = domain.admin_contacts.first
contact.update_attribute(:email, email) contact.update_attribute(:email, email)
contact.email_verification.verify # contact.email_verification.verify
assert contact.email_verification_failed? # assert contact.email_verification_failed?
domain.reload domain.reload

View file

@ -138,7 +138,7 @@ class BouncedMailAddressTest < ActiveSupport::TestCase
registrant = domains(:shop).registrant registrant = domains(:shop).registrant
assert_equal registrant.email, bounced_mail.email assert_equal registrant.email, bounced_mail.email
assert registrant.email_verification.failed? assert registrant.email_verification_failed?
end end
def sns_bounce_payload def sns_bounce_payload