mirror of
https://github.com/internetee/registry.git
synced 2025-08-01 07:26:22 +02:00
changed count of contact validations
This commit is contained in:
parent
5474991bd1
commit
34c14e5bef
4 changed files with 75 additions and 15 deletions
|
@ -145,7 +145,7 @@ module Api
|
||||||
contact.transaction do
|
contact.transaction do
|
||||||
contact.save!
|
contact.save!
|
||||||
|
|
||||||
contact.validate_email_by_regex_and_mx
|
contact.validate_email_by_regex_and_mx(single_email: true)
|
||||||
contact.remove_force_delete_for_valid_contact
|
contact.remove_force_delete_for_valid_contact
|
||||||
|
|
||||||
action = current_registrant_user.actions.create!(contact: contact, operation: :update)
|
action = current_registrant_user.actions.create!(contact: contact, operation: :update)
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
module Actions
|
module Actions
|
||||||
class EmailCheck
|
class EmailCheck
|
||||||
attr_reader :email, :validation_eventable, :check_level
|
attr_reader :email, :validation_eventable, :check_level, :single_email
|
||||||
|
|
||||||
SAVE_RESULTS_BATCH_SIZE = 500
|
SAVE_RESULTS_BATCH_SIZE = 500
|
||||||
|
|
||||||
def initialize(email:, validation_eventable:, check_level: nil)
|
def initialize(email:, validation_eventable:, single_email: false, check_level: nil)
|
||||||
@email = email
|
@email = email
|
||||||
@validation_eventable = validation_eventable
|
@validation_eventable = validation_eventable
|
||||||
@check_level = check_level || :mx
|
@check_level = check_level || :mx
|
||||||
|
@single_email = single_email
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
result = check_email(email)
|
result = check_email(email)
|
||||||
save_result(result)
|
single_email ? save_single_result(validation_eventable: validation_eventable, result: result) : save_result(result)
|
||||||
handle_logging(result)
|
handle_logging(result)
|
||||||
result.success
|
result.success
|
||||||
end
|
end
|
||||||
|
@ -48,6 +49,15 @@ module Actions
|
||||||
contact.validation_events.destroy_all if success
|
contact.validation_events.destroy_all if success
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def save_single_result(validation_eventable:, result:)
|
||||||
|
handle_mx_validation(result) if !result.success && @check_level == 'mx'
|
||||||
|
result.configuration = nil
|
||||||
|
|
||||||
|
handle_saving_result(validation_eventable, result)
|
||||||
|
rescue ActiveRecord::RecordNotSaved
|
||||||
|
logger.info "Cannot save validation result for #{log_object_id}" and return true
|
||||||
|
end
|
||||||
|
|
||||||
def save_result(result)
|
def save_result(result)
|
||||||
contacts = Contact.where(email: email)
|
contacts = Contact.where(email: email)
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,12 @@ module EmailVerifable
|
||||||
scope :recently_not_validated, -> { where.not(id: ValidationEvent.validated_ids_by(name)) }
|
scope :recently_not_validated, -> { where.not(id: ValidationEvent.validated_ids_by(name)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_email_by_regex_and_mx
|
def validate_email_by_regex_and_mx(single_email: false)
|
||||||
# return if Rails.env.test?
|
verify_email(check_level: 'regex', single_email: single_email)
|
||||||
|
verify_email(check_level: 'mx', single_email: single_email)
|
||||||
verify_email(check_level: 'regex')
|
|
||||||
verify_email(check_level: 'mx')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_force_delete_for_valid_contact
|
def remove_force_delete_for_valid_contact
|
||||||
# return if Rails.env.test?
|
|
||||||
|
|
||||||
domains.each do |domain|
|
domains.each do |domain|
|
||||||
contact_emails_valid?(domain) ? domain.cancel_force_delete : nil
|
contact_emails_valid?(domain) ? domain.cancel_force_delete : nil
|
||||||
end
|
end
|
||||||
|
@ -68,14 +64,15 @@ module EmailVerifable
|
||||||
process_error(:billing_email) unless result
|
process_error(:billing_email) unless result
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_email(check_level: 'regex')
|
def verify_email(check_level: 'regex', single_email: false)
|
||||||
verify(email: email, check_level: check_level)
|
verify(email: email, check_level: check_level, single_email: single_email)
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify(email:, check_level: 'regex')
|
def verify(email:, check_level: 'regex', single_email: false)
|
||||||
action = Actions::EmailCheck.new(email: email,
|
action = Actions::EmailCheck.new(email: email,
|
||||||
validation_eventable: self,
|
validation_eventable: self,
|
||||||
check_level: check_level)
|
check_level: check_level,
|
||||||
|
single_email: single_email)
|
||||||
action.call
|
action.call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -375,6 +375,59 @@ class ContactTest < ActiveJob::TestCase
|
||||||
# assert_not @contact.domains.first.force_delete_scheduled?
|
# assert_not @contact.domains.first.force_delete_scheduled?
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
def test_should_be_validated_only_one_contact_with_same_email
|
||||||
|
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])
|
||||||
|
|
||||||
|
contact_1 = contacts(:john)
|
||||||
|
contact_2 = contacts(:william)
|
||||||
|
|
||||||
|
assert_equal contact_1.validation_events.count, 0
|
||||||
|
assert_equal contact_2.validation_events.count, 0
|
||||||
|
|
||||||
|
contact_1.email = 'test@example.com' && contact_1.save! && contact_1.reload
|
||||||
|
contact_2.email = 'test@example.com' && contact_2.save! && contact_2.reload
|
||||||
|
|
||||||
|
contact_1.validate_email_by_regex_and_mx(single_email: true)
|
||||||
|
|
||||||
|
contact_1.reload && contact_2.reload
|
||||||
|
|
||||||
|
assert_equal contact_1.validation_events.count, 1
|
||||||
|
assert_equal contact_2.validation_events.count, 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_should_be_validated_serial_contacts_with_same_email
|
||||||
|
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])
|
||||||
|
|
||||||
|
contact_1 = contacts(:john)
|
||||||
|
contact_2 = contacts(:william)
|
||||||
|
|
||||||
|
assert_equal contact_1.validation_events.count, 0
|
||||||
|
assert_equal contact_2.validation_events.count, 0
|
||||||
|
|
||||||
|
contact_1.update_attribute(:email, 'tester@example.com')
|
||||||
|
contact_2.update_attribute(:email, 'tester@example.com')
|
||||||
|
contact_1.reload && contact_2.reload
|
||||||
|
|
||||||
|
contact_1.validate_email_by_regex_and_mx(single_email: false)
|
||||||
|
|
||||||
|
contact_1.reload && contact_2.reload
|
||||||
|
|
||||||
|
assert_equal contact_1.validation_events.count, 1
|
||||||
|
assert_equal contact_2.validation_events.count, 1
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
|
def make_contact_free_of_domains_where_it_acts_as_a_registrant(contact)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue