Skip to content

Commit

Permalink
fix xml return sample service to handle invalid keys gracefully (#4694)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotondo authored Aug 8, 2024
1 parent e26e5d3 commit 0b8e00f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
16 changes: 8 additions & 8 deletions app/services/state_file/xml_return_sample_service.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module StateFile
class XmlReturnSampleService
def initialize
@_samples = {}
@_submission_id_lookup = {
@samples = {}
@submission_id_lookup = {
'ny_rudy_v2' => '1016422024027ate001k',
'ny_javier' => '1016422024018atw000x',
'ny_matthew_v2' => '1016422024026atw001u',
Expand All @@ -18,7 +18,7 @@ def initialize

def samples
load_samples
@_samples
@samples
end

def self.key(us_state, sample_name)
Expand All @@ -30,7 +30,7 @@ def self.label(sample_name)
end

def lookup_submission_id(key)
@_submission_id_lookup[key] || '12345202201011234570'
@submission_id_lookup[key] || '12345202201011234570'
end

def include?(key)
Expand All @@ -51,13 +51,13 @@ def old_sample
TAX_YEAR = Rails.configuration.statefile_current_tax_year.to_s.freeze

def load_samples
return if @_samples.present?
return if @samples.present?

StateFile::StateInformationService.active_state_codes.each do |us_state|
@_samples[us_state] = []
@samples[us_state] = []
xml_path_glob = File.join(BASE_PATH, TAX_YEAR, us_state, '*.xml')
Dir.glob(xml_path_glob).each do |xml_path|
@_samples[us_state].push(File.basename(xml_path, ".xml"))
@samples[us_state].push(File.basename(xml_path, ".xml"))
end
end
end
Expand All @@ -67,7 +67,7 @@ def path(key)
return @old_sample if key == "abcdefg"

us_state, sample_name = key.split("_", 2)
if @_samples[us_state].include? sample_name
if @samples.include?(us_state) && @samples[us_state].include?(sample_name)
File.join(BASE_PATH, TAX_YEAR, us_state, "#{sample_name}.xml")
end
end
Expand Down
18 changes: 14 additions & 4 deletions spec/services/state_file/xml_return_sample_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
let(:state_code) { 'az' }
let(:sample_name) { 'superman_v2' }
let(:key) { 'az_superman_v2' }
let(:bad_key) { 'az_superman_does_not_exist' }
let(:missing_key) { 'az_superman_does_not_exist' }
let(:invalid_key) { 'asdf' }
let(:label) { 'Az superman v2' }
let(:unique_file_contents) { 'KENT' }
let(:old_sample_unique_file_contents) { 'TESTERSON' }
Expand All @@ -31,7 +32,7 @@
end

it 'returns the default submission id if one is not found' do
expect(xml_return_sample_service.lookup_submission_id(bad_key)).to eq default_submission_id
expect(xml_return_sample_service.lookup_submission_id(missing_key)).to eq default_submission_id
end
end

Expand All @@ -41,8 +42,13 @@
end

it 'returns false if the sample does not exist' do
expect(xml_return_sample_service.include?(bad_key)).to be_falsey
expect(xml_return_sample_service.include?(missing_key)).to be_falsey
end

it 'returns false if the key is invalid' do
expect(xml_return_sample_service.include?(invalid_key)).to be_falsey
end

end

describe '#read' do
Expand All @@ -51,7 +57,11 @@
end

it 'returns nil if the sample does not exist' do
expect(xml_return_sample_service.read(bad_key)).to be_nil
expect(xml_return_sample_service.read(missing_key)).to be_nil
end

it 'returns nil if the key is invalid' do
expect(xml_return_sample_service.read(invalid_key)).to be_nil
end
end

Expand Down

0 comments on commit 0b8e00f

Please sign in to comment.