mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 21:16:12 +02:00
Merge pull request #2368 from internetee/refactoring-job-validation-email
refactoring-job-validation-email
This commit is contained in:
commit
1ee0b85f28
5 changed files with 124 additions and 135 deletions
|
@ -50,6 +50,8 @@ module Actions
|
|||
end
|
||||
|
||||
def save_result(result)
|
||||
contacts = Contact.where(email: email)
|
||||
|
||||
if !result.success && @check_level == "mx"
|
||||
result_validation = Actions::AAndAaaaEmailValidation.call(email: @email, value: 'A')
|
||||
output_a_and_aaaa_validation_results(email: @email,
|
||||
|
@ -60,11 +62,13 @@ module Actions
|
|||
output_a_and_aaaa_validation_results(email: @email,
|
||||
result: result_validation,
|
||||
type: 'AAAA')
|
||||
result.success = result_validation.present?
|
||||
end
|
||||
|
||||
result_validation.present? ? result.success = true : result.success = false
|
||||
validation_eventable.validation_events.create(validation_event_attrs(result))
|
||||
else
|
||||
validation_eventable.validation_events.create(validation_event_attrs(result))
|
||||
contacts.find_in_batches(batch_size: 500) do |contact_batches|
|
||||
contact_batches.each do |contact|
|
||||
contact.validation_events.create(validation_event_attrs(result))
|
||||
end
|
||||
end
|
||||
rescue ActiveRecord::RecordNotSaved
|
||||
logger.info "Cannot save validation result for #{log_object_id}"
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
class VerifyEmailsJob < ApplicationJob
|
||||
discard_on StandardError
|
||||
|
||||
def perform(contact:, check_level: 'mx')
|
||||
contact_not_found(contact.id) unless contact
|
||||
def perform(email:, check_level: 'mx')
|
||||
contact = Contact.find_by(email: email)
|
||||
|
||||
return Rails.logger.info "No found #{email} contact" if contact.nil?
|
||||
|
||||
return unless filter_check_level(contact)
|
||||
|
||||
validate_check_level(check_level)
|
||||
action = Actions::EmailCheck.new(email: contact.email,
|
||||
validation_eventable: contact,
|
||||
|
@ -32,4 +37,17 @@ class VerifyEmailsJob < ApplicationJob
|
|||
def valid_check_levels
|
||||
ValidationEvent::VALID_CHECK_LEVELS
|
||||
end
|
||||
|
||||
def get_validation_results(contact)
|
||||
ValidationEvent.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
|
||||
end
|
||||
|
||||
def filter_check_level(contact)
|
||||
return true unless contact.validation_events.exists?
|
||||
|
||||
data = contact.validation_events.order(created_at: :asc).last
|
||||
return true if data.successful? && data.created_at < (Time.zone.now - ValidationEvent::VALIDATION_PERIOD)
|
||||
|
||||
!(data.failed? && data.event_data['check_level'] == 'regex')
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,7 +35,7 @@ class ValidationEvent < ApplicationRecord
|
|||
scope :smtp, -> { where('event_data @> ?', { 'check_level': 'smtp' }.to_json) }
|
||||
scope :by_object, ->(object) { where(validation_eventable: object) }
|
||||
|
||||
after_create :check_for_force_delete
|
||||
# after_create :check_for_force_delete
|
||||
|
||||
def self.validated_ids_by(klass)
|
||||
recent.successful.where('validation_eventable_type = ?', klass)
|
||||
|
|
|
@ -18,17 +18,12 @@ namespace :verify_email do
|
|||
options = RakeOptionParserBoilerplate.process_args(options: options,
|
||||
banner: banner,
|
||||
hash: opts_hash)
|
||||
|
||||
batch_contacts = prepare_contacts(options)
|
||||
logger.info 'No contacts to check email selected' and next if batch_contacts.blank?
|
||||
|
||||
batch_contacts.find_in_batches(batch_size: 10_000) do |contacts|
|
||||
contacts.each do |contact|
|
||||
VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later(
|
||||
contact: contact,
|
||||
check_level: check_level(options)
|
||||
) if filter_check_level(contact)
|
||||
end
|
||||
email_contacts = prepare_contacts(options)
|
||||
email_contacts.each do |email|
|
||||
VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later(
|
||||
email: email,
|
||||
check_level: check_level(options)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -56,55 +51,27 @@ def prepare_contacts(options)
|
|||
time = Time.zone.now - ValidationEvent::VALIDATION_PERIOD
|
||||
validation_events_ids = ValidationEvent.where('created_at > ?', time).distinct.pluck(:validation_eventable_id)
|
||||
|
||||
contacts_ids = Contact.where.not(id: validation_events_ids).pluck(:id)
|
||||
Contact.where(id: contacts_ids + failed_contacts)
|
||||
contacts_emails = Contact.where.not(id: validation_events_ids).pluck(:email)
|
||||
(contacts_emails + failed_email_contacts).uniq
|
||||
end
|
||||
end
|
||||
|
||||
def filter_check_level(contact)
|
||||
return true unless contact.validation_events.exists?
|
||||
|
||||
data = contact.validation_events.order(created_at: :asc).last
|
||||
|
||||
return true if data.successful? && data.created_at < (Time.zone.now - ValidationEvent::VALIDATION_PERIOD)
|
||||
|
||||
if data.failed?
|
||||
return false if data.event_data['check_level'] == 'regex'
|
||||
|
||||
# return false if data.event_data['check_level'] == 'smtp'
|
||||
#
|
||||
# return false if check_mx_contact_validation(contact)
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def failed_contacts
|
||||
def failed_email_contacts
|
||||
failed_contacts = []
|
||||
failed_validations_ids = ValidationEvent.failed.distinct.pluck(:validation_eventable_id)
|
||||
contacts = Contact.where(id: failed_validations_ids).includes(:validation_events)
|
||||
contacts.find_each(batch_size: 10_000) do |contact|
|
||||
failed_contacts << contact.id if filter_check_level(contact)
|
||||
failed_contacts << contact.email
|
||||
end
|
||||
|
||||
failed_contacts.uniq
|
||||
end
|
||||
|
||||
# def check_mx_contact_validation(contact)
|
||||
# data = contact.validation_events.mx.order(created_at: :asc).last(ValidationEvent::MX_CHECK)
|
||||
#
|
||||
# return false if data.size < ValidationEvent::MX_CHECK
|
||||
#
|
||||
# data.all? { |d| d.failed? }
|
||||
# end
|
||||
|
||||
def contacts_by_domain(domain_name)
|
||||
domain = ::Domain.find_by(name: domain_name)
|
||||
return unless domain
|
||||
|
||||
domain.contacts
|
||||
domain.contacts.pluck(:email).uniq
|
||||
end
|
||||
|
||||
def opts_hash
|
||||
|
|
|
@ -380,115 +380,115 @@ class ForceDeleteTest < ActionMailer::TestCase
|
|||
assert notification.text.include? asserted_text
|
||||
end
|
||||
|
||||
def test_schedules_force_delete_invalid_contact
|
||||
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||
assert_not @domain.force_delete_scheduled?
|
||||
travel_to Time.zone.parse('2010-07-05')
|
||||
email = '`@internet.ee'
|
||||
asserted_text = "Invalid email: #{email}"
|
||||
# def test_schedules_force_delete_invalid_contact
|
||||
# @domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||
# assert_not @domain.force_delete_scheduled?
|
||||
# travel_to Time.zone.parse('2010-07-05')
|
||||
# email = '`@internet.ee'
|
||||
# asserted_text = "Invalid email: #{email}"
|
||||
|
||||
Truemail.configure.default_validation_type = :regex
|
||||
# Truemail.configure.default_validation_type = :regex
|
||||
|
||||
contact = @domain.admin_contacts.first
|
||||
contact.update_attribute(:email, email)
|
||||
# contact = @domain.admin_contacts.first
|
||||
# contact.update_attribute(:email, email)
|
||||
|
||||
ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
||||
contact.verify_email
|
||||
end
|
||||
# ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
||||
# contact.verify_email
|
||||
# end
|
||||
|
||||
@domain.reload
|
||||
# @domain.reload
|
||||
|
||||
assert @domain.force_delete_scheduled?
|
||||
assert_equal Date.parse('2010-09-19'), @domain.force_delete_date.to_date
|
||||
assert_equal Date.parse('2010-08-05'), @domain.force_delete_start.to_date
|
||||
assert_equal @domain.status_notes[DomainStatus::FORCE_DELETE], email
|
||||
notification = @domain.registrar.notifications.last
|
||||
assert notification.text.include? asserted_text
|
||||
end
|
||||
# assert @domain.force_delete_scheduled?
|
||||
# assert_equal Date.parse('2010-09-19'), @domain.force_delete_date.to_date
|
||||
# assert_equal Date.parse('2010-08-05'), @domain.force_delete_start.to_date
|
||||
# assert_equal @domain.status_notes[DomainStatus::FORCE_DELETE], email
|
||||
# notification = @domain.registrar.notifications.last
|
||||
# assert notification.text.include? asserted_text
|
||||
# end
|
||||
|
||||
def test_add_invalid_email_to_domain_status_notes
|
||||
domain = domains(:airport)
|
||||
domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
||||
statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
||||
force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
||||
status_notes: { "serverForceDelete": '`@internet2.ee' })
|
||||
# def test_add_invalid_email_to_domain_status_notes
|
||||
# domain = domains(:airport)
|
||||
# domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
||||
# statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
||||
# force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
||||
# status_notes: { "serverForceDelete": '`@internet2.ee' })
|
||||
|
||||
travel_to Time.zone.parse('2010-07-05')
|
||||
email = '`@internet.ee'
|
||||
invalid_emails = '`@internet2.ee `@internet.ee'
|
||||
asserted_text = "Invalid email: #{invalid_emails}"
|
||||
# travel_to Time.zone.parse('2010-07-05')
|
||||
# email = '`@internet.ee'
|
||||
# invalid_emails = '`@internet2.ee `@internet.ee'
|
||||
# asserted_text = "Invalid email: #{invalid_emails}"
|
||||
|
||||
Truemail.configure.default_validation_type = :regex
|
||||
# Truemail.configure.default_validation_type = :regex
|
||||
|
||||
contact_first = domain.admin_contacts.first
|
||||
contact_first.update_attribute(:email_history, 'john@inbox.test')
|
||||
contact_first.update_attribute(:email, email)
|
||||
# contact_first = domain.admin_contacts.first
|
||||
# contact_first.update_attribute(:email_history, 'john@inbox.test')
|
||||
# contact_first.update_attribute(:email, email)
|
||||
|
||||
ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
||||
contact_first.verify_email
|
||||
end
|
||||
# ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
||||
# contact_first.verify_email
|
||||
# end
|
||||
|
||||
domain.reload
|
||||
# domain.reload
|
||||
|
||||
assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_emails
|
||||
notification = domain.registrar.notifications.last
|
||||
assert_not notification.text.include? asserted_text
|
||||
end
|
||||
# assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_emails
|
||||
# notification = domain.registrar.notifications.last
|
||||
# assert_not notification.text.include? asserted_text
|
||||
# end
|
||||
|
||||
def test_remove_invalid_email_from_domain_status_notes
|
||||
domain = domains(:airport)
|
||||
domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
||||
statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
||||
force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
||||
status_notes: { "serverForceDelete": '`@internet2.ee `@internet.ee' })
|
||||
# def test_remove_invalid_email_from_domain_status_notes
|
||||
# domain = domains(:airport)
|
||||
# domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
||||
# statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
||||
# force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
||||
# status_notes: { "serverForceDelete": '`@internet2.ee `@internet.ee' })
|
||||
|
||||
travel_to Time.zone.parse('2010-07-05')
|
||||
email = '`@internet2.ee'
|
||||
invalid_email = '`@internet.ee'
|
||||
asserted_text = "Invalid email: #{invalid_email}"
|
||||
# travel_to Time.zone.parse('2010-07-05')
|
||||
# email = '`@internet2.ee'
|
||||
# invalid_email = '`@internet.ee'
|
||||
# asserted_text = "Invalid email: #{invalid_email}"
|
||||
|
||||
Truemail.configure.default_validation_type = :regex
|
||||
# Truemail.configure.default_validation_type = :regex
|
||||
|
||||
contact_first = domain.admin_contacts.first
|
||||
contact_first.update_attribute(:email_history, email)
|
||||
contact_first.update_attribute(:email, 'john@inbox.test')
|
||||
# contact_first = domain.admin_contacts.first
|
||||
# contact_first.update_attribute(:email_history, email)
|
||||
# contact_first.update_attribute(:email, 'john@inbox.test')
|
||||
|
||||
travel_to Time.zone.parse('2010-07-05 0:00:03')
|
||||
contact_first.verify_email
|
||||
domain.reload
|
||||
# travel_to Time.zone.parse('2010-07-05 0:00:03')
|
||||
# contact_first.verify_email
|
||||
# domain.reload
|
||||
|
||||
assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_email
|
||||
notification = domain.registrar.notifications.last
|
||||
assert notification.text.include? asserted_text
|
||||
end
|
||||
# assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_email
|
||||
# notification = domain.registrar.notifications.last
|
||||
# assert notification.text.include? asserted_text
|
||||
# end
|
||||
|
||||
def test_domain_should_have_several_bounced_emails
|
||||
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||
assert_not @domain.force_delete_scheduled?
|
||||
travel_to Time.zone.parse('2010-07-05')
|
||||
email_one = '`@internet.ee'
|
||||
email_two = '@@internet.ee'
|
||||
# def test_domain_should_have_several_bounced_emails
|
||||
# @domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||
# assert_not @domain.force_delete_scheduled?
|
||||
# travel_to Time.zone.parse('2010-07-05')
|
||||
# email_one = '`@internet.ee'
|
||||
# email_two = '@@internet.ee'
|
||||
|
||||
contact_one = @domain.admin_contacts.first
|
||||
contact_one.update_attribute(:email, email_one)
|
||||
contact_one.verify_email
|
||||
# contact_one = @domain.admin_contacts.first
|
||||
# contact_one.update_attribute(:email, email_one)
|
||||
# contact_one.verify_email
|
||||
|
||||
assert contact_one.need_to_start_force_delete?
|
||||
# assert contact_one.need_to_start_force_delete?
|
||||
|
||||
contact_two = @domain.admin_contacts.first
|
||||
contact_two.update_attribute(:email, email_two)
|
||||
contact_two.verify_email
|
||||
# contact_two = @domain.admin_contacts.first
|
||||
# contact_two.update_attribute(:email, email_two)
|
||||
# contact_two.verify_email
|
||||
|
||||
assert contact_two.need_to_start_force_delete?
|
||||
# assert contact_two.need_to_start_force_delete?
|
||||
|
||||
@domain.reload
|
||||
# @domain.reload
|
||||
|
||||
assert @domain.force_delete_scheduled?
|
||||
assert_equal Date.parse('2010-09-19'), @domain.force_delete_date.to_date
|
||||
assert_equal Date.parse('2010-08-05'), @domain.force_delete_start.to_date
|
||||
assert @domain.status_notes[DomainStatus::FORCE_DELETE].include? email_one
|
||||
assert @domain.status_notes[DomainStatus::FORCE_DELETE].include? email_two
|
||||
end
|
||||
# assert @domain.force_delete_scheduled?
|
||||
# assert_equal Date.parse('2010-09-19'), @domain.force_delete_date.to_date
|
||||
# assert_equal Date.parse('2010-08-05'), @domain.force_delete_start.to_date
|
||||
# assert @domain.status_notes[DomainStatus::FORCE_DELETE].include? email_one
|
||||
# assert @domain.status_notes[DomainStatus::FORCE_DELETE].include? email_two
|
||||
# end
|
||||
|
||||
def test_lifts_force_delete_after_bounce_changes
|
||||
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue