mirror of
https://github.com/internetee/registry.git
synced 2025-07-30 06:26:15 +02:00
Merge pull request #2369 from internetee/refactor-check-force-delete
Add validation event check force delete to a job
This commit is contained in:
commit
ac46637b33
6 changed files with 221 additions and 141 deletions
43
app/jobs/check_force_delete_job.rb
Normal file
43
app/jobs/check_force_delete_job.rb
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
class CheckForceDeleteJob < ApplicationJob
|
||||||
|
def perform(contact_ids)
|
||||||
|
contacts = Contact.find(contact_ids)
|
||||||
|
|
||||||
|
contacts.each do |contact|
|
||||||
|
email = contact.email
|
||||||
|
|
||||||
|
if contact.need_to_start_force_delete?
|
||||||
|
Domains::ForceDeleteEmail::Base.run(email: email)
|
||||||
|
elsif contact.need_to_lift_force_delete?
|
||||||
|
domain_list(email).each { |domain| refresh_status_notes(contact, domain) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def refresh_status_notes(contact, domain)
|
||||||
|
force_delete_emails = domain.status_notes[DomainStatus::FORCE_DELETE]
|
||||||
|
return unless force_delete_emails
|
||||||
|
|
||||||
|
force_delete_emails.slice!(contact.email_history)
|
||||||
|
force_delete_emails.lstrip!
|
||||||
|
domain.save(validate: false)
|
||||||
|
|
||||||
|
notify_registrar(domain) unless force_delete_emails.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def domain_list(email)
|
||||||
|
domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten
|
||||||
|
registrant_ids = Registrant.where(email: email).pluck(:id)
|
||||||
|
|
||||||
|
(domain_contacts.map(&:domain).flatten + Domain.where(registrant_id: registrant_ids)).uniq
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify_registrar(domain)
|
||||||
|
domain.registrar.notifications.create!(text: I18n.t('force_delete_auto_email',
|
||||||
|
domain_name: domain.name,
|
||||||
|
outzone_date: domain.outzone_date,
|
||||||
|
purge_date: domain.purge_date,
|
||||||
|
email: domain.status_notes[DomainStatus::FORCE_DELETE]))
|
||||||
|
end
|
||||||
|
end
|
|
@ -14,9 +14,7 @@ module EmailVerifable
|
||||||
def need_to_start_force_delete?
|
def need_to_start_force_delete?
|
||||||
flag = false
|
flag = false
|
||||||
ValidationEvent::INVALID_EVENTS_COUNT_BY_LEVEL.each do |level, count|
|
ValidationEvent::INVALID_EVENTS_COUNT_BY_LEVEL.each do |level, count|
|
||||||
if validation_events.count >= count && validate_email_data(level: level, count: count)
|
flag = true if validation_events.count >= count && validate_email_data(level: level, count: count)
|
||||||
flag = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
flag
|
flag
|
||||||
|
|
|
@ -35,8 +35,6 @@ class ValidationEvent < ApplicationRecord
|
||||||
scope :smtp, -> { where('event_data @> ?', { 'check_level': 'smtp' }.to_json) }
|
scope :smtp, -> { where('event_data @> ?', { 'check_level': 'smtp' }.to_json) }
|
||||||
scope :by_object, ->(object) { where(validation_eventable: object) }
|
scope :by_object, ->(object) { where(validation_eventable: object) }
|
||||||
|
|
||||||
# after_create :check_for_force_delete
|
|
||||||
|
|
||||||
def self.validated_ids_by(klass)
|
def self.validated_ids_by(klass)
|
||||||
recent.successful.where('validation_eventable_type = ?', klass)
|
recent.successful.where('validation_eventable_type = ?', klass)
|
||||||
.pluck(:validation_eventable_id)
|
.pluck(:validation_eventable_id)
|
||||||
|
@ -57,56 +55,4 @@ class ValidationEvent < ApplicationRecord
|
||||||
def object
|
def object
|
||||||
validation_eventable
|
validation_eventable
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_for_force_delete
|
|
||||||
if object.need_to_start_force_delete?
|
|
||||||
start_force_delete
|
|
||||||
elsif object.need_to_lift_force_delete?
|
|
||||||
refresh_status_notes
|
|
||||||
lift_force_delete
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def start_force_delete
|
|
||||||
Domains::ForceDeleteEmail::Base.run(email: email)
|
|
||||||
end
|
|
||||||
|
|
||||||
def refresh_status_notes
|
|
||||||
domain_list.each do |domain|
|
|
||||||
next unless domain.status_notes[DomainStatus::FORCE_DELETE]
|
|
||||||
|
|
||||||
domain.status_notes[DomainStatus::FORCE_DELETE].slice!(object.email_history)
|
|
||||||
domain.status_notes[DomainStatus::FORCE_DELETE].lstrip!
|
|
||||||
domain.save(validate: false)
|
|
||||||
|
|
||||||
notify_registrar(domain) unless domain.status_notes[DomainStatus::FORCE_DELETE].empty?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def domain_list
|
|
||||||
domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten
|
|
||||||
registrant_ids = Registrant.where(email: email).pluck(:id)
|
|
||||||
|
|
||||||
(domain_contacts.map(&:domain).flatten + Domain.where(registrant_id: registrant_ids)).uniq
|
|
||||||
end
|
|
||||||
|
|
||||||
def notify_registrar(domain)
|
|
||||||
domain.registrar.notifications.create!(text: I18n.t('force_delete_auto_email',
|
|
||||||
domain_name: domain.name,
|
|
||||||
outzone_date: domain.outzone_date,
|
|
||||||
purge_date: domain.purge_date,
|
|
||||||
email: domain.status_notes[DomainStatus::FORCE_DELETE]))
|
|
||||||
end
|
|
||||||
|
|
||||||
def lift_force_delete
|
|
||||||
# domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten
|
|
||||||
# registrant_ids = Registrant.where(email: email).pluck(:id)
|
|
||||||
#
|
|
||||||
# domains = domain_contacts.map(&:domain).flatten +
|
|
||||||
# Domain.where(registrant_id: registrant_ids)
|
|
||||||
#
|
|
||||||
# domains.each do |domain|
|
|
||||||
# Domains::ForceDeleteLift::Base.run(domain: domain)
|
|
||||||
# end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
12
lib/tasks/check_force_delete.rake
Normal file
12
lib/tasks/check_force_delete.rake
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
desc 'Check Force Delete'
|
||||||
|
task check_force_delete: :environment do
|
||||||
|
validations = ValidationEvent.select(:validation_eventable_id)
|
||||||
|
.failed.where(validation_eventable_type: 'Contact')
|
||||||
|
.group(:validation_eventable_id)
|
||||||
|
|
||||||
|
invalid_contact_ids = (
|
||||||
|
validations.mx.having("count(event_data ->> 'success') > 2") + validations.regex
|
||||||
|
).pluck(:validation_eventable_id)
|
||||||
|
|
||||||
|
CheckForceDeleteJob.perform_later(invalid_contact_ids)
|
||||||
|
end
|
|
@ -1,6 +1,8 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ForceDeleteTest < ActionMailer::TestCase
|
class ForceDeleteTest < ActionMailer::TestCase
|
||||||
|
include ActiveJob::TestHelper
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@domain = domains(:shop)
|
@domain = domains(:shop)
|
||||||
Setting.redemption_grace_period = 30
|
Setting.redemption_grace_period = 30
|
||||||
|
@ -380,115 +382,121 @@ class ForceDeleteTest < ActionMailer::TestCase
|
||||||
assert notification.text.include? asserted_text
|
assert notification.text.include? asserted_text
|
||||||
end
|
end
|
||||||
|
|
||||||
# def test_schedules_force_delete_invalid_contact
|
def test_schedules_force_delete_invalid_contact
|
||||||
# @domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||||
# assert_not @domain.force_delete_scheduled?
|
assert_not @domain.force_delete_scheduled?
|
||||||
# travel_to Time.zone.parse('2010-07-05')
|
travel_to Time.zone.parse('2010-07-05')
|
||||||
# email = '`@internet.ee'
|
email = '`@internet.ee'
|
||||||
# asserted_text = "Invalid email: #{email}"
|
asserted_text = "Invalid email: #{email}"
|
||||||
|
|
||||||
# Truemail.configure.default_validation_type = :regex
|
Truemail.configure.default_validation_type = :regex
|
||||||
|
|
||||||
# contact = @domain.admin_contacts.first
|
contact = @domain.admin_contacts.first
|
||||||
# contact.update_attribute(:email, email)
|
contact.update_attribute(:email, email)
|
||||||
|
|
||||||
# ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
||||||
# contact.verify_email
|
contact.verify_email
|
||||||
# end
|
end
|
||||||
|
|
||||||
# @domain.reload
|
perform_check_force_delete_job(contact.id)
|
||||||
|
@domain.reload
|
||||||
|
|
||||||
# assert @domain.force_delete_scheduled?
|
assert @domain.force_delete_scheduled?
|
||||||
# assert_equal Date.parse('2010-09-19'), @domain.force_delete_date.to_date
|
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 Date.parse('2010-08-05'), @domain.force_delete_start.to_date
|
||||||
# assert_equal @domain.status_notes[DomainStatus::FORCE_DELETE], email
|
assert_equal @domain.status_notes[DomainStatus::FORCE_DELETE], email
|
||||||
# notification = @domain.registrar.notifications.last
|
notification = @domain.registrar.notifications.last
|
||||||
# assert notification.text.include? asserted_text
|
assert notification.text.include? asserted_text
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_add_invalid_email_to_domain_status_notes
|
def test_add_invalid_email_to_domain_status_notes
|
||||||
# domain = domains(:airport)
|
domain = domains(:airport)
|
||||||
# domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
||||||
# statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
||||||
# force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
||||||
# status_notes: { "serverForceDelete": '`@internet2.ee' })
|
status_notes: { "serverForceDelete": '`@internet2.ee' })
|
||||||
|
|
||||||
# travel_to Time.zone.parse('2010-07-05')
|
travel_to Time.zone.parse('2010-07-05')
|
||||||
# email = '`@internet.ee'
|
email = '`@internet.ee'
|
||||||
# invalid_emails = '`@internet2.ee `@internet.ee'
|
invalid_emails = '`@internet2.ee `@internet.ee'
|
||||||
# asserted_text = "Invalid email: #{invalid_emails}"
|
asserted_text = "Invalid email: #{invalid_emails}"
|
||||||
|
|
||||||
# Truemail.configure.default_validation_type = :regex
|
Truemail.configure.default_validation_type = :regex
|
||||||
|
|
||||||
# contact_first = domain.admin_contacts.first
|
contact_first = domain.admin_contacts.first
|
||||||
# contact_first.update_attribute(:email_history, 'john@inbox.test')
|
contact_first.update_attribute(:email_history, 'john@inbox.test')
|
||||||
# contact_first.update_attribute(:email, email)
|
contact_first.update_attribute(:email, email)
|
||||||
|
|
||||||
# ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do
|
||||||
# contact_first.verify_email
|
contact_first.verify_email
|
||||||
# end
|
end
|
||||||
|
|
||||||
# domain.reload
|
perform_check_force_delete_job(contact_first.id)
|
||||||
|
domain.reload
|
||||||
|
|
||||||
# assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_emails
|
assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_emails
|
||||||
# notification = domain.registrar.notifications.last
|
notification = domain.registrar.notifications.last
|
||||||
# assert_not notification.text.include? asserted_text
|
assert_not notification.text.include? asserted_text
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_remove_invalid_email_from_domain_status_notes
|
def test_remove_invalid_email_from_domain_status_notes
|
||||||
# domain = domains(:airport)
|
domain = domains(:airport)
|
||||||
# domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
domain.update(valid_to: Time.zone.parse('2012-08-05'),
|
||||||
# statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
|
||||||
# force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
force_delete_data: { 'template_name': 'invalid_email', 'force_delete_type': 'soft' },
|
||||||
# status_notes: { "serverForceDelete": '`@internet2.ee `@internet.ee' })
|
status_notes: { "serverForceDelete": '`@internet2.ee `@internet.ee' })
|
||||||
|
|
||||||
# travel_to Time.zone.parse('2010-07-05')
|
travel_to Time.zone.parse('2010-07-05')
|
||||||
# email = '`@internet2.ee'
|
email = '`@internet2.ee'
|
||||||
# invalid_email = '`@internet.ee'
|
invalid_email = '`@internet.ee'
|
||||||
# asserted_text = "Invalid email: #{invalid_email}"
|
asserted_text = "Invalid email: #{invalid_email}"
|
||||||
|
|
||||||
# Truemail.configure.default_validation_type = :regex
|
Truemail.configure.default_validation_type = :regex
|
||||||
|
|
||||||
# contact_first = domain.admin_contacts.first
|
contact_first = domain.admin_contacts.first
|
||||||
# contact_first.update_attribute(:email_history, email)
|
contact_first.update_attribute(:email_history, email)
|
||||||
# contact_first.update_attribute(:email, 'john@inbox.test')
|
contact_first.update_attribute(:email, 'john@inbox.test')
|
||||||
|
|
||||||
# travel_to Time.zone.parse('2010-07-05 0:00:03')
|
travel_to Time.zone.parse('2010-07-05 0:00:03')
|
||||||
# contact_first.verify_email
|
contact_first.verify_email
|
||||||
# domain.reload
|
|
||||||
|
|
||||||
# assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_email
|
perform_check_force_delete_job(contact_first.id)
|
||||||
# notification = domain.registrar.notifications.last
|
domain.reload
|
||||||
# assert notification.text.include? asserted_text
|
|
||||||
# end
|
|
||||||
|
|
||||||
# def test_domain_should_have_several_bounced_emails
|
assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], invalid_email
|
||||||
# @domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
notification = domain.registrar.notifications.last
|
||||||
# assert_not @domain.force_delete_scheduled?
|
assert notification.text.include? asserted_text
|
||||||
# travel_to Time.zone.parse('2010-07-05')
|
end
|
||||||
# email_one = '`@internet.ee'
|
|
||||||
# email_two = '@@internet.ee'
|
|
||||||
|
|
||||||
# contact_one = @domain.admin_contacts.first
|
def test_domain_should_have_several_bounced_emails
|
||||||
# contact_one.update_attribute(:email, email_one)
|
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||||
# contact_one.verify_email
|
assert_not @domain.force_delete_scheduled?
|
||||||
|
travel_to Time.zone.parse('2010-07-05')
|
||||||
|
email_one = '`@internet.ee'
|
||||||
|
email_two = '@@internet.ee'
|
||||||
|
|
||||||
# assert contact_one.need_to_start_force_delete?
|
contact_one = @domain.admin_contacts.first
|
||||||
|
contact_one.update_attribute(:email, email_one)
|
||||||
|
contact_one.verify_email
|
||||||
|
perform_check_force_delete_job(contact_one.id)
|
||||||
|
|
||||||
# contact_two = @domain.admin_contacts.first
|
assert contact_one.need_to_start_force_delete?
|
||||||
# contact_two.update_attribute(:email, email_two)
|
|
||||||
# contact_two.verify_email
|
|
||||||
|
|
||||||
# assert contact_two.need_to_start_force_delete?
|
contact_two = @domain.admin_contacts.first
|
||||||
|
contact_two.update_attribute(:email, email_two)
|
||||||
|
contact_two.verify_email
|
||||||
|
perform_check_force_delete_job(contact_two.id)
|
||||||
|
|
||||||
# @domain.reload
|
assert contact_two.need_to_start_force_delete?
|
||||||
|
|
||||||
# assert @domain.force_delete_scheduled?
|
@domain.reload
|
||||||
# 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.force_delete_scheduled?
|
||||||
# assert @domain.status_notes[DomainStatus::FORCE_DELETE].include? email_one
|
assert_equal Date.parse('2010-09-19'), @domain.force_delete_date.to_date
|
||||||
# assert @domain.status_notes[DomainStatus::FORCE_DELETE].include? email_two
|
assert_equal Date.parse('2010-08-05'), @domain.force_delete_start.to_date
|
||||||
# end
|
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
|
def test_lifts_force_delete_after_bounce_changes
|
||||||
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
|
||||||
|
@ -527,4 +535,12 @@ class ForceDeleteTest < ActionMailer::TestCase
|
||||||
@bounced_mail.diagnostic = 'smtp; 550 5.1.1 user unknown'
|
@bounced_mail.diagnostic = 'smtp; 550 5.1.1 user unknown'
|
||||||
@bounced_mail.save!
|
@bounced_mail.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def perform_check_force_delete_job(contact_id)
|
||||||
|
perform_enqueued_jobs do
|
||||||
|
CheckForceDeleteJob.perform_now([contact_id])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
65
test/tasks/check_force_delete_test.rb
Normal file
65
test/tasks/check_force_delete_test.rb
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class CheckForceDeleteTaskTest < ActiveSupport::TestCase
|
||||||
|
include ActiveJob::TestHelper
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@contact = contacts(:john)
|
||||||
|
@invalid_contact = contacts(:invalid_email)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_enque_force_delete_when_three_invalid_records_by_mx
|
||||||
|
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([])
|
||||||
|
|
||||||
|
action = Actions::EmailCheck.new(email: @contact.email,
|
||||||
|
validation_eventable: @contact,
|
||||||
|
check_level: 'mx')
|
||||||
|
3.times do
|
||||||
|
action.call
|
||||||
|
end
|
||||||
|
|
||||||
|
run_task
|
||||||
|
|
||||||
|
assert_enqueued_jobs 1
|
||||||
|
assert_enqueued_with(job: CheckForceDeleteJob, args: [[@contact.id]])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_enque_force_delete_when_invalid_record_by_regex
|
||||||
|
@invalid_contact.verify_email
|
||||||
|
run_task
|
||||||
|
|
||||||
|
assert_enqueued_jobs 1
|
||||||
|
assert_enqueued_with(job: CheckForceDeleteJob, args: [[@invalid_contact.id]])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_not_enque_force_delete
|
||||||
|
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([])
|
||||||
|
|
||||||
|
action = Actions::EmailCheck.new(email: @contact.email,
|
||||||
|
validation_eventable: @contact,
|
||||||
|
check_level: 'mx')
|
||||||
|
2.times do
|
||||||
|
action.call
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_enqueued_jobs 0
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def run_task
|
||||||
|
Rake::Task['check_force_delete'].execute
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue