Class: ONCCertificationG10TestKit::SMARTScopesTest

Inherits:
Inferno::Test
  • Object
show all
Includes:
G10Options
Defined in:
lib/onc_certification_g10_test_kit/smart_scopes_test.rb

Constant Summary collapse

VALID_RESOURCE_TYPES =
[
  '*',
  'Patient',
  'AllergyIntolerance',
  'Binary',
  'CarePlan',
  'CareTeam',
  'Condition',
  'Device',
  'DiagnosticReport',
  'DocumentReference',
  'Encounter',
  'Goal',
  'Immunization',
  'Location',
  'Medication',
  'MedicationOrder',
  'MedicationRequest',
  'MedicationStatement',
  'Observation',
  'Organization',
  'Person',
  'Practitioner',
  'PractitionerRole',
  'Procedure',
  'Provenance',
  'RelatedPerson'
].freeze
V5_VALID_RESOURCE_TYPES =
(VALID_RESOURCE_TYPES + ['ServiceRequest', 'QuestionnaireResponse', 'Media']).freeze
V6_VALID_RESOURCE_TYPES =
(V5_VALID_RESOURCE_TYPES + ['Coverage', 'MedicationDispense', 'RelatedPerson', 'Specimen']).freeze
V7_VALID_RESOURCE_TYPES =
(V6_VALID_RESOURCE_TYPES + ['Location'])
PATIENT_COMPARTMENT_RESOURCE_TYPES =
[
  '*',
  'Patient',
  'AllergyIntolerance',
  'CarePlan',
  'CareTeam',
  'Condition',
  'DiagnosticReport',
  'DocumentReference',
  'Goal',
  'Immunization',
  'MedicationRequest',
  'Observation',
  'Procedure',
  'Provenance'
].freeze
V5_PATIENT_COMPARTMENT_RESOURCE_TYPES =
(PATIENT_COMPARTMENT_RESOURCE_TYPES + ['ServiceRequest']).freeze
V6_PATIENT_COMPARTMENT_RESOURCE_TYPES =
(V5_PATIENT_COMPARTMENT_RESOURCE_TYPES + ['Coverage', 'MedicationDispense', 'Specimen']).freeze
V7_PATIENT_COMPARTMENT_RESOURCE_TYPES =
(V6_PATIENT_COMPARTMENT_RESOURCE_TYPES + ['Location']).freeze

Constants included from G10Options

G10Options::BULK_DATA_1, G10Options::BULK_DATA_1_REQUIREMENT, G10Options::BULK_DATA_2, G10Options::BULK_DATA_2_REQUIREMENT, G10Options::SMART_1, G10Options::SMART_1_REQUIREMENT, G10Options::SMART_2, G10Options::SMART_2_2, G10Options::SMART_2_2_REQUIREMENT, G10Options::SMART_2_REQUIREMENT, G10Options::US_CORE_3, G10Options::US_CORE_3_REQUIREMENT, G10Options::US_CORE_4, G10Options::US_CORE_4_REQUIREMENT, G10Options::US_CORE_5, G10Options::US_CORE_5_REQUIREMENT, G10Options::US_CORE_6, G10Options::US_CORE_6_REQUIREMENT, G10Options::US_CORE_7, G10Options::US_CORE_7_REQUIREMENT, G10Options::US_CORE_VERSION_NUMBERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from G10Options

#us_core_7_and_above?, #us_core_version, #using_us_core_3?, #using_us_core_4?, #using_us_core_5?, #using_us_core_6?, #using_us_core_7?, #versioned_us_core_module

Instance Attribute Details

#received_or_requestedObject

Returns the value of attribute received_or_requested.



77
78
79
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 77

def received_or_requested
  @received_or_requested
end

Instance Method Details

#access_level_regexObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 140

def access_level_regex
  case scope_version
  when :v1
    /\A(\*|read)\b/
  when :v2, :v22
    /\A(\*|c?ru?d?s?)\b/
  else
    /\A(\*|read|c?ru?d?s?)\b/
  end
end

#assert_correct_scope_type(scope, scope_type, resource_type, scope_direction) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 166

def assert_correct_scope_type(scope, scope_type, resource_type, scope_direction)
  if required_scope_type == 'patient' && patient_compartment_resource_types.exclude?(resource_type)
    assert ['user', 'patient'].include?(scope_type),
           "#{scope_direction} scope '#{scope}' must begin with either 'user/' or 'patient/'"
  else
    assert scope_type == required_scope_type, bad_format_message(scope, scope_direction)
  end
end

#bad_format_message(scope, scope_direction = 'Requested') ⇒ Object



