Skip to content

Commit

Permalink
[Automated] Merged master into target preview_envs_k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
va-vsp-bot authored Apr 17, 2024
2 parents 84df0d6 + be21fde commit 8c20d70
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
29 changes: 13 additions & 16 deletions modules/appeals_api/app/services/appeals_api/remove_pii.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'ddtrace'

module AppealsApi
class RemovePii
include SentryLogging
Expand All @@ -15,13 +17,20 @@ def initialize(form_type:)
end

def run!
validate_form_type!
Datadog::Tracing.trace("#{self.class.name} - #{form_type}") do
validate_form_type!

result = remove_pii!
result = remove_pii!

log_failure_to_sentry if records_were_not_cleared(result)
if result.blank? && records_to_be_expunged.present?
ids = records_to_be_expunged.pluck(:id)
msg = "Failed to remove expired #{form_type} PII from records"
Rails.logger.error(msg, ids)
AppealsApi::Slack::Messager.new({ msg:, ids: }).notify!
end

result
result
end
end

private
Expand Down Expand Up @@ -49,17 +58,5 @@ def records_to_be_expunged
.pii_expunge_policy
end
end

def records_were_not_cleared(result)
result.blank? && records_to_be_expunged.present?
end

def log_failure_to_sentry
log_message_to_sentry(
"Failed to expunge PII from #{form_type} (modules/appeals_api)",
:error,
ids: records_to_be_expunged.pluck(:id)
)
end
end
end
35 changes: 26 additions & 9 deletions modules/appeals_api/spec/services/appeals_api/remove_pii_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'rails_helper'
require AppealsApi::Engine.root.join('spec', 'spec_helper.rb')

def update_appeal_status(appeal, status, code: nil, detail: nil)
# At the time of writing, the `update_status` method for each appeal model involves kicking off a sidekiq job to
Expand Down Expand Up @@ -159,6 +158,32 @@ def create_misc_appeals = create_appeals + misc_appeal_types.map { |f| FactoryBo
expect { AppealsApi::RemovePii.new(form_type: 'Invalid').run! }.to raise_error(ArgumentError)
end

context 'when the removal fails' do
let!(:appeals) do
Timecop.freeze(100.days.ago) do
status = 'complete'
[create(:supplemental_claim, status:), create(:supplemental_claim_v0, status:)]
end
end

before do
instance = AppealsApi::RemovePii.new(form_type: AppealsApi::SupplementalClaim)
msg = 'Failed to remove expired AppealsApi::SupplementalClaim PII from records'
expect(Rails.logger).to receive(:error).with(msg, appeals.map(&:id))
expect_any_instance_of(AppealsApi::Slack::Messager).to receive(:notify!)
allow(instance).to receive(:remove_pii!).and_return []
instance.run!
end

it 'logs an error and the IDs of records whose PII failed to be removed' do
appeals.each do |appeal|
appeal.reload
expect(appeal.auth_headers).to be_present
expect(appeal.form_data).to be_present
end
end
end

it 'removes PII from HLR records needing PII removal' do
day_old_has_pii_v2 = create :higher_level_review_v2, status: 'complete'
day_old_has_pii_v2.update updated_at: 1.day.ago
Expand Down Expand Up @@ -271,13 +296,5 @@ def create_misc_appeals = create_appeals + misc_appeal_types.map { |f| FactoryBo
expect(week_old_has_pii_error.reload.form_data_ciphertext).to be_present
end
end

it 'sends a message to sentry if the removal failed.' do
allow_any_instance_of(AppealsApi::RemovePii).to receive(:records_were_not_cleared).and_return(true)
service = AppealsApi::RemovePii.new(form_type: AppealsApi::NoticeOfDisagreement)
expect(service).to receive(:log_failure_to_sentry)

service.run!
end
end
end

0 comments on commit 8c20d70

Please sign in to comment.