Skip to content

Commit

Permalink
added tests and factories
Browse files Browse the repository at this point in the history
  • Loading branch information
pmclaren19 committed Dec 19, 2024
1 parent 0c3279c commit b63cc68
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 72 deletions.
3 changes: 3 additions & 0 deletions app/models/evidence_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class EvidenceSubmission < ApplicationRecord
has_kms_key
has_encrypted :template_metadata, key: :kms_key, **lockbox_options

scope :va_notify_email_sent, lambda {
where('upload_status = ? AND va_notify_date IS NOT NULL', BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED])
}
scope :va_notify_email_not_sent, lambda {
where(upload_status: BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED], va_notify_date: nil)
}
Expand Down
2 changes: 0 additions & 2 deletions app/sidekiq/evss/document_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class EVSS::DocumentUpload
include Sidekiq::Job
extend SentryLogging
extend Logging::ThirdPartyTransaction::MethodWrapper

FILENAME_EXTENSION_MATCHER = /\.\w*$/
Expand Down Expand Up @@ -61,7 +60,6 @@ class EVSS::DocumentUpload
error_message = "#{job_class} failed to create EvidenceSubmission"
::Rails.logger.info(error_message, { messsage: e.message })
StatsD.increment('silent_failure', tags: ['service:claim-status', "function: #{error_message}"])
log_exception_to_sentry(e)
end

def perform(auth_headers, user_uuid, document_hash)
Expand Down
2 changes: 0 additions & 2 deletions app/sidekiq/lighthouse/benefits_documents/document_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class Lighthouse::BenefitsDocuments::DocumentUpload
include Sidekiq::Job
extend SentryLogging

attr_accessor :user_icn, :document_hash

Expand Down Expand Up @@ -49,7 +48,6 @@ class Lighthouse::BenefitsDocuments::DocumentUpload
error_message = "#{job_class} failed to create EvidenceSubmission"
::Rails.logger.info(error_message, { messsage: e.message })
StatsD.increment('silent_failure', tags: ['service:claim-status', "function: #{error_message}"])
log_exception_to_sentry(e)
end

def self.obscured_filename(original_filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
module BenefitsDocuments
class FailureNotificationEmailJob
include Sidekiq::Job
include SentryLogging

sidekiq_options retry: false, unique_for: 30.minutes

FAILED_STATUS = BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED]
NOTIFY_SETTINGS = Settings.vanotify.services.benefits_management_tools
MAILER_TEMPLATE_ID = NOTIFY_SETTINGS.template_id.evidence_submission_failure_email
# TODO: need to add statsd logic
# STATSD_KEY_PREFIX = ''
Expand Down Expand Up @@ -35,56 +35,36 @@ def notify_client

def send_failed_evidence_submissions
failed_uploads.each do |upload|
byebug

Check failure on line 38 in app/sidekiq/lighthouse/benefits_documents/failure_notification_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Debugger: Remove debugger entry point `byebug`.
response = notify_client.send_email(
recipient_identifier: { id_value: upload.user_account.icn, id_type: 'ICN' },
template_id: MAILER_TEMPLATE_ID,
personalisation: upload.template_metadata_ciphertext.personalisation
)

byebug

Check failure on line 44 in app/sidekiq/lighthouse/benefits_documents/failure_notification_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Debugger: Remove debugger entry point `byebug`.
record_email_send_success(upload, response)
rescue => e
byebug

Check failure on line 47 in app/sidekiq/lighthouse/benefits_documents/failure_notification_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Debugger: Remove debugger entry point `byebug`.
record_email_send_failure(upload, e)
end

nil
end

def record_email_send_success(upload, response)
byebug

Check failure on line 55 in app/sidekiq/lighthouse/benefits_documents/failure_notification_email_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Debugger: Remove debugger entry point `byebug`.
EvidenceSubmission.update(id: upload.id, va_notify_id: response.id, va_notify_date: DateTime.now)
error_message = "#{upload.job_class} va notify failure email queued"
::Rails.logger.info(error_message)
message = "#{upload.job_class} va notify failure email queued"
::Rails.logger.info(message)
StatsD.increment('silent_failure_avoided_no_confirmation',
tags: ['service:claim-status', "function: #{error_message}"])
tags: ['service:claim-status', "function: #{message}"])
end

