2015-12-15 11:02:07 +01:00
|
|
|
|
RSpec.describe NotificationMailer, type: :mailer do
|
2021-04-29 19:10:22 +02:00
|
|
|
|
let(:administrateur) { create(:administrateur) }
|
2019-07-22 16:10:18 +02:00
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
let(:procedure) { create(:simple_procedure) }
|
|
|
|
|
|
2022-05-10 16:23:22 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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')
|
2022-05-18 14:27:26 +02:00
|
|
|
|
expect(mail.attachments.first.filename).to eq("attestation-de-depot.pdf")
|
2022-05-10 16:23:22 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-29 19:10:22 +02:00
|
|
|
|
describe 'send_en_instruction_notification' do
|
|
|
|
|
let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) }
|
2019-07-22 16:10:18 +02:00
|
|
|
|
let(:email_template) { create(:received_mail, subject: 'Email subject', body: 'Your dossier was processed. Thanks.') }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
dossier.procedure.received_mail = email_template
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-29 19:10:22 +02:00
|
|
|
|
subject(:mail) { described_class.send_en_instruction_notification(dossier) }
|
2019-07-22 16:10:18 +02:00
|
|
|
|
|
|
|
|
|
it 'creates a commentaire in the messagerie' do
|
2017-10-13 18:35:12 +02:00
|
|
|
|
expect { subject.deliver_now }.to change { Commentaire.count }.by(1)
|
2021-04-29 19:10:22 +02:00
|
|
|
|
expect(subject.perform_deliveries).to be_truthy
|
2017-10-13 18:35:12 +02:00
|
|
|
|
|
|
|
|
|
commentaire = Commentaire.last
|
2018-01-08 17:26:13 +01:00
|
|
|
|
expect(commentaire.body).to include(email_template.subject_for_dossier(dossier), email_template.body_for_dossier(dossier))
|
2017-10-13 18:35:12 +02:00
|
|
|
|
expect(commentaire.dossier).to eq(dossier)
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-22 16:10:18 +02:00
|
|
|
|
it 'renders the template' do
|
|
|
|
|
expect(mail.subject).to eq('Email subject')
|
|
|
|
|
expect(mail.body).to include('Your dossier was processed')
|
|
|
|
|
expect(mail.body).to have_link('messagerie')
|
2018-05-25 23:08:47 +02:00
|
|
|
|
end
|
2017-03-05 20:20:29 +01:00
|
|
|
|
|
2019-07-22 15:21:05 +02:00
|
|
|
|
it 'renders the actions' do
|
|
|
|
|
expect(mail.body).to have_link('Consulter mon dossier', href: dossier_url(dossier))
|
|
|
|
|
expect(mail.body).to have_link('J’ai une question', href: messagerie_dossier_url(dossier))
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-22 16:10:18 +02:00
|
|
|
|
context 'when the template body contains tags' do
|
|
|
|
|
let(:email_template) { create(:received_mail, subject: 'Email subject', body: 'Hello --nom--, your dossier --lien dossier-- was processed.') }
|
2018-09-05 15:25:47 +02:00
|
|
|
|
|
2019-07-22 16:10:18 +02:00
|
|
|
|
it 'replaces value tags with the proper value' do
|
|
|
|
|
expect(mail.body).to have_content(dossier.individual.nom)
|
|
|
|
|
end
|
2017-05-11 18:07:37 +02:00
|
|
|
|
|
2019-07-22 16:10:18 +02:00
|
|
|
|
it 'replaces link tags with a clickable link' do
|
|
|
|
|
expect(mail.body).to have_link(dossier_url(dossier))
|
|
|
|
|
end
|
2017-05-11 18:07:37 +02:00
|
|
|
|
end
|
2017-10-13 18:35:12 +02:00
|
|
|
|
|
2019-07-22 16:10:18 +02:00
|
|
|
|
context 'when the template body contains HTML' do
|
|
|
|
|
let(:email_template) { create(:received_mail, body: 'Your <b>dossier</b> was processed. <iframe src="#">Foo</iframe>') }
|
2017-10-13 18:35:12 +02:00
|
|
|
|
|
2019-07-22 16:10:18 +02:00
|
|
|
|
it 'allows basic formatting tags' do
|
|
|
|
|
expect(mail.body).to include('<b>dossier</b>')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'sanitizes sensitive content' do
|
|
|
|
|
expect(mail.body).not_to include('iframe')
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-10 13:29:06 +02:00
|
|
|
|
|
|
|
|
|
it 'sends the mail from a no-reply address' do
|
|
|
|
|
expect(subject.from.first).to eq(Mail::Address.new(NO_REPLY_EMAIL).address)
|
|
|
|
|
end
|
2017-03-05 20:20:29 +01:00
|
|
|
|
end
|
2021-04-29 19:10:22 +02:00
|
|
|
|
|
|
|
|
|
describe 'send_accepte_notification' do
|
|
|
|
|
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
|
|
|
|
|
let(:email_template) { create(:closed_mail, subject: 'Email subject', body: 'Your dossier was accepted. Thanks.') }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
dossier.procedure.closed_mail = email_template
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
subject(:mail) { described_class.send_accepte_notification(dossier) }
|
|
|
|
|
|
|
|
|
|
context 'when dossier user is deleted' do
|
|
|
|
|
before do
|
2022-11-14 16:45:32 +01:00
|
|
|
|
dossier.user.delete_and_keep_track_dossiers_also_delete_user(administrateur)
|
2021-04-29 19:10:22 +02:00
|
|
|
|
dossier.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'should not send notification' do
|
|
|
|
|
expect { subject.deliver_now }.not_to change { Commentaire.count }
|
|
|
|
|
expect(subject.perform_deliveries).to be_falsey
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-02-09 17:39:56 +01:00
|
|
|
|
|
|
|
|
|
describe 'subject length' do
|
|
|
|
|
let(:procedure) { create(:simple_procedure, libelle: "My super long title " + ("xo " * 100)) }
|
|
|
|
|
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
|
|
|
|
|
let(:email_template) { create(:closed_mail, subject:, body: 'Your dossier was accepted. Thanks.') }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
dossier.procedure.closed_mail = email_template
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
subject(:mail) { described_class.send_accepte_notification(dossier) }
|
|
|
|
|
|
|
|
|
|
context "subject is too long" do
|
|
|
|
|
let(:subject) { 'Un long libellé --libellé démarche--' }
|
|
|
|
|
it { expect(mail.subject.length).to be <= 100 }
|
|
|
|
|
end
|
2023-02-09 17:49:43 +01:00
|
|
|
|
|
|
|
|
|
context "subject should fallback to default" do
|
|
|
|
|
let(:subject) { "" }
|
|
|
|
|
it { expect(mail.subject).to match(/^Votre dossier .+ a été accepté \(My super long title/) }
|
|
|
|
|
it { expect(mail.subject.length).to be <= 100 }
|
|
|
|
|
end
|
2023-02-09 17:39:56 +01:00
|
|
|
|
end
|
2023-03-31 16:58:41 +02:00
|
|
|
|
|
|
|
|
|
describe 'subject with apostrophe' do
|
|
|
|
|
let(:procedure) { create(:simple_procedure, libelle: "Mon titre avec l'apostrophe") }
|
|
|
|
|
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
|
|
|
|
|
let(:email_template) { create(:closed_mail, subject:, body: 'Your dossier was accepted. Thanks.') }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
dossier.procedure.closed_mail = email_template
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
subject(:mail) { described_class.send_accepte_notification(dossier) }
|
|
|
|
|
|
|
|
|
|
context "subject has a special character" do
|
|
|
|
|
let(:subject) { '--libellé démarche--' }
|
|
|
|
|
it { expect(mail.subject).to eq("Mon titre avec l'apostrophe") }
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-12-15 11:02:07 +01:00
|
|
|
|
end
|