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