Skip to content

Commit

Permalink
Cleanup Report results pdf title escape
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Mar 4, 2024
1 parent 5467ea7 commit efd7a63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/miq_report_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def purge_for_user
def to_pdf
# Create the pdf header section
html_string = generate_pdf_header(
:title => name.gsub(/'/, '\\\\\&'), # Escape single quotes
:title => name.gsub("'", "\\\\'"), # Escape single quotes
:page_size => report.page_size,
:run_date => format_timezone(last_run_on, user_timezone, "gtl")
)
Expand Down
34 changes: 34 additions & 0 deletions spec/models/miq_report_result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,38 @@
])
end
end

describe "#to_pdf" do
let(:user) { FactoryBot.create(:user) }
let(:report) { FactoryBot.create(:miq_report, :title => report_title) }
let(:report_result) do
FactoryBot.create(:miq_report_result, :name => report.title, :miq_report_id => report.id, :report => report, :userid => user.userid)
end

context "with a normal report" do
let(:report_title) { "VMs using thin provisioned disks" }
let(:pdf_title) { report_title }

it "renders the report" do
expect(PdfGenerator).to receive(:pdf_from_string).with(<<~EOHTML.chomp, "pdf_report.css")
<head><style>@page{size: a4 landscape}@page{margin: 40pt 30pt 40pt 30pt}@page{@top{content: '#{pdf_title}';color:blue}}@page{@bottom-center{font-size: 75%;content: 'Report date: '}}@page{@bottom-right{font-size: 75%;content: 'Page ' counter(page) ' of ' counter(pages)}}</style></head><table class="table table-striped table-bordered "><thead><tr><tbody></tbody></table>
EOHTML

report_result.to_pdf
end
end

context "with a report with single quotes in the name" do
let(:report_title) { "Fred's VMs using thin provisioned disks" }
let(:pdf_title) { "Fred\\'s VMs using thin provisioned disks" }

it "renders the report" do
expect(PdfGenerator).to receive(:pdf_from_string).with(<<~EOHTML.chomp, "pdf_report.css")
<head><style>@page{size: a4 landscape}@page{margin: 40pt 30pt 40pt 30pt}@page{@top{content: '#{pdf_title}';color:blue}}@page{@bottom-center{font-size: 75%;content: 'Report date: '}}@page{@bottom-right{font-size: 75%;content: 'Page ' counter(page) ' of ' counter(pages)}}</style></head><table class="table table-striped table-bordered "><thead><tr><tbody></tbody></table>
EOHTML

report_result.to_pdf
end
end
end
end

0 comments on commit efd7a63

Please sign in to comment.