def record_email_send_failure(upload, error)
error_message = "#{upload.job_class} va notify failure email errored"
::Rails.logger.error(error_message, { message: error.message })
StatsD.increment('silent_failure', tags: ['service:claim-status', "function: #{error_message}"])
log_exception_to_sentry(e)
log_exception_to_sentry(error)
end

# add va_notify_date to the evidence_submissions table - done
# grab failed records that dont have a va_notify_date - done
# reach out to va notify with an id of the template - go off of what was in the
# call for this job app/sidekiq/lighthouse/failure_notification.rb, no retrys
# for each file - done
# va notify should return an id when a record is created (take a look at record_evidence_email_send_successful() for an example)
# update evidence submissions with a va notify id, and va notify date - done
#
# use job_class from the es table to determine if we
# should send evss or lighthouse log - done
# Update app/sidekiq/lighthouse/document_upload.rb method sidekiq_retries_exhausted()
# and create a new record and set the upload status to FAILED - done
# Update app/sidekiq/evss/document_upload.rb method sidekiq_retries_exhausted()
# and set the upload status to FAILED - done
#
# Remove app/sidekiq/evss/failure_notification.rb and tests - done
# Remove app/sidekiq/lighthouse/failure_notification.rb and tests - done
# Update tests for doc upload lighthouse and evss
# Add tests for failure notification email job
#
# update periodic_job.rb with your job
# modules/decision_reviews/app/sidekiq/decision_reviews/failure_notification_email_job.rb
end
end
2 changes: 1 addition & 1 deletion lib/periodic_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,5 @@
mgr.register('00 19 * * 1-5', 'Vye::SundownSweep')

# Send Benefits Documents failure notification emails to Veteran for failed evidence submissions
mgr.register('5 0 * * *', 'Lighthouse::BenefitsDocuments::FailureNotificationEmailJob')
mgr.register('5 0 * * *', 'BenefitsDocuments::FailureNotificationEmailJob')
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,32 @@
created_at { DateTime.new(1985, 10, 26).utc }
end

factory :bd_evidence_submission_pending, class: 'EvidenceSubmission' do
association :user_account, factory: :user_account
created_at { DateTime.now.utc }
upload_status { BenefitsDocuments::Constants::UPLOAD_STATUS[:PENDING] }
end

factory :bd_evidence_submission_failed, class: 'EvidenceSubmission' do
association :user_account, factory: :user_account
created_at { DateTime.now.utc }
upload_status { BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED] }
template_metadata_ciphertext do
'personalisation' => {
'first_name' => 'test',

Check failure on line 28 in spec/factories/lighthouse/benefits_documents/evidence_submission.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Syntax: unexpected token tSTRING (Using Ruby 3.3 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)
'filename' => 'test.txt',

Check failure on line 29 in spec/factories/lighthouse/benefits_documents/evidence_submission.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Syntax: unexpected token tSTRING (Using Ruby 3.3 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)
'date_submitted' => "#{DateTime.now.utc}",
'date_failed' => "#{DateTime.now.utc}"

Check failure on line 31 in spec/factories/lighthouse/benefits_documents/evidence_submission.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Syntax: unexpected token tSTRING_BEG (Using Ruby 3.3 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)
}

Check failure on line 32 in spec/factories/lighthouse/benefits_documents/evidence_submission.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Syntax: unexpected token tRCURLY (Using Ruby 3.3 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)
end
end

factory :bd_evidence_submission_failed_va_notify_email_sent, class: 'EvidenceSubmission' do
association :user_account, factory: :user_account
created_at { DateTime.now.utc - 2 }
upload_status { BenefitsDocuments::Constants::UPLOAD_STATUS[:FAILED] }
va_notify_id { 123 }
va_notify_date { DateTime.now.utc }
va_notify_status { 'success' }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
submission2 = AppealSubmission.find_by(submitted_appeal_uuid: guid2)
expect(submission2.failure_notification_sent_at).to eq DateTime.new(2023, 1, 2)

submission3 = AppealSubmission.find_by(submitted_appeal_uuid: guid3)
submission3 = AppealSubmission.find_by (submitted_appeal_uuid: guid3)

Check failure on line 122 in spec/sidekiq/decision_review/failure_notification_email_job_spec.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/Syntax: unexpected token tLABEL (Using Ruby 3.3 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)
expect(submission3.failure_notification_sent_at).to be_nil

expect(mpi_service).not_to have_received(:find_profile_by_identifier)
Expand Down
17 changes: 7 additions & 10 deletions spec/sidekiq/lighthouse/benefits_documents/document_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@

