Skip to content

Commit

Permalink
Modified letters generator route to be POST and updated tests (#12794)
Browse files Browse the repository at this point in the history
* modified letters generator route to be POST and updated tests

* updated letters generator download endpoint to work with POST requests and Benefits Summary options

* restored boolean casting and selection in letters generator controller

* fixing option filtering
  • Loading branch information
samcoforma authored Jun 6, 2023
1 parent 32de1a0 commit 9bf5b9b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
24 changes: 13 additions & 11 deletions app/controllers/v0/letters_generator_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ class LettersGeneratorController < ApplicationController
DOWNLOAD_PARAMS = %i[
id
format
militaryService
serviceConnectedDisabilities
serviceConnectedEvaluation
nonServiceConnectedPension
monthlyAward
military_service
service_connected_disabilities
service_connected_evaluation
non_service_connected_pension
monthly_award
unemployable
specialMonthlyCompensation
adaptedHousing
chapter35Eligibility
deathResultOfDisability
survivorsAward
special_monthly_compensation
adapted_housing
chapter35_eligibility
death_result_of_disability
survivors_award
letters_generator
].freeze

def index
Expand All @@ -32,8 +33,9 @@ def download
permitted_params = params.permit(DOWNLOAD_PARAMS)
letter_options =
permitted_params.to_h
.select { |_, v| v == 'true' }
.except('id')
.transform_values { |v| ActiveModel::Type::Boolean.new.cast(v) }
.transform_keys { |k| k.camelize(:lower) }
response = service.download_letter(@current_user.icn, params[:id], letter_options)
send_data response,
filename: "#{params[:id]}.pdf",
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
resources :letters_generator, only: [:index] do
collection do
get 'beneficiary', to: 'letters_generator#beneficiary'
get 'download/:id', to: 'letters_generator#download'
post 'download/:id', to: 'letters_generator#download'
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/lighthouse/letters_generator/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,13 @@ def download_letter(icn, letter_type, options = {})
end

endpoint = "letters/#{letter_type}/letter"
letter_options = options.select { |_, v| v == true }

begin
log = "Retrieving benefit information from #{config.generator_url}/#{endpoint}"
response = Lighthouse::LettersGenerator.measure_time(log) do
config.connection.get(
endpoint,
{ icn: }.merge(letter_options),
{ icn: }.merge(options),
{ Authorization: "Bearer #{config.get_access_token}" }
)
end
Expand Down
27 changes: 20 additions & 7 deletions spec/controllers/v0/letters_generator_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

it 'returns a pdf' do
VCR.use_cassette('lighthouse/letters_generator/download') do
get :download, params: { id: 'BENEFIT_SUMMARY' }
post :download, params: { id: 'BENEFIT_SUMMARY' }

expect(response.header['Content-Type']).to eq('application/pdf')
end
Expand All @@ -45,13 +45,26 @@
context 'with options' do
before { sign_in_as(user) }

let(:options) do
{
id: 'BENEFIT_SUMMARY',
'military_service' => true,
'service_connected_disabilities' => true,
'service_connected_evaluation' => false,
'non_service_connected_pension' => false,
'monthly_award' => false,
'unemployable' => false,
'special_monthly_compensation' => false,
'adapted_housing' => false,
'chapter35_eligibility' => false,
'death_result_of_disability' => false,
'survivors_award' => false
}
end

it 'returns a pdf' do
VCR.use_cassette('lighthouse/letters_generator/download_with_options') do
get :download, params: {
id: 'BENEFIT_SUMMARY',
militaryService: 'true',
serviceConnectedDisabilities: 'true'
}
post :download, params: options
expect(response.header['Content-Type']).to eq('application/pdf')
end
end
Expand All @@ -62,7 +75,7 @@

it 'raises Lighthouse::LettersGenerator::ServiceError' do
VCR.use_cassette('lighthouse/letters_generator/download_error') do
get :download, params: { id: 'BENEFIT_SUMMARY' }
post :download, params: { id: 'BENEFIT_SUMMARY' }
response_body = JSON.parse(response.body)
expect(response_body['errors'].first).to include('status' => '422')
end
Expand Down

Large diffs are not rendered by default.

0 comments on commit 9bf5b9b

Please sign in to comment.