Skip to content

Commit

Permalink
Complete e2e testing and debugging
Browse files Browse the repository at this point in the history
Adds live VCR tests with the status polling endpoint using documents provided by Lighthouse in their QA environment.

Makes necessary changes across service components and in tests to reflect bugs that were found when testing with the actual endpoint
  • Loading branch information
NB28VT committed Mar 25, 2024
1 parent a2476c1 commit c0dac15
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 228 deletions.
10 changes: 1 addition & 9 deletions app/sidekiq/lighthouse/form_526_document_upload_polling_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,8 @@ def perform
) do |document_batch|
lighthouse_document_request_ids = document_batch.pluck(:lighthouse_document_request_id)
response = BenefitsDocuments::Form526::DocumentsStatusPollingService.call(lighthouse_document_request_ids)
# TODO: CATCH POLLING SERVICE TIMEOUT AND FAILURE RESPONSES

# TODO: RESOLVING ISSUES WITH QA ENDPOINT WITH LIGHTHOUSE,
# NEED TO ADDRESS BEFORE WE CAN RECORD VCR CASSETES FOR THESE TESTS,
# CALLER MAY BE DIFFERENT HERE
BenefitsDocuments::Form526::UpdateDocumentsStatusService.call(document_batch, response)

document_batch.update_all(
status_last_polled_at: DateTime.now
)
BenefitsDocuments::Form526::UpdateDocumentsStatusService.call(document_batch, response.body)
end
end
end
Expand Down
23 changes: 18 additions & 5 deletions lib/lighthouse/benefits_documents/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Configuration < Common::Client::Configuration::REST
DOCUMENTS_PATH = 'services/benefits-documents/v1/documents'
DOCUMENTS_STATUS_PATH = 'services/benefits-documents/v1/uploads/status'
TOKEN_PATH = 'oauth2/benefits-documents/system/v1/token'
QA_TESTING_DOMAIN = 'https://dev-api.va.gov'

##
# @return [Config::Options] Settings for benefits_claims API.
Expand Down Expand Up @@ -96,25 +97,37 @@ def generate_upload_body(document_data, file_body)
end

def get_documents_status(lighthouse_document_request_ids)
headers = { 'Authorization' => "Bearer #{access_token}",
'Content-Type' => 'Content-Type: application/json' }
headers = {
'Authorization' => "Bearer #{documents_status_access_token}",
'Content-Type' => 'application/json',
}

body = {
data: {
requestIds: lighthouse_document_request_ids
}
}.to_json

connection.post(DOCUMENTS_STATUS_PATH, body, headers)
document_status_api_connection.post(DOCUMENTS_STATUS_PATH, body, headers)
end

def documents_status_access_token
# Lighthouse is having us test the documents status endpoint on the QA testing domain
ENV['RAILS_ENV'] == 'test' ? access_token(nil, nil, { host: QA_TESTING_DOMAIN }) : access_token
end

def document_status_api_connection
# Lighthouse is having us test the documents status endpoint on the QA testing domain
ENV['RAILS_ENV'] == 'test' ? connection(QA_TESTING_DOMAIN) : connection
end

##
# Creates a Faraday connection with parsing json and breakers functionality.
#
# @return [Faraday::Connection] a Faraday connection instance.
#
def connection
@conn ||= Faraday.new(base_api_path, headers: base_request_headers, request: request_options) do |faraday|
def connection(api_path = base_api_path)
@conn ||= Faraday.new(api_path, headers: base_request_headers, request: request_options) do |faraday|
faraday.use :breakers
faraday.use Faraday::Response::RaiseError

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def call
private

def update_documents_status
JSON.parse(@lighthouse_status_response).dig('data', 'statuses').each do |document_progress|
@lighthouse_status_response.dig('data', 'statuses').each do |document_progress|
update_document_status(document_progress)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def update_status
'BenefitsDocuments::Form526::UploadStatusUpdater',
status: @lighthouse526_document_status['status'],
status_response: @lighthouse526_document_status,
updated_at: DateTime.now
updated_at: DateTime.now.utc
)

if completed? || failed?
Expand All @@ -85,6 +85,8 @@ def update_status
@lighthouse526_document_upload.fail!
end
end

@lighthouse526_document_upload.update!(status_last_polled_at: DateTime.now.utc)
end

