foreman/test/models/lookup_value_test.rb @ 630061d2
6c492cce | Joseph Mitchell Magen | require 'test_helper'
|
|
832c0925 | Joseph Magen | class LookupValueTest < ActiveSupport::TestCase
|
|
e14b5758 | Greg Sutcliffe | def setup
|
|
8c6bc83e | Marek Hulan | @host1 = FactoryBot.create(:host)
|
|
@host2 = FactoryBot.create(:host)
|
|||
6c492cce | Joseph Mitchell Magen | end
|
|
e14b5758 | Greg Sutcliffe | def valid_attrs1
|
|
{ :match => "fqdn=#{@host2.name}",
|
|||
6c492cce | Joseph Mitchell Magen | :value => "3001",
|
|
:lookup_key_id => lookup_keys(:one).id
|
|||
}
|
|||
end
|
|||
e14b5758 | Greg Sutcliffe | def valid_attrs2
|
|
6c492cce | Joseph Mitchell Magen | { :match => "hostgroup=Common",
|
|
:value => "3001",
|
|||
:lookup_key_id => lookup_keys(:one).id
|
|||
}
|
|||
end
|
|||
test "create lookup value by admin" do
|
|||
as_admin do
|
|||
assert_difference('LookupValue.count') do
|
|||
e14b5758 | Greg Sutcliffe | LookupValue.create!(valid_attrs1)
|
|
6c492cce | Joseph Mitchell Magen | end
|
|
end
|
|||
end
|
|||
test "update lookup value by admin" do
|
|||
e14b5758 | Greg Sutcliffe | lookup_value = lookup_values(:hostgroupcommon)
|
|
6c492cce | Joseph Mitchell Magen | as_admin do
|
|
9ae8fa19 | Michael Moll | assert lookup_value.update!(:value => "9000")
|
|
6c492cce | Joseph Mitchell Magen | end
|
|
end
|
|||
fbbda128 | Marek Hulan | test "non-admin user can view only his hosts allowed by filters" do
|
|
acfbc458 | Marek Hulan | # Host.authorized(:view_hosts, Host) returns only hosts(:one)
|
|
6c492cce | Joseph Mitchell Magen | user = users(:one)
|
|
8c6bc83e | Marek Hulan | role = FactoryBot.create(:role, :name => 'user_view_host_by_ip')
|
|
FactoryBot.create(:filter, :role => role, :permissions => [Permission.find_by_name(:view_hosts)], :search => "name = #{@host1.name}")
|
|||
43c4bd72 | Marek Hulan | # Todo, restore the ip test variant once our scoped-search works with host.ip again
|
|
8c6bc83e | Marek Hulan | # FactoryBot.create(:filter, :role => role, :permissions => [Permission.find_by_name(:view_hosts)], :search => "ip = #{@host1.ip}")
|
|
acfbc458 | Marek Hulan | user.roles<< [ role ]
|
|
6c492cce | Joseph Mitchell Magen | as_user :one do
|
|
e14b5758 | Greg Sutcliffe | assert Host.authorized(:view_hosts, Host).where(:name => @host1.name).exists?
|
|
refute Host.authorized(:view_hosts, Host).where(:name => @host2.name).exists?
|
|||
6c492cce | Joseph Mitchell Magen | end
|
|
end
|
|||
bd48d5c9 | Joseph Magen | test "any user including admin cannot create lookup value if match fqdn= does not match existing host" do
|
|
as_admin do
|
|||
attrs = { :match => "fqdn=non.existing.com", :value => "123", :lookup_key_id => lookup_keys(:one).id }
|
|||
lookup_value = LookupValue.new(attrs)
|
|||
refute lookup_value.save
|
|||
assert_match /Match fqdn=non.existing.com does not match an existing host/, lookup_value.errors.full_messages.join("\n")
|
|||
end
|
|||
end
|
|||
test "any user including admin cannot create lookup value if match hostgroup= does not match existing hostgroup" do
|
|||
as_admin do
|
|||
attrs = { :match => "hostgroup=non_existing_group", :value => "123", :lookup_key_id => lookup_keys(:one).id }
|
|||
lookup_value = LookupValue.new(attrs)
|
|||
refute lookup_value.save
|
|||
assert_match /Match hostgroup=non_existing_group does not match an existing host group/, lookup_value.errors.full_messages.join("\n")
|
|||
end
|
|||
end
|
|||
6c492cce | Joseph Mitchell Magen | test "can create lookup value if user has matching hostgroup " do
|
|
as_user :one do
|
|||
e14b5758 | Greg Sutcliffe | lookup_value = LookupValue.new(valid_attrs2)
|
|
6c492cce | Joseph Mitchell Magen | assert_difference('LookupValue.count') do
|
|
assert lookup_value.save
|
|||
end
|
|||
end
|
|||
end
|
|||
cfc2bb66 | Stephen Benjamin | test "smart class parameter accepts valid data" do
|
|
as_admin do
|
|||
lk = LookupValue.new(:value => "---\nfoo:\n bar: baz", :match => "hostgroup=Common", :lookup_key => lookup_keys(:six))
|
|||
assert lk.valid?
|
|||
assert lk.save!
|
|||
end
|
|||
end
|
|||
test "smart class parameter validation detects invalid data" do
|
|||
as_admin do
|
|||
lk = LookupValue.new(:value => "---\n[[\n;", :match => "hostgroup=Common", :lookup_key => lookup_keys(:six))
|
|||
refute lk.valid?
|
|||
assert lk.errors.messages[:value].include? "is invalid yaml"
|
|||
end
|
|||
end
|
|||
801a90d1 | Stephen Benjamin | test "should cast and uncast string containing a Hash" do
|
|
lk1 = LookupValue.new(:value => "---\n foo: bar", :match => "hostgroup=Common", :lookup_key => lookup_keys(:six))
|
|||
assert lk1.save!
|
|||
assert lk1.value.is_a? Hash
|
|||
445dd7ab | Daniel Lobato | assert_includes lk1.value_before_type_cast, 'foo: bar'
|
|
801a90d1 | Stephen Benjamin | ||
lk2 = LookupValue.new(:value => "{'foo': 'bar'}", :match => "environment=Production", :lookup_key => lookup_keys(:six))
|
|||
assert lk2.save!
|
|||
assert lk2.value.is_a? Hash
|
|||
445dd7ab | Daniel Lobato | assert_includes lk2.value_before_type_cast, 'foo: bar'
|
|
801a90d1 | Stephen Benjamin | end
|
|
test "should cast and uncast string containing an Array" do
|
|||
lk = LookupValue.new(:value => "[{\"foo\":\"bar\"},{\"baz\":\"qux\"},\"baz\"]", :match => "hostgroup=Common", :lookup_key => lookup_keys(:seven))
|
|||
assert lk.save!
|
|||
assert lk.value.is_a? Array
|
|||
assert_equal lk.value_before_type_cast, "[{\"foo\":\"bar\"},{\"baz\":\"qux\"},\"baz\"]"
|
|||
end
|
|||
971fee79 | Dominic Cleal | ||
706de3cf | Tom Caspy | test "should not cast string if object invalid" do
|
|
lk = LookupValue.new(:value => '{"foo" => "bar"}', :match => "hostgroup=Common", :lookup_key => lookup_keys(:seven))
|
|||
refute lk.valid?
|
|||
assert_equal lk.value_before_type_cast, '{"foo" => "bar"}'
|
|||
end
|
|||
971fee79 | Dominic Cleal | test "when created, an audit entry should be added" do
|
|
8c6bc83e | Marek Hulan | env = FactoryBot.create(:environment)
|
|
pc = FactoryBot.create(:puppetclass, :with_parameters, :environments => [env])
|
|||
971fee79 | Dominic Cleal | key = pc.class_params.first
|
|
lvalue = nil
|
|||
assert_difference('Audit.count') do
|
|||
8c6bc83e | Marek Hulan | lvalue = FactoryBot.create :lookup_value, :with_auditing, :lookup_key_id => key.id, :value => 'test', :match => 'os=bar'
|
|
971fee79 | Dominic Cleal | end
|
|
assert_equal "#{pc.name}::#{key.key}", lvalue.audits.last.associated_name
|
|||
end
|
|||
test "when changed, an audit entry should be added" do
|
|||
8c6bc83e | Marek Hulan | env = FactoryBot.create(:environment)
|
|
pc = FactoryBot.create(:puppetclass, :environments => [env])
|
|||
key = FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param, :with_override, :puppetclass => pc)
|
|||
971fee79 | Dominic Cleal | lvalue = key.lookup_values.first
|
|
assert_difference('Audit.count') do
|
|||
lvalue.value = 'new overridden value'
|
|||
lvalue.save!
|
|||
end
|
|||
assert_equal "#{pc.name}::#{key.key}", lvalue.audits.last.associated_name
|
|||
end
|
|||
f8a56f5b | Marek Hulan | ||
test "shuld not cast string with erb" do
|
|||
8c6bc83e | Marek Hulan | key = FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param,
|
|
f8a56f5b | Marek Hulan | :override => true, :key_type => 'array', :merge_overrides => true, :avoid_duplicates => true,
|
|
3cd8c84b | Michael Moll | :default_value => [1, 2, 3], :puppetclass => puppetclasses(:one))
|
|
f8a56f5b | Marek Hulan | ||
lv = LookupValue.new(:value => "<%= [4,5,6] %>", :match => "hostgroup=Common", :lookup_key => key)
|
|||
# does not cast on save (validate_and_cast_value)
|
|||
assert lv.save!
|
|||
# does not cast on load (value_before_type_cast)
|
|||
assert_equal lv.value_before_type_cast, "<%= [4,5,6] %>"
|
|||
assert_equal lv.value, "<%= [4,5,6] %>"
|
|||
end
|
|||
ee6fc204 | Tom Caspy | ||
test "boolean lookup value should allow for false value" do
|
|||
68388bc2 | Michael Moll | # boolean key
|
|
ee6fc204 | Tom Caspy | key = lookup_keys(:three)
|
|
value = LookupValue.new(:value => false, :match => "hostgroup=Common", :lookup_key_id => key.id)
|
|||
assert value.valid?
|
|||
end
|
|||
09ce8a63 | Ori Rabin | test "boolean lookup value should not allow for nil value" do
|
|
68388bc2 | Michael Moll | # boolean key
|
|
ee6fc204 | Tom Caspy | key = lookup_keys(:three)
|
|
value = LookupValue.new(:value => nil, :match => "hostgroup=Common", :lookup_key_id => key.id)
|
|||
refute value.valid?
|
|||
end
|
|||
92eb3d7c | Ori Rabin | ||
569650b7 | Shimon Shtein | test "boolean lookup value should allow nil value if omit is true" do
|
|
68388bc2 | Michael Moll | # boolean key
|
|
baa21c79 | Ori Rabin | key = lookup_keys(:three)
|
|
569650b7 | Shimon Shtein | value = LookupValue.new(:value => nil, :match => "hostgroup=Common", :lookup_key_id => key.id, :omit => true)
|
|
baa21c79 | Ori Rabin | assert_valid value
|
|
end
|
|||
09ce8a63 | Ori Rabin | test "lookup value should allow valid key" do
|
|
key = lookup_keys(:three)
|
|||
value = LookupValue.new(:value => true, :match => "hostgroup=Common", :lookup_key_id => key.id)
|
|||
assert_valid value
|
|||
end
|
|||
test "lookup value should allow valid multiple key" do
|
|||
key = lookup_keys(:three)
|
|||
value = LookupValue.new(:value => true, :match => "hostgroup=Common,domain=example.com", :lookup_key_id => key.id)
|
|||
assert_valid value
|
|||
end
|
|||
test "lookup value should not allow for nil key" do
|
|||
key = lookup_keys(:three)
|
|||
value = LookupValue.new(:value => true, :match => "", :lookup_key_id => key.id)
|
|||
refute_valid value
|
|||
end
|
|||
test "lookup value will be rejected for invalid key" do
|
|||
key = lookup_keys(:three)
|
|||
value = LookupValue.new(:value => true, :match => "hostgroup=", :lookup_key_id => key.id)
|
|||
refute_valid value
|
|||
assert_equal "is invalid", value.errors[:match].first
|
|||
end
|
|||
test "lookup value will be rejected for invalid multiple key" do
|
|||
key = lookup_keys(:three)
|
|||
value = LookupValue.new(:value => true, :match => "hostgroup=Common,domain=", :lookup_key_id => key.id)
|
|||
refute_valid value
|
|||
assert_equal "is invalid", value.errors[:match].first
|
|||
end
|
|||
0b5f7acb | Ori Rabin | test "lookup value will be rejected for invalid matcher" do
|
|
key = lookup_keys(:three)
|
|||
value = LookupValue.new(:value => true, :match => "something=Common", :lookup_key_id => key.id)
|
|||
refute_valid value
|
|||
assert_equal "something does not exist in order field", value.errors[:match].first
|
|||
end
|
|||
d836a838 | orrabin | test "shouldn't save with empty boolean matcher for smart class parameter" do
|
|
8c6bc83e | Marek Hulan | lookup_key = FactoryBot.create(:puppetclass_lookup_key, :key_type => 'boolean', :override => true,
|
|
d836a838 | orrabin | :default_value => "true", :description => 'description')
|
|
34f84c8d | Michael Moll | lookup_value = FactoryBot.build_stubbed(:lookup_value, :lookup_key => lookup_key, :match => "os=fake", :value => '')
|
|
d836a838 | orrabin | refute lookup_value.valid?
|
|
end
|
|||
92eb3d7c | Ori Rabin | context "when key is a boolean and default_value is a string" do
|
|
def setup
|
|||
8c6bc83e | Marek Hulan | @key = FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param,
|
|
92eb3d7c | Ori Rabin | :override => true, :key_type => 'boolean',
|
|
569650b7 | Shimon Shtein | :default_value => 'whatever', :puppetclass => puppetclasses(:one), :omit => true)
|
|
@value = LookupValue.new(:value => 'abc', :match => "hostgroup=Common", :lookup_key_id => @key.id, :omit => true)
|
|||
92eb3d7c | Ori Rabin | end
|
|
569650b7 | Shimon Shtein | test "value is not validated if omit is true" do
|
|
92eb3d7c | Ori Rabin | assert_valid @value
|
|
end
|
|||
569650b7 | Shimon Shtein | test "value is validated if omit is false" do
|
|
@value.omit = false
|
|||
92eb3d7c | Ori Rabin | refute_valid @value
|
|
end
|
|||
end
|
|||
d4b8428f | amirfefer | ||
context "when key type is puppetclass lookup and value is empty" do
|
|||
def setup
|
|||
8c6bc83e | Marek Hulan | @key = FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param,
|
|
0b5f7acb | Ori Rabin | :with_override, :with_omit, :path => "hostgroup\ncomment",
|
|
d4b8428f | amirfefer | :key_type => 'string',
|
|
:puppetclass => puppetclasses(:one))
|
|||
8c6bc83e | Marek Hulan | @value = FactoryBot.build_stubbed(:lookup_value, :value => "",
|
|
d4b8428f | amirfefer | :match => "hostgroup=Common",
|
|
:lookup_key_id => @key.id,
|
|||
569650b7 | Shimon Shtein | :omit => true)
|
|
d4b8428f | amirfefer | end
|
|
569650b7 | Shimon Shtein | test "value is validated if omit is true" do
|
|
d4b8428f | amirfefer | assert_valid @value
|
|
end
|
|||
569650b7 | Shimon Shtein | test "value is not validated if omit is false" do
|
|
@value.omit = false
|
|||
d4b8428f | amirfefer | refute_valid @value
|
|
end
|
|||
end
|
|||
c2ae41a8 | Tomer Brisker | ||
test "should allow white space in value" do
|
|||
8c6bc83e | Marek Hulan | key = FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param,
|
|
0b5f7acb | Ori Rabin | :with_override, :path => "hostgroup\ncomment",
|
|
c2ae41a8 | Tomer Brisker | :key_type => 'string',
|
|
:puppetclass => puppetclasses(:one))
|
|||
text = <<EOF
|
|||
this is a multiline value
|
|||
with leading and trailing whitespace
|
|||
EOF
|
|||
value = LookupValue.new(:value => text, :match => "hostgroup=Common", :lookup_key_id =>key.id)
|
|||
assert value.save!
|
|||
assert_equal value.value, text
|
|||
end
|
|||
ffdcc07d | Tomer Brisker | ||
test "path should return the correct path for the key" do
|
|||
value = LookupValue.new(:match => 'fqdn=abc.example.com')
|
|||
3cd8c84b | Michael Moll | assert_equal('fqdn', value.path)
|
|
ffdcc07d | Tomer Brisker | value.match = "hostgroup=Common,domain=example.com"
|
|
assert_equal('hostgroup,domain', value.path)
|
|||
end
|
|||
124647de | ldjebran | ||
test "should create override value for smart variable with list validator and matching value" do
|
|||
630061d2 | Michael Moll | values_list = [ 'test', 'example', 30 ]
|
|
124647de | ldjebran | validator_type = 'list'
|
|
validator_rule = values_list.join(', ')
|
|||
smart_variable = FactoryBot.create(
|
|||
:variable_lookup_key,
|
|||
:variable => RFauxFactory.gen_alpha,
|
|||
:puppetclass_id => puppetclasses(:one).id,
|
|||
:validator_type => validator_type,
|
|||
:validator_rule => validator_rule,
|
|||
:default_value => 'example'
|
|||
)
|
|||
match = 'domain=example.com'
|
|||
values_list.each do |value|
|
|||
sv_lookup_value = FactoryBot.build(
|
|||
:lookup_value,
|
|||
:lookup_key_id => smart_variable.id,
|
|||
:match => match,
|
|||
:value => value
|
|||
)
|
|||
assert sv_lookup_value.valid?
|
|||
assert_equal match, sv_lookup_value.match
|
|||
assert_equal value, sv_lookup_value.value
|
|||
end
|
|||
end
|
|||
6c492cce | Joseph Mitchell Magen | end
|