Skip to content

Commit

Permalink
Merge branch 'master' into VI-252_map_sts_validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bramleyjl authored Jan 2, 2025
2 parents c4b3586 + 2e05fd9 commit dea2155
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/danger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
bundler-cache: true

- name: Run Danger
run: bundle exec danger --head=${{ github.sha }} --base=${{ github.event.pull_request.base.sha }} --verbose
run: bundle exec danger --verbose

- name: Add Danger Label
uses: actions-ecosystem/action-remove-labels@v1
Expand Down
4 changes: 2 additions & 2 deletions Dangerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'ostruct'

module VSPDanger
HEAD_SHA = `git rev-parse --abbrev-ref HEAD`.chomp.freeze
BASE_SHA = 'origin/master'
HEAD_SHA = ENV.fetch('GITHUB_HEAD_REF', '').empty? ? `git rev-parse --abbrev-ref HEAD`.chomp.freeze : "origin/#{ENV.fetch('GITHUB_HEAD_REF')}"
BASE_SHA = ENV.fetch('GITHUB_BASE_REF', '').empty? ? 'origin/master' : "origin/#{ENV.fetch('GITHUB_BASE_REF')}"

class Runner
def self.run
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ group :development, :test do
gem 'guard-rspec'
gem 'parallel_tests'
gem 'pry-byebug'
gem 'rack-test', '2.1.0', require: 'rack/test'
gem 'rack-test', '2.2.0', require: 'rack/test'
gem 'rack-vcr'
gem 'rainbow' # Used to colorize output for rake tasks
gem 'rspec-instrumentation-matcher'
Expand Down
13 changes: 7 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ GEM
i18n
benchmark (0.4.0)
bigdecimal (3.1.9)
bigdecimal (3.1.9-java)
bindex (0.8.1)
blind_index (2.6.1)
activesupport (>= 7)
argon2-kdf (>= 0.2)
blueprinter (1.1.2)
bootsnap (1.18.4)
msgpack (~> 1.2)
brakeman (6.2.2)
brakeman (7.0.0)
racc
breakers (0.7.1)
faraday (>= 1.2.0, < 3.0)
Expand Down Expand Up @@ -651,7 +652,7 @@ GEM
llhttp-ffi (0.5.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
lockbox (2.0.0)
lockbox (2.0.1)
logger (1.6.4)
loofah (2.23.1)
crass (~> 1.0.2)
Expand Down Expand Up @@ -816,7 +817,7 @@ GEM
rack (~> 2.2, >= 2.2.4)
rack-session (1.0.2)
rack (< 3)
rack-test (2.1.0)
rack-test (2.2.0)
rack (>= 1.3)
rack-timeout (0.7.0)
rack-vcr (0.1.6)
Expand Down Expand Up @@ -873,7 +874,7 @@ GEM
connection_pool
redis-namespace (1.11.0)
redis (>= 4)
regexp_parser (2.9.3)
regexp_parser (2.10.0)
reline (0.6.0)
io-console (~> 0.5)
representable (3.2.0)
Expand Down Expand Up @@ -964,7 +965,7 @@ GEM
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-junit-formatter (0.1.4)
rubocop-rails (2.27.0)
rubocop-rails (2.28.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
Expand Down Expand Up @@ -1272,7 +1273,7 @@ DEPENDENCIES
rack
rack-attack
rack-cors
rack-test (= 2.1.0)
rack-test (= 2.2.0)
rack-timeout
rack-vcr
rails (~> 7.2.2)
Expand Down
3 changes: 2 additions & 1 deletion app/models/persistent_attachments/va_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class PersistentAttachments::VAForm < PersistentAttachment
{ max_pages: 10, min_pages: 1 }
).merge(
{
'21-0779' => { max_pages: 4, min_pages: 2 }
'21-0779' => { max_pages: 4, min_pages: 2 },
'21-509' => { max_pages: 4, min_pages: 2 }
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def self.sco_emails(scos)

def get_institution(facility_code)
GI::Client.new.get_institution_details_v0({ id: facility_code }).body[:data][:attributes]
rescue Common::Exceptions::RecordNotFound
StatsD.increment("#{stats_key}.skipped.institution_not_approved")
nil
end

def school_changed?
Expand Down
2 changes: 1 addition & 1 deletion app/uploaders/form_upload/uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FormUpload::Uploader < VetsShrine

Attacher.validate do
validate_virus_free
validate_max_size 20.megabytes
validate_max_size 25.megabytes
validate_min_size 1.kilobyte
validate_mime_type_inclusion %w[image/jpg image/jpeg image/png application/pdf]
validate_max_width 5000 if get.width
Expand Down
6 changes: 0 additions & 6 deletions config/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ development: &defaults
namespace: mpi-profile-response
each_ttl: 86400
failure_ttl: 1800
okta_response:
namespace: okta-response
each_ttl: 3600
profile:
namespace: profile
each_ttl: 3600
Expand All @@ -78,9 +75,6 @@ development: &defaults
charon_response:
namespace: charon-response
each_ttl: 3600
okta_response_app:
namespace: okta-response
each_ttl: 86400
saml_store:
namespace: single-logout-request
each_ttl: 43200
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ intent_to_file:

ivc_champva:
prefill: true
pega_api:
api_key: fake_api_key

form_upload_flow:
prefill: true
Expand Down
58 changes: 58 additions & 0 deletions modules/ivc_champva/lib/pega_api/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'common/client/base'
require_relative 'configuration'

module IvcChampva
module PegaApi
class PegaApiError < StandardError; end

class Client < Common::Client::Base
configuration IvcChampva::PegaApi::Configuration

##
# HTTP POST call to the Pega API to retrieve a report
#
# @param date_start [Date, nil] the start date of the report
# @param date_end [Date, nil] the end date of the report
# @return [Array<Hash>] the report rows
def get_report(date_start, date_end)
resp = connection.post(config.base_path) do |req|
req.headers = headers(date_start, date_end)
end

raise "response code: #{resp.status}, response body: #{resp.body}" unless resp.status == 200

# We also need to check the StatusCode in the response body.
# It seems that when this API errors out, it will return responses with HTTP 200 statuses, but
# the StatusCode in the response body will be something other than 200.
response = JSON.parse(resp.body, symbolize_names: false)
unless response['statusCode'] == 200
raise "alternate response code: #{response['statusCode']}, response body: #{response['body']}"
end

# With both status codes checked and passing, we should now have a body that is more JSON embedded in a string.
# This is our report, let's decode it.
JSON.parse(response['body'])
rescue => e
raise PegaApiError, e.message.to_s
end

##
# Assembles headers for the Pega API request
#
# @param date_start [Date, nil] the start date of the report
# @param date_end [Date, nil] the end date of the report
# @return [Hash] the headers
def headers(date_start, date_end)
{
:content_type => 'application/json',
'x-api-key' => Settings.ivc_champva.pega_api.api_key.to_s,
'date_start' => date_start.to_s,
'date_end' => date_end.to_s,
'case_id' => '' # case_id seems to have no effect, but it is required by the API
}
end
end
end
end
29 changes: 29 additions & 0 deletions modules/ivc_champva/lib/pega_api/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'common/client/configuration/rest'
require 'common/client/middleware/response/raise_custom_error'

module IvcChampva
module PegaApi
class Configuration < Common::Client::Configuration::REST
def base_path
'https://bt41mfpkj5.execute-api.us-gov-west-1.amazonaws.com/prod/'
end

def connection
Faraday.new(base_path, headers: base_request_headers, request: request_options) do |conn|
conn.use :breakers
# conn.use :instrumentation, name: 'dhp.fitbit.request.faraday'

# Uncomment this if you want curlggg command equivalent or response output to log
# conn.request(:curl, ::Logger.new(STDOUT), :warn) unless Rails.env.production?
# conn.response(:logger, ::Logger.new(STDOUT), bodies: true) unless Rails.env.production?

# conn.response :raise_custom_error, error_prefix: service_name

conn.adapter Faraday.default_adapter
end
end
end
end
end

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"statusCode": 500,
"body": "'case_id'"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Forbidden"
}
122 changes: 122 additions & 0 deletions modules/ivc_champva/spec/lib/pega_api_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# frozen_string_literal: true

require 'rails_helper'
require 'pega_api/client'

RSpec.describe IvcChampva::PegaApi::Client do
subject { described_class.new }

describe 'get_report' do
let(:body200and200) do # pega api response with HTTP status 200 and alternate status 200
fixture_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pega_api_json',
'report_response_200_200.json')
fixture_path.read
end

let(:body200and500) do # pega api response with HTTP status 200 and alternate status 500
fixture_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pega_api_json',
'report_response_200_500.json')
fixture_path.read
end

