From 2803c733f1b0abfa87306b9cf1d23a2ad70616a1 Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Wed, 11 May 2022 14:44:59 +0300 Subject: [PATCH 1/5] Contact destroy dependent validation event --- app/models/contact.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index 62dac37cf1..92535bebd0 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -15,7 +15,7 @@ class Contact < ApplicationRecord has_many :domain_contacts has_many :domains, through: :domain_contacts has_many :legal_documents, as: :documentable - has_many :validation_events, as: :validation_eventable + has_many :validation_events, as: :validation_eventable, dependent: :destroy has_many :registrant_domains, class_name: 'Domain', foreign_key: 'registrant_id' has_many :actions, dependent: :destroy From 7a2256ae931a636b80002ba4298050e6aa1d7c43 Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Wed, 11 May 2022 15:22:26 +0300 Subject: [PATCH 2/5] DRY code --- app/interactions/actions/contact_update.rb | 11 ++++--- app/interactions/actions/email_check.rb | 35 +++++++++------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/app/interactions/actions/contact_update.rb b/app/interactions/actions/contact_update.rb index 3442a56437..81b7e74090 100644 --- a/app/interactions/actions/contact_update.rb +++ b/app/interactions/actions/contact_update.rb @@ -37,12 +37,11 @@ def maybe_change_email end def maybe_filtering_old_failed_records - if contact.validation_events.count > 1 - contact.validation_events.order!(created_at: :asc) - while contact.validation_events.count >= 1 - contact.validation_events.first.destroy - end - end + validation_events = contact.validation_events + return unless validation_events.count > 1 + + validation_events.order!(created_at: :asc) + validation_events.first.destroy while validation_events.count >= 1 end def maybe_remove_address diff --git a/app/interactions/actions/email_check.rb b/app/interactions/actions/email_check.rb index 9c6dbfe880..0f3ca52a1f 100644 --- a/app/interactions/actions/email_check.rb +++ b/app/interactions/actions/email_check.rb @@ -26,33 +26,27 @@ def calculate_check_level Rails.env.test? && check_level == 'smtp' ? :mx : check_level.to_sym end + def destroy_old_validations(validation_events, minimum_size) + return unless validation_events.count > minimum_size + + validation_events.order!(created_at: :asc) + validation_events.first.destroy while validation_events.count > minimum_size + end + def filtering_old_failed_records(result) - if @check_level == "mx" && !result.success && validation_eventable.validation_events.count > 3 - validation_eventable.validation_events.order!(created_at: :asc) - while validation_eventable.validation_events.count > 3 - validation_eventable.validation_events.first.destroy - end - end + events = validation_eventable.validation_events - if @check_level == "mx" && result.success && validation_eventable.validation_events.count > 1 - validation_eventable.validation_events.order!(created_at: :asc) - while validation_eventable.validation_events.count > 1 - validation_eventable.validation_events.first.destroy - end - end + destroy_old_validations(events, 3) if @check_level == 'mx' && !result.success - if @check_level == "smtp" && validation_eventable.validation_events.count > 1 - validation_eventable.validation_events.order!(created_at: :asc) - while validation_eventable.validation_events.count > 1 - validation_eventable.validation_events.first.destroy - end - end + destroy_old_validations(events, 1) if @check_level == 'mx' && result.success + + destroy_old_validations(events, 1) if @check_level == 'smtp' end def save_result(result) contacts = Contact.where(email: email) - if !result.success && @check_level == "mx" + 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, @@ -96,8 +90,7 @@ def check_for_records_value(domain:, value:) when 'AAAA' ress = dns.getresources domain, Resolv::DNS::Resource::IN::AAAA end - - result = ress.map { |r| r.address } + result = ress.map(&:address) end result From ec71ae1137cc08e21b780580baec85a9820cf62b Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Thu, 12 May 2022 13:50:58 +0300 Subject: [PATCH 3/5] Prevent deprecated validations when email is changed --- app/interactions/actions/contact_update.rb | 5 ++-- .../epp/contact/update/base_test.rb | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/interactions/actions/contact_update.rb b/app/interactions/actions/contact_update.rb index 81b7e74090..bfa3f3b7c2 100644 --- a/app/interactions/actions/contact_update.rb +++ b/app/interactions/actions/contact_update.rb @@ -115,8 +115,9 @@ def commit contact.email_history = old_email updated = contact.save - if updated && email_changed && contact.registrant? - ContactMailer.email_changed(contact: contact, old_email: old_email).deliver_now + if updated && email_changed + contact.validation_events.where('event_data @> ?', { 'email': old_email }.to_json).destroy_all + ContactMailer.email_changed(contact: contact, old_email: old_email).deliver_now if contact.registrant? end updated diff --git a/test/integration/epp/contact/update/base_test.rb b/test/integration/epp/contact/update/base_test.rb index 364741f0c1..0c55c52237 100644 --- a/test/integration/epp/contact/update/base_test.rb +++ b/test/integration/epp/contact/update/base_test.rb @@ -80,6 +80,35 @@ def test_notifies_contact_by_email_when_email_is_changed assert_emails 1 end + def test_destroy_old_validation_when_email_is_changed + @contact.verify_email + old_validation_event = @contact.validation_events.first + @contact.update_columns(code: @contact.code.upcase) + + request_xml = <<-XML + + + + + + john-001 + + john-new@inbox.test + + + + + + XML + + post epp_update_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + + assert_raises(ActiveRecord::RecordNotFound) do + ValidationEvent.find(old_validation_event.id) + end + end + def test_skips_notifying_contact_when_email_is_not_changed assert_equal 'john-001', @contact.code assert_equal 'john@inbox.test', @contact.email From 060416d20c9f1442f4ab3c1ca7ae0e490686c951 Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Mon, 16 May 2022 13:55:33 +0300 Subject: [PATCH 4/5] Use validation event constants --- app/interactions/actions/email_check.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/interactions/actions/email_check.rb b/app/interactions/actions/email_check.rb index 0f3ca52a1f..f83f2dd2c5 100644 --- a/app/interactions/actions/email_check.rb +++ b/app/interactions/actions/email_check.rb @@ -36,11 +36,11 @@ def destroy_old_validations(validation_events, minimum_size) def filtering_old_failed_records(result) events = validation_eventable.validation_events - destroy_old_validations(events, 3) if @check_level == 'mx' && !result.success + destroy_old_validations(events, ValidationEvent::MX_CHECK) if @check_level == 'mx' && !result.success - destroy_old_validations(events, 1) if @check_level == 'mx' && result.success + destroy_old_validations(events, ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL[:mx]) if @check_level == 'mx' && result.success - destroy_old_validations(events, 1) if @check_level == 'smtp' + destroy_old_validations(events, ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL[:smtp]) if @check_level == 'smtp' end def save_result(result) From c02101aa66636500b317f8ae91248e6742b9b25a Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Mon, 16 May 2022 14:04:44 +0300 Subject: [PATCH 5/5] Fix code climate --- app/interactions/actions/email_check.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/interactions/actions/email_check.rb b/app/interactions/actions/email_check.rb index f83f2dd2c5..a927a32741 100644 --- a/app/interactions/actions/email_check.rb +++ b/app/interactions/actions/email_check.rb @@ -26,8 +26,8 @@ def calculate_check_level Rails.env.test? && check_level == 'smtp' ? :mx : check_level.to_sym end - def destroy_old_validations(validation_events, minimum_size) - return unless validation_events.count > minimum_size + def destroy_old_validations(validation_events, minimum_size, check_level) + return unless validation_events.count > minimum_size && @check_level == check_level validation_events.order!(created_at: :asc) validation_events.first.destroy while validation_events.count > minimum_size @@ -36,11 +36,11 @@ def destroy_old_validations(validation_events, minimum_size) def filtering_old_failed_records(result) events = validation_eventable.validation_events - destroy_old_validations(events, ValidationEvent::MX_CHECK) if @check_level == 'mx' && !result.success + destroy_old_validations(events, ValidationEvent::MX_CHECK, 'mx') unless result.success - destroy_old_validations(events, ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL[:mx]) if @check_level == 'mx' && result.success + destroy_old_validations(events, ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL[:mx], 'mx') if result.success - destroy_old_validations(events, ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL[:smtp]) if @check_level == 'smtp' + destroy_old_validations(events, ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL[:smtp], 'smtp') end def save_result(result)