Module: USCoreTestKit::GranularScopeReadTest

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

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



60
61
62
63
64
65
66
67
68
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 60

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
52
53
54
55
56
57
58
# 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."

    begin
      res = resource
    rescue NoMethodError
      res = nil
    end

    assert (!res || res.resourceType == 'OperationOutcome'), "Read shall fail due to scopes. Server shall return 4xx status code with an optional OperationOutcome payload."
  else
    info "Unable to find a resource that does not match scopes."
  end
end

#search_params_tag(params) ⇒ Object



74
75
76
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 74

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

#unescape_search_value(value) ⇒ Object



70
71
72
# File 'lib/us_core_test_kit/granular_scope_read_test.rb', line 70

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