From 151e5744e645970120f296d68766fc56ce466786 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 21 Jan 2019 16:34:19 +0000 Subject: [PATCH 1/2] contact: rewrite specs to use finer-grained mocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before the specs mocked entire controller methods–which caused them not to be tested. Now we use finer mocks (or no mocks at all), to actually run more code. --- spec/controllers/support_controller_spec.rb | 67 ++++++++++++--------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/spec/controllers/support_controller_spec.rb b/spec/controllers/support_controller_spec.rb index 4852a3b4b..4ae584139 100644 --- a/spec/controllers/support_controller_spec.rb +++ b/spec/controllers/support_controller_spec.rb @@ -7,6 +7,7 @@ describe SupportController, type: :controller do before do sign_in user end + let(:user) { create(:user) } it 'should not have email field' do @@ -51,55 +52,67 @@ describe SupportController, type: :controller do end describe "send form" do - it 'should create conversation' do - expect(subject).not_to receive(:create_commentaire) - allow(subject).to receive(:create_conversation).and_return(true) + subject do + post :create, params: { subject: 'bonjour', text: 'un message' } + end - post :create, params: { - subject: 'bonjour', - text: 'un message' - } + it 'creates a conversation on HelpScout' do + expect_any_instance_of(Helpscout::FormAdapter).to receive(:send_form).and_return(true) + + expect { subject }.to change(Commentaire, :count).by(0) expect(flash[:notice]).to match('Votre message a été envoyé.') expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true) end - context "with dossier" do - let(:user) { dossier.user } + context 'when a drafted dossier is mentionned' do let(:dossier) { create(:dossier) } + let(:user) { dossier.user } - it 'should create conversation' do - expect(subject).not_to receive(:create_commentaire) - allow(subject).to receive(:create_conversation).and_return(true) - + subject do post :create, params: { dossier_id: dossier.id, type: Helpscout::FormAdapter::TYPE_INSTRUCTION, subject: 'bonjour', text: 'un message' } + end + + it 'creates a conversation on HelpScout' do + expect_any_instance_of(Helpscout::FormAdapter).to receive(:send_form).and_return(true) + + expect { subject }.to change(Commentaire, :count).by(0) expect(flash[:notice]).to match('Votre message a été envoyé.') expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true) end + end - context "en_construction" do - let(:dossier) { create(:dossier, :en_construction) } + context 'when a submitted dossier is mentionned' do + let(:dossier) { create(:dossier, :en_construction) } + let(:user) { dossier.user } - it 'should create commentaire' do - allow(subject).to receive(:create_commentaire).and_return(true) - expect(subject).not_to receive(:create_conversation) + subject do + post :create, params: { + dossier_id: dossier.id, + type: Helpscout::FormAdapter::TYPE_INSTRUCTION, + subject: 'bonjour', + text: 'un message' + } + end - post :create, params: { - dossier_id: dossier.id, - type: Helpscout::FormAdapter::TYPE_INSTRUCTION, - subject: 'bonjour', - text: 'un message' - } + it 'posts the message to the dossier messagerie' do + expect_any_instance_of(Helpscout::FormAdapter).not_to receive(:send_form) - expect(flash[:notice]).to match('Votre message a été envoyé sur la messagerie de votre dossier.') - expect(response).to redirect_to messagerie_dossier_path(dossier) - end + expect { subject }.to change(Commentaire, :count).by(1) + + expect(Commentaire.last.email).to eq(user.email) + expect(Commentaire.last.dossier).to eq(dossier) + expect(Commentaire.last.body).to include('[bonjour]') + expect(Commentaire.last.body).to include('un message') + + expect(flash[:notice]).to match('Votre message a été envoyé sur la messagerie de votre dossier.') + expect(response).to redirect_to messagerie_dossier_path(dossier) end end end From 022cf338531d0f0167f0e35e8ade8f78ca7f43d6 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 21 Jan 2019 17:36:36 +0100 Subject: [PATCH 2/2] contact: fix sending a message to the messagerie Fix #3313 --- app/controllers/support_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/support_controller.rb b/app/controllers/support_controller.rb index 034ba0a0e..b907e20a5 100644 --- a/app/controllers/support_controller.rb +++ b/app/controllers/support_controller.rb @@ -60,11 +60,11 @@ class SupportController < ApplicationController end def create_commentaire - params = { + attributes = { file: params[:file], body: "[#{params[:subject]}]

#{params[:text]}" } - commentaire = CommentaireService.build_with_email(email, dossier, params) + commentaire = CommentaireService.build_with_email(email, dossier, attributes) commentaire.save! end