21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-27 06:20:50 +02:00
chexpire/test/jobs/whois_sync_job_test.rb
Colin Darie 5d1fa13fba
Do not automatically mark as inactive checks for domain not found
This renders explicit the failures after X fails, and notify the user.
2018-08-29 18:27:31 +02:00

107 lines
2.9 KiB
Ruby

# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
# License: GNU AGPL-3+ (see full text in LICENSE file)
require "test_helper"
class WhoisSyncJobTest < ActiveJob::TestCase
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.perform_now(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_expires_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")
mock_system_command("whois", "domain.fr", stdout: "not a response") do
WhoisSyncJob.perform_now(check.id)
end
check.reload
assert_just_now check.last_run_at
assert_nil check.last_success_at
assert check.active?
assert_equal 1, check.consecutive_failures
end
test "should ignore not found (removed) checks" do
assert_nothing_raised do
WhoisSyncJob.perform_now("9999999")
end
end
test "should log and re-raise StandardError" do
check = create(:check)
assert_raise StandardError do
Whois.stub :ask, nil do
WhoisSyncJob.perform_now(check.id)
end
end
check.reload
assert_equal 1, check.logs.count
assert_match(/undefined method \W+valid\?/, check.logs.last.error)
assert check.logs.last.failed?
assert_equal 1, check.consecutive_failures
end
test "increment consecutive failures 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.perform_now(check.id)
end
check.reload
assert_just_now check.last_run_at
assert_nil check.last_success_at
assert_equal 1, check.consecutive_failures
end
test "default logger is CheckLogger" do
check = create(:check)
mock_system_command("whois", check.domain) do
WhoisSyncJob.perform_now(check.id)
end
assert_equal 1, check.logs.count
end
test "should reset consecutive failures with a valid response" do
domain = "domain.fr"
check = create(:check, :nil_dates, domain: domain, consecutive_failures: 1)
mock_system_command("whois", domain, stdout: whois_response(domain)) do
WhoisSyncJob.perform_now(check.id)
end
check.reload
assert_equal 0, check.consecutive_failures
end
private
def whois_response(domain)
file_fixture("whois/#{domain}.txt").read
end
end