let(:body403) do # pega api response with HTTP status 403 forbidden
fixture_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pega_api_json',
'report_response_403.json')
fixture_path.read
end

context 'successful response from pega' do
let(:faraday_response) { double('Faraday::Response', status: 200, body: body200and200) }

before do
allow_any_instance_of(Faraday::Connection).to receive(:post).with(anything).and_return(faraday_response)
end

it 'returns the body as an array of hashes' do
result = subject.get_report(Date.new(2024, 11, 1), Date.new(2024, 12, 31))

expect(result[0]['Creation Date']).to eq('2024-11-27T08:42:11.372000')
expect(result[0]['PEGA Case ID']).to eq('D-55824')
expect(result[0]['Status']).to eq('Open')
end
end

context 'unsuccessful pega response with bad HTTP status' do
let(:faraday_response) { double('Faraday::Response', status: 403, body: body403) }

before do
allow_any_instance_of(Faraday::Connection).to receive(:post).with(anything).and_return(faraday_response)
end

it 'raises error when response is 404' do
expect { subject.get_report(nil, nil) }.to raise_error(IvcChampva::PegaApi::PegaApiError)
end
end

context 'unsuccessful pega response with bad alternate status' do
let(:faraday_response) { double('Faraday::Response', status: 200, body: body200and500) }

