21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-05-04 17:55:11 +02:00

WhoisSyncJob tests

This commit is contained in:
Colin Darie 2018-06-02 15:13:25 +02:00
parent 66de2e146a
commit 878f7340e5
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 63 additions and 4 deletions

View file

@ -7,8 +7,9 @@ class WhoisSyncJob < ApplicationJob
def perform(check_id)
@check = Check.find(check_id)
response = Whois.ask(check.domain)
check.update_attribute(:last_run_at, Time.now)
response = Whois.ask(check.domain)
return unless response.valid?
update_from_response(response)
@ -17,11 +18,14 @@ class WhoisSyncJob < ApplicationJob
rescue Whois::DomainNotFoundError
check.active = false
check.save!
rescue Whois::ParserError # rubocop:disable Lint/HandleExceptions
# already logged
end
def update_from_response(response)
check.domain_created_at = response.created_at
check.domain_updated_at = response.updated_at
check.domain_expire_at = response.expire_at
check.last_success_at = Time.now
end
end

View file

@ -1,7 +1,62 @@
require "test_helper"
class WhoisSyncJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
test "calls whois database and update check with the response (domain.fr)" do
domain = "domain.fr"
check = create(:check, :nil_dates, domain: domain)
mock_system_command("whois", domain, stdout: whois_response(domain)) do
WhoisSyncJob.new.perform(check.id)
end
check.reload
assert_just_now check.last_run_at
assert_just_now check.last_success_at
assert_equal Time.new(2019, 2, 17, 0, 0, 0, 0), check.domain_expire_at
assert_equal Time.new(2017, 1, 28, 0, 0, 0, 0), check.domain_updated_at
assert_equal Time.new(2004, 2, 18, 0, 0, 0, 0), check.domain_created_at
assert check.active?
end
test "ignore invalid response (domain.fr)" do
check = create(:check, :nil_dates, domain: "domain.fr")
original_updated_at = check.updated_at
mock_system_command("whois", "domain.fr", stdout: "not a response") do
WhoisSyncJob.new.perform(check.id)
end
check.reload
assert_just_now check.last_run_at
assert_nil check.last_success_at
assert_equal original_updated_at, check.updated_at
assert check.active?
end
test "Disable check when whois responds domain not found" do
domain = "willneverexist.fr"
check = create(:check, :nil_dates, domain: domain)
mock_system_command("whois", domain, stdout: whois_response(domain)) do
WhoisSyncJob.new.perform(check.id)
end
check.reload
refute check.active?
assert_just_now check.last_run_at
assert_nil check.last_success_at
end
private
def whois_response(domain)
file_fixture("whois/#{domain}.txt").read
end
def assert_just_now(expected)
assert_in_delta expected.to_i, Time.now.to_i, 1.0
end
end