Skip to content
Open
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: 1 addition & 1 deletion app/controllers/org_admin/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/public_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions spec/requests/template_docx_export_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading