From 7c5d27d8e9a254576defec6554e31ae6b8abb921 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 7 Dec 2022 23:04:50 +0100 Subject: [PATCH] feat(attachment): allow user to "view" a file without the complete download UI --- app/components/attachment/edit_component.rb | 5 ++++ .../edit_component/edit_component.html.haml | 2 +- .../attachment/edit_component_spec.rb | 27 ++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 28bcf969e..16a08f85a 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -119,6 +119,11 @@ class Attachment::EditComponent < ApplicationComponent def downloadable? return false unless user_can_download? + + viewable? + end + + def viewable? return false if attachment.virus_scanner_error? return false if attachment.watermark_pending? diff --git a/app/components/attachment/edit_component/edit_component.html.haml b/app/components/attachment/edit_component/edit_component.html.haml index e98f990c4..90ddb9512 100644 --- a/app/components/attachment/edit_component/edit_component.html.haml +++ b/app/components/attachment/edit_component/edit_component.html.haml @@ -9,7 +9,7 @@ - if downloadable? = render Dsfr::DownloadComponent.new(attachment:) - else - %span.attachment-filename.fr-mr-1w-= attachment.filename.to_s + %span.attachment-filename.fr-mr-1w= link_to_if(viewable?, attachment.filename.to_s, helpers.url_for(attachment.blob), title: "Ouvrir le fichier #{attachment.filename.to_s}", **helpers.external_link_attributes) = render Attachment::ProgressComponent.new(attachment: attachment) diff --git a/spec/components/attachment/edit_component_spec.rb b/spec/components/attachment/edit_component_spec.rb index 8fd077374..51c24fbaf 100644 --- a/spec/components/attachment/edit_component_spec.rb +++ b/spec/components/attachment/edit_component_spec.rb @@ -86,13 +86,19 @@ RSpec.describe Attachment::EditComponent, type: :component do context 'when user can download' do let(:kwargs) { { user_can_download: true } } - it 'renders a link to download the file' do - expect(subject).to have_link(filename) + context 'when watermarking is done' do + before do + attachment.metadata['watermark'] = true + end + + it 'renders a complete downlaod interface with details to download the file' do + expect(subject).to have_link(text: filename) + expect(subject).to have_text(/PNG.+\d+ octets/) + end end context 'when watermark is pending' do let(:champ) { create(:champ_titre_identite) } - let(:kwargs) { { user_can_download: true } } it 'displays the filename, but doesn’t allow to download the file' do expect(attachment.watermark_pending?).to be_truthy @@ -104,6 +110,21 @@ RSpec.describe Attachment::EditComponent, type: :component do end end + context 'when user cannot download' do + let(:kwargs) { { user_can_download: false } } + + context 'when watermarking is done' do + before do + attachment.metadata['watermark'] = true + end + + it 'renders a simple link to view file' do + expect(subject).to have_link(text: filename) + expect(subject).not_to have_text(/PNG.+\d+ octets/) + end + end + end + context 'with non nominal or final antivirus status' do before do champ.piece_justificative_file[0].blob.update(metadata: attachment.blob.metadata.merge(virus_scan_result: virus_scan_result))