Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Hasjanov authored and Oleg Hasjanov committed Jan 11, 2024
1 parent ee8ab80 commit 4fbda4d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
11 changes: 9 additions & 2 deletions app/models/concerns/email_verifable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ module EmailVerifable

included do
scope :recently_not_validated, -> { where.not(id: ValidationEvent.validated_ids_by(name)) }
end

def validate_email_by_regex_and_mx
return if Rails.env.test?

after_save :verify_email, if: :email_changed?
verify_email(check_level: 'regex')
verify_email(check_level: 'mx')
end

def remove_force_delete
return if Rails.env.test?

domains.each do |domain|
contact_emails_valid?(domain) ? domain.cancel_force_delete : domain.schedule_force_delete
contact_emails_valid?(domain) ? domain.cancel_force_delete : nil
end
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Contact < ApplicationRecord
after_save :update_related_whois_records
before_validation :clear_address_modifications, if: -> { !self.class.address_processing? }

after_save :validate_email_by_regex_and_mx, if: :email_previously_changed?
after_save :remove_force_delete, if: :email_previously_changed?

self.ignored_columns = %w[legacy_id legacy_history_id]

ORG = 'org'.freeze
Expand Down
6 changes: 6 additions & 0 deletions test/models/domain/force_delete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class ForceDeleteTest < ActionMailer::TestCase
ActionMailer::Base.deliveries.clear
@old_validation_type = Truemail.configure.default_validation_type
ValidationEvent.destroy_all

Truemail.configure.whitelisted_domains = ['email.com', 'internet2.ee']
end

teardown do
Expand Down Expand Up @@ -403,6 +405,8 @@ def test_schedules_force_delete_invalid_contact
end

def test_add_invalid_email_to_domain_status_notes
Contact.skip_callback(:save, :after, :remove_force_delete)

domain = domains(:airport)
domain.update(valid_to: Time.zone.parse('2012-08-05'),
statuses: %w[serverForceDelete serverRenewProhibited serverTransferProhibited],
Expand All @@ -417,6 +421,8 @@ def test_add_invalid_email_to_domain_status_notes
Truemail.configure.default_validation_type = :regex

contact_first = domain.admin_contacts.first


contact_first.update_attribute(:email_history, '[email protected]')
contact_first.update_attribute(:email, email)

Expand Down
1 change: 1 addition & 0 deletions test/models/validation_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_fd_didnt_set_if_mx_interation_less_then_value
assert_not @domain.force_delete_scheduled?
travel_to Time.zone.parse('2010-07-05')

Contact.skip_callback(:save, :after, :validate_email_by_regex_and_mx)
email = '[email protected]'
contact = @domain.admin_contacts.first
contact.update_attribute(:email, email)
Expand Down
14 changes: 8 additions & 6 deletions test/tasks/emails/verify_email_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,22 @@ def test_should_not_affect_successfully_verified_emails
end

def test_should_verify_contact_email_which_was_not_verified

assert_equal ValidationEvent.count, 0

run_task

assert_equal ValidationEvent.count, Contact.count - 1
assert_equal Contact.count, 9

assert_difference 'Contact.count', 1 do
create_valid_contact
end

assert_difference 'ValidationEvent.where(success: true).count', 1 do
run_task
end
# Validation email of new contact will be skipped because it validated in during create
# assert_difference 'ValidationEvent.where(success: true).count', 1 do
# run_task
# end
end

def test_fd_should_not_be_removed_if_email_changed_to_another_invalid_one
Expand Down

0 comments on commit 4fbda4d

Please sign in to comment.