context 'when upload succeeds' do
let(:uploader_stub) { instance_double(LighthouseDocumentUploader) }
let(:evidence_submission_stub) do
evidence_submission = EvidenceSubmission.new(claim_id: '4567',
tracked_item_id: tracked_item_ids,
job_id: job_id,
job_class: described_class,
upload_status: 'pending')
evidence_submission.user_account = user_account
evidence_submission.save!
evidence_submission
let(:evidence_submission_pending) do
create(:bd_evidence_submission_pending,
tracked_item_id: tracked_item_ids,
claim_id:,
job_id:,
job_class: described_class)
end
let(:response) do
{
Expand All @@ -84,7 +81,7 @@
job_id:,
job_class:,
upload_status: BenefitsDocuments::Constants::UPLOAD_STATUS[:PENDING] })
.and_return(evidence_submission_stub)
.and_return(evidence_submission_pending)
described_class.drain # runs all queued jobs of this class
# After running DocumentUpload job, there should be a new EvidenceSubmission record
# with the response request_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require 'rails_helper'

require 'lighthouse/benefits_documents/failure_notification'
require 'lighthouse/benefits_documents/failure_notification_email_job'
require 'va_notify/service'

RSpec.describe Lighthouse::BenefitsDocuments::FailureNotificationEmailJob, type: :job do
RSpec.describe BenefitsDocuments::FailureNotificationEmailJob, type: :job do
subject { described_class }

let(:notify_client_stub) { instance_double(VaNotify::Service) }
Expand All @@ -24,32 +24,60 @@
let(:date_submitted) { formatted_submit_date }
let(:date_failed) { formatted_submit_date }

let(:notification_id) { SecureRandom.uuid }

let(:vanotify_service) do
service = instance_double(VaNotify::Service)

response = instance_double(Notifications::Client::ResponseNotification, id: notification_id)
allow(service).to receive(:send_email).and_return(response)

service
end

before do
allow(Rails.logger).to receive(:info)
allow(VaNotify::Service).to receive(:new).and_return(vanotify_service)
end

context 'when Lighthouse::FailureNotification is called' do
it 'enqueues a failure notification mailer to send to the veteran' do
allow(VaNotify::Service).to receive(:new) { notify_client_stub }

subject.perform_async(icn, first_name, filename, date_submitted, date_failed) do
expect(notify_client_stub).to receive(:send_email).with(
{
recipient_identifier: { id_value: user_account.icn, id_type: 'ICN' },
template_id: 'fake_template_id',
personalisation: {
first_name: 'Bob',
filename: 'docXXXX-XXte.pdf',
date_submitted: formatted_submit_date,
date_failed: formatted_submit_date
}
}
)

expect(Rails.logger)
.to receive(:info)
.with('Lighthouse::FailureNotification email sent')
end
# context 'when there are no FAILED records' do
# it 'doesnt send anything' do
# expect(EvidenceSubmission).not_to receive(:update)
# expect(EvidenceSubmission.va_notify_email_sent.length).to equal(0)
# end
# end

context 'when there is a FAILED record with a va_notify_date' do
before do
allow(EvidenceSubmission).to receive(:va_notify_email_not_sent)
create(:bd_evidence_submission_failed_va_notify_email_sent)
end

it 'doesnt update an evidence submission record' do
expect(EvidenceSubmission.va_notify_email_sent.length).to equal(1)

subject.new.perform
expect(EvidenceSubmission).not_to receive(:update)

# This is 1 since is has already been sent
expect(EvidenceSubmission.va_notify_email_sent.length).to equal(1)
end
end

context 'when there is 1 FAILED record without a va_notify_date' do
before do
allow(EvidenceSubmission).to receive(:va_notify_email_not_sent).and_return([evidence_submission_failed])
allow(EvidenceSubmission).to receive(:update)
end

let!(:evidence_submission_failed) { create(:bd_evidence_submission_failed) }

it 'successfully enqueues a failure notification mailer to send to the veteran' do
expect(EvidenceSubmission.va_notify_email_sent.length).to equal(0)

subject.new.perform
# expect(EvidenceSubmission).to receive(:va_notify_email_not_sent).and_return(evidence_submission_failed)
expect(EvidenceSubmission).to receive(:update)
expect(EvidenceSubmission.va_notify_email_sent.length).to equal(1)
end
end
end
end

0 comments on commit b63cc68

Please sign in to comment.