151
152
153
154
155
156
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 151

def bad_format_message(scope, scope_direction = 'Requested')
  %(
    #{scope_direction} scope `#{scope}` does not follow the format:
    `#{required_scope_type}/[ <ResourceType> | * ].[ #{read_format} ]`
  )
end

#patient_compartment_resource_typesObject



79
80
81
82
83
84
85
86
87
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 79

def patient_compartment_resource_types
  return V5_PATIENT_COMPARTMENT_RESOURCE_TYPES if using_us_core_5?

  return V6_PATIENT_COMPARTMENT_RESOURCE_TYPES if using_us_core_6?

  return V7_PATIENT_COMPARTMENT_RESOURCE_TYPES if using_us_core_7?

  PATIENT_COMPARTMENT_RESOURCE_TYPES
end

#read_formatObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 126

def read_format
  v1_read_format = 'read'
  v2_read_format = 'c?ru?d?s?'

  case scope_version
  when :v1
    "#{v1_read_format} | *"
  when :v2, :v22
    "#{v2_read_format} | *"
  else
    [v1_read_format, v2_read_format, '*'].join(' | ')
  end
end

#received_scope_test(scopes) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 205

def received_scope_test(scopes)
  granted_resource_types = []

  scopes.each do |full_scope|
    scope = strip_experimental_scope_syntax(full_scope)

    scope_pieces = scope.split('/')
    next unless scope_pieces.length == 2

    scope_type, resource_scope = scope_pieces

    resource_scope_parts = resource_scope.split('.')
    next unless resource_scope_parts.length == 2

    resource_type, access_level = resource_scope_parts
    next unless access_level =~ access_level_regex

    next unless ['patient', 'user', 'system'].include?(scope_type)

    assert_correct_scope_type(scope, scope_type, resource_type, 'Received')

    granted_resource_types << resource_type
  end

  missing_resource_types =
    if granted_resource_types.include?('*')
      []
    else
      patient_compartment_resource_types - granted_resource_types - ['*']
    end

  assert missing_resource_types.empty?,
         "Request scopes #{missing_resource_types.join(', ')} were not granted by authorization server."
end

#requested_scope_test(scopes) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 175

def requested_scope_test(scopes)
  correct_scope_type_found = false

  scopes.each do |full_scope|
    scope = strip_experimental_scope_syntax(full_scope)

    scope_pieces = scope.split('/')
    assert scope_pieces.length == 2, bad_format_message(scope)

    scope_type, resource_scope = scope_pieces

    resource_scope_parts = resource_scope.split('.')
    assert resource_scope_parts.length == 2, bad_format_message(scope)

    resource_type, access_level = resource_scope_parts
    assert access_level =~ access_level_regex, bad_format_message(scope)

    assert_correct_scope_type(scope, scope_type, resource_type, 'Requested')

    assert valid_resource_types.include?(resource_type),
           "'#{resource_type}' must be either a permitted resource type or '*'"

    correct_scope_type_found = true if scope_type == required_scope_type
  end

  assert correct_scope_type_found,
         "#{required_scope_type.capitalize}-level scope in the format: " \
         "`#{required_scope_type}/[ <ResourceType> | * ].[ #{read_format} ]` was not requested."
end

#requested_scope_versionObject



122
123
124
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 122

def requested_scope_version
  config.options[:requested_scope_version]
end

#requested_scopesObject



99
100
101
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 99

def requested_scopes
  smart_auth_info.requested_scopes
end

#required_scope_typeObject



103
104
105
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 103

def required_scope_type
  config.options[:required_scope_type]
end

#required_scopesObject



107
108
109
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 107

def required_scopes
  config.options[:required_scopes]
end

#scope_versionObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 111

def scope_version
  case received_or_requested
  when 'received'
    config.options[:received_scope_version] || config.options[:scope_version]
  when 'requested'
    config.options[:requested_scope_version] || config.options[:scope_version]
  else
    config.options[:scope_version]
  end
end

#strip_experimental_scope_syntax(full_scope) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 158

def strip_experimental_scope_syntax(full_scope)
  if scope_version == :v1
    full_scope
  else
    full_scope.split('?').first
  end
end

#valid_resource_typesObject



89
90
91
92
93
94
95
96
97
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 89

def valid_resource_types
  return V5_VALID_RESOURCE_TYPES if using_us_core_5?

  return V6_VALID_RESOURCE_TYPES if using_us_core_6?

  return V7_VALID_RESOURCE_TYPES if using_us_core_7?

  VALID_RESOURCE_TYPES
end