refactor(attachment): bring batch user_can_destroy, update tests
This commit is contained in:
parent
df135b080b
commit
b609c3dae4
9 changed files with 152 additions and 89 deletions
112
spec/components/attachment/edit_component_spec.rb
Normal file
112
spec/components/attachment/edit_component_spec.rb
Normal file
|
@ -0,0 +1,112 @@
|
|||
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(:kwargs) { {} }
|
||||
|
||||
let(:component) do
|
||||
described_class.new(
|
||||
champ:,
|
||||
attached_file:,
|
||||
attachment:,
|
||||
**kwargs
|
||||
)
|
||||
end
|
||||
|
||||
subject { render_inline(component).to_html }
|
||||
|
||||
context 'when there is no attachment yet' do
|
||||
let(:attachment) { nil }
|
||||
|
||||
it 'renders a form field for uploading a file' do
|
||||
expect(subject).to have_selector('input[type=file]:not(.hidden)')
|
||||
end
|
||||
|
||||
it 'renders max size' do
|
||||
expect(subject).to have_content(/Taille maximale :\s+20 Mo/)
|
||||
end
|
||||
|
||||
it 'renders allowed formats' do
|
||||
expect(subject).to have_content(/Formats supportés :\s+jpeg, png/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is an attachment' do
|
||||
it 'renders the filename' do
|
||||
expect(subject).to have_content(attachment.filename.to_s)
|
||||
end
|
||||
|
||||
it 'hides the file field by default' do
|
||||
expect(subject).to have_selector('input[type=file].hidden')
|
||||
end
|
||||
|
||||
it 'shows the Delete button by default' do
|
||||
expect(subject).to have_selector('[title^="Supprimer le fichier"]')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user cannot destroy the attachment' do
|
||||
let(:kwargs) { { user_can_destroy: false } }
|
||||
|
||||
it 'hides the Delete button' do
|
||||
expect(subject).not_to have_selector("[title^='Supprimer le fichier']")
|
||||
end
|
||||
end
|
||||
|
||||
context 'within multiple attachments' do
|
||||
let(:index) { 0 }
|
||||
let(:component) do
|
||||
described_class.new(
|
||||
champ:,
|
||||
attached_file:,
|
||||
attachment: nil,
|
||||
as_multiple: true,
|
||||
index:
|
||||
)
|
||||
end
|
||||
|
||||
it 'does not render an empty file' do # (is is rendered by MultipleComponent)
|
||||
expect(subject).not_to have_selector('input[type=file]')
|
||||
end
|
||||
|
||||
it 'renders max size for first index' do
|
||||
expect(subject).to have_content(/Taille maximale :\s+20 Mo/)
|
||||
end
|
||||
|
||||
context 'when index is not 0' do
|
||||
let(:index) { 1 }
|
||||
|
||||
it 'renders max size for first index' do
|
||||
expect(subject).not_to have_content('Taille maximale')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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)
|
||||
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
|
||||
expect(subject).to have_text(filename)
|
||||
expect(subject).to have_link('Supprimer')
|
||||
expect(subject).to have_no_link(text: filename) # don't match "Delete" link which also include filename in title attribute
|
||||
expect(subject).to have_text('Traitement en cours')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'TODO: with a pending antivirus scan' do
|
||||
end
|
||||
|
||||
context 'TODO: with an error' do
|
||||
end
|
||||
end
|
|
@ -1,13 +1,21 @@
|
|||
describe 'shared/attachment/_show.html.haml', type: :view do
|
||||
RSpec.describe Attachment::ShowComponent, type: :component do
|
||||
let(:champ) { create(:champ_piece_justificative) }
|
||||
let(:virus_scan_result) { nil }
|
||||
|
||||
let(:attachment) {
|
||||
champ.piece_justificative_file.attachments.first
|
||||
}
|
||||
|
||||
let(:component) do
|
||||
described_class.new(attachment:)
|
||||
end
|
||||
|
||||
subject { render_inline(component).to_html }
|
||||
|
||||
before do
|
||||
champ.piece_justificative_file[0].blob.update(metadata: champ.piece_justificative_file[0].blob.metadata.merge(virus_scan_result: virus_scan_result))
|
||||
end
|
||||
|
||||
subject { render Attachment::ShowComponent.new(attachment: champ.piece_justificative_file.attachments.first) }
|
||||
|
||||
context 'when there is no anti-virus scan' do
|
||||
let(:virus_scan_result) { nil }
|
||||
|
||||
|
@ -27,17 +35,6 @@ describe 'shared/attachment/_show.html.haml', type: :view do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when watermark is pending' do
|
||||
let(:champ) { create(:champ_titre_identite) }
|
||||
|
||||
it 'displays the filename, but doesn’t allow to download the file' do
|
||||
expect(champ.piece_justificative_file.attachments[0].watermark_pending?).to be_truthy
|
||||
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('Traitement en cours')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the file is scanned and safe' do
|
||||
let(:virus_scan_result) { ActiveStorage::VirusScanner::SAFE }
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
describe 'shared/attachment/_update.html.haml', type: :view do
|
||||
let(:champ) { build(:champ_titre_identite, dossier: create(:dossier)) }
|
||||
let(:attached_file) { champ.piece_justificative_file }
|
||||
let(:user_can_destroy) { false }
|
||||
let(:template) { nil }
|
||||
|
||||
subject do
|
||||
view.render Attachment::EditComponent.new(champ: champ, attached_file: attached_file, attachment: attached_file[0])
|
||||
end
|
||||
|
||||
context 'when there is no attached file' do
|
||||
before do
|
||||
champ.piece_justificative_file = nil
|
||||
end
|
||||
|
||||
it 'renders a form field for uploading a file' do
|
||||
expect(subject).to have_selector('input[type=file]:not(.hidden)')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is an attached file' do
|
||||
it 'renders a form field for uploading a file' do
|
||||
expect(subject).to have_selector('input[type=file]:not(.hidden)')
|
||||
end
|
||||
|
||||
it 'does not renders a link to the unsaved file' do
|
||||
expect(subject).not_to have_content(attached_file.attachments[0].filename.to_s)
|
||||
end
|
||||
|
||||
it 'does not render action buttons' do
|
||||
expect(subject).not_to have_link('Supprimer')
|
||||
end
|
||||
|
||||
context 'and the attachment has been saved' do
|
||||
before { champ.save! }
|
||||
|
||||
it 'renders a link to the file' do
|
||||
expect(subject).to have_content(attached_file.attachments[0].filename.to_s)
|
||||
end
|
||||
|
||||
it 'hides the form field by default' do
|
||||
expect(subject).to have_selector('input[type=file].hidden')
|
||||
end
|
||||
|
||||
it 'shows the Delete button by default' do
|
||||
is_expected.to have_link('Supprimer')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user cannot destroy the attachment' do
|
||||
subject do
|
||||
render Attachment::EditComponent.new(champ: champ,
|
||||
attached_file: attached_file,
|
||||
attachment: attached_file[0])
|
||||
end
|
||||
|
||||
it 'hides the Delete button' do
|
||||
is_expected.not_to have_link('Supprimer')
|
||||
end
|
||||
end
|
||||
end
|
|
@ -114,7 +114,7 @@ describe 'shared/dossiers/edit.html.haml', type: :view do
|
|||
before { dossier.champs_public << champ }
|
||||
|
||||
it 'cannot delete a piece justificative' do
|
||||
expect(subject).not_to have_text('Supprimer')
|
||||
expect(subject).not_to have_selector("[title='Supprimer le fichier #{champ.piece_justificative_file.attachments[0].filename}']")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -124,7 +124,7 @@ describe 'shared/dossiers/edit.html.haml', type: :view do
|
|||
end
|
||||
|
||||
it 'can delete a piece justificative' do
|
||||
expect(subject).to have_text('Supprimer')
|
||||
expect(subject).to have_selector("[title='Supprimer le fichier #{champ.piece_justificative_file.attachments[0].filename}']")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue