From d344e4307ed69e095d2c7869f12448d0b6e240a3 Mon Sep 17 00:00:00 2001 From: Joe VLcek Date: Fri, 28 Sep 2018 09:07:03 -0400 Subject: [PATCH 1/3] Validate towhat policy field https://bugzilla.redhat.com/show_bug.cgi?id=1435780 --- app/models/miq_policy.rb | 11 +++++++++++ spec/models/miq_policy_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/models/miq_policy.rb b/app/models/miq_policy.rb index e42db38d480..cc2e69fe312 100644 --- a/app/models/miq_policy.rb +++ b/app/models/miq_policy.rb @@ -1,6 +1,16 @@ # TODO: Import/Export support class MiqPolicy < ApplicationRecord + TOWHAT_APPLIES_TO_CLASSES = %w(Host + Vm + ContainerReplicator + ContainerGroup + ContainerNode + ContainerImage + ContainerProject + ExtManagementSystem + PhysicalServer).freeze + acts_as_miq_taggable acts_as_miq_set_member include_concern 'ImportExport' @@ -29,6 +39,7 @@ 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 } 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 2cb80949490..280419623bf 100644 --- a/spec/models/miq_policy_spec.rb +++ b/spec/models/miq_policy_spec.rb @@ -307,4 +307,25 @@ ) end end + + context '.validates' do + it 'validates towhat and mode' do + expect(described_class.create!(:towhat => "Host", + :mode => "compliance", + :active => false, + :description => 'x',)).to have_attributes(:towhat => "Host", + :active => false, + :mode => "compliance") + end + + it 'reports invalid towhat' do + expect do + described_class.create!(:towhat => "BobsYourUncle", + :mode => "compliance", + :active => false, + :description => 'x') + end.to raise_error(ActiveRecord::RecordInvalid, + "Validation failed: MiqPolicy: Towhat is not included in the list") + end + end end From 6152ab436cf7feb0610e41e6d4bc32f7f709c581 Mon Sep 17 00:00:00 2001 From: Joe VLcek Date: Fri, 28 Sep 2018 11:59:20 -0400 Subject: [PATCH 2/3] Alphabetize TOWHAT_APPLIES_TO_CLASSES and focus new tests on towhat. --- app/models/miq_policy.rb | 12 ++++++------ spec/models/miq_policy_spec.rb | 7 ++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/models/miq_policy.rb b/app/models/miq_policy.rb index cc2e69fe312..89a96df6939 100644 --- a/app/models/miq_policy.rb +++ b/app/models/miq_policy.rb @@ -1,15 +1,15 @@ # TODO: Import/Export support class MiqPolicy < ApplicationRecord - TOWHAT_APPLIES_TO_CLASSES = %w(Host - Vm - ContainerReplicator - ContainerGroup - ContainerNode + TOWHAT_APPLIES_TO_CLASSES = %w(ContainerGroup ContainerImage + ContainerNode ContainerProject + ContainerReplicator ExtManagementSystem - PhysicalServer).freeze + Host + PhysicalServer + Vm).freeze acts_as_miq_taggable acts_as_miq_set_member diff --git a/spec/models/miq_policy_spec.rb b/spec/models/miq_policy_spec.rb index 280419623bf..6c3644a4a7b 100644 --- a/spec/models/miq_policy_spec.rb +++ b/spec/models/miq_policy_spec.rb @@ -309,19 +309,16 @@ end context '.validates' do - it 'validates towhat and mode' do + it 'validates towhat' do expect(described_class.create!(:towhat => "Host", - :mode => "compliance", :active => false, :description => 'x',)).to have_attributes(:towhat => "Host", - :active => false, - :mode => "compliance") + :active => false) end it 'reports invalid towhat' do expect do described_class.create!(:towhat => "BobsYourUncle", - :mode => "compliance", :active => false, :description => 'x') end.to raise_error(ActiveRecord::RecordInvalid, From a9bdbd92ab9f04e90539f6288e5853649c56b87e Mon Sep 17 00:00:00 2001 From: Joe VLcek Date: Fri, 28 Sep 2018 13:19:02 -0400 Subject: [PATCH 3/3] Simplify the new specs based on review feedback. --- spec/models/miq_policy_spec.rb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/spec/models/miq_policy_spec.rb b/spec/models/miq_policy_spec.rb index 6c3644a4a7b..73c7f9eb4d2 100644 --- a/spec/models/miq_policy_spec.rb +++ b/spec/models/miq_policy_spec.rb @@ -310,19 +310,11 @@ context '.validates' do it 'validates towhat' do - expect(described_class.create!(:towhat => "Host", - :active => false, - :description => 'x',)).to have_attributes(:towhat => "Host", - :active => false) + expect(FactoryGirl.build(:miq_policy, :towhat => "Host")).to be_valid end it 'reports invalid towhat' do - expect do - described_class.create!(:towhat => "BobsYourUncle", - :active => false, - :description => 'x') - end.to raise_error(ActiveRecord::RecordInvalid, - "Validation failed: MiqPolicy: Towhat is not included in the list") + expect(FactoryGirl.build(:miq_policy, :towhat => "BobsYourUncle")).not_to be_valid end end end