diff --git a/app/interactions/actions/email_check.rb b/app/interactions/actions/email_check.rb index c16c035ed..ca3664962 100644 --- a/app/interactions/actions/email_check.rb +++ b/app/interactions/actions/email_check.rb @@ -12,17 +12,12 @@ module Actions result = check_email(email) save_result(result) filtering_old_failed_records(result) - remove_old_records! result.success ? log_success : log_failure(result) result.success end private - def remove_old_records! - validation_eventable.validation_events.old_records.destroy_all - end - def check_email(parsed_email) Truemail.validate(parsed_email, with: calculate_check_level).result end @@ -53,14 +48,10 @@ module Actions if !result.success && @check_level == 'mx' result_validation = Actions::AAndAaaaEmailValidation.call(email: @email, value: 'A') - output_a_and_aaaa_validation_results(email: @email, - result: result_validation, - type: 'A') + output_a_and_aaaa_validation_results(email: @email, result: result_validation, type: 'A') result_validation = Actions::AAndAaaaEmailValidation.call(email: @email, value: 'AAAA') if result_validation.empty? - output_a_and_aaaa_validation_results(email: @email, - result: result_validation, - type: 'AAAA') + output_a_and_aaaa_validation_results(email: @email, result: result_validation, type: 'AAAA') result.success = result_validation.present? end diff --git a/app/jobs/verify_emails_job.rb b/app/jobs/verify_emails_job.rb index 7274fcad4..441a46569 100644 --- a/app/jobs/verify_emails_job.rb +++ b/app/jobs/verify_emails_job.rb @@ -20,10 +20,6 @@ class VerifyEmailsJob < ApplicationJob private - def contact_not_found(contact_id) - raise StandardError, "Contact with contact_id #{contact_id} not found" - end - def validate_check_level(check_level) return if valid_check_levels.include? check_level @@ -38,10 +34,6 @@ class VerifyEmailsJob < ApplicationJob 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? diff --git a/lib/tasks/verify_email.rake b/lib/tasks/verify_email.rake index b90fde0d6..ebc9c234b 100644 --- a/lib/tasks/verify_email.rake +++ b/lib/tasks/verify_email.rake @@ -20,11 +20,11 @@ namespace :verify_email do hash: opts_hash) 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) - ) + VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)) + .perform_later(email: email, check_level: check_level(options)) end + + ValidationEvent.old_records.destroy_all end end @@ -40,10 +40,6 @@ def spam_protect_timeout(options) spam_protect(options) ? 0.seconds : SPAM_PROTECT_TIMEOUT end -def logger - @logger ||= ActiveSupport::TaggedLogging.new(Syslog::Logger.new('registry')) -end - def prepare_contacts(options) if options[:domain_name].present? contacts_by_domain(options[:domain_name]) diff --git a/test/interactions/email_check_test.rb b/test/interactions/email_check_test.rb index 0aed12ea0..4e77a5bee 100644 --- a/test/interactions/email_check_test.rb +++ b/test/interactions/email_check_test.rb @@ -95,27 +95,4 @@ class EmailCheckTest < ActiveSupport::TestCase assert_equal @contact.validation_events.count, 1 assert @contact.validation_events.last.success end - - def test_should_remove_old_validation_records - trumail_results = OpenStruct.new(success: false, - email: @contact.email, - domain: "box.tests", - errors: {:mx=>"target host(s) not found"}, - ) - - Spy.on_instance_method(Actions::EmailCheck, :check_email).and_return(trumail_results) - Spy.on_instance_method(Actions::AAndAaaaEmailValidation, :call).and_return([true]) - - action = Actions::EmailCheck.new(email: @contact.email, - validation_eventable: @contact, - check_level: 'regex') - - - action.call - assert_equal @contact.validation_events.count, 1 - - travel_to(Time.zone.now + ::ValidationEvent::VALIDATION_PERIOD + 1.minute) - action.call - assert_equal @contact.validation_events.count, 1 - end end diff --git a/test/tasks/emails/verify_email_task_test.rb b/test/tasks/emails/verify_email_task_test.rb index a366708d9..256bc0fdd 100644 --- a/test/tasks/emails/verify_email_task_test.rb +++ b/test/tasks/emails/verify_email_task_test.rb @@ -116,13 +116,32 @@ class VerifyEmailTaskTest < ActiveJob::TestCase ) Spy.on_instance_method(Actions::EmailCheck, :check_email).and_return(trumail_results) - 1.times do - run_task - end + run_task assert contact.domains.last.force_delete_scheduled? end + def test_should_remove_old_validation_records + trumail_results = OpenStruct.new(success: false, + email: @contact.email, + domain: 'box.tests', + errors: { mx: 'target host(s) not found' }) + + Spy.on_instance_method(Actions::EmailCheck, :check_email).and_return(trumail_results) + Spy.on_instance_method(Actions::AAndAaaaEmailValidation, :call).and_return([true]) + + Actions::EmailCheck.new(email: @contact.email, + validation_eventable: @contact, + check_level: 'regex').call + + travel_to(Time.zone.now + ::ValidationEvent::VALIDATION_PERIOD + 1.minute) + assert_equal ValidationEvent.old_records.count, 1 + + run_task + + assert_predicate ValidationEvent.old_records.count, :zero? + end + def run_task perform_enqueued_jobs do Rake::Task['verify_email:check_all'].execute