Skip to content

Commit

Permalink
Fixes #17870 - Empty boolean matcher should not turn into false
Browse files Browse the repository at this point in the history
  • Loading branch information
orrabin authored and dLobatog committed Dec 29, 2016
1 parent 997cdcc commit d836a83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
15 changes: 5 additions & 10 deletions app/models/lookup_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class LookupValue < ActiveRecord::Base
delegate :key, :to => :lookup_key
before_validation :sanitize_match

before_validation :validate_and_cast_value, :unless => Proc.new{|p| p.omit }
validate :validate_value, :ensure_fqdn_exists, :ensure_hostgroup_exists, :ensure_matcher_exists
validate :ensure_fqdn_exists, :ensure_hostgroup_exists, :ensure_matcher_exists
validate :validate_value, :unless => Proc.new{|p| p.omit }

attr_accessor :host_or_hostgroup

Expand Down Expand Up @@ -44,6 +44,7 @@ def value_before_type_cast
end

def validate_value
validate_and_cast_value
Foreman::Parameters::Validator.new(self,
:type => lookup_key.validator_type,
:validate_with => lookup_key.validator_rule,
Expand All @@ -58,17 +59,11 @@ def sanitize_match
end

def validate_and_cast_value
return true if self.marked_for_destruction? || !self.value.is_a?(String)
begin
unless self.lookup_key.contains_erb?(value)
Foreman::Parameters::Caster.new(self, :attribute_name => :value, :to => lookup_key.key_type).cast!
end
true
return if !self.value.is_a?(String) || self.lookup_key.contains_erb?(value)
Foreman::Parameters::Caster.new(self, :attribute_name => :value, :to => lookup_key.key_type).cast!
rescue StandardError, SyntaxError => e
Foreman::Logging.exception("Error while parsing #{lookup_key}", e)
errors.add(:value, _("is invalid %s") % lookup_key.key_type)
false
end
end

def ensure_fqdn_exists
Expand Down
7 changes: 7 additions & 0 deletions test/models/lookup_value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def valid_attrs2
assert_equal "something does not exist in order field", value.errors[:match].first
end

test "shouldn't save with empty boolean matcher for smart class parameter" do
lookup_key = FactoryGirl.create(:puppetclass_lookup_key, :key_type => 'boolean', :override => true,
:default_value => "true", :description => 'description')
lookup_value = FactoryGirl.build(:lookup_value, :lookup_key => lookup_key, :match => "os=fake", :value => '')
refute lookup_value.valid?
end

context "when key is a boolean and default_value is a string" do
def setup
@key = FactoryGirl.create(:puppetclass_lookup_key, :as_smart_class_param,
Expand Down

0 comments on commit d836a83

Please sign in to comment.