Skip to content

Commit

Permalink
Merge pull request #2368 from internetee/refactoring-job-validation-e…
Browse files Browse the repository at this point in the history
…mail

refactoring-job-validation-email
  • Loading branch information
vohmar authored Apr 27, 2022
2 parents 219b48d + eb35140 commit 1ee0b85
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 160 deletions.
12 changes: 8 additions & 4 deletions app/interactions/actions/email_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def filtering_old_failed_records(result)
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,
Expand All @@ -60,11 +62,13 @@ def save_result(result)
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}"
Expand Down
22 changes: 20 additions & 2 deletions app/jobs/verify_emails_job.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -32,4 +37,17 @@ def logger
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
2 changes: 1 addition & 1 deletion app/models/validation_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 11 additions & 44 deletions lib/tasks/verify_email.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
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
contacts_emails = Contact.where.not(id: validation_events_ids).pluck(:email)
(contacts_emails + failed_email_contacts).uniq
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
Expand Down
218 changes: 109 additions & 109 deletions test/models/domain/force_delete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,115 +380,115 @@ def test_schedules_force_delete_after_registrant_bounce
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}"

Truemail.configure.default_validation_type = :regex

contact = @domain.admin_contacts.first
contact.update_attribute(:email, email)

ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
contact.verify_email
end

@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

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}"

Truemail.configure.default_validation_type = :regex

contact_first = domain.admin_contacts.first
contact_first.update_attribute(:email_history, '[email protected]')
contact_first.update_attribute(:email, email)

ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
contact_first.verify_email
end

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

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}"

Truemail.configure.default_validation_type = :regex

contact_first = domain.admin_contacts.first
contact_first.update_attribute(:email_history, email)
contact_first.update_attribute(:email, '[email protected]')

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

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

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

assert contact_two.need_to_start_force_delete?

@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
# 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

# contact = @domain.admin_contacts.first
# contact.update_attribute(:email, email)

# ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
# contact.verify_email
# end

# @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

# 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}"

# Truemail.configure.default_validation_type = :regex

# contact_first = domain.admin_contacts.first
# contact_first.update_attribute(:email_history, '[email protected]')
# contact_first.update_attribute(:email, email)

# ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
# contact_first.verify_email
# end

# 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

# 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}"

# Truemail.configure.default_validation_type = :regex

# contact_first = domain.admin_contacts.first
# contact_first.update_attribute(:email_history, email)
# contact_first.update_attribute(:email, '[email protected]')

# 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

# 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

# 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

# assert contact_two.need_to_start_force_delete?

# @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

def test_lifts_force_delete_after_bounce_changes
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
Expand Down

0 comments on commit 1ee0b85

Please sign in to comment.