Skip to content

Commit

Permalink
Merge pull request #15273 from gtanzillo/report-import-symbolize-keys
Browse files Browse the repository at this point in the history
Ensure report columns serialized as hashes have symbolized keys before importing
(cherry picked from commit 832975b)

https://bugzilla.redhat.com/show_bug.cgi?id=1458810
  • Loading branch information
chessbyte authored and simaishi committed Jun 5, 2017
1 parent f59dd36 commit 409d679
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/models/miq_report/import_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def import_from_hash(report, options = nil)
raise _("Incorrect format, only policy records can be imported.")
end

# Ensure that all columns serialized as hashes in the report have keys that are symbols
self.column_names.each { |k| report[k].deep_symbolize_keys! if report[k].kind_of?(Hash) }

user = options[:user] || User.find_by_userid(options[:userid])
report.merge!("miq_group_id" => user.current_group_id, "user_id" => user.id)

Expand Down
20 changes: 15 additions & 5 deletions spec/models/miq_report/import_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
before do
@user = FactoryGirl.create(:user_admin)
@old_report = FactoryGirl.create(:miq_report,
:name => "Test Report",
:rpt_type => "Custom",
:tz => "Eastern Time (US & Canada)",
:col_order => ["name", "boot_time", "disks_aligned"],
:cols => ["name", "boot_time", "disks_aligned"]
:name => "Test Report",
:rpt_type => "Custom",
:tz => "Eastern Time (US & Canada)",
:col_order => ["name", "boot_time", "disks_aligned"],
:cols => ["name", "boot_time", "disks_aligned"],
:db_options => {:rpt_type => "ChargebackContainerProject"}
)
end

context ".import_from_hash" do
before do
report_string = MiqReport.export_to_yaml([@old_report.id], MiqReport)
@new_report = YAML.load(report_string).first
@from_json = JSON.parse(@new_report.to_json)
@options = {
:overwrite => true,
:user => @user
Expand All @@ -38,6 +40,14 @@
expect(result[:status]).to eq(:add)
expect(MiqReport.count).to eq(1)
end

it "imports from json and preserves symbolized keys in serialized columns" do
@options[:save] = true
MiqReport.import_from_hash(@from_json, @options)

report = MiqReport.last
expect(report.db_options[:rpt_type]).to_not be_nil
end
end

context "existing report" do
Expand Down

0 comments on commit 409d679

Please sign in to comment.