Skip to content

Commit

Permalink
Clarify the 'towhat' inclusion validation message on policy creation.
Browse files Browse the repository at this point in the history
https://bugzilla.redhat.com/show_bug.cgi?id=1663562

- Explicitly set the inclusion whitelist as part of the error message
when creating a policy with an invalid 'towhat' value to clarify the
expectation, particularly when creating via the API.

- Modify supportive spec which tests the validity of the policy record.
  • Loading branch information
coderbydesign committed Jan 8, 2019
1 parent 97c8dba commit 5f0ae21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/models/miq_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class MiqPolicy < ApplicationRecord
validates_presence_of :name, :description, :guid
validates_uniqueness_of :name, :description, :guid
validates :mode, :inclusion => { :in => %w(compliance control) }
validates :towhat, :inclusion => { :in => TOWHAT_APPLIES_TO_CLASSES }
validates :towhat, :inclusion => { :in => TOWHAT_APPLIES_TO_CLASSES,
:message => "should be one of #{TOWHAT_APPLIES_TO_CLASSES.join(", ")}" }

scope :with_mode, ->(mode) { where(:mode => mode) }
scope :with_towhat, ->(towhat) { where(:towhat => towhat) }
Expand Down
8 changes: 7 additions & 1 deletion spec/models/miq_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,13 @@
end

it 'reports invalid towhat' do
expect(FactoryBot.build(:miq_policy, :towhat => "BobsYourUncle")).not_to be_valid
policy = FactoryBot.build(:miq_policy, :towhat => "BobsYourUncle")
towhat_error = "should be one of ContainerGroup, ContainerImage, "\
"ContainerNode, ContainerProject, ContainerReplicator, "\
"ExtManagementSystem, Host, PhysicalServer, Vm"

expect(policy).not_to be_valid
expect(policy.errors.messages).to include(:towhat => [towhat_error])
end
end
end

0 comments on commit 5f0ae21

Please sign in to comment.