From 878f7340e5190865d0a8cb3a5eb4547208a5b22c Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Sat, 2 Jun 2018 15:13:25 +0200 Subject: [PATCH] WhoisSyncJob tests --- app/jobs/whois_sync_job.rb | 6 +++- test/jobs/whois_sync_job_test.rb | 61 ++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/app/jobs/whois_sync_job.rb b/app/jobs/whois_sync_job.rb index 45ecceb..b76637a 100644 --- a/app/jobs/whois_sync_job.rb +++ b/app/jobs/whois_sync_job.rb @@ -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 diff --git a/test/jobs/whois_sync_job_test.rb b/test/jobs/whois_sync_job_test.rb index 56c5ee4..ac44b7a 100644 --- a/test/jobs/whois_sync_job_test.rb +++ b/test/jobs/whois_sync_job_test.rb @@ -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