From 76c59d2da9005decafe4f21c3e8441569731908f Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Wed, 27 May 2020 13:00:18 -0400 Subject: [PATCH 1/2] return what went wrong if assigning tag failed BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1792106 --- app/models/classification.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/models/classification.rb b/app/models/classification.rb index 5cfe5a60641..453b41e060a 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -98,11 +98,16 @@ def ns # rubocop:disable Style/NumericPredicate def self.classify(obj, category_name, entry_name, is_request = true) - cat = Classification.lookup_by_name(category_name, obj.region_id) - unless cat.nil? - ent = cat.find_entry_by_name(entry_name, obj.region_id) - ent.assign_entry_to(obj, is_request) unless ent.nil? || obj.is_tagged_with?(ent.to_tag, :ns => "none") - end + cat = Classification.find_by_name(category_name, obj.region_id) + return " - FAILED. Tag category '#{category_name}' not found in region #{obj.region_id}" if cat.nil? + + ent = cat.find_entry_by_name(entry_name, obj.region_id) + return " - FAILED. Tag name '#{entry_name}' not found in region #{obj.region_id}" if ent.nil? + + return " - FAILED. Object already tagged with tag namespace set to 'none'" if obj.is_tagged_with?(ent.to_tag, :ns => "none") + + ent.assign_entry_to(obj, is_request) + " - SUCCESS." end def self.unclassify(obj, category_name, entry_name, is_request = true) From d9df6b4596f2b06d00e6b8b3731d1d8dc3b59b0b Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Wed, 27 May 2020 13:01:05 -0400 Subject: [PATCH 2/2] added test coverage for Classification.classify --- spec/models/classification_spec.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spec/models/classification_spec.rb b/spec/models/classification_spec.rb index c5d928af3fc..1ecd415014c 100644 --- a/spec/models/classification_spec.rb +++ b/spec/models/classification_spec.rb @@ -61,6 +61,35 @@ FactoryBot.create(:classification_tag, :name => "multi_entry_2", :parent => parent) end + describe ".classify" do + let(:entry) { "test_entry" } + let(:category) { "test_category" } + before { @vm = FactoryBot.create(:vm) } + + it "returns detailed message if tag category not found" do + category = "Hello, World" + msg = Classification.classify(@vm, category, entry) + expect(msg).to include("Tag category '#{category}' not found in region #{@vm.region_id}") + end + + it "returns detailed message message if tag entry not found" do + entry = "Hello, World" + msg = Classification.classify(@vm, category, entry) + expect(msg).to include("Tag name '#{entry}' not found in region #{@vm.region_id}") + end + + it "returns detailed message message if object already tagged with tag namespace set to 'none'" do + allow(@vm).to receive(:is_tagged_with?) + allow(@vm).to receive(:is_tagged_with?).with("/managed/test_category/test_entry", :ns => "none").and_return(true) + msg = Classification.classify(@vm, category, entry) + expect(msg).to include("Object already tagged with tag namespace set to 'none'") + end + + it "assign tag entry to object if tag category and tag name exist and returns 'SUCCESS'" do + expect(Classification.classify(@vm, category, entry)).to include("SUCCESS") + end + end + context "#destroy" do it "a category deletes all entries" do cat = Classification.lookup_by_name("test_category")