21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-05-07 02:58:39 +02:00
chexpire/test/services/ssl_test.rb

92 lines
3 KiB
Ruby
Raw Normal View History

# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
# License: GNU AGPL-3+ (see full text in LICENSE file)
2018-07-02 17:21:08 +02:00
require "test_helper"
module SSL
class ServiceTest < ActiveSupport::TestCase
test "should run the command, return the result" do
result = OpenStruct.new(exit_status: 0)
mock_system_klass("check_http", standard_args, result) do |system_klass|
2019-03-03 22:47:05 +01:00
service = SSL::Service.new("example.org", system_klass: system_klass)
2018-07-02 17:21:08 +02:00
assert_equal result, service.run_command
end
end
test "should raise an exception if exit status > 0" do
result = OpenStruct.new(exit_status: 1)
mock_system_klass("check_http", standard_args, result) do |system_klass|
2019-03-03 22:47:05 +01:00
service = SSL::Service.new("example.org", system_klass: system_klass)
2018-07-02 17:21:08 +02:00
2019-03-03 22:47:05 +01:00
assert_raises SSL::CommandError do
2018-07-02 17:21:08 +02:00
service.run_command
end
end
end
test "should parse from a command result" do
result = OpenStruct.new(
exit_status: 0,
stdout: file_fixture("ssl/ssl0.domain.org.txt").read,
)
2019-03-03 22:47:05 +01:00
service = SSL::Service.new("ssl0.domain.org")
2019-03-03 11:05:05 +01:00
assert_kind_of SSL::Response, service.parse(result)
2018-07-02 17:21:08 +02:00
end
test "should uses the command line arguments of the configuration" do
result = OpenStruct.new(exit_status: 0)
config = OpenStruct.new(check_http_args: ["-f", "-I 127.0.0.1"])
2018-07-02 17:21:08 +02:00
expected_args = standard_args.concat ["-f", "-I 127.0.0.1"]
2018-07-02 17:21:08 +02:00
mock_system_klass("check_http", expected_args, result) do |system_klass|
2019-03-03 22:47:05 +01:00
service = SSL::Service.new("example.org", configuration: config, system_klass: system_klass)
2018-07-02 17:21:08 +02:00
assert_equal result, service.run_command
end
end
test "should raise an error when check_http_args is not an array" do
black_hole = Naught.build(&:black_hole)
config = OpenStruct.new(check_http_args: "-f")
2019-03-03 22:47:05 +01:00
assert_raises SSL::ConfigurationError do
service = SSL::Service.new("example.org", configuration: config, system_klass: black_hole)
service.run_command
end
end
2018-07-02 17:21:08 +02:00
test "should uses the program path from the configuration" do
result = OpenStruct.new(exit_status: 0)
config = OpenStruct.new(check_http_path: "/usr/local/custom/path")
mock_system_klass("/usr/local/custom/path", standard_args, result) do |sys|
2019-03-03 22:47:05 +01:00
service = SSL::Service.new("example.org", configuration: config, system_klass: sys)
2018-07-02 17:21:08 +02:00
assert_equal result, service.run_command
end
end
private
def standard_args
["-C 0", "--sni", "-H", "example.org"]
end
2018-07-02 17:21:08 +02:00
def mock_system_klass(program, command_args, result)
system_klass = Minitest::Mock.new
system_command = Minitest::Mock.new.expect(:execute, result)
system_klass.expect(:new, system_command) do |arg1, arg2, logger:|
arg1 == program &&
arg2 == command_args &&
logger.class == NullLogger
end
yield system_klass
system_klass.verify
system_command.verify
end
end
end