Skip to content

Commit

Permalink
Authorized Ping Endpoint for Travel Pay API (#16089)
Browse files Browse the repository at this point in the history
* Add authorized_ping endpoint to BTSSS API

* Adds specs for authorized ping endpoint, cleanup and bug fixes

* Fixes for rubocop

* Adds line comment

---------

Co-authored-by: Athif Wulandana <[email protected]>
  • Loading branch information
athifw and Athif Wulandana authored Mar 26, 2024
1 parent 137d71f commit ab0a269
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
11 changes: 11 additions & 0 deletions modules/travel_pay/app/controllers/travel_pay/pings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ def ping
render json: { data: "Received ping from upstream server with status #{btsss_ping_response.status}." }
end

def authorized_ping
vagov_token = request.headers['Authorization'].split[1]
veis_token = client.request_veis_token
btsss_token = client.request_btsss_token(veis_token, vagov_token)

btsss_authorized_ping_response = client.authorized_ping(veis_token, btsss_token)
render json: {
data: "Received authorized ping from upstream server with status #{btsss_authorized_ping_response.status}."
}
end

def client
TravelPay::Client.new
end
Expand Down
16 changes: 16 additions & 0 deletions modules/travel_pay/app/services/travel_pay/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ def ping(veis_token)
end
end

##
# HTTP GET call to the BTSSS 'authorized-ping' endpoint to test liveness
#
# @return [Faraday::Response]
#
def authorized_ping(veis_token, btsss_token)
btsss_url = Settings.travel_pay.base_url
api_key = Settings.travel_pay.subscription_key

connection(server_url: btsss_url).get('api/v1/Sample/authorized-ping') do |req|
req.headers['Authorization'] = "Bearer #{veis_token}"
req.headers['BTSSS-Access-Token'] = btsss_token
req.headers['Ocp-Apim-Subscription-Key'] = api_key
end
end

##
# HTTP GET call to the BTSSS 'claims' endpoint
# API responds with travel pay claims including status
Expand Down
1 change: 1 addition & 0 deletions modules/travel_pay/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

TravelPay::Engine.routes.draw do
get '/pings/ping', to: 'pings#ping'
get '/pings/authorized_ping', to: 'pings#authorized_ping'
resources :claims
end
38 changes: 35 additions & 3 deletions modules/travel_pay/spec/controllers/pings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

before do
allow(TravelPay::Client).to receive(:new).and_return(client)
veis_response = double
allow(veis_response).to receive(:body).and_return('sample_token')
allow(client).to receive(:request_veis_token).and_return(veis_response)
allow(client).to receive(:request_veis_token).and_return('sample_token')
btsss_ping_response = double
allow(btsss_ping_response).to receive(:status).and_return(200)

Expand Down Expand Up @@ -45,4 +43,38 @@
end
end
end

describe '#authorized_ping' do
before do
btsss_authorized_ping_response = double
allow(btsss_authorized_ping_response).to receive(:status).and_return(200)
allow(client)
.to receive(:request_btsss_token)
.and_return('sample_btsss_token')
allow(client)
.to receive(:authorized_ping)
.with('sample_token', 'sample_btsss_token')
.and_return(btsss_authorized_ping_response)
end

context 'the feature switch is enabled' do
before do
Flipper.enable :travel_pay_power_switch
end

it 'requests a token and sends a ping to BTSSS' do
expect(client).to receive(:authorized_ping)
get '/travel_pay/pings/authorized_ping', headers: { 'Authorization' => 'Bearer vagov_token' }
expect(response.body).to include('authorized ping')
end
end

context 'the feature switch is disabled' do
it 'raises the proper error' do
get '/travel_pay/pings/authorized_ping', headers: { 'Authorization' => 'Bearer vagov_token' }
expect(response).to have_http_status(:service_unavailable)
expect(response.body).to include('This feature has been temporarily disabled')
end
end
end
end
17 changes: 17 additions & 0 deletions modules/travel_pay/spec/services/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@
actual_claim_ids = claims.pluck(:id)

expect(actual_claim_ids).to eq(expected_ordered_ids)
end
end

context 'authorized_ping' do
it 'receives response from authorized-ping endpoint' do
allow(Settings.travel_pay.veis).to receive(:auth_url).and_return('sample_url')
allow(Settings.travel_pay.veis).to receive(:tenant_id).and_return('sample_id')
@stubs.get('/api/v1/Sample/authorized-ping') do
[
200,
{ 'Content-Type': 'application/json' }
]
end
client = TravelPay::Client.new
response = client.authorized_ping('veis_token', 'btsss_token')

expect(response).to be_success
@stubs.verify_stubbed_calls
end
end
Expand Down

0 comments on commit ab0a269

Please sign in to comment.