Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error importing Widget on Custom Report page #16034

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/miq_policy/import_export.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
1 change: 1 addition & 0 deletions app/models/miq_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions app/models/miq_widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions app/models/mixins/yaml_import_export_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
18 changes: 16 additions & 2 deletions spec/models/mixins/yaml_import_export_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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