From 5ea5dd7197f8965b92444e7fed8b1197ed3fb2d7 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 11 Nov 2021 14:24:02 +0200 Subject: [PATCH] updated: also validated contacts which was failed without any time limits --- app/jobs/verify_emails_job.rb | 1 - app/models/validation_event.rb | 2 +- lib/tasks/verify_email.rake | 13 ++++-- test/tasks/emails/verify_email_task_test.rb | 49 ++++++++++++++------- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/app/jobs/verify_emails_job.rb b/app/jobs/verify_emails_job.rb index af1974b67..4f66601cc 100644 --- a/app/jobs/verify_emails_job.rb +++ b/app/jobs/verify_emails_job.rb @@ -25,7 +25,6 @@ class VerifyEmailsJob < ApplicationJob contact_ids = Contact.where(email: contact.email).where('created_at > ?', time).pluck(:id) r = ValidationEvent.where(validation_eventable_id: contact_ids).order(created_at: :desc) - # return false if r[0].success == false r.present? end diff --git a/app/models/validation_event.rb b/app/models/validation_event.rb index c38870a34..8e6a0a16d 100644 --- a/app/models/validation_event.rb +++ b/app/models/validation_event.rb @@ -12,7 +12,7 @@ class ValidationEvent < ApplicationRecord INVALID_EVENTS_COUNT_BY_LEVEL = { regex: 1, - mx: 5, + mx: 2, smtp: 1, }.freeze diff --git a/lib/tasks/verify_email.rake b/lib/tasks/verify_email.rake index a9655c1a8..62f80aabb 100644 --- a/lib/tasks/verify_email.rake +++ b/lib/tasks/verify_email.rake @@ -21,7 +21,7 @@ namespace :verify_email do contacts = prepare_contacts(options) logger.info 'No contacts to check email selected' and next if contacts.blank? - contacts.find_each do |contact| + contacts.each do |contact| VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later( contact_id: contact.id, check_level: check_level(options) @@ -58,12 +58,19 @@ def prepare_contacts(options) validation_events_ids = ValidationEvent.where('created_at > ?', time).pluck(:validation_eventable_id) # Contact.where.not(id: validation_events_ids) + Contact.where(id: failed_contacts) - Contact.where.not(id: validation_events_ids) + Contact.where.not(id: validation_events_ids) | failed_contacts end end def failed_contacts - ValidationEvent.failed.pluck(:id) + failed_contacts = [] + failed_validations_ids = ValidationEvent.failed.pluck(:validation_eventable_id) + contacts = Contact.where(id: failed_validations_ids) + contacts.each do |contact| + failed_contacts << contact unless contact.validation_events.last.success + end + + failed_contacts end def contacts_by_domain(domain_name) diff --git a/test/tasks/emails/verify_email_task_test.rb b/test/tasks/emails/verify_email_task_test.rb index 3113bcb38..f16ce2871 100644 --- a/test/tasks/emails/verify_email_task_test.rb +++ b/test/tasks/emails/verify_email_task_test.rb @@ -70,22 +70,19 @@ class VerifyEmailTaskTest < ActiveJob::TestCase assert_equal ValidationEvent.count, Contact.count - 1 end - # def test_should_verify_again_contact_which_has_faield_verification - # expired_date = Time.now - ValidationEvent::VALIDATION_PERIOD - 1.day - # - # assert_equal ValidationEvent.count, 0 - # run_task - # assert_equal Contact.count, 9 - # assert_equal ValidationEvent.count, 8 # Contact has duplicate email and it is skip - # - # contact = contacts(:john) - # v = ValidationEvent.find_by(validation_eventable_id: contact.id) - # v.update!(success: false) - # - # run_task - # binding.pry - # assert_equal ValidationEvent.all.count, 9 - # end + def test_should_verify_again_contact_which_has_faield_verification + assert_equal ValidationEvent.count, 0 + run_task + assert_equal Contact.count, 9 + assert_equal ValidationEvent.count, 8 # Contact has duplicate email and it is skip + + contact = contacts(:john) + v = ValidationEvent.find_by(validation_eventable_id: contact.id) + v.update!(success: false) + + run_task + assert_equal ValidationEvent.all.count, 9 + end def test_should_verify_contact_which_has_expired_date_of_verification expired_date = Time.now - ValidationEvent::VALIDATION_PERIOD - 1.day @@ -103,6 +100,26 @@ class VerifyEmailTaskTest < ActiveJob::TestCase assert_equal ValidationEvent.all.count, 9 end + def test_should_set_fd_for_domains_which_related_to_failed_emails + assert_equal ValidationEvent.count, 0 + run_task + assert_equal Contact.count, 9 + assert_equal ValidationEvent.count, 8 # Contact has duplicate email and it is skip + + contact = contacts(:john) + v = ValidationEvent.find_by(validation_eventable_id: contact.id) + v.update!(success: false) + + 4.times do + contact.validation_events << v.dup + end + + run_task + assert_equal ValidationEvent.all.count, 13 + + assert contact.domains.last.force_delete_scheduled? + end + def test_tasks_verifies_emails capture_io { run_task }