From 1da1e67067cc386e3eae086f145b51fedf035168 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 5 Jul 2023 23:22:05 +0200 Subject: [PATCH 1/3] fix(mail): use default template when no custom template was found --- app/models/concerns/mail_template_concern.rb | 4 +-- spec/mailers/notification_mailer_spec.rb | 35 ++++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index a5035bbd8..20fc017d1 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -34,12 +34,12 @@ module MailTemplateConcern before_save :update_rich_body end - module ClassMethods + class_methods do def default_for_procedure(procedure) template_name = default_template_name_for_procedure(procedure) rich_body = ActionController::Base.render template: template_name trix_rich_body = rich_body.gsub(/(?)\n/, '') - new(subject: const_get(:DEFAULT_SUBJECT), rich_body: trix_rich_body, procedure: procedure) + new(subject: const_get(:DEFAULT_SUBJECT), body: trix_rich_body, rich_body: trix_rich_body, procedure: procedure) end def default_template_name_for_procedure(procedure) diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 074e5704a..ac14a9063 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -1,22 +1,35 @@ RSpec.describe NotificationMailer, type: :mailer do let(:administrateur) { create(:administrateur) } let(:user) { create(:user) } - let(:procedure) { create(:simple_procedure) } + let(:procedure) { create(:simple_procedure, :with_service) } describe 'send_en_construction_notification' do - let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) } - let(:email_template) { create(:initiated_mail, subject: 'Email subject', body: 'Your dossier was received. Thanks.') } - - before do - dossier.procedure.initiated_mail = email_template - end + let(:dossier) { create(:dossier, :en_construction, :with_individual, user: user, procedure: procedure) } subject(:mail) { described_class.send_en_construction_notification(dossier) } - it 'renders the template' do - expect(mail.subject).to eq('Email subject') - expect((mail.html_part || mail).body).to include('Your dossier was received') - expect(mail.attachments.first.filename).to eq("attestation-de-depot.pdf") + let(:body) { (mail.html_part || mail).body } + + context "without custom template" do + it 'renders default template' do + expect(mail.subject).to eq("Votre dossier nº #{dossier.id} a bien été déposé (#{procedure.libelle})") + expect(body).to include("Votre dossier nº #{dossier.id}") + expect(body).to include(procedure.service.nom) + expect(mail.attachments.first.filename).to eq("attestation-de-depot.pdf") + end + end + + context "with a custom template" do + let(:email_template) { create(:initiated_mail, subject: 'Email subject', body: 'Your dossier was received. Thanks.') } + before do + dossier.procedure.initiated_mail = email_template + end + + it 'renders the template' do + expect(mail.subject).to eq('Email subject') + expect(body).to include('Your dossier was received') + expect(mail.attachments.first.filename).to eq("attestation-de-depot.pdf") + end end end From edc790be8f44617e33bb72bb62004ab438a715b9 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 6 Jul 2023 10:56:01 +0200 Subject: [PATCH 2/3] test: remove false positive warning about expect.not_to raise(SpecificError) syntax --- spec/models/procedure_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 50f791e5c..6f0a076c8 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1019,7 +1019,7 @@ describe Procedure do procedure.reset_draft_revision! - expect { published_tdc.reload }.not_to raise_error(ActiveRecord::RecordNotFound) + expect { published_tdc.reload }.not_to raise_error expect { draft_tdc.reload }.to raise_error(ActiveRecord::RecordNotFound) end end From 219f71f64be1b5dc10c85f4daec706e511e7361b Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 6 Jul 2023 10:58:44 +0200 Subject: [PATCH 3/3] fix(mail): replace new line by space to avoid glued words --- app/models/concerns/mail_template_concern.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index 20fc017d1..9c957e906 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -38,7 +38,7 @@ module MailTemplateConcern def default_for_procedure(procedure) template_name = default_template_name_for_procedure(procedure) rich_body = ActionController::Base.render template: template_name - trix_rich_body = rich_body.gsub(/(?)\n/, '') + trix_rich_body = rich_body.gsub(/(?)\n/, ' ') new(subject: const_get(:DEFAULT_SUBJECT), body: trix_rich_body, rich_body: trix_rich_body, procedure: procedure) end