def get_failure_step
Expand All @@ -98,7 +100,7 @@ def get_failure_step
def processing_timeout?
return false if @lighthouse526_document_status.dig('time', 'endTime')

start_time < PROCESSING_TIMEOUT_WINDOW_IN_HOURS.hours.ago
start_time < PROCESSING_TIMEOUT_WINDOW_IN_HOURS.hours.ago.utc
end

private
Expand All @@ -107,16 +109,15 @@ def status_changed?
@lighthouse526_document_status != @lighthouse526_document_upload.last_status_response
end

# Lighthouse returns date times as UNIX timestamps in milliseconds
def start_time
# Lighthouse returns date times as UNIX timestamps
unix_start_time = @lighthouse526_document_status.dig('time', 'startTime')
DateTime.strptime(unix_start_time, '%s')
Time.at(unix_start_time).utc.to_datetime
end

def end_time
# Lighthouse returns date times as UNIX timestamps
unix_end_time = @lighthouse526_document_status.dig('time', 'endTime')
DateTime.strptime(unix_end_time, '%s')
Time.at(unix_end_time).utc.to_datetime
end

def failed?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
'requestId' => pending_document_upload.lighthouse_document_request_id,
'status' => 'SUCCESS',
'time' => {
'startTime' => '499152030',
'endTime' => '499152060'
'startTime' => 499152030,
'endTime' => 499152060
},
'steps' => [
{
Expand All @@ -35,7 +35,7 @@
}
]
}
}.to_json
}
end

it 'transitions that document to the complete state' do
Expand All @@ -62,8 +62,8 @@
'requestId' => pending_document_upload.lighthouse_document_request_id,
'status' => 'FAILED',
'time' => {
'startTime' => '499152030',
'endTime' => '499152060'
'startTime' => 499152030,
'endTime' => 499152060
},
'steps' => [
{
Expand All @@ -82,7 +82,7 @@
}
]
}
}.to_json
}
end

it 'transitions the document to the failed state' do
Expand Down Expand Up @@ -110,7 +110,7 @@
'requestId' => pending_document_upload.lighthouse_document_request_id,
'status' => 'IN_PROGRESS',
'time' => {
'startTime' => lighthouse_processing_start_time.to_time.to_i.to_s,
'startTime' => lighthouse_processing_start_time.to_time.to_i,
'endTime' => nil
},
'steps' => [
Expand All @@ -126,7 +126,7 @@
}
]
}
}.to_json
}
end

context 'when it has been more than 24 hours since Lighthouse started processing the document' do
Expand Down Expand Up @@ -164,13 +164,13 @@ def mock_success_response(request_id)
'requestId' => request_id,
'status' => 'SUCCESS',
'time' => {
'startTime' => '499152030',
'endTime' => '499152060'
'startTime' => 499152030,
'endTime' => 499152060
}
}
]
}
}.to_json
}
end

before do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

context 'when the document is completed' do
# Lighthouse returns datetimes as UNIX timestamps
let(:start_time) { '499152060' }
let(:end_time) { '499153000' }
let(:unix_start_time) { 499152060 }
let(:unix_end_time) { 499153000 }