before do
allow_any_instance_of(Faraday::Connection).to receive(:post).with(anything).and_return(faraday_response)
end

it 'raises error when alternate status is 500' do
expect { subject.get_report(nil, nil) }.to raise_error(IvcChampva::PegaApi::PegaApiError)
end
end
end

describe 'headers' do
it 'returns the right headers' do
result = subject.headers(Date.new(2024, 11, 1), Date.new(2024, 12, 31))

expect(result[:content_type]).to eq('application/json')
expect(result['x-api-key']).to eq('fake_api_key')
expect(result['date_start']).to eq('2024-11-01')
expect(result['date_end']).to eq('2024-12-31')
expect(result['case_id']).to eq('')
end

it 'returns the right headers with nil dates' do
result = subject.headers(nil, nil)

expect(result[:content_type]).to eq('application/json')
expect(result['x-api-key']).to eq('fake_api_key')
expect(result['date_start']).to eq('')
expect(result['date_end']).to eq('')
expect(result['case_id']).to eq('')
end
end

# Temporary, delete me
# This test is used to hit the production endpoint when running locally.
# It can be removed once we have some real code that uses the Pega API client.
describe 'hit the production endpoint', skip: 'this is useful as a way to hit the API during local development' do
let(:forced_headers) do
{
:content_type => 'application/json',
# use the following line when running locally tp pull the key from an environment variable
'x-api-key' => ENV.fetch('PEGA_API_KEY'), # to set: export PEGA_API_KEY=insert1the2api3key4here
'date_start' => '', # '2024-11-01', # '11/01/2024',
'date_end' => '', # '2024-12-31', # '12/07/2024',
'case_id' => ''
}
end

before do
allow_any_instance_of(IvcChampva::PegaApi::Client).to receive(:headers).with(anything, anything)
.and_return(forced_headers)
end

it 'returns report data' do
VCR.configure do |c|
c.allow_http_connections_when_no_cassette = true
end

result = subject.get_report(Date.new(2024, 11, 1), Date.new(2024, 12, 31))
expect(result.count).to be_positive

# byebug # in byebug, type 'p result' to view the response
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def upload_response
stamper.stamp_pdf
metadata = validated_metadata
status, confirmation_number = upload_pdf(file_path, metadata)
file_size = File.size(file_path).to_f / (2**20)

Rails.logger.info(
'Simple forms api - scanned form uploaded',
{ form_number: params[:form_number], status:, confirmation_number:, file_size: }
)
{ confirmation_number:, status: }
end

Expand Down
Loading

0 comments on commit dea2155

Please sign in to comment.