2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2015-12-15 11:02:07 +01:00
RSpec . describe NotificationMailer , type : :mailer do
2024-06-01 22:50:48 +02:00
let ( :administrateur ) { administrateurs ( :default_admin ) }
2019-07-22 16:10:18 +02:00
let ( :user ) { create ( :user ) }
2023-07-05 23:22:05 +02:00
let ( :procedure ) { create ( :simple_procedure , :with_service ) }
2019-07-22 16:10:18 +02:00
2023-12-04 17:28:37 +01:00
describe 'send_notification_for_tiers' do
let ( :dossier_for_tiers ) { create ( :dossier , :en_construction , :for_tiers_with_notification , procedure : create ( :simple_procedure ) ) }
subject { described_class . send_notification_for_tiers ( dossier_for_tiers ) }
2024-03-26 17:23:42 +01:00
it 'verifies email subject, recipient, and body content for updated dossier by mandataire' do
expect ( subject . subject ) . to include ( " Votre dossier rempli par le mandataire #{ dossier_for_tiers . mandataire_first_name } #{ dossier_for_tiers . mandataire_last_name } a été mis à jour " )
expect ( subject . to ) . to eq ( [ dossier_for_tiers . individual . email ] )
expect ( subject . body ) . to include ( " a été déposé le " )
expect ( subject . body ) . to include ( " Pour en savoir plus, veuillez vous rapprocher de \r \n <a href= \" mailto: #{ dossier_for_tiers . user . email } \" > #{ dossier_for_tiers . user . email } </a>. " )
end
2023-12-20 10:49:09 +01:00
end
describe 'send_notification_for_tiers for repasser_en_instruction' do
let ( :dossier_for_tiers ) { create ( :dossier , :accepte , :for_tiers_with_notification , procedure : create ( :simple_procedure ) ) }
subject { described_class . send_notification_for_tiers ( dossier_for_tiers , repasser_en_instruction : true ) }
2024-03-26 17:23:42 +01:00
it 'verifies email subject, recipient, and body content for dossier re-examination notification' do
expect ( subject . subject ) . to include ( " Votre dossier rempli par le mandataire #{ dossier_for_tiers . mandataire_first_name } #{ dossier_for_tiers . mandataire_last_name } a été mis à jour " )
expect ( subject . to ) . to eq ( [ dossier_for_tiers . individual . email ] )
expect ( subject . body ) . to include ( " va être réexaminé, la précédente décision sur ce dossier est caduque. " )
expect ( subject . body ) . to include ( " Pour en savoir plus, veuillez vous rapprocher de \r \n <a href= \" mailto: #{ dossier_for_tiers . user . email } \" > #{ dossier_for_tiers . user . email } </a>. " )
end
2023-12-04 17:28:37 +01:00
end
2024-04-03 11:07:40 +02:00
describe 'send_notification_for_tiers with accuse lecture procedure' do
let ( :dossier_for_tiers ) { create ( :dossier , :accepte , :for_tiers_with_notification , procedure : create ( :procedure , :accuse_lecture , :for_individual ) ) }
subject { described_class . send_notification_for_tiers ( dossier_for_tiers ) }
it { expect ( subject . subject ) . to include ( " Votre dossier rempli par le mandataire #{ dossier_for_tiers . mandataire_first_name } #{ dossier_for_tiers . mandataire_last_name } a été mis à jour " ) }
it { expect ( subject . to ) . to eq ( [ dossier_for_tiers . individual . email ] ) }
it { expect ( subject . body ) . to include ( " a été traité le " ) }
it { expect ( subject . body ) . to include ( " Pour en savoir plus, veuillez vous rapprocher de \r \n <a href= \" mailto: #{ dossier_for_tiers . user . email } \" > #{ dossier_for_tiers . user . email } </a>. " ) }
end
2024-03-21 15:03:20 +01:00
describe 'send_accuse_lecture_notification' do
2024-03-27 17:28:54 +01:00
let ( :dossier ) { create ( :dossier , :accepte , procedure : create ( :procedure , :accuse_lecture ) ) }
2024-03-21 15:03:20 +01:00
subject { described_class . send_accuse_lecture_notification ( dossier ) }
it { expect ( subject . subject ) . to include ( " La décision a été rendue pour votre démarche #{ dossier . procedure . libelle } " ) }
it { expect ( subject . body ) . to include ( " Pour en connaitre la nature, veuillez vous connecter à votre compte \r \n <a href= \" #{ dossier_url ( dossier ) } \" >demarches-simplifiees.fr</a> " ) }
end
2022-05-10 16:23:22 +02:00
describe 'send_en_construction_notification' do
2023-11-16 15:33:36 +01:00
let ( :dossier ) { create ( :dossier , :en_construction , :with_individual , user : user , procedure : ) }
2022-05-10 16:23:22 +02:00
subject ( :mail ) { described_class . send_en_construction_notification ( dossier ) }
2023-07-05 23:22:05 +02:00
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
2023-11-16 15:33:36 +01:00
let ( :email_template ) { create ( :initiated_mail , subject : 'Email subject' , body : 'Your dossier was received. Thanks.' , procedure : ) }
2023-07-05 23:22:05 +02:00
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
2022-05-10 16:23:22 +02:00
end
end
2021-04-29 19:10:22 +02:00
describe 'send_en_instruction_notification' do
2023-11-16 15:33:36 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual , :with_service , user : user , procedure : ) }
let ( :email_template ) { create ( :received_mail , subject : 'Email subject' , body : 'Your dossier was processed. Thanks.' , procedure : ) }
2019-07-22 16:10:18 +02:00
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
2024-03-26 17:23:42 +01:00
it 'renders the template with subject and body' do
2019-07-22 16:10:18 +02:00
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
2024-03-26 17:23:42 +01:00
it 'renders the actions with links to dossier and messagerie' do
2024-03-26 18:37:25 +01:00
expect ( mail . body ) . to have_link ( 'Consulter mon dossier' , href : dossier_url ( dossier , host : ENV . fetch ( " APP_HOST_LEGACY " ) ) )
expect ( mail . body ) . to have_link ( 'J’ ai une question' , href : messagerie_dossier_url ( dossier , host : ENV . fetch ( " APP_HOST_LEGACY " ) ) )
2019-07-22 15:21:05 +02:00
end
2019-07-22 16:10:18 +02:00
context 'when the template body contains tags' do
2023-11-16 15:33:36 +01:00
let ( :email_template ) { create ( :received_mail , subject : 'Email subject' , body : 'Hello --nom--, your dossier --lien dossier-- was processed.' , procedure : ) }
2018-09-05 15:25:47 +02:00
2024-03-26 17:23:42 +01:00
it 'replaces value tags with the proper value and renders links correctly' do
expect ( mail . body ) . to include ( dossier . individual . nom )
expect ( mail . body ) . to have_link ( href : dossier_url ( dossier , host : ENV . fetch ( " APP_HOST_LEGACY " ) ) )
2024-03-26 18:37:25 +01:00
end
context " when user has preferred domain " do
let ( :user ) { create ( :user , preferred_domain : :demarches_gouv_fr ) }
2024-03-26 17:23:42 +01:00
it 'adjusts links and sender email for user preferred domain' do
2024-04-18 18:16:22 +02:00
expect ( mail . body ) . to have_link ( href : dossier_url ( dossier , host : 'demarches.gouv.fr' ) )
2024-03-26 18:37:25 +01:00
expect ( header_value ( " From " , mail ) ) . to include ( " @demarches.gouv.fr " )
end
2019-07-22 16:10:18 +02:00
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
2023-11-16 15:33:36 +01:00
let ( :email_template ) { create ( :received_mail , body : 'Your <b>dossier</b> was processed. <iframe src="#">Foo</iframe>' , procedure : ) }
2017-10-13 18:35:12 +02:00
2024-03-26 17:23:42 +01:00
it 'allows basic formatting tags but sanitizes sensitive content' do
2019-07-22 16:10:18 +02:00
expect ( mail . body ) . to include ( '<b>dossier</b>' )
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
2023-02-09 17:39:56 +01:00
describe 'subject length' do
let ( :procedure ) { create ( :simple_procedure , libelle : " My super long title " + ( " xo " * 100 ) ) }
2023-11-16 15:33:36 +01:00
let ( :dossier ) { create ( :dossier , :accepte , :with_individual , :with_service , user : user , procedure : ) }
let ( :email_template ) { create ( :closed_mail , subject : , body : 'Your dossier was accepted. Thanks.' , procedure : ) }
2023-02-09 17:39:56 +01:00
before do
dossier . procedure . closed_mail = email_template
end
subject ( :mail ) { described_class . send_accepte_notification ( dossier ) }
2024-03-26 17:23:42 +01:00
context " when the subject is too long " do
2023-02-09 17:39:56 +01:00
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
2024-03-26 17:23:42 +01:00
context " when the subject should fallback to default " do
2023-02-09 17:49:43 +01:00
let ( :subject ) { " " }
2024-03-26 17:23:42 +01:00
it 'provides a default subject within the length limit including procedure title beginning' do
expect ( mail . subject ) . to match ( / ^Votre dossier .+ a été accepté \ (My super long title / )
expect ( mail . subject . length ) . to be < = 100
end
2023-02-09 17:49:43 +01:00
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 " ) }
2023-11-16 15:33:36 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual , :with_service , user : user , procedure : ) }
let ( :email_template ) { create ( :received_mail , subject : , body : 'Your dossier was accepted. Thanks.' , procedure : ) }
2023-03-31 16:58:41 +02:00
before do
2023-07-07 14:06:10 +02:00
dossier . procedure . received_mail = email_template
2023-03-31 16:58:41 +02:00
end
2023-11-29 12:59:55 +01:00
subject ( :mail ) { described_class . send_en_instruction_notification ( dossier ) }
2023-03-31 16:58:41 +02:00
2024-03-26 17:23:42 +01:00
context " when the subject has a special character that should not be escaped " do
2023-03-31 16:58:41 +02:00
let ( :subject ) { '--libellé démarche--' }
2024-03-26 17:23:42 +01:00
it 'includes the apostrophe without escaping it' do
expect ( mail . subject ) . to eq ( " Mon titre avec l'apostrophe " )
end
2023-03-31 16:58:41 +02:00
end
end
2015-12-15 11:02:07 +01:00
end