From 5f0ae21705d0213d96b8ae0a5936c584a79307e4 Mon Sep 17 00:00:00 2001 From: Keith Walsh Date: Mon, 7 Jan 2019 12:07:44 -0500 Subject: [PATCH] Clarify the 'towhat' inclusion validation message on policy creation. 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. --- app/models/miq_policy.rb | 3 ++- spec/models/miq_policy_spec.rb | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/models/miq_policy.rb b/app/models/miq_policy.rb index 89a96df6939..c519f2dafd9 100644 --- a/app/models/miq_policy.rb +++ b/app/models/miq_policy.rb @@ -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) } diff --git a/spec/models/miq_policy_spec.rb b/spec/models/miq_policy_spec.rb index bca5e801aa3..ae4a01ea21f 100644 --- a/spec/models/miq_policy_spec.rb +++ b/spec/models/miq_policy_spec.rb @@ -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