21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-28 06:50:49 +02:00
chexpire/test/jobs/ssl_sync_job_test.rb
2018-07-02 17:21:08 +02:00

69 lines
1.8 KiB
Ruby

require "test_helper"
class SSLSyncJobTest < ActiveJob::TestCase
test "calls whois database and update check with the response (domain.fr)" do
domain = "ssl0.domain.org"
check = create(:check, :nil_dates, domain: domain)
mock_system_command("check_http", expected_command_arg(domain), stdout: ssl_response(domain)) do
SSLSyncJob.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(2028, 6, 10, 9, 14, 18, 0), check.domain_expires_at
assert_nil check.domain_updated_at
assert_nil check.domain_created_at
assert check.active?
end
test "ignore invalid response" do
domain = "domain.fr"
check = create(:check, :nil_dates, domain: domain)
original_updated_at = check.updated_at
mock_system_command("check_http", expected_command_arg(domain), stdout: "not a response") do
SSLSyncJob.perform_now(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 "should ignore not found (removed) checks" do
assert_nothing_raised do
SSLSyncJob.perform_now("9999999")
end
end
test "should log and re-raise StandardError" do
check = create(:check)
assert_raise StandardError do
SSL.stub :ask, nil do
SSLSyncJob.perform_now(check.id)
end
end
assert_equal 1, check.logs.count
assert_match(/undefined method \W+valid\?/, check.logs.last.error)
assert check.logs.last.failed?
end
private
def ssl_response(domain)
file_fixture("ssl/#{domain}.txt").read
end
def expected_command_arg(domain)
["-H '#{domain}'"]
end
end