From 3a895fbd4a3267f58e346b51408d21d03efacbab Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 2 Apr 2024 09:53:11 +0200 Subject: [PATCH] tech(review): add a guard clause to prevent missing pdf, enhance specs --- .../dossiers/_infos_generales.html.haml | 2 +- .../dossiers/show.html.haml_spec.rb | 31 ---------- .../_infos_generales.html.haml_spec.rb | 58 +++++++++++++++++++ 3 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 spec/views/shared/dossiers/_infos_generales.html.haml_spec.rb diff --git a/app/views/shared/dossiers/_infos_generales.html.haml b/app/views/shared/dossiers/_infos_generales.html.haml index 9ab00e6b0..d9f54812b 100644 --- a/app/views/shared/dossiers/_infos_generales.html.haml +++ b/app/views/shared/dossiers/_infos_generales.html.haml @@ -20,7 +20,7 @@ - c.with_value do = simple_format dossier.motivation - - if dossier.attestation.present? + - if dossier.attestation.present? && dossier.attestation.pdf.attached? = render Dossiers::RowShowComponent.new(label: "Attestation") do |c| - c.with_value do = render Dsfr::DownloadComponent.new(attachment: dossier.attestation.pdf, name: t(:download_attestation, scope: [:views, :shared, :dossiers, :form])) diff --git a/spec/views/instructeur/dossiers/show.html.haml_spec.rb b/spec/views/instructeur/dossiers/show.html.haml_spec.rb index 09981eb5d..c9cd5eec9 100644 --- a/spec/views/instructeur/dossiers/show.html.haml_spec.rb +++ b/spec/views/instructeur/dossiers/show.html.haml_spec.rb @@ -23,37 +23,6 @@ describe 'instructeurs/dossiers/show', type: :view do expect(subject).to have_text('en construction') end - context 'with a motivation' do - let(:dossier) { create :dossier, :accepte, :with_motivation } - - it 'displays the motivation text' do - expect(subject).to have_content(dossier.motivation) - end - end - - context 'with an attestation' do - let(:dossier) { create :dossier, :accepte, :with_attestation } - - it 'provides a link to the attestation' do - pdf = dossier.attestation.pdf - expect(dossier).to receive(:attestation).and_return(double(pdf: pdf)).at_least(2) - expect(subject).to have_text('Attestation') - expect(subject).to have_text("Télécharger l‘attestation") - end - end - - context 'with a justificatif' do - let(:dossier) do - dossier = create(:dossier, :accepte, :with_justificatif) - dossier.justificatif_motivation.blob.update(virus_scan_result: ActiveStorage::VirusScanner::SAFE) - dossier - end - - it 'allows to download the justificatif' do - expect(subject).to have_css("a[href*='/rails/active_storage/blobs/']", text: dossier.justificatif_motivation.attachment.filename.to_s) - end - end - context 'en_construction' do let(:dossier) { create(:dossier, :en_construction) } it 'displays the correct actions' do diff --git a/spec/views/shared/dossiers/_infos_generales.html.haml_spec.rb b/spec/views/shared/dossiers/_infos_generales.html.haml_spec.rb new file mode 100644 index 000000000..627f637b3 --- /dev/null +++ b/spec/views/shared/dossiers/_infos_generales.html.haml_spec.rb @@ -0,0 +1,58 @@ +describe 'shared/dossiers/_infos_generales', type: :view do + let(:dossier) { create(:dossier, :en_construction) } + subject { render } + before do + sign_in(current_role.user) + allow(view).to receive(:current_instructeur).and_return(current_role) + allow(view).to receive(:dossier).and_return(dossier) + end + + context 'when expert' do + let(:current_role) { create(:expert) } + + context 'with an attestation' do + let(:dossier) { create :dossier, :accepte, :with_attestation } + + it 'provides a link to the attestation' do + pdf = dossier.attestation.pdf + expect(dossier).to receive(:attestation).and_return(double(pdf: pdf)).at_least(2) + expect(subject).to have_text('Attestation') + expect(subject).to have_text("Télécharger l‘attestation") + end + end + end + + context 'when instructeur' do + let(:current_role) { create(:instructeur) } + context 'with a motivation' do + let(:dossier) { create :dossier, :accepte, :with_motivation } + + it 'displays the motivation text' do + expect(subject).to have_content(dossier.motivation) + end + end + + context 'with an attestation' do + let(:dossier) { create :dossier, :accepte, :with_attestation } + + it 'provides a link to the attestation' do + pdf = dossier.attestation.pdf + expect(dossier).to receive(:attestation).and_return(double(pdf: pdf)).at_least(2) + expect(subject).to have_text('Attestation') + expect(subject).to have_text("Télécharger l‘attestation") + end + end + + context 'with a justificatif' do + let(:dossier) do + dossier = create(:dossier, :accepte, :with_justificatif) + dossier.justificatif_motivation.blob.update(virus_scan_result: ActiveStorage::VirusScanner::SAFE) + dossier + end + + it 'allows to download the justificatif' do + expect(subject).to have_css("a[href*='/rails/active_storage/blobs/']", text: dossier.justificatif_motivation.attachment.filename.to_s) + end + end + end +end