Redirect to pdf attachement old name if not attached

This commit is contained in:
Paul Chavard 2019-09-10 11:09:51 +02:00
parent 0201a501c6
commit 347f03d2a9
6 changed files with 23 additions and 32 deletions

View file

@ -14,10 +14,10 @@ module Instructeurs
after_action :mark_annotations_privees_as_read, only: [:annotations_privees, :update_annotations] after_action :mark_annotations_privees_as_read, only: [:annotations_privees, :update_annotations]
def attestation def attestation
if dossier.attestation.pdf_active_storage.attached? if dossier.attestation.pdf.attached?
redirect_to url_for(dossier.attestation.pdf)
elsif dossier.attestation.pdf_active_storage.attached?
redirect_to url_for(dossier.attestation.pdf_active_storage) redirect_to url_for(dossier.attestation.pdf_active_storage)
else
send_data(dossier.attestation.pdf.read, filename: 'attestation.pdf', type: 'application/pdf')
end end
end end

View file

@ -48,10 +48,10 @@ module Users
end end
def attestation def attestation
if dossier.attestation.pdf_active_storage.attached? if dossier.attestation.pdf.attached?
redirect_to url_for(dossier.attestation.pdf)
elsif dossier.attestation.pdf_active_storage.attached?
redirect_to url_for(dossier.attestation.pdf_active_storage) redirect_to url_for(dossier.attestation.pdf_active_storage)
else
send_data(dossier.attestation.pdf.read, filename: 'attestation.pdf', type: 'application/pdf')
end end
end end

View file

@ -40,6 +40,8 @@ describe Admin::AttestationTemplatesController, type: :controller do
end end
context 'if an attestation template exists on the procedure' do context 'if an attestation template exists on the procedure' do
after { procedure.attestation_template.destroy }
context 'with logos' do context 'with logos' do
let!(:attestation_template) do let!(:attestation_template) do
create(:attestation_template, logo: logo, signature: signature) create(:attestation_template, logo: logo, signature: signature)
@ -50,7 +52,14 @@ describe Admin::AttestationTemplatesController, type: :controller do
it { expect(assigns[:created_at]).to eq(Time.zone.now) } it { expect(assigns[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo).download).to eq(logo2.read) } it { expect(assigns(:logo).download).to eq(logo2.read) }
it { expect(assigns(:signature).download).to eq(signature2.read) } it { expect(assigns(:signature).download).to eq(signature2.read) }
after { procedure.attestation_template.destroy } end
context 'with empty logo' do
it { expect(subject.status).to eq(200) }
it { expect(assigns).to include(upload_params.stringify_keys) }
it { expect(assigns[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:logo)).to eq(nil) }
it { expect(assigns(:signature)).to eq(nil) }
end end
end end
end end

View file

@ -15,21 +15,11 @@ describe Instructeurs::DossiersController, type: :controller do
describe '#attestation' do describe '#attestation' do
context 'when a dossier has an attestation' do context 'when a dossier has an attestation' do
let(:fake_pdf) { double(read: 'pdf content') } let(:dossier) { create(:dossier, :accepte, attestation: create(:attestation, :with_pdf), procedure: procedure) }
let!(:dossier) { create(:dossier, :en_construction, attestation: Attestation.new, procedure: procedure) }
let!(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let!(:dossier) { create(:dossier, :en_construction, attestation: Attestation.new, procedure: procedure) }
it 'returns the attestation pdf' do
allow_any_instance_of(Attestation).to receive(:pdf).and_return(fake_pdf)
expect(controller).to receive(:send_data)
.with('pdf content', filename: 'attestation.pdf', type: 'application/pdf') do
controller.head :ok
end
it 'redirects to attestation pdf' do
get :attestation, params: { procedure_id: procedure.id, dossier_id: dossier.id } get :attestation, params: { procedure_id: procedure.id, dossier_id: dossier.id }
expect(response).to have_http_status(:success) expect(response).to redirect_to(dossier.attestation.pdf_url.gsub('http://localhost:3000', ''))
end end
end end
end end

View file

@ -141,19 +141,11 @@ describe Users::DossiersController, type: :controller do
before { sign_in(user) } before { sign_in(user) }
context 'when a dossier has an attestation' do context 'when a dossier has an attestation' do
let(:fake_pdf) { double(read: 'pdf content') } let(:dossier) { create(:dossier, :accepte, attestation: create(:attestation, :with_pdf), user: user) }
let!(:dossier) { create(:dossier, attestation: Attestation.new, user: user) }
it 'returns the attestation pdf' do
allow_any_instance_of(Attestation).to receive(:pdf).and_return(fake_pdf)
expect(controller).to receive(:send_data)
.with('pdf content', filename: 'attestation.pdf', type: 'application/pdf') do
controller.head :ok
end
it 'redirects to attestation pdf' do
get :attestation, params: { id: dossier.id } get :attestation, params: { id: dossier.id }
expect(response).to have_http_status(:success) expect(response).to redirect_to(dossier.attestation.pdf_url.gsub('http://localhost:3000', ''))
end end
end end
end end

View file

@ -4,7 +4,7 @@ FactoryBot.define do
dossier { create(:dossier) } dossier { create(:dossier) }
end end
trait :with_legacy_pdf do trait :with_pdf do
pdf { Rack::Test::UploadedFile.new("./spec/fixtures/files/dossierPDF.pdf", 'application/pdf') } pdf { Rack::Test::UploadedFile.new("./spec/fixtures/files/dossierPDF.pdf", 'application/pdf') }
end end
end end