let(:completed_document_status) do
{
'status' => 'SUCCESS',
'time' => {
'startTime' => start_time,
'endTime' => end_time
'startTime' => unix_start_time,
'endTime' => unix_end_time
},
'steps' => [
{
Expand All @@ -41,15 +41,15 @@

expect { status_updater.update_status }.to change(
lighthouse526_document_upload, :lighthouse_processing_started_at
).to(DateTime.strptime(start_time, '%s'))
).to(Time.at(unix_start_time).utc.to_datetime)
end

it 'saves a lighthouse_processing_ended_at time' do
status_updater = described_class.new(completed_document_status, lighthouse526_document_upload)

expect { status_updater.update_status }.to change(
lighthouse526_document_upload, :lighthouse_processing_ended_at
).to(DateTime.strptime(end_time, '%s'))
).to(Time.at(unix_end_time).utc.to_datetime)
end

it 'transitions the document to a complete state' do
Expand Down Expand Up @@ -79,19 +79,28 @@
status_updater.update_status
end
end

it 'updates the status_last_polled_at time on the document' do
Timecop.freeze(DateTime.new(1985, 10, 26)) do
status_updater = described_class.new(completed_document_status, lighthouse526_document_upload)
status_updater.update_status

expect(lighthouse526_document_upload.status_last_polled_at).to eq(DateTime.new(1985, 10, 26))
end
end
end

context 'when the document has failed' do
# Lighthouse returns datetimes as UNIX timestamps
let(:start_time) { '499152060' }
let(:end_time) { '499153000' }
let(:unix_start_time) { 499152060 }
let(:unix_end_time) { 499153000 }

let(:failed_document_status) do
{
'status' => 'FAILED',
'time' => {
'startTime' => start_time,
'endTime' => end_time
'startTime' => unix_start_time,
'endTime' => unix_end_time
},
'steps' => [
{
Expand All @@ -115,15 +124,15 @@

expect { status_updater.update_status }.to change(
lighthouse526_document_upload, :lighthouse_processing_started_at
).to(DateTime.strptime(start_time, '%s'))
).to(Time.at(unix_start_time).utc.to_datetime)
end

it 'saves a lighthouse_processing_ended_at time' do
status_updater = described_class.new(failed_document_status, lighthouse526_document_upload)

expect { status_updater.update_status }.to change(
lighthouse526_document_upload, :lighthouse_processing_ended_at
).to(DateTime.strptime(end_time, '%s'))
).to(Time.at(unix_end_time).utc.to_datetime)
end

it 'transitions the document to a failed state' do
Expand Down Expand Up @@ -165,6 +174,15 @@
status_updater.update_status
end
end

it 'updates the status_last_polled_at time on the document' do
Timecop.freeze(DateTime.new(1985, 10, 26)) do
status_updater = described_class.new(failed_document_status, lighthouse526_document_upload)
status_updater.update_status

expect(lighthouse526_document_upload.status_last_polled_at).to eq(DateTime.new(1985, 10, 26))
end
end
end

context 'when the document is in progress' do
Expand All @@ -177,13 +195,13 @@
end

# Lighthouse returns datetimes as UNIX timestamps
let(:start_time) { '499152060' }
let(:unix_start_time) { 499152060 }

let(:in_progress_document_status) do
{
'status' => 'IN_PROGRESS',
'time' => {
'startTime' => start_time,
'startTime' => unix_start_time,
'endTime' => nil
},
'steps' => [
Expand Down Expand Up @@ -212,7 +230,7 @@

expect { status_updater.update_status }.to change(
lighthouse526_new_document_upload, :lighthouse_processing_started_at
).to(DateTime.strptime(start_time, '%s'))
).to(Time.at(unix_start_time).utc.to_datetime)
end

it 'saves the last_status_response' do
Expand Down Expand Up @@ -243,8 +261,8 @@
{
'status' => 'FAILED',
'time' => {
'startTime' => '499152060',
'endTime' => '499153000'
'startTime' => 499152060,
'endTime' => 499153000
},
'steps' => [
{
Expand Down Expand Up @@ -272,14 +290,14 @@
describe '#processing_timeout?' do
context 'when the document has been in progress for more than 24 hours' do
it 'returns true' do
Timecop.freeze(DateTime.new(1985, 10, 26)) do
Timecop.freeze(DateTime.new(1985, 10, 26).utc) do
# Lighthouse returns datetimes as UNIX timestamps
start_time = DateTime.new(1985, 10, 23).to_time.to_i.to_s
unix_start_time = DateTime.new(1985, 10, 23).to_time.to_i

delayed_document_status = {
'status' => 'IN_PROGRESS',
'time' => {
'startTime' => start_time,
'startTime' => unix_start_time,
'endTime' => nil
},
'steps' => [
Expand All @@ -302,14 +320,14 @@

context 'when the document has been in progress for less than 24 hours' do
it 'returns false' do
Timecop.freeze(DateTime.new(1985, 10, 26)) do
Timecop.freeze(DateTime.new(1985, 10, 26).utc) do
# Lighthouse returns datetimes as UNIX timestamps
start_time = DateTime.new(1985, 10, 25, 20).to_time.to_i.to_s
unix_start_time = DateTime.new(1985, 10, 25, 20).utc.to_time.to_i

in_progress_document_status = {
'status' => 'IN_PROGRESS',
'time' => {
'startTime' => start_time,
'startTime' => unix_start_time,
'endTime' => nil
},
'steps' => [
Expand Down
Loading

0 comments on commit c0dac15

Please sign in to comment.