Skip to content

Commit

Permalink
pension module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne-weibel committed Oct 23, 2024
1 parent 7e56186 commit 47fe544
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 78 deletions.
4 changes: 2 additions & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ vanotify:
email:
confirmation:
template_id: form527ez_confirmation_email_template_id
flipper: false
flipper_id: false
error: null
received: null
pensions: *vanotify_services_pension
Expand All @@ -1411,7 +1411,7 @@ vanotify:
email:
confirmation:
template_id: burial_claim_confirmation_email_template_id
flipper: false
flipper_id: false
error: null
received: null
burials: *vanotify_services_burial
Expand Down
27 changes: 27 additions & 0 deletions lib/va_notify/notification_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module VANotify
module NotificationEmail

STATSD = 'api.va_notify.notification_email'

CONFIRMATION = :confirmation
ERROR = :error
RECEIVED = :received

# error indicating failure to send email
class FailureToSend < StandardError; end

def monitor_send_failure(error_message, tags:, context: nil)
metric = "#{VANotify::NotificationEmail::STATSD}.failure"
payload = {
statsd: metric,
error_message:,
context:
}

StatsD.increment(metric, tags:)
Rails.logger.error('VANotify::NotificationEmail #send failure!', **payload)
end
end
end
42 changes: 11 additions & 31 deletions lib/va_notify/notification_email/saved_claim.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
# frozen_string_literal: true

require 'va_notify/notification_email'

module VANotify
module NotificationEmail
STATSD = 'api.va_notify.notification_email'

CONFIRMATION = :confirmation
ERROR = :error
RECEIVED = :received

# error indicating failure to send email
class FailureToSend < StandardError; end

def monitor_send_failure(error_message, tags:, context: {})
metric = "#{VANotify::NotificationEmail::STATSD}.failure"
StatsD.increment(metric, tags:)

payload = {
statsd: metric,
error_message:,
context:
}
Rails.logger.error('VANotify::NotificationEmail #send failure!', **payload)
end

class SavedClaim
def initialize(saved_claim, user: nil, service_name: nil)
def initialize(saved_claim, service_name: nil)
@claim = saved_claim
@user = user
@vanotify_service = service_name
@config = Settings.vanotify.services[vanotify_service]
raise ArgumentError, "Invalid service_name '#{vanotify_service}'" unless config
end

def send(email_type, at: nil)
email_config = config&.email&.[](email_type)
def deliver(email_type, at: nil)
email_config = config&.email[email_type]
raise ArgumentError, "Invalid email_type '#{email_type}'" unless email_config

email_template_id = able_to_send?(email_config)
return unless email_template_id

at ? enqueue_email(email_template_id, at) : send_email_now(email_template_id)
at ? enqueue_email(email_template_id, at) : send_email(email_template_id)

claim.insert_notification(email_config.template_id)
rescue => e
Expand All @@ -57,7 +37,7 @@ def send(email_type, at: nil)

private

attr_reader :claim, :config, :user
attr_reader :claim, :config

def vanotify_service
@vanotify_service ||= claim.form_id.downcase.gsub(/-/, '_')
Expand All @@ -75,7 +55,7 @@ def able_to_send?(email_config)
raise VANotify::NotificationEmail::FailureToSend, 'Notification already sent'
end

email_config.template_id if flipper?(email_config.flipper)
email_config.template_id if flipper?(email_config.flipper_id)
end

def enqueue_email(email_template_id, at)
Expand All @@ -87,7 +67,7 @@ def enqueue_email(email_template_id, at)
)
end

