2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2019-07-09 17:08:27 +02:00
|
|
|
|
RSpec.describe UserMailer, type: :mailer do
|
2023-10-18 07:10:26 +02:00
|
|
|
|
let(:user) { create(:user) }
|
2019-07-09 17:08:27 +02:00
|
|
|
|
|
|
|
|
|
describe '.new_account_warning' do
|
|
|
|
|
subject { described_class.new_account_warning(user) }
|
|
|
|
|
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'sends email to the correct user with expected body content and link' do
|
|
|
|
|
expect(subject.to).to eq([user.email])
|
|
|
|
|
expect(subject.body).to include(user.email)
|
|
|
|
|
expect(subject.body).to have_link('J’ai oublié mon mot de passe')
|
|
|
|
|
end
|
2020-02-11 18:17:09 +01:00
|
|
|
|
|
|
|
|
|
context 'when a procedure is provided' do
|
|
|
|
|
let(:procedure) { build(:procedure) }
|
|
|
|
|
|
|
|
|
|
subject { described_class.new_account_warning(user, procedure) }
|
|
|
|
|
|
2024-03-26 18:58:13 +01:00
|
|
|
|
it { expect(subject.body).to have_link("Commencer la démarche « #{procedure.libelle} »", href: commencer_sign_in_url(path: procedure.path, host: ENV.fetch("APP_HOST_LEGACY"))) }
|
|
|
|
|
|
|
|
|
|
context "when user has preferred domain" do
|
|
|
|
|
let(:user) { create(:user, preferred_domain: :demarches_gouv_fr) }
|
|
|
|
|
|
|
|
|
|
it do
|
2024-04-18 18:16:22 +02:00
|
|
|
|
expect(subject.body).to have_link("Commencer la démarche « #{procedure.libelle} »", href: commencer_sign_in_url(path: procedure.path, host: "demarches.gouv.fr"))
|
2024-03-26 18:58:13 +01:00
|
|
|
|
expect(header_value("From", subject)).to include("@demarches.gouv.fr")
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-02-11 18:17:09 +01:00
|
|
|
|
end
|
2023-01-23 14:56:05 +01:00
|
|
|
|
|
|
|
|
|
context 'without SafeMailer configured' do
|
|
|
|
|
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(nil) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with SafeMailer configured' do
|
|
|
|
|
let(:forced_delivery_method) { :kikoo }
|
|
|
|
|
before { allow(SafeMailer).to receive(:forced_delivery_method).and_return(forced_delivery_method) }
|
|
|
|
|
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(forced_delivery_method.to_s) }
|
|
|
|
|
end
|
2023-10-18 07:10:26 +02:00
|
|
|
|
|
|
|
|
|
context 'when perform_later is called' do
|
|
|
|
|
it 'enqueues email in default queue for high priority delivery' do
|
|
|
|
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-07-09 17:08:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-10-26 13:36:14 +02:00
|
|
|
|
describe '.ask_for_merge' do
|
2019-07-09 17:08:27 +02:00
|
|
|
|
let(:requested_email) { 'new@exemple.fr' }
|
|
|
|
|
|
2021-10-26 13:36:14 +02:00
|
|
|
|
subject { described_class.ask_for_merge(user, requested_email) }
|
2019-07-09 17:08:27 +02:00
|
|
|
|
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'correctly addresses the email and includes the requested email in the body' do
|
|
|
|
|
expect(subject.to).to eq([requested_email])
|
|
|
|
|
expect(subject.body).to include(requested_email)
|
|
|
|
|
end
|
2023-01-23 14:56:05 +01:00
|
|
|
|
|
|
|
|
|
context 'without SafeMailer configured' do
|
|
|
|
|
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(nil) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with SafeMailer configured' do
|
|
|
|
|
let(:forced_delivery_method) { :kikoo }
|
|
|
|
|
before { allow(SafeMailer).to receive(:forced_delivery_method).and_return(forced_delivery_method) }
|
|
|
|
|
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(forced_delivery_method.to_s) }
|
|
|
|
|
end
|
2023-10-18 07:10:26 +02:00
|
|
|
|
|
|
|
|
|
context 'when perform_later is called' do
|
|
|
|
|
it 'enqueues email in default queue for high priority delivery' do
|
|
|
|
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-07-09 17:08:27 +02:00
|
|
|
|
end
|
2021-11-17 16:21:55 +01:00
|
|
|
|
|
|
|
|
|
describe '.france_connect_merge_confirmation' do
|
2024-03-26 17:23:42 +01:00
|
|
|
|
let(:email) { 'new@exemple.fr' }
|
2021-11-17 16:21:55 +01:00
|
|
|
|
let(:code) { '123456' }
|
|
|
|
|
|
2021-11-23 10:54:59 +01:00
|
|
|
|
subject { described_class.france_connect_merge_confirmation(email, code, 15.minutes.from_now) }
|
2021-11-17 16:21:55 +01:00
|
|
|
|
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'sends to correct email with merge link' do
|
|
|
|
|
expect(subject.to).to eq([email])
|
2024-07-31 17:50:14 +02:00
|
|
|
|
expect(subject.body).to include(france_connect_particulier_merge_using_email_link_url(email_merge_token: code))
|
2024-03-26 17:23:42 +01:00
|
|
|
|
end
|
2023-01-23 14:56:05 +01:00
|
|
|
|
|
|
|
|
|
context 'without SafeMailer configured' do
|
|
|
|
|
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(nil) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with SafeMailer configured' do
|
|
|
|
|
let(:forced_delivery_method) { :kikoo }
|
|
|
|
|
before { allow(SafeMailer).to receive(:forced_delivery_method).and_return(forced_delivery_method) }
|
|
|
|
|
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(forced_delivery_method.to_s) }
|
|
|
|
|
end
|
2023-10-18 07:10:26 +02:00
|
|
|
|
|
|
|
|
|
context 'when perform_later is called' do
|
|
|
|
|
it 'enqueues email in default queue for high priority delivery' do
|
|
|
|
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-11-17 16:21:55 +01:00
|
|
|
|
end
|
2022-06-23 13:51:58 +02:00
|
|
|
|
|
2024-07-04 12:47:53 +02:00
|
|
|
|
describe '#custom_confirmation_instructions' do
|
|
|
|
|
let(:user) { create(:user, email: 'user@example.com') }
|
|
|
|
|
let(:token) { 'confirmation_token_123' }
|
|
|
|
|
let(:mail) { UserMailer.custom_confirmation_instructions(user, token) }
|
|
|
|
|
|
|
|
|
|
it 'renders the headers' do
|
|
|
|
|
expect(mail.subject).to eq('Confirmez votre email')
|
|
|
|
|
expect(mail.to).to eq([user.email])
|
|
|
|
|
expect(mail.from).to eq(['contact@demarches-simplifiees.fr'])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders the body' do
|
|
|
|
|
expect(mail.body.encoded).to match(user.email)
|
|
|
|
|
expect(mail.body.encoded).to match(token)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'assigns @user' do
|
|
|
|
|
expect(mail.body.encoded).to match(user.email)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'assigns @token' do
|
|
|
|
|
expect(mail.body.encoded).to include(token)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-23 13:51:58 +02:00
|
|
|
|
describe '.send_archive' do
|
|
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
|
let(:archive) { create(:archive) }
|
|
|
|
|
subject { described_class.send_archive(role, procedure, archive) }
|
|
|
|
|
|
|
|
|
|
context 'instructeur' do
|
|
|
|
|
let(:role) { create(:instructeur) }
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'sends email with correct links to instructeur' do
|
|
|
|
|
expect(subject.to).to eq([role.user.email])
|
|
|
|
|
expect(subject.body).to have_link('Consulter mes archives', href: instructeur_archives_url(procedure, host: ENV.fetch("APP_HOST_LEGACY")))
|
|
|
|
|
expect(subject.body).to have_link("#{procedure.id} − #{procedure.libelle}", href: instructeur_procedure_url(procedure, host: ENV.fetch("APP_HOST_LEGACY")))
|
|
|
|
|
end
|
2022-06-23 13:51:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-03-26 17:23:42 +01:00
|
|
|
|
context 'administrateur' do
|
2024-06-01 22:50:48 +02:00
|
|
|
|
let(:role) { administrateurs(:default_admin) }
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'sends email with correct links to administrateur' do
|
|
|
|
|
expect(subject.to).to eq([role.user.email])
|
|
|
|
|
expect(subject.body).to have_link('Consulter mes archives', href: admin_procedure_archives_url(procedure, host: ENV.fetch("APP_HOST_LEGACY")))
|
|
|
|
|
expect(subject.body).to have_link("#{procedure.id} − #{procedure.libelle}", href: admin_procedure_url(procedure, host: ENV.fetch("APP_HOST_LEGACY")))
|
|
|
|
|
end
|
2022-06-23 13:51:58 +02:00
|
|
|
|
end
|
2023-10-18 07:10:26 +02:00
|
|
|
|
|
|
|
|
|
context 'when perform_later is called' do
|
2024-06-01 22:50:48 +02:00
|
|
|
|
let(:role) { administrateurs(:default_admin) }
|
2024-10-10 12:20:03 +02:00
|
|
|
|
let(:custom_queue) { 'default' }
|
|
|
|
|
it 'enqueues email is custom queue for non critical delivery' do
|
2023-10-18 07:10:26 +02:00
|
|
|
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-06-23 13:51:58 +02:00
|
|
|
|
end
|
2023-11-03 10:39:41 +01:00
|
|
|
|
|
|
|
|
|
describe '.notify_inactive_close_to_deletion' do
|
|
|
|
|
subject { described_class.notify_inactive_close_to_deletion(user) }
|
|
|
|
|
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'alerts user of inactivity with correct recipient and message' do
|
|
|
|
|
expect(subject.to).to eq([user.email])
|
|
|
|
|
expect(subject.body).to include("Cela fait plus de deux ans que vous ne vous êtes pas connecté à #{APPLICATION_NAME}.")
|
|
|
|
|
end
|
2023-11-03 10:39:41 +01:00
|
|
|
|
|
|
|
|
|
context 'when perform_later is called' do
|
2024-10-10 12:20:03 +02:00
|
|
|
|
let(:custom_queue) { 'default' }
|
|
|
|
|
it 'enqueues email is custom queue for non critical delivery' do
|
2023-11-03 10:39:41 +01:00
|
|
|
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-02-15 11:09:00 +01:00
|
|
|
|
|
|
|
|
|
describe '.notify_after_closing' do
|
|
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
|
let(:content) { "Bonjour,\r\nsaut de ligne" }
|
|
|
|
|
subject { described_class.notify_after_closing(user, content, procedure) }
|
|
|
|
|
|
2024-03-26 17:23:42 +01:00
|
|
|
|
it 'notifies user about procedure closing with detailed message' do
|
|
|
|
|
expect(subject.to).to eq([user.email])
|
2024-10-10 12:20:03 +02:00
|
|
|
|
expect(subject.body).to include("Clôture d'une démarche sur #{APPLICATION_NAME}")
|
2024-03-26 17:23:42 +01:00
|
|
|
|
expect(subject.body).to include("Bonjour,\r\n<br />saut de ligne")
|
|
|
|
|
end
|
2024-02-15 11:09:00 +01:00
|
|
|
|
|
|
|
|
|
context 'when perform_later is called' do
|
2024-10-10 12:20:03 +02:00
|
|
|
|
let(:custom_queue) { 'default' }
|
|
|
|
|
it 'enqueues email is custom queue for non critical delivery' do
|
2024-02-15 11:09:00 +01:00
|
|
|
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-05-29 12:24:05 +02:00
|
|
|
|
|
|
|
|
|
describe '.invite_instructeur' do
|
|
|
|
|
subject { described_class.invite_instructeur(user, "reset_token") }
|
|
|
|
|
|
|
|
|
|
it { expect(subject['BYPASS_UNVERIFIED_MAIL_PROTECTION']).to be_present }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '.invite_gestionnaire' do
|
|
|
|
|
let(:groupe_gestionnaire) { create(:groupe_gestionnaire) }
|
|
|
|
|
subject { described_class.invite_gestionnaire(user, "reset_token", groupe_gestionnaire) }
|
|
|
|
|
|
|
|
|
|
it { expect(subject['BYPASS_UNVERIFIED_MAIL_PROTECTION']).to be_present }
|
|
|
|
|
end
|
2019-07-09 17:08:27 +02:00
|
|
|
|
end
|