From d05dc1210ee636ce071ec9b0a80ab10a33df65fe Mon Sep 17 00:00:00 2001 From: aaronskiba <71047780+aaronskiba@users.noreply.github.com> Date: Thu, 3 Dec 2026 11:59:50 -0600 Subject: [PATCH 1/2] test: cover DOCX template exports Add request specs for public and organization admin template exports. Verify the downloaded document, filename, and content. --- spec/requests/template_docx_export_spec.rb | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 spec/requests/template_docx_export_spec.rb diff --git a/spec/requests/template_docx_export_spec.rb b/spec/requests/template_docx_export_spec.rb new file mode 100644 index 0000000000..e4c3e33024 --- /dev/null +++ b/spec/requests/template_docx_export_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'zip' + +RSpec.describe 'Template DOCX export', type: :request do + let(:template) { create(:template, :published, title: 'Exportable template', phases: 1) } + let!(:section) { create(:section, phase: template.phases.first, title: 'Export section') } + let!(:question) { create(:question, :textarea, section: section, text: 'Export question') } + + it 'returns a readable Word document for the public template' do + get template_export_path(template.family_id, format: :docx) + + expect_template_docx + end + + it 'returns a readable Word document for the organization admin' do + sign_in create(:user, :org_admin, org: template.org) + get template_export_org_admin_template_path(template, format: :docx) + + expect_template_docx + end + + # rubocop:disable Metrics/AbcSize + def expect_template_docx + expect(response).to have_http_status(:ok) + expect(response.media_type).to eq('application/vnd.openxmlformats-officedocument.wordprocessingml.document') + expect(response.headers['Content-Disposition']).to include("Exportable_template_v#{template.version}.docx") + Zip::File.open_buffer(response.body) do |archive| + document = Nokogiri::XML(archive.read('word/document.xml')) + expect(document.text).to include(template.title, section.title, question.text) + end + # rubocop:enable Metrics/AbcSize + end +end From 85529baf7b3f4fcb9a4fb6fd831f43ca8e95e4c5 Mon Sep 17 00:00:00 2001 From: aaronskiba <71047780+aaronskiba@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:19:54 -0600 Subject: [PATCH 2/2] fix: restore DOCX template exports on Rails 8 Resolve a breaking change from the Rails 8 upgrade that caused DOCX exports using htmltoword to raise ActionView::MissingTemplate. Pass the template path explicitly through the :template option. This fixes the failing tests added in the previous commit (test: cover DOCX template exports). Rails change that caused the issue: https://github.com/rails/rails/commit/42d75ed3a8b96ee4610601ecde7c40e9d65e003f --- app/controllers/org_admin/templates_controller.rb | 2 +- app/controllers/public_pages_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/org_admin/templates_controller.rb b/app/controllers/org_admin/templates_controller.rb index eb177fc149..a2e028c6a5 100644 --- a/app/controllers/org_admin/templates_controller.rb +++ b/app/controllers/org_admin/templates_controller.rb @@ -337,7 +337,7 @@ def template_export file_name = "#{safe_title}_v#{@template.version}" respond_to do |format| format.docx do - render docx: 'template_exports/template_export', filename: "#{file_name}.docx" + render docx: "#{file_name}.docx", template: 'template_exports/template_export' end format.pdf do diff --git a/app/controllers/public_pages_controller.rb b/app/controllers/public_pages_controller.rb index 1b5d9cff93..7c21d3f411 100644 --- a/app/controllers/public_pages_controller.rb +++ b/app/controllers/public_pages_controller.rb @@ -56,7 +56,7 @@ def template_export file_name = "#{file_name}_v#{@template.version}" respond_to do |format| format.docx do - render docx: 'template_exports/template_export', filename: "#{file_name}.docx" + render docx: "#{file_name}.docx", template: 'template_exports/template_export' end format.pdf do