refactor(attachment): extract shared Attachment::ProgressComponent

This commit is contained in:
Colin Darie 2022-11-22 15:47:28 +01:00
parent 7cbe6bbaa4
commit fefc326e6b
7 changed files with 78 additions and 24 deletions

View file

@ -80,15 +80,6 @@ class Attachment::EditComponent < ApplicationComponent
false
end
def progress_bar_label
case
when attachment.virus_scanner.pending?
"Analyse antivirus en cours…"
when attachment.watermark_pending?
"Traitement en cours…"
end
end
def poll_controller_options
{
controller: 'turbo-poll',

View file

@ -8,9 +8,8 @@
.fr-py-1v
= link_to_if(downloadable?, attachment.filename.to_s, attachment.url, class: "attachment-filename", download: "") do
%span.attachment-filename= attachment.filename.to_s
- if in_progress?
%p.fr-badge.fr-badge--info.fr-badge--sm.fr-badge--no-icon.fr-ml-1w
= progress_bar_label
= render Attachment::ProgressComponent.new(attachment: attachment)
- if error?
%p.fr-error-text= error_message

View file

@ -0,0 +1,20 @@
class Attachment::ProgressComponent < ApplicationComponent
attr_reader :attachment
def initialize(attachment:)
@attachment = attachment
end
def progress_label
case
when attachment.virus_scanner.pending?
"Analyse antivirus en cours…"
when attachment.watermark_pending?
"Traitement en cours…"
end
end
def render?
progress_label.present?
end
end

View file

@ -0,0 +1,2 @@
%p.fr-badge.fr-badge--info.fr-badge--sm.fr-badge--no-icon.fr-ml-1w
= progress_label

View file

@ -8,5 +8,7 @@
- else
.attachment-filename.fr-mb-1w= attachment.filename.to_s
= render Attachment::ProgressComponent.new(attachment: attachment)
- if error?
%p.fr-error-text= error_message

View file

@ -2,6 +2,7 @@ RSpec.describe Attachment::EditComponent, type: :component do
let(:champ) { create(:champ_titre_identite, dossier: create(:dossier)) }
let(:attached_file) { champ.piece_justificative_file }
let(:attachment) { attached_file.attachments.first }
let(:filename) { attachment.filename.to_s }
let(:kwargs) { {} }
let(:component) do
@ -84,7 +85,6 @@ RSpec.describe Attachment::EditComponent, type: :component do
context 'when user can download' do
let(:kwargs) { { user_can_download: true } }
let(:filename) { champ.piece_justificative_file[0].filename.to_s }
it 'renders a link to download the file' do
expect(subject).to have_link(filename)
@ -104,9 +104,47 @@ RSpec.describe Attachment::EditComponent, type: :component do
end
end
context 'TODO: with a pending antivirus scan' do
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))
end
context 'TODO: with an error' do
context 'when the anti-virus scan is pending' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::PENDING }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(filename)
expect(subject).to have_no_link(text: filename)
expect(subject).to have_text('Analyse antivirus en cours')
end
end
context 'when the file is scanned and safe' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::SAFE }
it 'allows to download the file' do
expect(subject).to have_link(filename)
end
end
context 'when the file is scanned and infected' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INFECTED }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_no_link(text: filename)
expect(subject).to have_text('Virus détecté')
end
end
context 'when the file is corrupted' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INTEGRITY_ERROR }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(filename)
expect(subject).to have_no_link(text: filename)
expect(subject).to have_text('corrompu')
end
end
end
end

View file

@ -6,6 +6,8 @@ RSpec.describe Attachment::ShowComponent, type: :component do
champ.piece_justificative_file.attachments.first
}
let(:filename) { attachment.filename.to_s }
let(:component) do
described_class.new(attachment:)
end
@ -20,7 +22,7 @@ RSpec.describe Attachment::ShowComponent, type: :component do
let(:virus_scan_result) { nil }
it 'allows to download the file' do
expect(subject).to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_link(filename)
expect(subject).to have_text('ce fichier na pas été analysé par notre antivirus')
end
end
@ -29,8 +31,8 @@ RSpec.describe Attachment::ShowComponent, type: :component do
let(:virus_scan_result) { ActiveStorage::VirusScanner::PENDING }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text(filename)
expect(subject).not_to have_link(filename)
expect(subject).to have_text('Analyse antivirus en cours')
end
end
@ -39,7 +41,7 @@ RSpec.describe Attachment::ShowComponent, type: :component do
let(:virus_scan_result) { ActiveStorage::VirusScanner::SAFE }
it 'allows to download the file' do
expect(subject).to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_link(filename)
end
end
@ -47,8 +49,8 @@ RSpec.describe Attachment::ShowComponent, type: :component do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INFECTED }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text(filename)
expect(subject).not_to have_link(filename)
expect(subject).to have_text('Virus détecté')
end
end
@ -57,8 +59,8 @@ RSpec.describe Attachment::ShowComponent, type: :component do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INTEGRITY_ERROR }
it 'displays the filename, but doesnt allow to download the file' do
expect(subject).to have_text(champ.piece_justificative_file[0].filename.to_s)
expect(subject).not_to have_link(champ.piece_justificative_file[0].filename.to_s)
expect(subject).to have_text(filename)
expect(subject).not_to have_link(filename)
expect(subject).to have_text('corrompu')
end
end