Module: USCoreTestKit::GranularScopeReadTest

Constant Summary

Constants included from FHIRResourceNavigation

FHIRResourceNavigation::DAR_EXTENSION_URL, FHIRResourceNavigation::PRIMITIVE_DATA_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GranularScope

#granular_scope_search_params, #granular_scopes, #previous_request_resources, #resource_specific_granular_scope_search_params

Methods included from ResourceSearchParamChecker

#element_has_valid_value?, #resource_matches_param?, #search_param_paths

Methods included from FHIRResourceNavigation

#find_a_value_at, #find_slice_via_discriminator, #get_next_value, #get_primitive_type_value, #resolve_path, #verify_slice_by_values

Class Method Details

.included(klass) ⇒ Object



11
12
13
14
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 11

def self.included(klass)
  klass.input(:received_scopes)
  klass.attr_accessor :previous_requests
end

Instance Method Details

#load_previous_requestsObject



53
54
55
56
57
58
59
60
61
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 53

def load_previous_requests
  params = .searches.first[:names]
  search_params_as_hash = params.each_with_object({}) do |name, hash|
    hash[name] = nil
  end
  @previous_requests ||=
    load_tagged_requests(search_params_tag(search_params_as_hash))
      .sort_by { |request| request.index }
end

#run_scope_read_testObject



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
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 16

def run_scope_read_test
  assert granular_scopes.present?, "No granular scopes were received for #{resource_type} resources"
  resource_scopes = granular_scopes.select {|scope| scope.include?(resource_type)}

  load_previous_requests
  skip_if previous_requests.blank?,
          "No #{resource_type} reads found"
  previous_resources_for_reads = previous_request_resources.values.flatten

  resource_specific_granular_scope_search_params.each do |scope| 
    current_scope = granular_scopes.find {|granular| granular.include?(scope[:name]) && granular.include?(scope[:value])}

    resource_matching_scope = previous_resources_for_reads.find do |prev_resource|
      resource_matches_param?(prev_resource, scope[:name], current_scope.split('=').last)
    end
    skip_if resource_matching_scope.nil?, "Unable to find any resources to match scope #{current_scope}"

    fhir_read resource_type, resource_matching_scope.id

    assert_response_status(200)
    assert_resource_type(resource_type)
  end

  nonmatching_resource = previous_resources_for_reads.find do |prev_resource|
    resource_specific_granular_scope_search_params.none? do |scope| 
      resource_matches_param?(prev_resource, scope[:name], scope[:value])
    end
  end
  if nonmatching_resource
    fhir_read resource_type, nonmatching_resource.id
    assert (response && response[:status]) != 200, "Server incorrectly responded with a successful status, read should fail due to scopes."
    assert (resource && resource.resourceType != resource_type), "Server incorrectly returned a #{resource_type}, read should fail due to scopes"
  else
    info "Unable to find a resource that does not match scopes."
  end
end

#search_params_tag(params) ⇒ Object



67
68
69
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 67

def search_params_tag(params)
  "#{resource_type}?#{params.keys.join('&')}"
end

#unescape_search_value(value) ⇒ Object



63
64
65
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 63

def unescape_search_value(value)
  value&.gsub('\\,', ',')
end