Class: SubscriptionsTestKit::SubscriptionCreateEndpoint

Inherits:
Inferno::DSL::SuiteEndpoint
  • Object
show all
Includes:
SubscriptionsTestKit::SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils
Defined in:
lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb

Constant Summary

Constants included from SubscriptionsTestKit::SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils

SubscriptionsTestKit::SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils::ALLOWED_MIME_TYPES, SubscriptionsTestKit::SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils::DEFAULT_MIME_TYPE

Instance Method Summary collapse

Methods included from SubscriptionsTestKit::SubscriptionsR5BackportR4Client::SubscriptionSimulationUtils

#actual_mime_type, #client_access_token_input, #derive_event_notification, #derive_handshake_notification, #derive_status_bundle, #determine_event_count, #determine_subscription_status_code, #find_subscription, #notification_bundle_input, #operation_outcome

Instance Method Details

#find_heartbeat_period(subscription) ⇒ Object



81
82
83
84
85
# File 'lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb', line 81

def find_heartbeat_period(subscription)
  subscription&.channel&.extension&.find do |e|
    e.url == 'http://hl7.org/fhir/uv/subscriptions-backport/StructureDefinition/backport-heartbeat-period'
  end
end

#make_responseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb', line 13

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

  begin
    subscription = FHIR.from_contents(request.body.string)
  rescue StandardError
    response.body = operation_outcome('error', 'invalid', 'No recognized R4 Subscription in request body').to_json
    return
  end

  verification_outcome = verify_subscription(subscription)
  if verification_outcome.present?
    response.body = verification_outcome.to_json
    return
  end

  # Deny subscription if one already created
  requests = requests_repo.tagged_requests(test_run.test_session_id, tags)
  existing_subscription_request = requests.find { |r| r.status == 201 }
  if existing_subscription_request.present?
    subscription_hash = JSON.parse(existing_subscription_request.response_body)
    error_text = 'Inferno only supports one subscription per test run. Subscription already created with ' \
                 "ID #{subscription_hash['id']}"
    response.body = operation_outcome('error', 'business-rule', error_text).to_json
    return
  end

  # Form response
  notification_json = notification_bundle_input(result)
  subscription_id = SecureRandom.uuid
  # We have to manipulate the raw hash so that we don't lose the _payload primitive extension
  subscription_hash = JSON.parse(request.body.string).merge('id' => subscription_id, 'status' => 'requested')
  subscription_hash['channel']['payload'] = actual_mime_type(subscription)
  response.status = 201
  response.body = subscription_hash.to_json

  # Kick off notification job
  subscription_url = "#{request.url}/#{subscription_id}"
  client_endpoint = subscription.channel.endpoint
  bearer_token = client_access_token_input(result)
  test_suite_base_url = request.url.chomp('/').chomp(FHIR_SUBSCRIPTION_PATH)
  Inferno::Jobs.perform(Jobs::SendSubscriptionNotifications, test_run.id, test_run.test_session_id, result.id,
                        subscription_id, subscription_url, client_endpoint, bearer_token, notification_json,
                        test_run_identifier, test_suite_base_url)
end

#tagsObject



60
61
62
# File 'lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb', line 60

def tags
  [SUBSCRIPTION_CREATE_TAG]
end

#test_run_identifierObject



9
10
11
# File 'lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb', line 9

def test_run_identifier
  request.headers['authorization']&.delete_prefix('Bearer ')
end

#valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb', line 87

def valid_url?(url)
  uri = URI.parse(url)
  %w[http https].include?(uri.scheme)
rescue URI::InvalidURIError
  false
end

#verify_subscription(subscription) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/subscriptions_test_kit/endpoints/subscription_create_endpoint.rb', line 64

def verify_subscription(subscription)
  unless subscription.is_a? FHIR::Subscription
    return operation_outcome('error', 'invalid', 'No recognized R4 Subscription in request body')
  end

  unless subscription.channel&.type == 'rest-hook'
    return operation_outcome('error', 'business-rule', 'channel.type must be rest-hook')
  end

  unless valid_url?(subscription.channel&.endpoint)
    return operation_outcome('error', 'value', 'channel.endpoint is not recognized as a conformant URL')
  end

  heartbeat_period = find_heartbeat_period(subscription)
  operation_outcome('error', 'not-supported', 'heartbeatPeriod is not supported') unless heartbeat_period.nil?
end