2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2019-09-13 04:10:59 +00:00
|
|
|
require "inspec/input"
|
2019-02-07 16:50:54 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "type validation" do
|
2019-02-07 16:50:54 +00:00
|
|
|
let(:opts) { {} }
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:input) { Inspec::Input.new("test_input", opts) }
|
2019-02-07 16:50:54 +00:00
|
|
|
|
|
|
|
#==============================================================#
|
|
|
|
# Requiredness
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "enforce_required_validation" do
|
|
|
|
it "does not error if a value is set" do
|
|
|
|
input = Inspec::Input.new("test_input", value: "some_value", required: true)
|
|
|
|
input.value.must_equal "some_value"
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not error if a value is specified by value=" do
|
|
|
|
input = Inspec::Input.new("test_input", required: true)
|
|
|
|
input.value = "test_value"
|
|
|
|
input.value.must_equal "test_value"
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an error if no value is set" do
|
2019-02-07 16:50:54 +00:00
|
|
|
# Assigning the cli_command is needed because in check mode, we don't error
|
|
|
|
# on unset inputs. This is how you tell the input system we are not in
|
|
|
|
# check mode, apparently.
|
|
|
|
Inspec::BaseCLI.inspec_cli_command = :exec
|
2019-06-11 22:24:35 +00:00
|
|
|
input = Inspec::Input.new("test_input", required: true)
|
2019-02-07 16:50:54 +00:00
|
|
|
ex = assert_raises(Inspec::Input::RequiredError) { input.value }
|
2019-05-31 21:59:06 +00:00
|
|
|
ex.message.must_match(/Input 'test_input' is required and does not have a value./)
|
2019-02-07 16:50:54 +00:00
|
|
|
Inspec::BaseCLI.inspec_cli_command = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#==============================================================#
|
|
|
|
# Type Validation
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "enforce_type_validation" do
|
2019-02-07 16:50:54 +00:00
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
"string" => { good: "a_string", bad: 123.3, norm: "String" },
|
|
|
|
"numeric" => { good: 123, bad: "not a number", norm: "Numeric" },
|
|
|
|
"regex" => { good: /\d+.+/, bad: "/(.+/", norm: "Regexp" },
|
|
|
|
"array" => { good: [1, 2, 3], bad: { a: 1, b: 2, c: 3 }, norm: "Array" },
|
|
|
|
"hash" => { good: { a: 1, b: 2, c: 3 }, bad: "i am not a hash", norm: "Hash" },
|
|
|
|
"boolean" => { good: true, bad: "i am not a boolean", norm: "Boolean" },
|
2019-02-07 16:50:54 +00:00
|
|
|
}.each do |type, examples|
|
|
|
|
it "validates a #{type} in the constructor - (good)" do
|
|
|
|
opts = { type: type, value: examples[:good] }
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::Input.new("test_input", opts) # No exception
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "validates a #{type} in the constructor - (bad)" do
|
|
|
|
opts = { type: type, value: examples[:bad] }
|
2019-06-11 22:24:35 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { Inspec::Input.new("test_input", opts) }
|
|
|
|
ex.message.must_include "test_input"
|
2019-02-07 16:50:54 +00:00
|
|
|
ex.message.must_include "'#{examples[:bad]}'"
|
|
|
|
ex.message.must_include "does not validate to type '#{examples[:norm]}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "validates a #{type} in value= (good)" do
|
2019-06-11 22:24:35 +00:00
|
|
|
att = Inspec::Input.new("test_input", type: type)
|
2019-02-07 16:50:54 +00:00
|
|
|
att.value = examples[:good]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "validates a #{type} in the value= - (bad)" do
|
2019-06-11 22:24:35 +00:00
|
|
|
att = Inspec::Input.new("test_input", type: type)
|
2019-02-07 16:50:54 +00:00
|
|
|
ex = assert_raises(Inspec::Input::ValidationError) { att.value = examples[:bad] }
|
2019-06-11 22:24:35 +00:00
|
|
|
ex.message.must_include "test_input"
|
2019-02-07 16:50:54 +00:00
|
|
|
ex.message.must_include "'#{examples[:bad]}'"
|
|
|
|
ex.message.must_include "does not validate to type '#{examples[:norm]}'"
|
|
|
|
end
|
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
it "validates the Any type" do
|
|
|
|
Inspec::Input.new("test_input", type: "any", value: false) # No exception
|
|
|
|
Inspec::Input.new("test_input", type: "any", value: true) # No exception
|
|
|
|
Inspec::Input.new("test_input", type: "any", value: "bob") # No exception
|
|
|
|
Inspec::Input.new("test_input", type: "any", value: 1) # No exception
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#==============================================================#
|
|
|
|
# Type Option Validation and Normalization
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "validate type option" do
|
|
|
|
it "converts regex to Regexp" do
|
|
|
|
opts[:type] = "regex"
|
|
|
|
input.type.must_equal "Regexp"
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the same value if there is nothing to clean" do
|
|
|
|
opts[:type] = "String"
|
|
|
|
input.type.must_equal "String"
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an error if a invalid type is sent" do
|
|
|
|
opts[:type] = "dressing"
|
2019-02-07 18:05:46 +00:00
|
|
|
ex = assert_raises(Inspec::Input::TypeError) { input }
|
2019-05-31 21:59:06 +00:00
|
|
|
ex.message.must_match(/Type 'Dressing' is not a valid input type./)
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "valid_regexp? method" do
|
|
|
|
it "validates a string regex" do
|
|
|
|
input.send(:valid_regexp?, "/.*/").must_equal true
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "validates a slash regex" do
|
2019-02-07 16:50:54 +00:00
|
|
|
input.send(:valid_regexp?, /.*/).must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not validate a invalid regex" do
|
|
|
|
input.send(:valid_regexp?, "/.*(/").must_equal false
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "valid_numeric? method" do
|
|
|
|
it "validates a string number" do
|
|
|
|
input.send(:valid_numeric?, "123").must_equal true
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "validates a float number" do
|
2019-02-07 16:50:54 +00:00
|
|
|
input.send(:valid_numeric?, 44.55).must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "validats a wrong padded number" do
|
|
|
|
input.send(:valid_numeric?, "00080").must_equal true
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not vaildate a invalid number" do
|
|
|
|
input.send(:valid_numeric?, "55.55.55.5").must_equal false
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not validate a invalid string" do
|
|
|
|
input.send(:valid_numeric?, "one").must_equal false
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not validate a fraction" do
|
|
|
|
input.send(:valid_numeric?, "1/2").must_equal false
|
2019-02-07 16:50:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|