From 996918a395cae10a8540c87f56fa0df7aa56fdb9 Mon Sep 17 00:00:00 2001 From: Dan Hinze Date: Thu, 5 Sep 2024 16:17:21 -0500 Subject: [PATCH] Add travel_pay/v0/claims route (same as travel_pay/claims) (#18319) --- .../travel_pay/claims_controller.rb | 30 ------------ .../travel_pay/v0/claims_controller.rb | 32 +++++++++++++ modules/travel_pay/config/routes.rb | 8 +++- .../spec/requests/travel_pay/claims_spec.rb | 46 ++++++++++++++----- 4 files changed, 74 insertions(+), 42 deletions(-) delete mode 100644 modules/travel_pay/app/controllers/travel_pay/claims_controller.rb create mode 100644 modules/travel_pay/app/controllers/travel_pay/v0/claims_controller.rb diff --git a/modules/travel_pay/app/controllers/travel_pay/claims_controller.rb b/modules/travel_pay/app/controllers/travel_pay/claims_controller.rb deleted file mode 100644 index 0d801ac892f..00000000000 --- a/modules/travel_pay/app/controllers/travel_pay/claims_controller.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -module TravelPay - class ClaimsController < ApplicationController - def index - begin - claims = service.get_claims(@current_user) - rescue Faraday::Error => e - TravelPay::ServiceError.raise_mapped_error(e) - end - - render json: claims, status: :ok - end - - private - - def service - @service ||= TravelPay::Service.new - end - - def common_exception(e) - case e - when Faraday::ResourceNotFound - Common::Exceptions::ResourceNotFound.new - else - Common::Exceptions::InternalServerError.new - end - end - end -end diff --git a/modules/travel_pay/app/controllers/travel_pay/v0/claims_controller.rb b/modules/travel_pay/app/controllers/travel_pay/v0/claims_controller.rb new file mode 100644 index 00000000000..d7c64b2ec86 --- /dev/null +++ b/modules/travel_pay/app/controllers/travel_pay/v0/claims_controller.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module TravelPay + module V0 + class ClaimsController < ApplicationController + def index + begin + claims = service.get_claims(@current_user) + rescue Faraday::Error => e + TravelPay::ServiceError.raise_mapped_error(e) + end + + render json: claims, status: :ok + end + + private + + def service + @service ||= TravelPay::Service.new + end + + def common_exception(e) + case e + when Faraday::ResourceNotFound + Common::Exceptions::ResourceNotFound.new + else + Common::Exceptions::InternalServerError.new + end + end + end + end +end diff --git a/modules/travel_pay/config/routes.rb b/modules/travel_pay/config/routes.rb index a142e0ba333..34f50abcbc1 100644 --- a/modules/travel_pay/config/routes.rb +++ b/modules/travel_pay/config/routes.rb @@ -1,5 +1,11 @@ # frozen_string_literal: true TravelPay::Engine.routes.draw do - resources :claims + # TODO: remove this mapping once vets-website + # is pointing to the /v0/claims routes + resources :claims, controller: '/travel_pay/v0/claims' + + namespace :v0 do + resources :claims + end end diff --git a/modules/travel_pay/spec/requests/travel_pay/claims_spec.rb b/modules/travel_pay/spec/requests/travel_pay/claims_spec.rb index 1b665428b75..bef8295091d 100644 --- a/modules/travel_pay/spec/requests/travel_pay/claims_spec.rb +++ b/modules/travel_pay/spec/requests/travel_pay/claims_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe 'TravelPay::Claims', type: :request do +RSpec.describe TravelPay::V0::ClaimsController, type: :request do let(:user) { build(:user) } before do @@ -19,21 +19,45 @@ ] end - it 'responds with 200' do - VCR.use_cassette('travel_pay/200_claims', match_requests_on: %i[method path]) do - get '/travel_pay/claims', params: nil, headers: { 'Authorization' => 'Bearer vagov_token' } - expect(response).to have_http_status(:ok) - claim_ids = JSON.parse(response.body)['data'].pluck('id') - expect(claim_ids).to eq(expected_claim_ids) + context '(Older) unversioned API route' do + it 'responds with 200' do + VCR.use_cassette('travel_pay/200_claims', match_requests_on: %i[method path]) do + get '/travel_pay/claims', params: nil, headers: { 'Authorization' => 'Bearer vagov_token' } + expect(response).to have_http_status(:ok) + claim_ids = JSON.parse(response.body)['data'].pluck('id') + expect(claim_ids).to eq(expected_claim_ids) + end + end + end + + context 'Versioned v0 API route' do + it 'responds with 200' do + VCR.use_cassette('travel_pay/200_claims', match_requests_on: %i[method path]) do + get '/travel_pay/v0/claims', params: nil, headers: { 'Authorization' => 'Bearer vagov_token' } + expect(response).to have_http_status(:ok) + claim_ids = JSON.parse(response.body)['data'].pluck('id') + expect(claim_ids).to eq(expected_claim_ids) + end end end end context 'unsuccessful response from API' do - it 'responds with a 404 if the API endpoint is not found' do - VCR.use_cassette('travel_pay/404_claims', match_requests_on: %i[method path]) do - get '/travel_pay/claims', params: nil, headers: { 'Authorization' => 'Bearer vagov_token' } - expect(response).to have_http_status(:bad_request) + context '(Older) unversioned API route' do + it 'responds with a 404 if the API endpoint is not found' do + VCR.use_cassette('travel_pay/404_claims', match_requests_on: %i[method path]) do + get '/travel_pay/claims', params: nil, headers: { 'Authorization' => 'Bearer vagov_token' } + expect(response).to have_http_status(:bad_request) + end + end + end + + context 'Versioned v0 API route' do + it 'responds with a 404 if the API endpoint is not found' do + VCR.use_cassette('travel_pay/404_claims', match_requests_on: %i[method path]) do + get '/travel_pay/v0/claims', params: nil, headers: { 'Authorization' => 'Bearer vagov_token' } + expect(response).to have_http_status(:bad_request) + end end end end