def test_empty_key(self): # Test 2: Empty key payload = {'id': 1234} response = payload_var_replace(payload) self.assertEqual(response, payload)
def test_payload_key_not_found(self): # Test 3: Valid key and matching old_value with no new_value payload = {'id': '1234', 'patient': 'oldvalue'} key = 'patient' old_value = 'oldvalue' response = payload_var_replace(payload, key, new_value=None, old_value=old_value) expected = {'id': '1234'} self.assertEqual(response, expected)
def test_payload_match_old_value(self): # Test 4: Match old_value and replace with new_value payload = {'id': '1234', 'patient': 'oldvalue'} key = 'patient' old_value = 'oldvalue' new_value = 'newvalue' response = payload_var_replace(payload, key, new_value=new_value, old_value=old_value) expected = {'id': '1234', 'patient': 'newvalue'} self.assertEqual(response, expected)
def test_payload_fail_old_value_match(self): # Test 5: old value doesn't match so don't replace payload = {'id': '1234', 'patient': 'oldestvalue'} key = 'patient' old_value = 'oldvalue' new_value = 'newvalue' response = payload_var_replace(payload, key, new_value=new_value, old_value=old_value) expected = payload # Should be Unchanged self.assertEqual(response, expected)
def read_search(request, interaction_type, resource_type, id=None, vid=None, *args, **kwargs): """ Read from remote FHIR Server :param request: :param interaction_type: :param resource_type: :param id: :param vid: :param args: :param kwargs: :return: # Example client use in curl: # curl -X GET http://127.0.0.1:8000/fhir/Practitioner/1234?_format=json """ logger.debug('\n========================\n' 'INTERACTION_TYPE: %s' % interaction_type) # Get the users crosswalk cx = get_crosswalk(request.user) # cx will be the crosswalk record or None rr = get_resourcerouter(cx) # Check if this interaction type and resource type combo is allowed. deny = check_access_interaction_and_resource_type(resource_type, interaction_type, rr) if deny: # if not allowed, return a 4xx error. return deny srtc = check_rt_controls(resource_type, rr) # We get back a Supported ResourceType Control record or None # with earlier if deny step we should have a valid srtc. if srtc.secure_access and request.user.is_anonymous(): return kickout_403('Error 403: %s Resource access is controlled.' ' Login is required:' '%s' % (resource_type, request.user.is_anonymous())) # logger.debug('srtc: %s' % srtc) if (cx is None and srtc is not None): # There is a srtc record so we need to check override_search if srtc.override_search: # If user is not in Crosswalk and srtc has search_override = True # We need to prevent search to avoid data leakage. return kickout_403('Error 403: %s Resource is access controlled.' ' No records are linked to user:'******'%s' % (resource_type, request.user)) ################################################ # # Now we should have sorted out all the bounces # Now to structure the call to the back-end # ################################################ # construct the server address, path and release # Get the crosswalk.fhir_source = ResourceRouter # Build url from rr.server_address + rr.server_path + rr.server_release target_url = rr.fhir_url # add resource_type + '/' target_url += srtc.resourceType target_url += "/" # Analyze the _format parameter # Sve the display _format input_parameters = request.GET # requested_format = 'json' | 'xml' | 'html' requested_format = save_request_format(input_parameters) format_mode = eval_format_type(requested_format) # prepare the back-end _format setting back_end_format = set_fhir_format(format_mode) # remove the oauth parameters payload = strip_oauth(request.GET) # Get payload with oauth parameters removed # Add the format for back-end payload['_format'] = back_end_format # remove the srtc.search_block parameters payload = block_params(payload, srtc) # print("id:%s" % str(id)) # move resource_id to _id=resource_id id_dict = set_resource_id(srtc, id, cx.fhir_id) # id_dict['query_mode'] = 'search' | 'read' # id_dict['url_id'] = '' | id # id_dict['_id'] = id | '' # id_dict['patient'] = patient_id | '' # resource_id = id_dict['url_id'] # Add the srtc.search_add parameters # added_params = add_params(srtc, # patient_id=id_dict['patient'], # key=id_dict['url_id']) # print("Added Params:%s" % added_params) params_list = search_add_to_list(srtc.search_add) # print("Params_List:%s" % params_list) payload = payload_additions(payload, params_list) # print('id_dict:%s' % id_dict) # print("what have we got?:%s" % id_dict) if id_dict['_id']: # add rt_id into the search parameters if id_dict['_id'] is not None: payload['_id'] = id_dict['_id'] if resource_type.lower() == 'patient': # print("Working resource:%s" % resource_type) payload['_id'] = id_dict['patient'] if payload['patient']: del payload['patient'] for pyld_k, pyld_v in payload.items(): if pyld_v is None: pass elif '%PATIENT%' in pyld_v: # replace %PATIENT% with cx.fhir_id payload = payload_var_replace(payload, pyld_k, new_value=id_dict['patient'], old_value='%PATIENT%') # print("post futzing:%s" % id_dict) # add the _format setting payload['_format'] = back_end_format # Make the request_call r = request_get_with_parms(request, target_url, json.loads(json.dumps(payload)), cx, reverse_lazy('home'), timeout=settings.REQUEST_CALL_TIMEOUT) ################################################ # # Now we process the response from the back-end # ################################################ # if 'status_code' in r: # r_status_code = r.status_code # else: # r_status_code = 500 rewrite_list = build_rewrite_list(cx) host_path = get_host_url(request, resource_type)[:-1] try: text_in = r.text except: text_in = "" text_out = post_process_request(request, back_end_format, host_path, text_in, rewrite_list) if resource_type.lower() == 'patient': display_key = id_dict['patient'] else: display_key = id od = build_output_dict(request, OrderedDict(), resource_type, display_key, vid, interaction_type, requested_format, text_out) ################################################ # # Now display the result # ################################################ ikey = '' try: ikey = find_ikey(r.text) except: ikey = '' if ikey is not '': save_url = get_target_url(target_url, resource_type) # print("Store target_url:%s but only: %s" % (target_url,save_url)) content = { 'fhir_to': save_url, 'rwrt_list': rewrite_list, 'res_type': resource_type, 'intn_type': interaction_type, 'key': display_key, 'vid': vid, 'resource_router': rr.id } sesn_var = write_session(request, ikey, content, skey=SESSION_KEY) if sesn_var: logger.debug("Problem writing session variables." " Returned %s" % sesn_var) if format_mode == 'xml': # logger.debug('We got xml back in od') return HttpResponse(r.text, content_type='application/%s' % requested_format) # return HttpResponse(tostring(dict_to_xml('content', od)), # content_type='application/%s' % requested_format) elif format_mode == 'json': # logger.debug('We got json back in od') return HttpResponse(pretty_json(od['bundle']), content_type='application/%s' % requested_format) query_string = build_querystring(request.GET.copy()) if "xml" in requested_format: # logger.debug("Sending text_out for display: %s" % text_out[0:100]) div_text = get_div_from_xml(text_out) # print("DIV TEXT returned:[%s]%s" % (type(div_text), div_text)) return render( request, 'bluebutton/default_xml.html', { 'output': text_out, 'content': { 'parameters': query_string, 'resource_type': resource_type, 'request_method': "GET", 'interaction_type': interaction_type, 'div_texts': [ div_text, ], 'source': cx.fhir_source.name } }) else: text_out = pretty_json(od['bundle']) div_text = get_div_from_json(od['bundle']) # logger.debug('We got a different format:%s' % requested_format) return render( request, 'bluebutton/default.html', { 'output': text_out, 'content': { 'parameters': query_string, 'resource_type': resource_type, 'request_method': "GET", 'interaction_type': interaction_type, 'div_texts': div_text, 'source': cx.fhir_source.name } })
def test_empty_payload(self): # Test 1: Nothing passed response = payload_var_replace() self.assertEqual(response, None)