Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions app/jobs/in_person/send_proofing_notification_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module InPerson
class SendProofingNotificationJob < ApplicationJob
include LocaleHelper

# @param [Number] enrollment_id primary key of the enrollment
def perform(enrollment_id)
return unless IdentityConfig.store.in_person_proofing_enabled &&
Expand Down Expand Up @@ -42,7 +44,7 @@ def perform(enrollment_id)
rescue StandardError => err
analytics(user: enrollment&.user || AnonymousUser.new).
idv_in_person_send_proofing_notification_job_exception(
enrollment_code: enrollment&.code,
enrollment_code: enrollment&.enrollment_code,
enrollment_id: enrollment_id,
exception_class: err.class.to_s,
exception_message: err.message,
Expand Down Expand Up @@ -72,12 +74,14 @@ def handle_telephony_response(enrollment:, phone:, telephony_response:)
end

def notification_message(enrollment:)
proof_date = enrollment.proofed_at ? I18n.l(enrollment.proofed_at, format: :sms_date) : 'NA'
I18n.t(
'telephony.confirmation_ipp_enrollment_result.sms',
app_name: APP_NAME,
proof_date: proof_date,
)
with_user_locale(enrollment.user) do
proof_date = I18n.l(enrollment.proofed_at, format: :sms_date)
I18n.t(
'telephony.confirmation_ipp_enrollment_result.sms',
app_name: APP_NAME,
proof_date: proof_date,
)
end
end

def analytics(user:)
Expand Down
78 changes: 69 additions & 9 deletions spec/jobs/in_person/send_proofing_notification_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@

let(:passed_enrollment_without_notification) { create(:in_person_enrollment, :passed) }
let(:passed_enrollment) do
enrollment = create(:in_person_enrollment, :passed, :with_notification_phone_configuration)
enrollment.proofed_at = Time.zone.now - 3.days
enrollment
create(
:in_person_enrollment,
:passed,
:with_notification_phone_configuration,
proofed_at: Time.zone.now - 3.days,
)
end
let(:failing_enrollment) do
enrollment = create(:in_person_enrollment, :failed, :with_notification_phone_configuration)
enrollment.proofed_at = Time.zone.now - 3.days
enrollment
create(
:in_person_enrollment,
:failed,
:with_notification_phone_configuration,
proofed_at: Time.zone.now - 3.days,
)
end
let(:expired_enrollment) do
enrollment = create(:in_person_enrollment, :expired, :with_notification_phone_configuration)
enrollment
create(:in_person_enrollment, :expired, :with_notification_phone_configuration)
end
let(:sms_success_response) do
Telephony::Response.new(
Expand All @@ -40,6 +45,7 @@
error: Telephony::DailyLimitReachedError.new,
)
end

before do
ActiveJob::Base.queue_adapter = :test
allow(job).to receive(:analytics).and_return(analytics)
Expand All @@ -62,6 +68,7 @@
expect(analytics).not_to have_logged_event('SendProofingNotificationJob: job completed')
end
end

context 'job disabled' do
let(:in_person_proofing_enabled) { true }
let(:in_person_send_proofing_notifications_enabled) { false }
Expand All @@ -73,9 +80,11 @@
expect(analytics).not_to have_logged_event('SendProofingNotificationJob: job completed')
end
end

context 'ipp and job enabled' do
let(:in_person_proofing_enabled) { true }
let(:in_person_send_proofing_notifications_enabled) { true }

context 'enrollment does not exist' do
it 'returns without doing anything' do
bad_id = (InPersonEnrollment.all.pluck(:id).max || 0) + 1
Expand All @@ -84,20 +93,23 @@
expect(analytics).to have_logged_event('SendProofingNotificationJob: job skipped')
end
end

context 'enrollment has an unsupported status' do
it 'returns without doing anything' do
job.perform(expired_enrollment.id)
expect(analytics).not_to have_logged_event('SendProofingNotificationJob: job started')
expect(analytics).to have_logged_event('SendProofingNotificationJob: job skipped')
end
end

context 'without notification phone notification' do
it 'returns without doing anything' do
job.perform(passed_enrollment_without_notification.id)
expect(analytics).not_to have_logged_event('SendProofingNotificationJob: job started')
expect(analytics).to have_logged_event('SendProofingNotificationJob: job skipped')
end
end

context 'with notification phone configuration' do
it 'sends notification successfully when enrollment is successful and enrollment updated' do
allow(Telephony).to receive(:send_notification).and_return(sms_success_response)
Expand All @@ -116,6 +128,7 @@
expect(passed_enrollment.reload.notification_phone_configuration).to be_nil
end
end

it 'sends notification successfully when enrollment failed' do
allow(Telephony).to receive(:send_notification).and_return(sms_success_response)

Expand All @@ -131,7 +144,30 @@
expect(failing_enrollment.reload.notification_phone_configuration).to be_nil
end
end

it 'sends a message that respects the user email locale preference' do
allow(Telephony).to receive(:send_notification).and_return(sms_success_response)

passed_enrollment.user.update!(email_language: 'fr')
passed_enrollment.update!(proofed_at: Time.zone.now)
proofed_date = Time.zone.now.strftime('%m/%d/%Y')
phone_number = passed_enrollment.notification_phone_configuration.formatted_phone

expect(Telephony).
to(
receive(:send_notification).
with(
to: phone_number,
message: "Login.gov: Vous avez tenté de vérifier votre identité dans un bureau " \
"de poste le #{proofed_date}. Vérifiez votre e-mail pour votre résultat.",
country_code: Phonelib.parse(phone_number).country,
),
)

job.perform(passed_enrollment.id)
end
end

context 'when failed to send notification' do
it 'logs sms send failure when number is opt out and enrollment not updated' do
allow(Telephony).to receive(:send_notification).and_return(sms_opt_out_response)
Expand All @@ -142,6 +178,7 @@
)
expect(passed_enrollment.reload.notification_sent_at).to be_nil
end

it 'logs sms send failure for delivery failure' do
allow(Telephony).to receive(:send_notification).and_return(sms_failure_response)

Expand All @@ -152,7 +189,8 @@
expect(passed_enrollment.reload.notification_sent_at).to be_nil
end
end
context 'when an exception is raised' do

context 'when an exception is raised trying to find the enrollment' do
it 'logs the exception details' do
allow(InPersonEnrollment).
to receive(:find_by).
Expand All @@ -169,6 +207,28 @@
)
end
end

context 'when an exception is raised trying to send the notification' do
let(:exception_message) { 'SMS unsupported' }

it 'logs the exception details' do
allow(Telephony).
to(
receive(:send_notification).
and_raise(Telephony::SmsUnsupportedError.new(exception_message)),
)

job.perform(passed_enrollment.id)

expect(analytics).to have_logged_event(
'SendProofingNotificationJob: exception raised',
enrollment_code: passed_enrollment.enrollment_code,
enrollment_id: passed_enrollment.id,
exception_class: 'Telephony::SmsUnsupportedError',
exception_message: exception_message,
)
end
end
end
end
end