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
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

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_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_VERSION_NUMBERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from G10Options

#us_core_version, #using_us_core_3?, #using_us_core_5?, #using_us_core_6?, #versioned_us_core_module

Instance Attribute Details

#received_or_requestedObject

Returns the value of attribute received_or_requested.



72
73
74
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 72

def received_or_requested
  @received_or_requested
end

Instance Method Details

#access_level_regexObject



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

def access_level_regex
  case scope_version
  when :v1
    /\A(\*|read)\b/
  when :v2
    /\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



153
154
155
156
157
158
159
160
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 153

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



138
139
140
141
142
143
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 138

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



74
75
76
77
78
79
80
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 74

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?

  PATIENT_COMPARTMENT_RESOURCE_TYPES
end

#read_formatObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 113

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

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

#received_scope_test(scopes) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 192

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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 162

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



109
110
111
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 109

def requested_scope_version
  config.options[:requested_scope_version]
end

#required_scope_typeObject



90
91
92
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 90

def required_scope_type
  config.options[:required_scope_type]
end

#required_scopesObject



94
95
96
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 94

def required_scopes
  config.options[:required_scopes]
end

#scope_versionObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 98

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



145
146
147
148
149
150
151
# File 'lib/onc_certification_g10_test_kit/smart_scopes_test.rb', line 145

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

#valid_resource_typesObject



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

def valid_resource_types
  return V5_VALID_RESOURCE_TYPES if using_us_core_5?

  return V6_VALID_RESOURCE_TYPES if using_us_core_6?

  VALID_RESOURCE_TYPES
end