Merge pull request #6344 from betagouv/main

2021-07-15-01
This commit is contained in:
Kara Diaby 2021-07-15 17:01:55 +02:00 committed by GitHub
commit 068389dde8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View file

@ -3,6 +3,10 @@ module NewAdministrateur
include ActiveSupport::NumberHelper include ActiveSupport::NumberHelper
ITEMS_PER_PAGE = 25 ITEMS_PER_PAGE = 25
CSV_MAX_SIZE = 1.megabytes CSV_MAX_SIZE = 1.megabytes
CSV_ACCEPTED_CONTENT_TYPES = [
"text/csv",
"application/vnd.ms-excel"
]
def index def index
@procedure = procedure @procedure = procedure
@ -161,7 +165,7 @@ module NewAdministrateur
end end
def import def import
if (group_csv_file.content_type != "text/csv") && (marcel_content_type != "text/csv") if !CSV_ACCEPTED_CONTENT_TYPES.include?(group_csv_file.content_type) && !CSV_ACCEPTED_CONTENT_TYPES.include?(marcel_content_type)
flash[:alert] = "Importation impossible : veuillez importer un fichier CSV" flash[:alert] = "Importation impossible : veuillez importer un fichier CSV"
redirect_to admin_procedure_groupe_instructeurs_path(procedure) redirect_to admin_procedure_groupe_instructeurs_path(procedure)
@ -171,7 +175,6 @@ module NewAdministrateur
else else
file = group_csv_file.read file = group_csv_file.read
# find the original encoding to avoid encoding errors
base_encoding = CharlockHolmes::EncodingDetector.detect(file) base_encoding = CharlockHolmes::EncodingDetector.detect(file)
groupes_emails = CSV.new(file.encode("UTF-8", base_encoding[:encoding], invalid: :replace, replace: ""), headers: true, header_converters: :downcase) groupes_emails = CSV.new(file.encode("UTF-8", base_encoding[:encoding], invalid: :replace, replace: ""), headers: true, header_converters: :downcase)
.map { |r| r.to_h.slice('groupe', 'email') } .map { |r| r.to_h.slice('groupe', 'email') }

View file

@ -354,7 +354,7 @@ describe NewAdministrateur::GroupeInstructeursController, type: :controller do
post :import, params: { procedure_id: procedure.id, group_csv_file: csv_file } post :import, params: { procedure_id: procedure.id, group_csv_file: csv_file }
end end
context 'when the csv file is less than 1 mo' do context 'when the csv file is less than 1 mo and content type text/csv' do
let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe-instructeur.csv', 'text/csv') } let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe-instructeur.csv', 'text/csv') }
before { subject } before { subject }
@ -365,6 +365,15 @@ describe NewAdministrateur::GroupeInstructeursController, type: :controller do
it { expect(flash.alert).to eq("Import terminé. Cependant les emails suivants ne sont pas pris en compte: kara") } it { expect(flash.alert).to eq("Import terminé. Cependant les emails suivants ne sont pas pris en compte: kara") }
end end
context 'when the file content type is application/vnd.ms-excel' do
let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe_avec_caracteres_speciaux.csv', "application/vnd.ms-excel") }
before { subject }
it { expect(flash.notice).to be_present }
it { expect(flash.notice).to eq("La liste des instructeurs a été importée avec succès") }
end
context 'when the content of csv contains special characters' do context 'when the content of csv contains special characters' do
let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe_avec_caracteres_speciaux.csv', 'text/csv') } let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe_avec_caracteres_speciaux.csv', 'text/csv') }
@ -387,7 +396,7 @@ describe NewAdministrateur::GroupeInstructeursController, type: :controller do
it { expect(flash.alert).to eq("Importation impossible : la poids du fichier est supérieur à 1 Mo") } it { expect(flash.alert).to eq("Importation impossible : la poids du fichier est supérieur à 1 Mo") }
end end
context 'when the file is not a csv' do context 'when the file content type is not accepted' do
let(:csv_file) { fixture_file_upload('spec/fixtures/files/french-flag.gif', 'image/gif') } let(:csv_file) { fixture_file_upload('spec/fixtures/files/french-flag.gif', 'image/gif') }
before { subject } before { subject }