def send_email_now(email_template_id)
def send_email(email_template_id)
VANotify::EmailJob.perform_async(
email,
email_template_id,
Expand All @@ -96,11 +76,11 @@ def send_email_now(email_template_id)
end

def email
claim.email || user&.email
claim.email
end

def first_name
claim.first_name || user&.first_name
claim.first_name
end

def personalization
Expand Down
23 changes: 0 additions & 23 deletions modules/pensions/app/models/pensions/saved_claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,6 @@ def first_name
parsed_form.dig('veteranFullName', 'first')
end

##
# enqueue the sending of the submission confirmation email
#
# @see VANotify::EmailJob
#
def send_confirmation_email
if email.blank? || va_notification?(Settings.vanotify.services.va_gov.template_id.form527ez_confirmation_email)
return
end

VANotify::EmailJob.perform_async(
email,
Settings.vanotify.services.va_gov.template_id.form527ez_confirmation_email,
{
'first_name' => parsed_form.dig('veteranFullName', 'first')&.upcase.presence,
'date_submitted' => Time.zone.today.strftime('%B %d, %Y'),
'confirmation_number' => guid
}
)

insert_notification(Settings.vanotify.services.va_gov.template_id.form527ez_confirmation_email)
end

# Run after a claim is saved, this processes any files and workflows that are present
# and sends them to our internal partners for processing.
# Only removed Sidekiq call from super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'lighthouse/benefits_intake/metadata'
require 'pensions/tag_sentry'
require 'pensions/monitor'
require 'pensions/notification_email'
require 'pdf_utilities/datestamp_pdf'

module Pensions
Expand Down Expand Up @@ -202,7 +203,8 @@ def form_submission_polling
# Being VANotify job to send email to veteran
#
def send_confirmation_email
@claim.respond_to?(:send_confirmation_email) && @claim.send_confirmation_email
Pensions::NotificationEmail.new(@claim).deliver(:confirmation)
# @claim.respond_to?(:send_confirmation_email) && @claim.send_confirmation_email
rescue => e
@pension_monitor.track_send_confirmation_email_failure(@claim, @intake_service, @user_account_uuid, e)
end
Expand Down
4 changes: 2 additions & 2 deletions modules/pensions/lib/pensions/notification_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Pensions
class NotificationEmail < ::VANotify::NotificationEmail::SavedClaim
# @see VANotify::NotificationEmail::SavedClaim
# pass thru to super class, no additional processing needed
def initialize(_saved_claim)
super(claim, 'pensions')
def initialize(saved_claim)
super(saved_claim, service_name: 'pensions')
end
end
end
24 changes: 6 additions & 18 deletions modules/pensions/spec/models/pensions/saved_claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,17 @@
expect { claim.destroy }.to change(PersistentAttachment, :count).by(-2)
end
end

it '#send_confirmation_email' do
allow(VANotify::EmailJob).to receive(:perform_async)
allow(Settings.vanotify.services.va_gov.template_id).to receive(:form527ez_confirmation_email).and_return(0)

claim.send_confirmation_email
claim.send_confirmation_email

expect(VANotify::EmailJob).to have_received(:perform_async).with(
'foo@foo.com',
0,
{
'first_name' => 'TEST',
'date_submitted' => Time.zone.today.strftime('%B %d, %Y'),
'confirmation_number' => claim.guid
}
).once
end
end

describe '#email' do
it 'returns the users email' do
expect(instance.email).to eq('foo@foo.com')
end
end

describe '#first_name' do
it 'returns the users first name' do
expect(instance.first_name).to eq('Test')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rails_helper'
require 'lighthouse/benefits_intake/service'
require 'lighthouse/benefits_intake/metadata'
require 'pensions/notification_email'

RSpec.describe Pensions::PensionBenefitIntakeJob, :uploader_helpers do
stub_virus_scan
Expand Down Expand Up @@ -161,16 +162,21 @@

describe '#send_confirmation_email' do
let(:monitor_error) { create(:monitor_error) }
let(:notification) { double('notification') }

before do
job.instance_variable_set(:@claim, claim)
allow(claim).to receive(:send_confirmation_email).and_raise(monitor_error)

allow(Pensions::NotificationEmail).to receive(:new).and_return(notification)
allow(notification).to receive(:deliver).and_raise(monitor_error)

job.instance_variable_set(:@pension_monitor, monitor)
allow(monitor).to receive(:track_send_confirmation_email_failure)
end

it 'errors and logs but does not reraise' do
expect(Pensions::NotificationEmail).to receive(:new).with claim
expect(notification).to receive(:deliver).with(:confirmation)
expect(monitor).to receive(:track_send_confirmation_email_failure)
job.send(:send_confirmation_email)
end
Expand Down

0 comments on commit 47fe544

Please sign in to comment.