From 19a6391dc83906e8ce69444f1fe12a1f266364b0 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Tue, 11 Sep 2018 09:28:13 +0200 Subject: [PATCH 1/8] [#835] Use the create_avis_params method instead of the params hash --- app/controllers/new_gestionnaire/avis_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/new_gestionnaire/avis_controller.rb b/app/controllers/new_gestionnaire/avis_controller.rb index 58887ea83..4be6b09ad 100644 --- a/app/controllers/new_gestionnaire/avis_controller.rb +++ b/app/controllers/new_gestionnaire/avis_controller.rb @@ -60,7 +60,7 @@ module NewGestionnaire end def create_avis - confidentiel = avis.confidentiel || params[:avis][:confidentiel] + confidentiel = avis.confidentiel || create_avis_params[:confidentiel] @new_avis = Avis.new(create_avis_params.merge(claimant: current_gestionnaire, dossier: avis.dossier, confidentiel: confidentiel)) if @new_avis.save @@ -143,7 +143,7 @@ module NewGestionnaire end def create_avis_params - params.require(:avis).permit(:email, :introduction) + params.require(:avis).permit(:email, :introduction, :confidentiel) end end end From 9833564863df6f8f70b4397aac172f4684a93a91 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Tue, 11 Sep 2018 09:49:33 +0200 Subject: [PATCH 2/8] [#835] Add a missing validation to avis --- app/models/avis.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/avis.rb b/app/models/avis.rb index f0b9eb6ec..024323fea 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -6,6 +6,7 @@ class Avis < ApplicationRecord belongs_to :claimant, class_name: 'Gestionnaire' validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true + validates :claimant, presence: true before_validation -> { sanitize_email(:email) } before_create :try_to_assign_gestionnaire From 02fa30c83b03bae7d78ca5670aa5618ecd539a70 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 31 Oct 2018 18:51:09 +0100 Subject: [PATCH 3/8] [#835] Remove redundant values from autoload_paths (Level 1 subdirectories of app are already globbed by default rails config) --- config/application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 5514cf471..5b13f29bd 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,7 +24,7 @@ module TPS config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] config.i18n.available_locales = [:fr] - config.autoload_paths += ["#{config.root}/lib", "#{config.root}/app/validators", "#{config.root}/app/facades"] + config.autoload_paths += ["#{config.root}/lib"] config.assets.paths << Rails.root.join('app', 'assets', 'javascript') config.assets.paths << Rails.root.join('app', 'assets', 'fonts') config.assets.precompile += ['.woff'] From 6a43be4f3908bc9c532c26a60ef06387a738b1cc Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 31 Oct 2018 19:30:06 +0100 Subject: [PATCH 4/8] [#835] Extra paths deserve eagler loading in production too --- config/application.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 5b13f29bd..bb335c9a4 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,7 +24,8 @@ module TPS config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] config.i18n.available_locales = [:fr] - config.autoload_paths += ["#{config.root}/lib"] + config.paths.add "#{config.root}/lib", eager_load: true + config.assets.paths << Rails.root.join('app', 'assets', 'javascript') config.assets.paths << Rails.root.join('app', 'assets', 'fonts') config.assets.precompile += ['.woff'] From 31ca0552ab9b4ea86548dd312725f0383515212a Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 31 Oct 2018 14:56:01 +0100 Subject: [PATCH 5/8] [#835] Extract avis creation into concern --- .../concerns/create_avis_concern.rb | 26 +++++++++++++++++++ .../new_gestionnaire/avis_controller.rb | 13 +++------- .../new_gestionnaire/dossiers_controller.rb | 12 +++------ config/application.rb | 1 + 4 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 app/controllers/concerns/create_avis_concern.rb diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb new file mode 100644 index 000000000..c086ee726 --- /dev/null +++ b/app/controllers/concerns/create_avis_concern.rb @@ -0,0 +1,26 @@ +module CreateAvisConcern + extend ActiveSupport::Concern + + private + + def create_avis_from_params(dossier, confidentiel = false) + confidentiel ||= create_avis_params[:confidentiel] + avis = Avis.new(create_avis_params.merge(claimant: current_gestionnaire, dossier: dossier, confidentiel: confidentiel)) + + if avis.save + flash.notice = "Une demande d'avis a été envoyée à #{avis.email_to_display}" + + nil + else + flash.now.alert = @avis.errors.full_messages + + # When an error occurs, return the avis back to the controller + # to give the user a chance to correct and resubmit + avis + end + end + + def create_avis_params + params.require(:avis).permit(:email, :introduction, :confidentiel) + end +end diff --git a/app/controllers/new_gestionnaire/avis_controller.rb b/app/controllers/new_gestionnaire/avis_controller.rb index 4be6b09ad..e8267f944 100644 --- a/app/controllers/new_gestionnaire/avis_controller.rb +++ b/app/controllers/new_gestionnaire/avis_controller.rb @@ -1,5 +1,7 @@ module NewGestionnaire class AvisController < GestionnaireController + include CreateAvisConcern + before_action :authenticate_gestionnaire!, except: [:sign_up, :create_gestionnaire] before_action :redirect_if_no_sign_up_needed, only: [:sign_up] before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_gestionnaire] @@ -60,14 +62,11 @@ module NewGestionnaire end def create_avis - confidentiel = avis.confidentiel || create_avis_params[:confidentiel] - @new_avis = Avis.new(create_avis_params.merge(claimant: current_gestionnaire, dossier: avis.dossier, confidentiel: confidentiel)) + @new_avis = create_avis_from_params(avis.dossier, avis.confidentiel) - if @new_avis.save - flash.notice = "Une demande d'avis a été envoyée à #{@new_avis.email_to_display}" + if @new_avis.nil? redirect_to instruction_gestionnaire_avis_path(avis) else - flash.now.alert = @new_avis.errors.full_messages set_avis_and_dossier render :instruction end @@ -141,9 +140,5 @@ module NewGestionnaire def commentaire_params params.require(:commentaire).permit(:body, :file) end - - def create_avis_params - params.require(:avis).permit(:email, :introduction, :confidentiel) - end end end diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index ce56a4617..47aafc301 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -2,6 +2,7 @@ module NewGestionnaire class DossiersController < ProceduresController include ActionView::Helpers::NumberHelper include ActionView::Helpers::TextHelper + include CreateAvisConcern after_action :mark_demande_as_read, only: :show after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire] @@ -130,12 +131,11 @@ module NewGestionnaire end def create_avis - @avis = Avis.new(avis_params.merge(claimant: current_gestionnaire, dossier: dossier)) - if @avis.save - flash.notice = "Une demande d'avis a été envoyée à #{@avis.email_to_display}" + @avis = create_avis_from_params(dossier) + + if @avis.nil? redirect_to avis_gestionnaire_dossier_path(procedure, dossier) else - flash.now.alert = @avis.errors.full_messages @avis_seen_at = current_gestionnaire.follows.find_by(dossier: dossier)&.avis_seen_at render :avis end @@ -163,10 +163,6 @@ module NewGestionnaire params.require(:commentaire).permit(:body, :file) end - def avis_params - params.require(:avis).permit(:email, :introduction, :confidentiel) - end - def champs_private_params params.require(:dossier).permit(champs_private_attributes: [ :id, :primary_value, :secondary_value, :piece_justificative_file, :value, value: [], diff --git a/config/application.rb b/config/application.rb index bb335c9a4..bb00d8c27 100644 --- a/config/application.rb +++ b/config/application.rb @@ -25,6 +25,7 @@ module TPS config.i18n.available_locales = [:fr] config.paths.add "#{config.root}/lib", eager_load: true + config.paths.add "#{config.root}/app/controllers/concerns", eager_load: true config.assets.paths << Rails.root.join('app', 'assets', 'javascript') config.assets.paths << Rails.root.join('app', 'assets', 'fonts') From c7358a8f53b36346e94aed5b03af6bd229708fd4 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 31 Oct 2018 16:09:11 +0100 Subject: [PATCH 6/8] [Fix #835] Enable asking for multiple avis at the same time --- .../concerns/create_avis_concern.rb | 29 +++++++++++++++---- app/models/avis.rb | 4 +++ .../shared/avis/_form.html.haml | 6 ++-- .../new_gestionnaire/avis_controller_spec.rb | 2 +- .../dossiers_controller_spec.rb | 2 +- .../new_gestionnaire/gestionnaire_spec.rb | 2 +- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index c086ee726..d87a86a29 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -5,22 +5,39 @@ module CreateAvisConcern def create_avis_from_params(dossier, confidentiel = false) confidentiel ||= create_avis_params[:confidentiel] - avis = Avis.new(create_avis_params.merge(claimant: current_gestionnaire, dossier: dossier, confidentiel: confidentiel)) + emails = create_avis_params[:emails].split(',').map(&:strip) - if avis.save - flash.notice = "Une demande d'avis a été envoyée à #{avis.email_to_display}" + create_results = Avis.create( + emails.map do |email| + { + email: email, + introduction: create_avis_params[:introduction], + claimant: current_gestionnaire, + dossier: dossier, + confidentiel: confidentiel + } + end + ) + + if create_results.all?(&:persisted?) + sent_emails_addresses = create_results.map(&:email_to_display).join(", ") + flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses}" nil else - flash.now.alert = @avis.errors.full_messages + flash.now.alert = create_results + .map(&:errors) + .reject(&:empty?) + .map(&:full_messages) + .flatten # When an error occurs, return the avis back to the controller # to give the user a chance to correct and resubmit - avis + Avis.new(create_avis_params) end end def create_avis_params - params.require(:avis).permit(:email, :introduction, :confidentiel) + params.require(:avis).permit(:emails, :introduction, :confidentiel) end end diff --git a/app/models/avis.rb b/app/models/avis.rb index 024323fea..e755f946b 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -19,6 +19,10 @@ class Avis < ApplicationRecord scope :by_latest, -> { order(updated_at: :desc) } scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) } + # The form allows subtmitting avis requests to several emails at once, + # hence this virtual attribute. + attr_accessor :emails + def email_to_display gestionnaire&.email || email end diff --git a/app/views/new_gestionnaire/shared/avis/_form.html.haml b/app/views/new_gestionnaire/shared/avis/_form.html.haml index 29aefebfa..71c7442f4 100644 --- a/app/views/new_gestionnaire/shared/avis/_form.html.haml +++ b/app/views/new_gestionnaire/shared/avis/_form.html.haml @@ -1,9 +1,9 @@ %section.ask-avis - %h1.tab-title Inviter une personne à donner son avis - %p.avis-notice L'invité pourra consulter, donner un avis sur le dossier et contribuer au fil de messagerie, mais il ne pourra le modifier. + %h1.tab-title Inviter des personnes à donner leur avis + %p.avis-notice Les invités pourront consulter, donner un avis sur le dossier et contribuer au fil de messagerie, mais ils ne pourront pas le modifier. = form_for avis, url: url, html: { class: 'form' } do |f| - = f.email_field :email, placeholder: 'Adresse email', required: true + = f.text_field :emails, placeholder: 'Adresses email, séparées par des virgules', required: true = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true .flex.justify-between.align-baseline - if must_be_confidentiel diff --git a/spec/controllers/new_gestionnaire/avis_controller_spec.rb b/spec/controllers/new_gestionnaire/avis_controller_spec.rb index 08c0f980c..d73c8e686 100644 --- a/spec/controllers/new_gestionnaire/avis_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/avis_controller_spec.rb @@ -105,7 +105,7 @@ describe NewGestionnaire::AvisController, type: :controller do let(:created_avis) { Avis.last } before do - post :create_avis, params: { id: previous_avis.id, avis: { email: email, introduction: intro, confidentiel: asked_confidentiel } } + post :create_avis, params: { id: previous_avis.id, avis: { emails: email, introduction: intro, confidentiel: asked_confidentiel } } end context 'when an invalid email' do diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index bd074d314..f076b87fe 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -313,7 +313,7 @@ describe NewGestionnaire::DossiersController, type: :controller do post :create_avis, params: { procedure_id: procedure.id, dossier_id: dossier.id, - avis: { email: email, introduction: 'intro', confidentiel: true } + avis: { emails: email, introduction: 'intro', confidentiel: true } } end diff --git a/spec/features/new_gestionnaire/gestionnaire_spec.rb b/spec/features/new_gestionnaire/gestionnaire_spec.rb index 95cc0896d..7c1b95f2c 100644 --- a/spec/features/new_gestionnaire/gestionnaire_spec.rb +++ b/spec/features/new_gestionnaire/gestionnaire_spec.rb @@ -192,7 +192,7 @@ feature 'The gestionnaire part' do end def ask_confidential_avis(to, introduction) - fill_in 'avis_email', with: to + fill_in 'avis_emails', with: to fill_in 'avis_introduction', with: introduction select 'confidentiel', from: 'avis_confidentiel' click_on 'Demander un avis' From 8cf235f2fd9b776241d67f7b562b06539eb6be6f Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 31 Oct 2018 16:21:16 +0100 Subject: [PATCH 7/8] [#835] Enable html5 validation of multiple mails --- app/controllers/concerns/create_avis_concern.rb | 9 +++++++-- app/views/new_gestionnaire/shared/avis/_form.html.haml | 2 +- .../controllers/new_gestionnaire/avis_controller_spec.rb | 2 +- .../new_gestionnaire/dossiers_controller_spec.rb | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index d87a86a29..30614d142 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -5,7 +5,12 @@ module CreateAvisConcern def create_avis_from_params(dossier, confidentiel = false) confidentiel ||= create_avis_params[:confidentiel] - emails = create_avis_params[:emails].split(',').map(&:strip) + + # Because of a limitation of the email_field rails helper, + # the :emails parameter is a 1-element array. + # Hence the call to first + # https://github.com/rails/rails/issues/17225 + emails = create_avis_params[:emails].first.split(',').map(&:strip) create_results = Avis.create( emails.map do |email| @@ -38,6 +43,6 @@ module CreateAvisConcern end def create_avis_params - params.require(:avis).permit(:emails, :introduction, :confidentiel) + params.require(:avis).permit(:introduction, :confidentiel, emails: []) end end diff --git a/app/views/new_gestionnaire/shared/avis/_form.html.haml b/app/views/new_gestionnaire/shared/avis/_form.html.haml index 71c7442f4..609d871e3 100644 --- a/app/views/new_gestionnaire/shared/avis/_form.html.haml +++ b/app/views/new_gestionnaire/shared/avis/_form.html.haml @@ -3,7 +3,7 @@ %p.avis-notice Les invités pourront consulter, donner un avis sur le dossier et contribuer au fil de messagerie, mais ils ne pourront pas le modifier. = form_for avis, url: url, html: { class: 'form' } do |f| - = f.text_field :emails, placeholder: 'Adresses email, séparées par des virgules', required: true + = f.email_field :emails, placeholder: 'Adresses email, séparées par des virgules', required: true, multiple: true = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true .flex.justify-between.align-baseline - if must_be_confidentiel diff --git a/spec/controllers/new_gestionnaire/avis_controller_spec.rb b/spec/controllers/new_gestionnaire/avis_controller_spec.rb index d73c8e686..17ef5c026 100644 --- a/spec/controllers/new_gestionnaire/avis_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/avis_controller_spec.rb @@ -105,7 +105,7 @@ describe NewGestionnaire::AvisController, type: :controller do let(:created_avis) { Avis.last } before do - post :create_avis, params: { id: previous_avis.id, avis: { emails: email, introduction: intro, confidentiel: asked_confidentiel } } + post :create_avis, params: { id: previous_avis.id, avis: { emails: [email], introduction: intro, confidentiel: asked_confidentiel } } end context 'when an invalid email' do diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index f076b87fe..19b517bab 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -313,7 +313,7 @@ describe NewGestionnaire::DossiersController, type: :controller do post :create_avis, params: { procedure_id: procedure.id, dossier_id: dossier.id, - avis: { emails: email, introduction: 'intro', confidentiel: true } + avis: { emails: [email], introduction: 'intro', confidentiel: true } } end From d564eb12255767964796c2c1573d9776374d0ec9 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Wed, 31 Oct 2018 20:38:32 +0100 Subject: [PATCH 8/8] [#835] Report failed and successful invitations --- .../concerns/create_avis_concern.rb | 24 ++++++++++--------- .../new_gestionnaire/avis_controller_spec.rb | 23 ++++++++++++++---- .../dossiers_controller_spec.rb | 19 +++++++++++---- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index 30614d142..87143a3ce 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -24,21 +24,23 @@ module CreateAvisConcern end ) - if create_results.all?(&:persisted?) - sent_emails_addresses = create_results.map(&:email_to_display).join(", ") - flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses}" + persisted, failed = create_results.partition(&:persisted?) - nil - else - flash.now.alert = create_results - .map(&:errors) - .reject(&:empty?) - .map(&:full_messages) - .flatten + if persisted.any? + sent_emails_addresses = persisted.map(&:email_to_display).join(", ") + flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses}" + end + + if failed.any? + flash.now.alert = failed + .select { |avis| avis.errors.present? } + .map { |avis| "#{avis.email} : #{avis.errors.full_messages.join(', ')}" } # When an error occurs, return the avis back to the controller # to give the user a chance to correct and resubmit - Avis.new(create_avis_params) + Avis.new(create_avis_params.merge(emails: [failed.map(&:email).join(", ")])) + else + nil end end diff --git a/spec/controllers/new_gestionnaire/avis_controller_spec.rb b/spec/controllers/new_gestionnaire/avis_controller_spec.rb index 17ef5c026..655f805c5 100644 --- a/spec/controllers/new_gestionnaire/avis_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/avis_controller_spec.rb @@ -100,24 +100,37 @@ describe NewGestionnaire::AvisController, type: :controller do describe '#create_avis' do let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, gestionnaire: gestionnaire, confidentiel: previous_avis_confidentiel) } - let(:email) { 'a@b.com' } + let(:emails) { ['a@b.com'] } let(:intro) { 'introduction' } let(:created_avis) { Avis.last } + let!(:old_avis_count) { Avis.count } before do - post :create_avis, params: { id: previous_avis.id, avis: { emails: [email], introduction: intro, confidentiel: asked_confidentiel } } + post :create_avis, params: { id: previous_avis.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel } } end context 'when an invalid email' do let(:previous_avis_confidentiel) { false } let(:asked_confidentiel) { false } - let(:email) { "toto.fr" } + let(:emails) { ["toto.fr"] } it { expect(response).to render_template :instruction } - it { expect(flash.alert).to eq(["Email n'est pas valide"]) } + it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) } it { expect(Avis.last).to eq(previous_avis) } end + context 'with multiple emails' do + let(:asked_confidentiel) { false } + let(:previous_avis_confidentiel) { false } + let(:emails) { ["toto.fr,titi@titimail.com"] } + + it { expect(response).to render_template :instruction } + it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) } + it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à titi@titimail.com") } + it { expect(Avis.count).to eq(old_avis_count + 1) } + it { expect(created_avis.email).to eq("titi@titimail.com") } + end + context 'when the previous avis is public' do let(:previous_avis_confidentiel) { false } @@ -125,7 +138,7 @@ describe NewGestionnaire::AvisController, type: :controller do let(:asked_confidentiel) { false } it { expect(created_avis.confidentiel).to be(false) } - it { expect(created_avis.email).to eq(email) } + it { expect(created_avis.email).to eq(emails.last) } it { expect(created_avis.introduction).to eq(intro) } it { expect(created_avis.dossier).to eq(previous_avis.dossier) } it { expect(created_avis.claimant).to eq(gestionnaire) } diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 19b517bab..ac3dc3361 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -308,12 +308,13 @@ describe NewGestionnaire::DossiersController, type: :controller do describe "#create_avis" do let(:saved_avis) { dossier.avis.first } + let!(:old_avis_count) { Avis.count } subject do post :create_avis, params: { procedure_id: procedure.id, dossier_id: dossier.id, - avis: { emails: [email], introduction: 'intro', confidentiel: true } + avis: { emails: emails, introduction: 'intro', confidentiel: true } } end @@ -321,7 +322,7 @@ describe NewGestionnaire::DossiersController, type: :controller do subject end - let(:email) { 'email@a.com' } + let(:emails) { ['email@a.com'] } it { expect(saved_avis.email).to eq('email@a.com') } it { expect(saved_avis.introduction).to eq('intro') } @@ -331,12 +332,22 @@ describe NewGestionnaire::DossiersController, type: :controller do it { expect(response).to redirect_to(avis_gestionnaire_dossier_path(dossier.procedure, dossier)) } context "with an invalid email" do - let(:email) { 'emaila.com' } + let(:emails) { ['emaila.com'] } it { expect(response).to render_template :avis } - it { expect(flash.alert).to eq(["Email n'est pas valide"]) } + it { expect(flash.alert).to eq(["emaila.com : Email n'est pas valide"]) } it { expect { subject }.not_to change(Avis, :count) } end + + context 'with multiple emails' do + let(:emails) { ["toto.fr,titi@titimail.com"] } + + it { expect(response).to render_template :avis } + it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) } + it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à titi@titimail.com") } + it { expect(Avis.count).to eq(old_avis_count + 1) } + it { expect(saved_avis.email).to eq("titi@titimail.com") } + end end describe "#update_annotations" do