diff --git a/app/controllers/new_administrateur/groupe_instructeurs_controller.rb b/app/controllers/new_administrateur/groupe_instructeurs_controller.rb index 23086104f..bf80c8995 100644 --- a/app/controllers/new_administrateur/groupe_instructeurs_controller.rb +++ b/app/controllers/new_administrateur/groupe_instructeurs_controller.rb @@ -3,6 +3,10 @@ module NewAdministrateur include ActiveSupport::NumberHelper ITEMS_PER_PAGE = 25 CSV_MAX_SIZE = 1.megabytes + CSV_ACCEPTED_CONTENT_TYPES = [ + "text/csv", + "application/vnd.ms-excel" + ] def index @procedure = procedure @@ -161,7 +165,7 @@ module NewAdministrateur end 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" redirect_to admin_procedure_groupe_instructeurs_path(procedure) @@ -171,7 +175,6 @@ module NewAdministrateur else file = group_csv_file.read - # find the original encoding to avoid encoding errors 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) .map { |r| r.to_h.slice('groupe', 'email') } diff --git a/spec/controllers/new_administrateur/groupe_instructeurs_controller_spec.rb b/spec/controllers/new_administrateur/groupe_instructeurs_controller_spec.rb index bc1bcf04e..ab3590211 100644 --- a/spec/controllers/new_administrateur/groupe_instructeurs_controller_spec.rb +++ b/spec/controllers/new_administrateur/groupe_instructeurs_controller_spec.rb @@ -354,7 +354,7 @@ describe NewAdministrateur::GroupeInstructeursController, type: :controller do post :import, params: { procedure_id: procedure.id, group_csv_file: csv_file } 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') } 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") } 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 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") } 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') } before { subject }