From 750e1e0c833d1cadac6f5b368f6bb26b0ac9ef68 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 4 Sep 2018 16:19:29 +0000 Subject: [PATCH] gestionnaire: move commentaire creation into a service --- .../new_gestionnaire/dossiers_controller.rb | 10 +--- app/services/commentaire_service.rb | 17 +++++++ .../dossiers_controller_spec.rb | 38 +++++---------- spec/services/commentaire_service_spec.rb | 48 +++++++++++++++++++ 4 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 app/services/commentaire_service.rb create mode 100644 spec/services/commentaire_service_spec.rb diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index c7e11d740..b23359872 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -113,15 +113,7 @@ module NewGestionnaire end def create_commentaire - commentaire_hash = commentaire_params.merge(email: current_gestionnaire.email, dossier: dossier) - - # avoid simple_format replacing '' by '

' - # and thus skipping the not empty constraint on commentaire's body - if commentaire_hash[:body].present? - commentaire_hash[:body] = simple_format(commentaire_hash[:body]) - end - - @commentaire = Commentaire.new(commentaire_hash) + @commentaire = CommentaireService.create(current_gestionnaire, dossier, commentaire_params) if @commentaire.save current_gestionnaire.follow(dossier) diff --git a/app/services/commentaire_service.rb b/app/services/commentaire_service.rb new file mode 100644 index 000000000..c70ddf815 --- /dev/null +++ b/app/services/commentaire_service.rb @@ -0,0 +1,17 @@ +class CommentaireService + class << self + def create(sender, dossier, params) + attributes = params.merge(email: sender.email, dossier: dossier) + + # If the user submits a empty message, simple_format will replace '' by '

', + # and thus bypass the not-empty constraint on commentaire's body. + # + # To avoid this, format the message only if a body is present in the first place. + if attributes[:body].present? + attributes[:body] = ActionController::Base.helpers.simple_format(attributes[:body]) + end + + Commentaire.new(attributes) + end + end +end diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 87a5eb02a..570a63cb9 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -269,7 +269,7 @@ describe NewGestionnaire::DossiersController, type: :controller do describe "#create_commentaire" do let(:saved_commentaire) { dossier.commentaires.first } let(:body) { "avant\napres" } - let(:file) { nil } + let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } let(:scan_result) { true } subject { @@ -287,35 +287,23 @@ describe NewGestionnaire::DossiersController, type: :controller do allow(ClamavService).to receive(:safe_file?).and_return(scan_result) end - it do - subject - - expect(saved_commentaire.body).to eq("

avant\n
apres

") - expect(saved_commentaire.email).to eq(gestionnaire.email) - expect(saved_commentaire.dossier).to eq(dossier) - expect(response).to redirect_to(messagerie_gestionnaire_dossier_path(dossier.procedure, dossier)) + it "creates a commentaire" do + expect { subject }.to change(Commentaire, :count).by(1) expect(gestionnaire.followed_dossiers).to include(dossier) - expect(saved_commentaire.file.present?).to eq(false) + + expect(response).to redirect_to(messagerie_gestionnaire_dossier_path(dossier.procedure, dossier)) + expect(flash.notice).to be_present end - it { expect { subject }.to change(Commentaire, :count).by(1) } + context "when the commentaire creation fails" do + let(:scan_result) { false } - context "without a body" do - let(:body) { nil } + it "renders the messagerie page with the invalid commentaire" do + expect { subject }.not_to change(Commentaire, :count) - it { expect { subject }.not_to change(Commentaire, :count) } - end - - context "with a file" do - let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } - - it { subject; expect(saved_commentaire.file.present?).to eq(true) } - it { expect { subject }.to change(Commentaire, :count).by(1) } - - context "and a virus" do - let(:scan_result) { false } - - it { expect { subject }.not_to change(Commentaire, :count) } + expect(response).to render_template :messagerie + expect(flash.alert).to be_present + expect(assigns(:commentaire).body).to eq("

avant\n
apres

") end end end diff --git a/spec/services/commentaire_service_spec.rb b/spec/services/commentaire_service_spec.rb new file mode 100644 index 000000000..7029d0c47 --- /dev/null +++ b/spec/services/commentaire_service_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe CommentaireService do + describe '.create' do + let(:dossier) { create :dossier } + let(:sender) { dossier.user } + let(:body) { 'Contenu du message.' } + let(:file) { nil } + let(:scan_result) { true } + + subject(:commentaire) { CommentaireService.create(sender, dossier, { body: body, file: file }) } + + before do + allow(ClamavService).to receive(:safe_file?).and_return(scan_result) + end + + it 'creates a new valid commentaire' do + expect(commentaire.email).to eq sender.email + expect(commentaire.dossier).to eq dossier + expect(commentaire.body).to eq '

Contenu du message.

' + expect(commentaire.file).to be_blank + expect(commentaire).to be_valid + end + + context 'when the body is empty' do + let(:body) { nil } + + it 'creates an invalid comment' do + expect(commentaire.body).to be nil + expect(commentaire.valid?).to be false + end + end + + context 'when it has a file' do + let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } + + it 'saves the attached file' do + expect(commentaire.file).to be_present + expect(commentaire).to be_valid + end + + context 'and a virus' do + let(:scan_result) { false } + it { expect(commentaire).not_to be_valid } + end + end + end +end