Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #17870 - Empty boolean matcher should not turn into false #4144

Merged
merged 1 commit into from
Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this no longer runs in a callback, it doesn't need to return true/false, which means it can be greatly simplified.

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