Class: SMART_UDAP_HarmonizationTestKit::SMART_UDAP_FHIR_ContextTest

Inherits:
SMART_UDAP_ContextTest show all
Defined in:
lib/smart_udap_harmonization_test_kit/smart_udap_fhir_context_test.rb

Overview

rubocop:disable Naming/ClassAndModuleCamelCase

Instance Attribute Summary

Attributes inherited from SMART_UDAP_ContextTest

#token_response

Instance Method Summary collapse

Methods inherited from SMART_UDAP_ContextTest

#context_field, #context_field_present?, #missing_received_context_scopes, #missing_received_context_scopes?, #missing_received_scopes_string, #missing_requested_context_scopes, #missing_requested_context_scopes?, #missing_requested_scopes_string, #received_scopes, #received_scopes_list, #requested_scopes, #requested_scopes_list

Instance Method Details

#context_field_nameObject



12
13
14
# File 'lib/smart_udap_harmonization_test_kit/smart_udap_fhir_context_test.rb', line 12

def context_field_name
  'fhirContext'
end

#context_scopesObject



16
17
18
# File 'lib/smart_udap_harmonization_test_kit/smart_udap_fhir_context_test.rb', line 16

def context_scopes
  [].freeze
end

#validate_context_fieldObject

rubocop:disable Metrics/CyclomaticComplexity



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/smart_udap_harmonization_test_kit/smart_udap_fhir_context_test.rb', line 20

def validate_context_field # rubocop:disable Metrics/CyclomaticComplexity
  assert context_field.is_a?(Array),
         "`fhirContext` field should be an Array, but found `#{context_field.class.name}`"

  context_field_types = context_field.map(&:class).uniq

  assert context_field_types.length == 1,
         "Inconsistent `fhirContext` types found: #{context_field_types.map(&:name).join(', ')}"

  if context_field.any? { |member| member.is_a? String }
    assert context_field.none? { |member| member&.start_with?('Patient/') || member&.start_with?('Encounter/') },
           'Patient and Encounter references are not permitted within fhirContext in SMART App Launch 2.0.0'

    pass
  end

  non_hash_fields = context_field.reject { |member| member.is_a? Hash }
  non_hash_fields_string = non_hash_fields.map { |member| "`#{member.class.name}`" }.join(', ')
  assert non_hash_fields.empty?,
         "All `fhirContext` elements should be JSON objects, but found #{non_hash_fields_string}"

  field_types = {
    'reference' => String,
    'canonical' => String,
    'identifier' => Hash,
    'type' => String,
    'role' => String
  }

  bad_fields = []
  context_field.each do |member|
    field_types.each do |name, type|
      if member.key?(name) && !member[name].is_a?(type)
        bad_fields << { name:, type: member[name].class.name, expected_type: type.name }
      end
    end
  end

  bad_types_list =
    bad_fields.map do |bad_field|
      "\n* `#{bad_field[:name]}` - Expected `#{bad_field[:expected_type]}`, but found `#{bad_field[:type]}`"
    end
  bad_types_message = "The following fields have incorrect types#{bad_types_list.join}"

  assert bad_fields.empty?, bad_types_message

  patient_and_encounter_references =
    context_field.select do |member|
      member['reference']&.start_with?('Patient/') || member['reference']&.start_with?('Encounter/')
    end
  good_patient_and_encounter_roles =
    patient_and_encounter_references.all? do |reference|
      reference['role'].present? && reference['role'] != 'launch'
    end

  assert good_patient_and_encounter_roles,
         'Patient and Encounter references are not allowed unless they have a role other than `launch`'

  assert context_field.all? { |member| member['role'].is_a?(String) ? member['role'].present? : true },
         '`role` SHALL NOT be an empty string'

  required_fields = ['reference', 'canonical', 'identifier']

  assert context_field.all? { |member| required_fields.any? { |field| member[field].present? } },
         'Each object in fhirContext SHALL include at least one of "reference", "canonical", or "identifier"'
end