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