diff --git a/modules/claims_api/app/controllers/concerns/claims_api/poa_verification.rb b/modules/claims_api/app/controllers/concerns/claims_api/poa_verification.rb index b7c17966f07..12fc5e63c0e 100644 --- a/modules/claims_api/app/controllers/concerns/claims_api/poa_verification.rb +++ b/modules/claims_api/app/controllers/concerns/claims_api/poa_verification.rb @@ -67,9 +67,10 @@ def target_veteran_is_current_user? # @param poa_code [String] poa code to match to @current_user # # @return [Boolean] True if valid poa code, False if not - def valid_poa_code_for_current_user?(poa_code) + def valid_poa_code_for_current_user?(poa_code) # rubocop:disable Metrics/MethodLength reps = ::Veteran::Service::Representative.all_for_user(first_name: @current_user.first_name, last_name: @current_user.last_name) + return false if reps.blank? if reps.count > 1 @@ -81,6 +82,12 @@ def valid_poa_code_for_current_user?(poa_code) last_name: @current_user.last_name, middle_initial:) + if reps.blank? || reps.count > 1 + reps = ::Veteran::Service::Representative.all_for_user(first_name: @current_user.first_name, + last_name: @current_user.last_name, + poa_code:) + end + raise ::Common::Exceptions::Unauthorized, detail: 'VSO Representative Not Found' if reps.blank? raise ::Common::Exceptions::Unauthorized, detail: 'Ambiguous VSO Representative Results' if reps.count > 1 end diff --git a/modules/claims_api/spec/concerns/claims_api/poa_verification_spec.rb b/modules/claims_api/spec/concerns/claims_api/poa_verification_spec.rb new file mode 100644 index 00000000000..5e35dc9405a --- /dev/null +++ b/modules/claims_api/spec/concerns/claims_api/poa_verification_spec.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +require 'rails_helper' + +class FakeController < ApplicationController + include ClaimsApi::PoaVerification + + def initialize + super + @current_user = ClaimsApi::ClaimsUser.new('test') + @current_user.first_name_last_name('John', 'Doe') + @current_user.middle_name = 'Alexander' + end +end + +describe FakeController do + context 'validating poa_code for current_user' do + let(:poa_code) { '091' } + let(:first_name) { 'John' } + let(:last_name) { 'Doe' } + let(:phone) { '123-456-7890' } + + context 'when no rep is found' do + it 'returns false' do + ret = subject.valid_poa_code_for_current_user?(poa_code) + expect(ret).to eq(false) + end + end + + context 'when a single match is found by first/last name' do + context 'when the poa_code matches' do + before do + create(:representative, representative_id: '12345', first_name:, last_name:, + poa_codes: [poa_code], phone:) + end + + it 'returns true' do + ret = subject.valid_poa_code_for_current_user?(poa_code) + expect(ret).to eq(true) + end + end + + context 'when the poa_code does not match' do + before do + create(:representative, representative_id: '12345', first_name:, last_name:, + poa_codes: ['ABC'], phone:) + end + + it 'returns false' do + ret = subject.valid_poa_code_for_current_user?(poa_code) + expect(ret).to eq(false) + end + end + end + + context 'when multiple matches are found by first/last name' do + before do + create(:representative, representative_id: '12345', first_name:, last_name:, + middle_initial: 'A', poa_codes: ['091'], phone:) + create(:representative, representative_id: '123456', first_name:, last_name:, + middle_initial: 'B', poa_codes: ['091'], phone:) + end + + it 'searches with middle name' do + res = subject.valid_poa_code_for_current_user?(poa_code) + expect(res).to eq(true) + end + end + + context 'when multiple matches are found by first/last/middle name' do + context 'when a single rep is found' do + before do + create(:representative, representative_id: '12345', first_name:, last_name:, + middle_initial: 'A', poa_codes: ['ABC'], phone:) + create(:representative, representative_id: '123456', first_name:, last_name:, + middle_initial: 'B', poa_codes: ['DEF'], phone:) + create(:representative, representative_id: '1234567', first_name:, last_name:, + middle_initial: 'A', poa_codes: ['091'], phone:) + end + + it 'returns true' do + res = subject.valid_poa_code_for_current_user?(poa_code) + expect(res).to eq(true) + end + end + + context 'when multiple reps are found' do + before do + create(:representative, representative_id: '12345', first_name:, last_name:, + middle_initial: 'A', poa_codes: ['091'], phone:) + create(:representative, representative_id: '123456', first_name:, last_name:, + middle_initial: 'B', poa_codes: ['091'], phone:) + create(:representative, representative_id: '1234567', first_name:, last_name:, + middle_initial: 'A', poa_codes: ['091'], phone:) + end + + it 'raises "Ambiguous VSO Representative Results"' do + expect { subject.valid_poa_code_for_current_user?(poa_code) }.to raise_error(Common::Exceptions::Unauthorized) + end + end + end + end +end