gestionnaire: move commentaire creation into a service

This commit is contained in:
Pierre de La Morinerie 2018-09-04 16:19:29 +00:00
parent d31cc04b23
commit 750e1e0c83
4 changed files with 79 additions and 34 deletions

View file

@ -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 '<p></p>'
# 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)

View file

@ -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 '<p></p>',
# 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

View file

@ -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("<p>avant\n<br />apres</p>")
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("<p>avant\n<br />apres</p>")
end
end
end

View file

@ -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 '<p>Contenu du message.</p>'
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