From b26b01394da24c6bc6d8f3cf67d1ef4f667296ac Mon Sep 17 00:00:00 2001 From: Brian McLaughlin Date: Fri, 1 Sep 2017 16:05:34 -0400 Subject: [PATCH] Fix error importing Widget on Custom Report page https://bugzilla.redhat.com/show_bug.cgi?id=1442728 --- app/models/miq_policy/import_export.rb | 2 ++ app/models/miq_report.rb | 1 + app/models/miq_widget.rb | 1 + app/models/mixins/yaml_import_export_mixin.rb | 9 +++++++++ .../mixins/yaml_import_export_mixin_spec.rb | 18 ++++++++++++++++-- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/models/miq_policy/import_export.rb b/app/models/miq_policy/import_export.rb index 52c5b344ba8..6266e8e74e4 100644 --- a/app/models/miq_policy/import_export.rb +++ b/app/models/miq_policy/import_export.rb @@ -1,6 +1,8 @@ module MiqPolicy::ImportExport extend ActiveSupport::Concern + IMPORT_CLASS_NAMES = %w(MiqPolicy MiqPolicySet MiqAlert).freeze + module ClassMethods def import_from_hash(policy, options = {}) raise _("No Policy to Import") if policy.nil? diff --git a/app/models/miq_report.rb b/app/models/miq_report.rb index 4586068d618..208b0e5d90c 100644 --- a/app/models/miq_report.rb +++ b/app/models/miq_report.rb @@ -52,6 +52,7 @@ class MiqReport < ApplicationRecord GROUPINGS = [[:min, "Minimum"], [:avg, "Average"], [:max, "Maximum"], [:total, "Total"]] PIVOTS = [[:min, "Minimum"], [:avg, "Average"], [:max, "Maximum"], [:total, "Total"]] + IMPORT_CLASS_NAMES = %w(MiqReport).freeze scope :for_user, lambda { |user| if user.admin_user? diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index 861e0fe948d..c0b2181a4d6 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -7,6 +7,7 @@ class MiqWidget < ApplicationRecord default_value_for :read_only, false DEFAULT_ROW_COUNT = 5 + IMPORT_CLASS_NAMES = %w(MiqWidget).freeze belongs_to :resource, :polymorphic => true belongs_to :miq_schedule diff --git a/app/models/mixins/yaml_import_export_mixin.rb b/app/models/mixins/yaml_import_export_mixin.rb index 4a9b9c45415..99faff4c907 100644 --- a/app/models/mixins/yaml_import_export_mixin.rb +++ b/app/models/mixins/yaml_import_export_mixin.rb @@ -32,6 +32,7 @@ def import(fd, options = {}) fd.rewind # ensure to be at the beginning as the file is read multiple times begin reps = YAML.load(fd.read) + validate_import_data_class(reps) rescue Psych::SyntaxError => err _log.error("Failed to load from #{fd}: #{err}") raise "Invalid YAML file" @@ -40,6 +41,14 @@ def import(fd, options = {}) return reps, import_from_array(reps, options) end + def validate_import_data_class(data) + data_class_name = data.first.keys.first + + unless self::IMPORT_CLASS_NAMES.include?(data_class_name) + raise _("Incorrect format: Expected #{self::IMPORT_CLASS_NAMES.join(", ")} and received #{data_class_name}.") + end + end + # Import from an array of hash of the objects # # @param input [Array] The objects to be imported. diff --git a/spec/models/mixins/yaml_import_export_mixin_spec.rb b/spec/models/mixins/yaml_import_export_mixin_spec.rb index bfc61444136..d6d110b8a38 100644 --- a/spec/models/mixins/yaml_import_export_mixin_spec.rb +++ b/spec/models/mixins/yaml_import_export_mixin_spec.rb @@ -42,10 +42,10 @@ end context ".import" do - subject { test_class } + subject { MiqReport } it "valid YAML file" do - @fd = StringIO.new("---\na:") + @fd = StringIO.new("---\n- MiqReport:\n") # if it gets to import_from_array, then it did not choke on yml expect(subject).to receive(:import_from_array) subject.import(@fd) @@ -56,4 +56,18 @@ expect { subject.import(@fd) }.to raise_error("Invalid YAML file") end end + + context ".validate_import_data_class" do + subject { MiqReport } + + it "confirms valid class" do + @data = YAML.safe_load(StringIO.new("---\n- MiqReport:\n").read) + expect { subject.validate_import_data_class(@data) }.not_to raise_error + end + + it "raises exception on invalid class" do + @data = YAML.safe_load(StringIO.new("---\n- MiqWidget:\n").read) + expect { subject.validate_import_data_class(@data) }.to raise_error("Incorrect format: Expected MiqReport and received MiqWidget.") + end + end end