From 05bd1a7ddabdc2d7b46c9bb2c089df0c592943ca Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 14 Jan 2025 10:29:24 +0000 Subject: [PATCH] Use `EmailDeliveryJob` This updates the email delivery to use the new `EmailDeliveryJob` added in the previous commit rather than using `ActionMailer`. By switching to this job we get automatic retry behaviour on GOV.UK Notify server errors, and tracking of message IDs. --- .../concerns/consent_form_mailer_concern.rb | 24 +++- .../concerns/triage_mailer_concern.rb | 12 +- .../concerns/vaccination_mailer_concern.rb | 29 ++-- app/models/consent_notification.rb | 21 +-- app/models/session_notification.rb | 38 ++--- .../consent_form_mailer_concern_spec.rb | 35 ++--- .../concerns/triage_mailer_concern_spec.rb | 52 +++---- .../vaccination_mailer_concern_spec.rb | 50 ++----- .../delete_vaccination_record_spec.rb | 2 +- spec/features/edit_vaccination_record_spec.rb | 2 +- .../scheduled_consent_requests_spec.rb | 2 +- spec/models/consent_notification_spec.rb | 135 +++++++----------- spec/models/session_notification_spec.rb | 70 +++------ spec/spec_helper.rb | 2 +- spec/support/email_expectations.rb | 16 +-- spec/support/matchers/have_delivered_email.rb | 23 +++ 16 files changed, 215 insertions(+), 298 deletions(-) create mode 100644 spec/support/matchers/have_delivered_email.rb diff --git a/app/controllers/concerns/consent_form_mailer_concern.rb b/app/controllers/concerns/consent_form_mailer_concern.rb index e975aa701..4f537b28b 100644 --- a/app/controllers/concerns/consent_form_mailer_concern.rb +++ b/app/controllers/concerns/consent_form_mailer_concern.rb @@ -4,12 +4,16 @@ module ConsentFormMailerConcern extend ActiveSupport::Concern def send_consent_form_confirmation(consent_form) - mailer = ConsentMailer.with(consent_form:) - if consent_form.contact_injection? - mailer.confirmation_injection.deliver_later + EmailDeliveryJob.perform_later( + :consent_confirmation_injection, + consent_form: + ) elsif consent_form.consent_refused? - mailer.confirmation_refused.deliver_later + EmailDeliveryJob.perform_later( + :consent_confirmation_refused, + consent_form: + ) if consent_form.parent_phone_receive_updates SMSDeliveryJob.perform_later( @@ -18,13 +22,19 @@ def send_consent_form_confirmation(consent_form) ) end elsif consent_form.needs_triage? - mailer.confirmation_triage.deliver_later + EmailDeliveryJob.perform_later( + :consent_confirmation_triage, + consent_form: + ) elsif consent_form.actual_upcoming_session == consent_form.organisation.generic_clinic_session || consent_form.actual_upcoming_session&.completed? - mailer.confirmation_clinic.deliver_later + EmailDeliveryJob.perform_later( + :consent_confirmation_clinic, + consent_form: + ) else - mailer.confirmation_given.deliver_later + EmailDeliveryJob.perform_later(:consent_confirmation_given, consent_form:) if consent_form.parent_phone_receive_updates SMSDeliveryJob.perform_later(:consent_confirmation_given, consent_form:) diff --git a/app/controllers/concerns/triage_mailer_concern.rb b/app/controllers/concerns/triage_mailer_concern.rb index ca94cc205..a979481d0 100644 --- a/app/controllers/concerns/triage_mailer_concern.rb +++ b/app/controllers/concerns/triage_mailer_concern.rb @@ -13,21 +13,21 @@ def send_triage_confirmation(patient_session, consent) params = { consent:, session:, sent_by: current_user } if vaccination_will_happen?(patient_session, consent) - TriageMailer.with(params).vaccination_will_happen.deliver_later + EmailDeliveryJob.perform_later(:triage_vaccination_will_happen, **params) elsif vaccination_wont_happen?(patient_session, consent) - TriageMailer.with(params).vaccination_wont_happen.deliver_later + EmailDeliveryJob.perform_later(:triage_vaccination_wont_happen, **params) elsif vaccination_at_clinic?(patient_session, consent) - TriageMailer.with(params).vaccination_at_clinic.deliver_later + EmailDeliveryJob.perform_later(:triage_vaccination_at_clinic, **params) elsif consent.triage_needed? - ConsentMailer.with(params).confirmation_triage.deliver_later + EmailDeliveryJob.perform_later(:consent_confirmation_triage, **params) elsif consent.response_refused? - ConsentMailer.with(params).confirmation_refused.deliver_later + EmailDeliveryJob.perform_later(:consent_confirmation_refused, **params) if consent.parent.phone_receive_updates SMSDeliveryJob.perform_later(:consent_confirmation_refused, **params) end elsif consent.response_given? - ConsentMailer.with(params).confirmation_given.deliver_later + EmailDeliveryJob.perform_later(:consent_confirmation_given, **params) if consent.parent.phone_receive_updates SMSDeliveryJob.perform_later(:consent_confirmation_given, **params) diff --git a/app/controllers/concerns/vaccination_mailer_concern.rb b/app/controllers/concerns/vaccination_mailer_concern.rb index 52998c6c0..33fca0cce 100644 --- a/app/controllers/concerns/vaccination_mailer_concern.rb +++ b/app/controllers/concerns/vaccination_mailer_concern.rb @@ -7,24 +7,20 @@ def send_vaccination_confirmation(vaccination_record) parents = parents_for_vaccination_mailer(vaccination_record) return if parents.empty? - mailer_action = + template_name = if vaccination_record.administered? - :confirmation_administered + :vaccination_confirmation_administered else - :confirmation_not_administered + :vaccination_confirmation_not_administered end - text_template_name = :"vaccination_#{mailer_action}" - parents.each do |parent| params = { parent:, vaccination_record:, sent_by: try(:current_user) } - if parent.email.present? - VaccinationMailer.with(params).public_send(mailer_action).deliver_later - end + EmailDeliveryJob.perform_later(template_name, **params) - if parent.phone.present? && parent.phone_receive_updates - SMSDeliveryJob.perform_later(text_template_name, **params) + if parent.phone_receive_updates + SMSDeliveryJob.perform_later(template_name, **params) end end end @@ -33,12 +29,15 @@ def send_vaccination_deletion(vaccination_record) parents = parents_for_vaccination_mailer(vaccination_record) return if parents.empty? - parents.each do |parent| - params = { parent:, vaccination_record:, sent_by: try(:current_user) } + sent_by = try(:current_user) - if parent.email.present? - VaccinationMailer.with(params).deleted.deliver_later - end + parents.each do |parent| + EmailDeliveryJob.perform_later( + :vaccination_deleted, + parent:, + vaccination_record:, + sent_by: + ) end end diff --git a/app/models/consent_notification.rb b/app/models/consent_notification.rb index 63f3678a9..e669e5801 100644 --- a/app/models/consent_notification.rb +++ b/app/models/consent_notification.rb @@ -69,24 +69,25 @@ def self.create_and_send!( is_school = session.location.school? - mailer_action = (is_school ? :"school_#{type}" : :"clinic_#{type}") + mail_template = + :"consent_#{(is_school ? :"school_#{type}" : :"clinic_#{type}")}" text_template = if type == :request - :"consent_#{mailer_action}" + mail_template elsif is_school :consent_school_reminder end parents.each do |parent| - unless parent.email.nil? - ConsentMailer - .with(parent:, patient:, programme:, session:, sent_by: current_user) - .send(mailer_action) - .deliver_later - end - - next if parent.phone.nil? + EmailDeliveryJob.perform_later( + mail_template, + parent:, + patient:, + programme:, + session:, + sent_by: current_user + ) SMSDeliveryJob.perform_later( text_template, diff --git a/app/models/session_notification.rb b/app/models/session_notification.rb index eead190fd..c652d0e03 100644 --- a/app/models/session_notification.rb +++ b/app/models/session_notification.rb @@ -79,42 +79,20 @@ def self.create_and_send!( if type == :school_reminder contacts.each do |consent| - if consent.parent.email.present? - SessionMailer - .with(consent:, patient_session:, sent_by: current_user) - .school_reminder - .deliver_later - end + params = { consent:, patient_session:, sent_by: current_user } - unless consent.parent.phone.present? && - consent.parent.phone_receive_updates - next - end + EmailDeliveryJob.perform_later(:session_school_reminder, **params) + + next unless consent.parent.phone_receive_updates - SMSDeliveryJob.perform_later( - :session_school_reminder, - consent:, - patient_session:, - sent_by: current_user - ) + SMSDeliveryJob.perform_later(:session_school_reminder, **params) end else contacts.each do |parent| - if parent.email.present? - SessionMailer - .with(parent:, patient_session:, sent_by: current_user) - .send(type) - .deliver_later - end - - next if parent.phone.blank? + params = { parent:, patient_session:, sent_by: current_user } - SMSDeliveryJob.perform_later( - :"session_#{type}", - parent:, - patient_session:, - sent_by: current_user - ) + EmailDeliveryJob.perform_later(:"session_#{type}", **params) + SMSDeliveryJob.perform_later(:"session_#{type}", **params) end end end diff --git a/spec/controllers/concerns/consent_form_mailer_concern_spec.rb b/spec/controllers/concerns/consent_form_mailer_concern_spec.rb index fba4f0811..61f4ac249 100644 --- a/spec/controllers/concerns/consent_form_mailer_concern_spec.rb +++ b/spec/controllers/concerns/consent_form_mailer_concern_spec.rb @@ -11,10 +11,9 @@ end it "sends a confirmation email" do - expect { send_consent_form_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_given - ).with(params: { consent_form: }, args: []) + expect { send_consent_form_confirmation }.to have_delivered_email( + :consent_confirmation_given + ).with(consent_form:) end it "sends a consent given text" do @@ -27,10 +26,9 @@ before { consent_form.contact_injection = true } it "sends an injection confirmation email" do - expect { send_consent_form_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_injection - ).with(params: { consent_form: }, args: []) + expect { send_consent_form_confirmation }.to have_delivered_email( + :consent_confirmation_injection + ).with(consent_form:) end it "doesn't send a text" do @@ -42,10 +40,9 @@ before { consent_form.response = :refused } it "sends an confirmation refused email" do - expect { send_consent_form_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_refused - ).with(params: { consent_form: }, args: []) + expect { send_consent_form_confirmation }.to have_delivered_email( + :consent_confirmation_refused + ).with(consent_form:) end it "sends a consent refused text" do @@ -59,10 +56,9 @@ before { consent_form.health_answers.last.response = "yes" } it "sends an confirmation needs triage email" do - expect { send_consent_form_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_triage - ).with(params: { consent_form: }, args: []) + expect { send_consent_form_confirmation }.to have_delivered_email( + :consent_confirmation_triage + ).with(consent_form:) end it "doesn't send a text" do @@ -85,10 +81,9 @@ end it "sends an confirmation needs triage email" do - expect { send_consent_form_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_clinic - ).with(params: { consent_form: }, args: []) + expect { send_consent_form_confirmation }.to have_delivered_email( + :consent_confirmation_clinic + ).with(consent_form:) end it "doesn't send a text" do diff --git a/spec/controllers/concerns/triage_mailer_concern_spec.rb b/spec/controllers/concerns/triage_mailer_concern_spec.rb index 993b73b21..5268d7725 100644 --- a/spec/controllers/concerns/triage_mailer_concern_spec.rb +++ b/spec/controllers/concerns/triage_mailer_concern_spec.rb @@ -32,10 +32,9 @@ def initialize(current_user:) end it "sends an email saying triage was needed and vaccination will happen" do - expect { send_triage_confirmation }.to have_enqueued_mail( - TriageMailer, - :vaccination_will_happen - ).with(params: { consent:, session:, sent_by: current_user }, args: []) + expect { send_triage_confirmation }.to have_delivered_email( + :triage_vaccination_will_happen + ).with(consent:, session:, sent_by: current_user) end it "doesn't send a text message" do @@ -49,10 +48,9 @@ def initialize(current_user:) end it "sends an email saying triage was needed but vaccination won't happen" do - expect { send_triage_confirmation }.to have_enqueued_mail( - TriageMailer, - :vaccination_wont_happen - ).with(params: { consent:, session:, sent_by: current_user }, args: []) + expect { send_triage_confirmation }.to have_delivered_email( + :triage_vaccination_wont_happen + ).with(consent:, session:, sent_by: current_user) end it "doesn't send a text message" do @@ -64,10 +62,9 @@ def initialize(current_user:) let(:patient_session) { create(:patient_session, :delay_vaccination) } it "sends an email saying triage was needed but vaccination won't happen" do - expect { send_triage_confirmation }.to have_enqueued_mail( - TriageMailer, - :vaccination_at_clinic - ).with(params: { consent:, session:, sent_by: current_user }, args: []) + expect { send_triage_confirmation }.to have_delivered_email( + :triage_vaccination_at_clinic + ).with(consent:, session:, sent_by: current_user) end it "doesn't send a text message" do @@ -81,10 +78,9 @@ def initialize(current_user:) end it "sends an email saying vaccination will happen" do - expect { send_triage_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_given - ).with(params: { consent:, session:, sent_by: current_user }, args: []) + expect { send_triage_confirmation }.to have_delivered_email( + :consent_confirmation_given + ).with(consent:, session:, sent_by: current_user) end it "sends a text message" do @@ -100,10 +96,9 @@ def initialize(current_user:) end it "sends an email saying triage is required" do - expect { send_triage_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_triage - ).with(params: { consent:, session:, sent_by: current_user }, args: []) + expect { send_triage_confirmation }.to have_delivered_email( + :consent_confirmation_triage + ).with(consent:, session:, sent_by: current_user) end it "doesn't send a text message" do @@ -115,7 +110,7 @@ def initialize(current_user:) let(:patient_session) { create(:patient_session, :consent_not_provided) } it "doesn't send an email" do - expect { send_triage_confirmation }.not_to have_enqueued_email + expect { send_triage_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -136,7 +131,7 @@ def initialize(current_user:) end it "doesn't send an email" do - expect { send_triage_confirmation }.not_to have_enqueued_email + expect { send_triage_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -148,10 +143,9 @@ def initialize(current_user:) let(:patient_session) { create(:patient_session, :consent_refused) } it "sends an email confirming they've refused consent" do - expect { send_triage_confirmation }.to have_enqueued_mail( - ConsentMailer, - :confirmation_refused - ).with(params: { consent:, session:, sent_by: current_user }, args: []) + expect { send_triage_confirmation }.to have_delivered_email( + :consent_confirmation_refused + ).with(consent:, session:, sent_by: current_user) end it "sends a text message" do @@ -166,7 +160,7 @@ def initialize(current_user:) let(:patient_session) { create(:patient_session, patient:) } it "doesn't send an email" do - expect { send_triage_confirmation }.not_to have_enqueued_email + expect { send_triage_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -179,7 +173,7 @@ def initialize(current_user:) let(:patient_session) { create(:patient_session, patient:) } it "doesn't send an email" do - expect { send_triage_confirmation }.not_to have_enqueued_email + expect { send_triage_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -192,7 +186,7 @@ def initialize(current_user:) let(:patient_session) { create(:patient_session, patient:) } it "doesn't send an email" do - expect { send_triage_confirmation }.not_to have_enqueued_email + expect { send_triage_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do diff --git a/spec/controllers/concerns/vaccination_mailer_concern_spec.rb b/spec/controllers/concerns/vaccination_mailer_concern_spec.rb index 1be37222b..4bdaac322 100644 --- a/spec/controllers/concerns/vaccination_mailer_concern_spec.rb +++ b/spec/controllers/concerns/vaccination_mailer_concern_spec.rb @@ -37,17 +37,9 @@ def initialize(current_user:) before { create(:consent, :given, patient:, programme:) } it "sends an email" do - expect { send_vaccination_confirmation }.to have_enqueued_mail( - VaccinationMailer, - :confirmation_administered - ).with( - params: { - parent:, - vaccination_record:, - sent_by: current_user - }, - args: [] - ) + expect { send_vaccination_confirmation }.to have_delivered_email( + :vaccination_confirmation_administered + ).with(parent:, vaccination_record:, sent_by: current_user) end it "sends a text message" do @@ -70,17 +62,9 @@ def initialize(current_user:) end it "sends an email" do - expect { send_vaccination_confirmation }.to have_enqueued_mail( - VaccinationMailer, - :confirmation_not_administered - ).with( - params: { - parent:, - vaccination_record:, - sent_by: current_user - }, - args: [] - ) + expect { send_vaccination_confirmation }.to have_delivered_email( + :vaccination_confirmation_not_administered + ).with(parent:, vaccination_record:, sent_by: current_user) end it "sends a text message" do @@ -108,17 +92,9 @@ def initialize(current_user:) end it "sends an email" do - expect { send_vaccination_confirmation }.to have_enqueued_mail( - VaccinationMailer, - :confirmation_administered - ).with( - params: { - parent:, - vaccination_record:, - sent_by: current_user - }, - args: [] - ) + expect { send_vaccination_confirmation }.to have_delivered_email( + :vaccination_confirmation_administered + ).with(parent:, vaccination_record:, sent_by: current_user) end it "sends a text message" do @@ -132,7 +108,7 @@ def initialize(current_user:) before { create(:consent, :given, :self_consent, patient:, programme:) } it "doesn't send an email" do - expect { send_vaccination_confirmation }.not_to have_enqueued_mail + expect { send_vaccination_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -145,7 +121,7 @@ def initialize(current_user:) let(:patient) { create(:patient, :deceased) } it "doesn't send an email" do - expect { send_vaccination_confirmation }.not_to have_enqueued_email + expect { send_vaccination_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -157,7 +133,7 @@ def initialize(current_user:) let(:patient) { create(:patient, :invalidated) } it "doesn't send an email" do - expect { send_vaccination_confirmation }.not_to have_enqueued_email + expect { send_vaccination_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do @@ -169,7 +145,7 @@ def initialize(current_user:) let(:patient) { create(:patient, :restricted) } it "doesn't send an email" do - expect { send_vaccination_confirmation }.not_to have_enqueued_email + expect { send_vaccination_confirmation }.not_to have_delivered_email end it "doesn't send a text message" do diff --git a/spec/features/delete_vaccination_record_spec.rb b/spec/features/delete_vaccination_record_spec.rb index 78bf5a87e..721990961 100644 --- a/spec/features/delete_vaccination_record_spec.rb +++ b/spec/features/delete_vaccination_record_spec.rb @@ -220,7 +220,7 @@ def and_the_parent_receives_an_email end def and_the_parent_doesnt_receives_an_email - expect(sent_emails).to be_empty + expect(email_deliveries).to be_empty end def then_i_cant_click_on_delete_vaccination_record diff --git a/spec/features/edit_vaccination_record_spec.rb b/spec/features/edit_vaccination_record_spec.rb index 35c96c2bf..0946f886a 100644 --- a/spec/features/edit_vaccination_record_spec.rb +++ b/spec/features/edit_vaccination_record_spec.rb @@ -489,7 +489,7 @@ def when_i_click_on_save_changes end def then_the_parent_doesnt_receive_an_email - expect(sent_emails).to be_empty + expect(email_deliveries).to be_empty end alias_method :and_the_parent_doesnt_receive_an_email, diff --git a/spec/features/scheduled_consent_requests_spec.rb b/spec/features/scheduled_consent_requests_spec.rb index 5a4ad6b46..28d1bce69 100644 --- a/spec/features/scheduled_consent_requests_spec.rb +++ b/spec/features/scheduled_consent_requests_spec.rb @@ -109,7 +109,7 @@ def when_1_more_day_passes def then_no_consent_requests_have_been_sent SchoolConsentRequestsJob.perform_now - expect(sent_emails).to be_empty + expect(email_deliveries).to be_empty expect(sms_deliveries).to be_empty end diff --git a/spec/models/consent_notification_spec.rb b/spec/models/consent_notification_spec.rb index 2a59e8dc4..4eff4a20e 100644 --- a/spec/models/consent_notification_spec.rb +++ b/spec/models/consent_notification_spec.rb @@ -73,27 +73,20 @@ end it "enqueues an email per parent" do - expect { create_and_send! }.to have_enqueued_mail( - ConsentMailer, - :school_request + expect { create_and_send! }.to have_delivered_email( + :consent_school_request ).with( - params: { - parent: parents.first, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] - ).and have_enqueued_mail(ConsentMailer, :school_request).with( - params: { - parent: parents.second, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] + parent: parents.first, + patient:, + programme:, + session:, + sent_by: current_user + ).and have_delivered_email(:consent_school_request).with( + parent: parents.second, + patient:, + programme:, + session:, + sent_by: current_user ) end @@ -143,27 +136,20 @@ end it "enqueues an email per parent" do - expect { create_and_send! }.to have_enqueued_mail( - ConsentMailer, - :clinic_request + expect { create_and_send! }.to have_delivered_email( + :consent_clinic_request ).with( - params: { - parent: parents.first, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] - ).and have_enqueued_mail(ConsentMailer, :clinic_request).with( - params: { - parent: parents.second, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] + parent: parents.first, + patient:, + programme:, + session:, + sent_by: current_user + ).and have_delivered_email(:consent_clinic_request).with( + parent: parents.second, + patient:, + programme:, + session:, + sent_by: current_user ) end @@ -212,27 +198,20 @@ end it "enqueues an email per parent" do - expect { create_and_send! }.to have_enqueued_mail( - ConsentMailer, - :school_initial_reminder + expect { create_and_send! }.to have_delivered_email( + :consent_school_initial_reminder ).with( - params: { - parent: parents.first, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] - ).and have_enqueued_mail(ConsentMailer, :school_initial_reminder).with( - params: { - parent: parents.second, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] + parent: parents.first, + patient:, + programme:, + session:, + sent_by: current_user + ).and have_delivered_email(:consent_school_initial_reminder).with( + parent: parents.second, + patient:, + programme:, + session:, + sent_by: current_user ) end @@ -281,30 +260,20 @@ end it "enqueues an email per parent" do - expect { create_and_send! }.to have_enqueued_mail( - ConsentMailer, - :school_subsequent_reminder + expect { create_and_send! }.to have_delivered_email( + :consent_school_subsequent_reminder ).with( - params: { - parent: parents.first, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] - ).and have_enqueued_mail( - ConsentMailer, - :school_subsequent_reminder - ).with( - params: { - parent: parents.second, - patient:, - programme:, - session:, - sent_by: current_user - }, - args: [] + parent: parents.first, + patient:, + programme:, + session:, + sent_by: current_user + ).and have_delivered_email(:consent_school_subsequent_reminder).with( + parent: parents.second, + patient:, + programme:, + session:, + sent_by: current_user ) end diff --git a/spec/models/session_notification_spec.rb b/spec/models/session_notification_spec.rb index 0b24f9ca7..e17f6fd9d 100644 --- a/spec/models/session_notification_spec.rb +++ b/spec/models/session_notification_spec.rb @@ -69,17 +69,9 @@ end it "enqueues an email per parent who gave consent" do - expect { create_and_send! }.to have_enqueued_mail( - SessionMailer, - :school_reminder - ).with( - params: { - consent:, - patient_session:, - sent_by: current_user - }, - args: [] - ) + expect { create_and_send! }.to have_delivered_email( + :session_school_reminder + ).with(consent:, patient_session:, sent_by: current_user) end it "enqueues a text per parent" do @@ -111,26 +103,16 @@ end it "enqueues an email per parent" do - expect { create_and_send! }.to have_enqueued_mail( - SessionMailer, - :clinic_initial_invitation + expect { create_and_send! }.to have_delivered_email( + :session_clinic_initial_invitation ).with( - params: { - parent: parents.first, - patient_session:, - sent_by: current_user - }, - args: [] - ).and have_enqueued_mail( - SessionMailer, - :clinic_initial_invitation - ).with( - params: { - parent: parents.second, - patient_session:, - sent_by: current_user - }, - args: [] + parent: parents.first, + patient_session:, + sent_by: current_user + ).and have_delivered_email(:session_clinic_initial_invitation).with( + parent: parents.second, + patient_session:, + sent_by: current_user ) end @@ -175,26 +157,16 @@ end it "enqueues an email per parent" do - expect { create_and_send! }.to have_enqueued_mail( - SessionMailer, - :clinic_subsequent_invitation + expect { create_and_send! }.to have_delivered_email( + :session_clinic_subsequent_invitation ).with( - params: { - parent: parents.first, - patient_session:, - sent_by: current_user - }, - args: [] - ).and have_enqueued_mail( - SessionMailer, - :clinic_subsequent_invitation - ).with( - params: { - parent: parents.second, - patient_session:, - sent_by: current_user - }, - args: [] + parent: parents.first, + patient_session:, + sent_by: current_user + ).and have_delivered_email(:session_clinic_subsequent_invitation).with( + parent: parents.second, + patient_session:, + sent_by: current_user ) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c1693a9e6..8c58741f4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -202,7 +202,7 @@ config.after(:each, :js) { WebMock.disable_net_connect! } config.before do - ActionMailer::Base.deliveries.clear + EmailDeliveryJob.deliveries.clear SMSDeliveryJob.deliveries.clear end diff --git a/spec/support/email_expectations.rb b/spec/support/email_expectations.rb index 30858c300..c597a5f53 100644 --- a/spec/support/email_expectations.rb +++ b/spec/support/email_expectations.rb @@ -1,26 +1,26 @@ # frozen_string_literal: true module EmailExpectations - def expect_email_to(to, template_name, nth = :first) + def expect_email_to(email_address, template_name, nth = :first) template_id = GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch(template_name) email = if nth == :any - sent_emails.find do |e| - e.to.include?(to) && e.template_id == template_id + email_deliveries.find do + it[:email_address] == email_address && it[:template_id] == template_id end else - sent_emails.send(nth) + email_deliveries.send(nth) end expect(email).not_to be_nil - expect(email.to).to eq([to]) - expect(email.template_id).to eq(template_id) + expect(email[:email_address]).to eq(email_address) + expect(email[:template_id]).to eq(template_id) end - def sent_emails + def email_deliveries perform_enqueued_jobs - ActionMailer::Base.deliveries + EmailDeliveryJob.deliveries end end diff --git a/spec/support/matchers/have_delivered_email.rb b/spec/support/matchers/have_delivered_email.rb new file mode 100644 index 000000000..19d2f27cf --- /dev/null +++ b/spec/support/matchers/have_delivered_email.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +RSpec::Matchers.matcher :have_delivered_email do |template_name = nil| + supports_block_expectations + + chain :with do |params| + @params = params + end + + match do |actual| + expect { actual.call }.to have_enqueued_job(EmailDeliveryJob).with( + *[template_name].compact, + **(@params || {}) + ) + end + + match_when_negated do |actual| + expect { actual.call }.not_to have_enqueued_job(EmailDeliveryJob).with( + *[template_name].compact, + **(@params || {}) + ) + end +end