-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathpoa_vbms_updater_spec.rb
277 lines (233 loc) · 9.86 KB
/
poa_vbms_updater_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true
require 'rails_helper'
require 'bgs_service/corporate_update_web_service'
RSpec.describe ClaimsApi::PoaVBMSUpdater, type: :job do
subject { described_class }
[true, false].each do |flipped|
before do
Sidekiq::Job.clear_all
allow(Flipper).to receive(:enabled?).with(:claims_api_poa_vbms_updater_uses_local_bgs).and_return(flipped)
@clazz = if flipped
ClaimsApi::CorporateUpdateWebService
else
BGS::Services
end
@corporate_update_stub = if flipped
@clazz.new(external_uid: 'uid', external_key: 'key')
else
@clazz.new(external_uid: 'uid', external_key: 'key').corporate_update
end
end
let(:user) { create(:user, :loa3) }
let(:auth_headers) do
headers = EVSS::DisabilityCompensationAuthHeaders.new(user).add_headers(EVSS::AuthHeaders.new(user).to_h)
headers['va_eauth_pnid'] = '796104437'
headers
end
it 'sets retry_for to 48 hours' do
expect(described_class.get_sidekiq_options['retry_for']).to eq(48.hours)
end
context 'when address change is present and allowed' do
let(:allow_poa_c_add) { 'Y' }
let(:consent_address_change) { true }
it 'updates a the BIRLS record for a qualifying POA submittal' do
poa = create_poa(allow_poa_access: true)
create_mock_lighthouse_service
subject.new.perform(poa.id)
process = ClaimsApi::Process.find_by(processable: poa, step_type: 'POA_ACCESS_UPDATE')
expect(process.step_status).to eq('SUCCESS')
end
end
context 'when address change is present and not allowed' do
let(:allow_poa_c_add) { 'N' }
let(:consent_address_change) { false }
it 'updates a the BIRLS record for a qualifying POA submittal' do
poa = create_poa(allow_poa_access: true)
create_mock_lighthouse_service
subject.new.perform(poa.id)
process = ClaimsApi::Process.find_by(processable: poa, step_type: 'POA_ACCESS_UPDATE')
expect(process.step_status).to eq('SUCCESS')
end
end
context 'consent details' do
let(:allow_poa_c_add) { 'Y' }
let(:consent_address_change) { true }
it 'logs correct with consentLimits included' do
poa = create_poa(allow_poa_access: true)
poa.form_data['consentLimits'] = ['HIV']
poa.save
create_mock_lighthouse_service_no_access
poa_code = poa.form_data['serviceOrganization']['poaCode']
consent_msg = 'Updating Access. recordConsent: true' \
', consentLimits included ' \
"for representative #{poa_code}"
detail_msg = ClaimsApi::ServiceBase.new.send(:form_logger_consent_detail, poa, poa_code)
expect(detail_msg).to eq(consent_msg)
subject.new.perform(poa.id)
end
end
context 'when address change is not present' do
let(:allow_poa_c_add) { 'N' }
let(:consent_address_change) { nil }
it 'updates a the BIRLS record for a qualifying POA submittal' do
poa = create_poa(allow_poa_access: true)
create_mock_lighthouse_service
subject.new.perform(poa.id)
process = ClaimsApi::Process.find_by(processable: poa, step_type: 'POA_ACCESS_UPDATE')
expect(process.step_status).to eq('SUCCESS')
end
end
context 'when BGS fails the error is handled' do
let(:allow_poa_c_add) { 'Y' }
let(:consent_address_change) { true }
it 'marks the form as errored' do
poa = create_poa(allow_poa_access: true)
create_mock_lighthouse_service_bgs_failure # API-31645 real life example
subject.new.perform(poa.id)
poa.reload
expect(poa.status).to eq('errored')
expect(poa.vbms_error_message).to eq('updatePoaAccess: No POA found on system of record')
process = ClaimsApi::Process.find_by(processable: poa, step_type: 'POA_ACCESS_UPDATE')
expect(process.step_status).to eq('FAILED')
end
end
context 'deciding to send a VA Notify email' do
let(:allow_poa_c_add) { 'Y' }
let(:consent_address_change) { true }
before do
create_mock_lighthouse_service
allow(Flipper).to receive(:enabled?).with(:lighthouse_claims_api_v2_poa_va_notify).and_return true
end
let(:poa) { create_poa }
let(:header_key) { ClaimsApi::V2::Veterans::PowerOfAttorney::BaseController::VA_NOTIFY_KEY }
context 'when the header key and rep are present' do
it 'sends the vanotify job' do
poa.auth_headers.merge!({
header_key => 'this_value'
})
poa.save!
allow_any_instance_of(ClaimsApi::ServiceBase).to receive(:vanotify?).and_return true
expect(ClaimsApi::VANotifyAcceptedJob).to receive(:perform_async)
subject.new.perform(poa.id, 'Rep Data')
end
end
context 'when the flipper is off' do
it 'does not send the vanotify job' do
allow(Flipper).to receive(:enabled?).with(:lighthouse_claims_api_v2_poa_va_notify).and_return false
Flipper.disable(:lighthouse_claims_api_v2_poa_va_notify)
poa.auth_headers.merge!({
header_key => 'this_value'
})
poa.save!
expect(ClaimsApi::VANotifyAcceptedJob).not_to receive(:perform_async)
subject.new.perform(poa.id, 'Rep Data')
end
end
context 'does not send the va notify job' do
it 'when the rep is not present' do
poa.auth_headers.merge!({
header_key => 'this_value'
})
poa.save!
expect(ClaimsApi::VANotifyAcceptedJob).not_to receive(:perform_async)
subject.new.perform(poa.id, nil)
end
it 'when the header key is not present' do
expect(ClaimsApi::VANotifyAcceptedJob).not_to receive(:perform_async)
subject.new.perform(poa.id, 'Rep data')
end
end
end
context 'inability to save the poa form' do
let(:allow_poa_c_add) { 'Y' }
let(:consent_address_change) { true }
it 'logs to the ClaimsApi logger & re-raises' do
err_msg = Faker::Lorem.sentence
Sidekiq::Testing.inline! do
poa = create_poa(allow_poa_access: true)
create_mock_lighthouse_service
allow_any_instance_of(ClaimsApi::PowerOfAttorney).to receive(:save!).and_raise StandardError.new(err_msg)
allow(ClaimsApi::Logger).to receive(:log) # Ignore other logger calls we're not testing for, if they exist
expect(ClaimsApi::Logger).to receive(:log).with(
'poa_vbms_updater',
poa_id: poa.id,
detail: 'Re-raising Error',
error: err_msg
)
expect { subject.new.perform(poa.id) }.to raise_error StandardError
end
end
end
context 'when an errored job has exhausted its retries' do
let(:allow_poa_c_add) { 'Y' }
let(:consent_address_change) { true }
it 'logs to the ClaimsApi Logger' do
poa = create_poa(allow_poa_access: true)
error_msg = 'An error occurred for the POA VBMS Updater Job'
msg = { 'args' => [poa.id],
'class' => subject,
'error_message' => error_msg }
described_class.within_sidekiq_retries_exhausted_block(msg) do
expect(ClaimsApi::Logger).to receive(:log).with(
'claims_api_retries_exhausted',
record_id: poa.id,
detail: "Job retries exhausted for #{subject}",
error: error_msg
)
end
end
end
end
private
def create_poa(allow_poa_access: true)
poa = create(:power_of_attorney)
poa.auth_headers = auth_headers
if consent_address_change.present?
poa.form_data = poa.form_data.merge('consentAddressChange' => consent_address_change)
end
poa.form_data = poa.form_data.merge('recordConsent' => allow_poa_access)
poa.save
poa
end
def create_mock_lighthouse_service
expect(@corporate_update_stub).to receive(:update_poa_access).with(
participant_id: user.participant_id,
poa_code: '074',
allow_poa_access: 'Y',
allow_poa_c_add:
).and_return({ return_code: 'GUIE50000' })
service_double = instance_double(BGS::Services)
expect(service_double).to receive(:corporate_update).and_return(@corporate_update_stub)
expect(BGS::Services).to receive(:new).and_return(service_double)
end
def create_mock_lighthouse_service_no_access
expect(@corporate_update_stub).to receive(:update_poa_access).with(
participant_id: user.participant_id,
poa_code: '074',
allow_poa_access: 'N',
allow_poa_c_add:
).and_return({ return_code: 'GUIE50000' })
service_double = instance_double(BGS::Services)
expect(service_double).to receive(:corporate_update).and_return(@corporate_update_stub)
expect(BGS::Services).to receive(:new).and_return(service_double)
end
def create_mock_lighthouse_service_bgs_failure
allow_any_instance_of(@clazz).to receive(:corporate_update) do |_instance|
corporate_update_stub = instance_double(BGS::CorporateUpdate)
allow(corporate_update_stub).to receive(:update_poa_access)
.with(
participant_id: user.participant_id,
poa_code: '074',
allow_poa_access: 'Y',
allow_poa_c_add:
).and_return({ return_code: 'GUIE50000' })
corporate_update_stub
end
allow(@clazz).to receive(:new) do
services_double = instance_double(BGS::Services)
allow(services_double).to receive(:corporate_update)
.and_raise(BGS::ShareError.new('updatePoaAccess: No POA found on system of record'))
services_double
end
end
end