piecejusticative file validation focus on size
This commit is contained in:
parent
c155de1d7e
commit
eb46cc6425
7 changed files with 1 additions and 133 deletions
|
@ -1,47 +0,0 @@
|
|||
module ContentTypeHelper
|
||||
def file_descriptions_from_content_types(content_types)
|
||||
content_types.map { |ct| CONTENT_TYPE_DESCRIPTIONS[ct] }.uniq.join(', ')
|
||||
end
|
||||
|
||||
def file_extensions_from_content_types(content_types)
|
||||
content_types.map do |ct|
|
||||
if CONTENT_TYPE_EXTENSIONS.include?(ct)
|
||||
".#{CONTENT_TYPE_EXTENSIONS[ct]}"
|
||||
end
|
||||
end.uniq.sort.join(', ')
|
||||
end
|
||||
|
||||
CONTENT_TYPE_DESCRIPTIONS = {
|
||||
"text/plain" => 'Word',
|
||||
"application/pdf" => "PDF",
|
||||
"application/msword" => "Word",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "Excel",
|
||||
"application/vnd.ms-excel" => "Excel",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "Excel",
|
||||
"application/vnd.ms-powerpoint" => "PowerPoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => "PowerPoint",
|
||||
"application/vnd.oasis.opendocument.text" => "LibreOffice",
|
||||
"application/vnd.oasis.opendocument.presentation" => "LibreOffice",
|
||||
"application/vnd.oasis.opendocument.spreadsheet" => "LibreOffice",
|
||||
"image/png" => "PNG",
|
||||
"image/jpeg" => "JPG"
|
||||
}
|
||||
|
||||
# Mapping between format and extension documented on MDN:
|
||||
# https://developer.mozilla.org/fr/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||
CONTENT_TYPE_EXTENSIONS = {
|
||||
"text/plain" => "txt",
|
||||
"application/pdf" => "pdf",
|
||||
"application/msword" => "doc",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "docx",
|
||||
"application/vnd.ms-excel" => "xls",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "xlsx",
|
||||
"application/vnd.ms-powerpoint" => "ppt",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => "pptx",
|
||||
"application/vnd.oasis.opendocument.text" => "odt",
|
||||
"application/vnd.oasis.opendocument.presentation" => "odp",
|
||||
"application/vnd.oasis.opendocument.spreadsheet" => "ods",
|
||||
"image/png" => "png",
|
||||
"image/jpeg" => "jpg"
|
||||
}
|
||||
end
|
|
@ -17,24 +17,7 @@
|
|||
class Champs::PieceJustificativeChamp < Champ
|
||||
MAX_SIZE = 200.megabytes
|
||||
|
||||
ACCEPTED_CONTENT_TYPES = [
|
||||
"text/plain",
|
||||
"application/pdf",
|
||||
"application/msword",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"application/vnd.ms-excel",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/vnd.ms-powerpoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
"application/vnd.oasis.opendocument.text",
|
||||
"application/vnd.oasis.opendocument.presentation",
|
||||
"application/vnd.oasis.opendocument.spreadsheet",
|
||||
"image/png",
|
||||
"image/jpeg"
|
||||
]
|
||||
|
||||
validates :piece_justificative_file,
|
||||
content_type: ACCEPTED_CONTENT_TYPES,
|
||||
size: { less_than: MAX_SIZE },
|
||||
if: -> { !type_de_champ.skip_pj_validation }
|
||||
|
||||
|
|
|
@ -31,11 +31,6 @@
|
|||
= button_tag type: 'button', class: 'button attachment-error-retry', data: { 'input-target': ".attachment-input-#{attachment_id}" } do
|
||||
%span.icon.retry
|
||||
Ré-essayer
|
||||
- if defined?(allowed_content_types) && allowed_content_types.present?
|
||||
%p.mb-1
|
||||
Formats acceptés : #{file_descriptions_from_content_types(allowed_content_types)}
|
||||
%sup
|
||||
%span.icon.info{ :title => "Cela correspond aux fichiers ayant comme extension #{file_extensions_from_content_types(allowed_content_types)}" }
|
||||
|
||||
= form.file_field attached_file.name,
|
||||
class: "attachment-input attachment-input-#{attachment_id} #{'hidden' if persisted}",
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
= render 'shared/attachment/edit',
|
||||
{ form: form,
|
||||
attached_file: champ.piece_justificative_file,
|
||||
template: champ.type_de_champ.piece_justificative_template, user_can_destroy: true,
|
||||
allowed_content_types: Champs::PieceJustificativeChamp::ACCEPTED_CONTENT_TYPES }
|
||||
template: champ.type_de_champ.piece_justificative_template, user_can_destroy: true }
|
||||
|
|
|
@ -207,22 +207,6 @@ feature 'The user' do
|
|||
expect(page).to have_text('RIB.pdf')
|
||||
end
|
||||
|
||||
scenario 'add an invalid attachment', js: true do
|
||||
log_in(user, procedure_with_pjs)
|
||||
fill_individual
|
||||
|
||||
# Test invalid file type
|
||||
attach_file('Pièce justificative 1', Rails.root + 'spec/fixtures/files/invalid_file_format.json')
|
||||
expect(page).to have_text('La pièce justificative n’est pas d’un type accepté')
|
||||
expect(page).to have_no_button('Ré-essayer', visible: true)
|
||||
|
||||
# Replace the file by another with a valid type
|
||||
attach_file('Pièce justificative 1', Rails.root + 'spec/fixtures/files/piece_justificative_0.pdf')
|
||||
expect(page).to have_no_text('La pièce justificative n’est pas d’un type accepté')
|
||||
expect(page).to have_text('analyse antivirus en cours')
|
||||
expect(page).to have_text('piece_justificative_0.pdf')
|
||||
end
|
||||
|
||||
scenario 'add an invalid attachment on an old procedure where pj validation is disabled', js: true do
|
||||
log_in(user, old_procedure_with_disabled_pj_validation)
|
||||
fill_individual
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
RSpec.describe ContentTypeHelper, type: :helper do
|
||||
describe ".file_descriptions_from_content_types" do
|
||||
subject { file_descriptions_from_content_types(content_types) }
|
||||
|
||||
context "when a content-type does not exist" do
|
||||
let(:content_types) { ['invalid content type'] }
|
||||
it { is_expected.to eq("") }
|
||||
end
|
||||
|
||||
context "when a content type is valid" do
|
||||
let(:content_types) { ["application/msword"] }
|
||||
it { is_expected.to eq 'Word' }
|
||||
end
|
||||
|
||||
context "when two content types have the same description" do
|
||||
let(:content_types) { ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-excel"] }
|
||||
it { is_expected.to eq 'Excel' }
|
||||
end
|
||||
|
||||
context "when two content types have different descriptions" do
|
||||
let(:content_types) { ["application/msword", "application/vnd.ms-excel"] }
|
||||
it { is_expected.to eq 'Word, Excel' }
|
||||
end
|
||||
end
|
||||
|
||||
describe ".file_extensions_from_content_types" do
|
||||
subject { file_extensions_from_content_types(content_types) }
|
||||
|
||||
context "when a content-type does not exist" do
|
||||
let(:content_types) { ['invalid content type'] }
|
||||
it { is_expected.to eq("") }
|
||||
end
|
||||
|
||||
context "when a content type is valid" do
|
||||
let(:content_types) { ["application/msword"] }
|
||||
it { is_expected.to eq '.doc' }
|
||||
end
|
||||
|
||||
context "when two content types have different extensions" do
|
||||
let(:content_types) { ["application/vnd.ms-excel", "application/msword"] }
|
||||
it { is_expected.to eq '.doc, .xls' }
|
||||
end
|
||||
end
|
||||
end
|
|
@ -22,7 +22,6 @@ describe Champs::PieceJustificativeChamp do
|
|||
|
||||
context "by default" do
|
||||
it { is_expected.to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) }
|
||||
it { is_expected.to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_CONTENT_TYPES) }
|
||||
it { expect(champ_pj.type_de_champ.skip_pj_validation).to be_falsy }
|
||||
end
|
||||
|
||||
|
@ -30,7 +29,6 @@ describe Champs::PieceJustificativeChamp do
|
|||
before { champ_pj.type_de_champ.update(skip_pj_validation: true) }
|
||||
|
||||
it { is_expected.not_to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) }
|
||||
it { is_expected.not_to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_CONTENT_TYPES) }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue