Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FI-3301: SuiteEndpoints #21

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions lib/davinci_pas_test_kit/client_suite.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require_relative 'ext/inferno_core/record_response_route'
require_relative 'ext/inferno_core/runnable'
require_relative 'ext/inferno_core/request'
require_relative 'validator_suppressions'
require_relative 'tags'
require_relative 'urls'
require_relative 'mock_server'
require_relative 'endpoints/claim_endpoint'
require_relative 'endpoints/token_endpoint'
require_relative 'custom_groups/v2.0.1/pas_client_authentication_group'
require_relative 'custom_groups/v2.0.1/pas_client_approval_group'
require_relative 'custom_groups/v2.0.1/pas_client_denial_group'
Expand All @@ -15,8 +13,6 @@

module DaVinciPASTestKit
class ClientSuite < Inferno::TestSuite
extend MockServer

id :davinci_pas_client_suite_v201
title 'Da Vinci PAS Client Suite v2.0.1'
version VERSION
Expand All @@ -37,10 +33,6 @@ class ClientSuite < Inferno::TestSuite
}
]

def self.test_resumes?(test)
!test.config.options[:accepts_multiple_requests]
end

fhir_resource_validator do
igs 'hl7.fhir.us.davinci-pas#2.0.1'

Expand All @@ -51,26 +43,16 @@ def self.test_resumes?(test)
end
end

record_response_route :post, TOKEN_PATH, AUTH_TAG, method(:token_response) do |request|
ClientSuite.extract_client_id(request)
end

record_response_route :post, SUBMIT_PATH, SUBMIT_TAG, method(:claim_response),
resumes: method(:test_resumes?) do |request|
ClientSuite.extract_bearer_token(request)
end

record_response_route :post, INQUIRE_PATH, INQUIRE_TAG, method(:claim_response),
resumes: method(:test_resumes?) do |request|
ClientSuite.extract_bearer_token(request)
end
suite_endpoint :post, TOKEN_PATH, TokenEndpoint
suite_endpoint :post, SUBMIT_PATH, ClaimEndpoint
suite_endpoint :post, INQUIRE_PATH, ClaimEndpoint

resume_test_route :get, RESUME_PASS_PATH do |request|
ClientSuite.extract_token_from_query_params(request)
request.query_parameters['token']
end

resume_test_route :get, RESUME_FAIL_PATH, result: 'fail' do |request|
ClientSuite.extract_token_from_query_params(request)
request.query_parameters['token']
end

group from: :pas_client_v201_authentication_group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
require_relative 'user_input_response'
require_relative '../user_input_response'

module DaVinciPASTestKit
# Serve responses to PAS requests
#
# Note that there are numerous expected validation issues that can safely be ignored.
# See here for full list: https://hl7.org/fhir/us/davinci-pas/STU2/qa.html#suppressed
module MockServer
def token_response(request, _test = nil, _test_result = nil)
# Placeholder for a more complete mock token endpoint
request.response_body = { access_token: SecureRandom.hex, token_type: 'bearer', expires_in: 300 }.to_json
request.status = 200
class ClaimEndpoint < Inferno::DSL::SuiteEndpoint
def test_run_identifier
request.headers['authorization']&.delete_prefix('Bearer ')
end

def claim_response(request, test = nil, test_result = nil)
request.status = 200
request.response_headers = { 'Content-Type': 'application/json' }
def tags
[operation == 'submit' ? SUBMIT_TAG : INQUIRE_TAG]
end

def make_response
response.status = 200
response.format = :json

user_inputted_response = UserInputResponse.user_inputted_response(test, test_result)
if test.present? && test_result.present? && user_inputted_response.present?
request.response_body = user_inputted_response
user_inputted_response = UserInputResponse.user_inputted_response(test, result)
if user_inputted_response.present?
response.body = user_inputted_response
return
end

operation = request&.url&.split('$')&.last
req_bundle = FHIR.from_contents(request&.request_body)
req_bundle = FHIR.from_contents(request.body.string)
claim_entry = req_bundle&.entry&.find { |e| e&.resource&.resourceType == 'Claim' }
claim_full_url = claim_entry&.fullUrl
if claim_entry.blank? || claim_full_url.blank?
handle_missing_required_elements(claim_entry, request)
handle_missing_required_elements(claim_entry, response)
return
end

root_url = base_url(claim_full_url)
claim_response = mock_claim_response(claim_entry.resource, req_bundle, operation, root_url)
claim_response = mock_claim_response(claim_entry.resource, req_bundle, root_url)

res_bundle = FHIR::Bundle.new(
id: SecureRandom.uuid,
Expand All @@ -51,16 +48,20 @@ def claim_response(request, test = nil, test_result = nil)

res_bundle.entry.concat(referenced_entities(claim_response, req_bundle.entry, root_url))

request.response_body = res_bundle.to_json
request.status = 200
request.response_headers = { 'Content-Type': 'application/json' }
response.body = res_bundle.to_json
end

def update_result
results_repo.update_result(result.id, 'pass') unless test.config.options[:accepts_multiple_requests]
end

private

# Note that references from the claim to other resources in the bundle need to be changed to absolute URLs
# if they are relative, because the ClaimResponse's fullUrl is a urn:uuid
#
# @private
def mock_claim_response(claim, bundle, operation, root_url)
def mock_claim_response(claim, bundle, root_url)
return FHIR::ClaimResponse.new(id: SecureRandom.uuid) if claim.blank?

now = Time.now.utc
Expand Down Expand Up @@ -128,33 +129,23 @@ def mock_claim_response(claim, bundle, operation, root_url)
)
end

def extract_client_id(request)
URI.decode_www_form(request.request_body).to_h['client_id']
end

# Header expected to be a bearer token of the form "Bearer: <token>"
def extract_bearer_token(request)
request.request_header('Authorization')&.value&.split&.last
end

def extract_token_from_query_params(request)
request.query_parameters['token']
end

def handle_missing_required_elements(claim_entry, request)
request.status = 400
request.response_headers = { 'Content-Type': 'application/json' }
def handle_missing_required_elements(claim_entry, response)
response.status = 400
details = if claim_entry.blank?
'Required Claim entry missing from bundle'
else
'Required element fullUrl missing from Claim entry'
end
request.response_body = FHIR::OperationOutcome.new(
response.body = FHIR::OperationOutcome.new(
issue: FHIR::OperationOutcome::Issue.new(severity: 'fatal', code: 'required',
details: FHIR::CodeableConcept.new(text: details))
).to_json
end

def operation
request.url&.split('$')&.last
end

# Drop the last two segments of a URL, i.e. the resource type and ID of a FHIR resource
# e.g. http://example.org/fhir/Patient/123 -> http://example.org/fhir
# @private
Expand All @@ -165,7 +156,6 @@ def base_url(url)
url.sub(%r{/[^/]*/[^/]*(/)?\z}, '')
end

# @private
def referenced_entities(resource, entries, root_url)
matches = []
attributes = resource&.source_hash&.keys
Expand All @@ -185,21 +175,18 @@ def referenced_entities(resource, entries, root_url)
matches
end

# @private
def absolute_reference(ref, entries, root_url)
url = find_matching_entry(ref&.reference, entries, root_url)&.fullUrl
ref.reference = url if url
ref
end

# @private
def find_matching_entry(ref, entries, root_url = '')
ref = "#{root_url}/#{ref}" if relative_reference?(ref) && root_url&.present?

entries&.find { |entry| entry&.fullUrl == ref }
end

# @private
def relative_reference?(ref)
ref&.count('/') == 1
end
Expand Down
22 changes: 22 additions & 0 deletions lib/davinci_pas_test_kit/endpoints/token_endpoint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module DaVinciPASTestKit
class TokenEndpoint < Inferno::DSL::SuiteEndpoint
def test_run_identifier
URI.decode_www_form(request.body.string).to_h['client_id']
end

def tags
[AUTH_TAG]
end

def make_response
# Placeholder for a more complete mock token endpoint
response.status = 200
response.format = :json
response.body = { access_token: SecureRandom.hex, token_type: 'bearer', expires_in: 300 }.to_json
end

def update_result
results_repo.update_result(result.id, 'pass')
end
end
end
98 changes: 0 additions & 98 deletions lib/davinci_pas_test_kit/ext/inferno_core/record_response_route.rb

This file was deleted.

19 changes: 0 additions & 19 deletions lib/davinci_pas_test_kit/ext/inferno_core/request.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/davinci_pas_test_kit/ext/inferno_core/runnable.rb

This file was deleted.

Loading