def test_fhir_client_patient_list_auth_fail() -> None: test_name = "test_fhir_client_patient_list_auth_fail" mock_server_url = "http://mock-server:1080" mock_client: MockServerFriendlyClient = MockServerFriendlyClient( base_url=mock_server_url) relative_url: str = test_name absolute_url: str = mock_server_url + "/" + test_name mock_client.clear(f"/{test_name}/*.*") mock_client.reset() mock_client.expect( mock_request(path=f"/{relative_url}/Patient", method="GET"), mock_response(code=403), timing=times(1), ) fhir_client = FhirClient() fhir_client = fhir_client.url(absolute_url).resource("Patient") response: FhirGetResponse = fhir_client.get() print(response.responses) assert response.error == "403"
def test_fhir_client_patient_list() -> None: test_name = "test_fhir_client_patient_list" mock_server_url = "http://mock-server:1080" mock_client: MockServerFriendlyClient = MockServerFriendlyClient( base_url=mock_server_url) relative_url: str = test_name absolute_url: str = mock_server_url + "/" + test_name mock_client.clear(f"/{test_name}/*.*") mock_client.reset() response_text: str = json.dumps({"resourceType": "Patient", "id": "12355"}) mock_client.expect( mock_request(path=f"/{relative_url}/Patient", method="GET"), mock_response(body=response_text), timing=times(1), ) fhir_client = FhirClient() fhir_client = fhir_client.url(absolute_url).resource("Patient") response: FhirGetResponse = fhir_client.get() print(response.responses) assert response.responses == response_text
def test_dev_server_get_patients() -> None: clean_fhir_server() url = "http://fhir:3000/4_0_0" fhir_client = FhirClient() fhir_client = fhir_client.url(url).resource("Patient") resource = { "resourceType": "Patient", "id": "12355", "meta": { "security": [{ "system": "https://www.icanbwell.com/access", "code": "bwell" }] }, } merge_response: FhirMergeResponse = fhir_client.merge( [json.dumps(resource)]) print(merge_response.responses) assert merge_response.status == 200, merge_response.responses assert merge_response.responses[0][ "created"] is True, merge_response.responses fhir_client = fhir_client.url(url).resource("Patient") response: FhirGetResponse = fhir_client.get() print(response.responses) responses_ = json.loads(response.responses)[0] assert responses_["id"] == resource["id"] assert responses_["resourceType"] == resource["resourceType"]
def test_dev_server_auth() -> None: url = "https://fhir-auth.dev.bwell.zone/4_0_0" fhir_client = FhirClient() fhir_client = fhir_client.url(url).resource("Patient") fhir_client = fhir_client.client_credentials( client_id="4opocimdhppokn1ks0hpbo9fkv", client_secret="{Put secret here}" ).auth_scopes(["user/*.read"]) response: FhirGetResponse = fhir_client.get() print(response.responses)
def test_fhir_client_patient_by_id() -> None: with requests_mock.Mocker() as mock: url = "http://foo" response_text = {"resourceType": "Patient", "id": "12355"} mock.get(f"{url}/Patient/12355", json=response_text) fhir_client = FhirClient() fhir_client = fhir_client.url(url).resource("Patient").id_("12355") response: FhirGetResponse = fhir_client.get() print(response.responses) assert response.responses == json.dumps(response_text)
def test_fhir_client_patient_list_auth_fail() -> None: with requests_mock.Mocker() as mock: url = "http://foo" mock.get(f"{url}/Patient", status_code=403) fhir_client = FhirClient() fhir_client = fhir_client.url(url).resource("Patient") with pytest.raises(AssertionError): response: FhirGetResponse = fhir_client.get() print(response.responses) assert response.error == "403"
def test_fhir_client_patient_list_auth_fail_retry() -> None: test_name = "test_fhir_client_patient_list_auth_fail_retry" mock_server_url = "http://mock-server:1080" mock_client: MockServerFriendlyClient = MockServerFriendlyClient( base_url=mock_server_url ) relative_url: str = test_name absolute_url: str = mock_server_url + "/" + test_name mock_client.clear(f"/{test_name}/*.*") mock_client.reset() mock_client.expect( mock_request(path=f"/{relative_url}/Patient", method="GET"), mock_response(code=401), timing=times(1), ) auth_response_text = { "access_token": "my_access_token", "expires_in": 86400, "token_type": "Bearer", } mock_client.expect( mock_request(path=f"/{relative_url}/auth", method="POST"), mock_response(code=200, body=auth_response_text), timing=times(1), ) response_text: str = json.dumps({"resourceType": "Patient", "id": "12355"}) mock_client.expect( mock_request(path=f"/{relative_url}/Patient", method="GET"), mock_response(body=response_text), timing=times(1), ) fhir_client = FhirClient() fhir_client = fhir_client.url(absolute_url).resource("Patient") fhir_client = fhir_client.client_credentials( client_id="client_id", client_secret="client_secret" ) fhir_client = fhir_client.auth_server_url(absolute_url + "/" + "auth").auth_scopes( ["user/*.ready"] ) response: FhirGetResponse = fhir_client.get() print(response.responses) assert response.responses == response_text
def test_fhir_client_patient_list_auth_fail_retry() -> None: with requests_mock.Mocker() as mock: url = "http://foo" response_text = {"resourceType": "Patient", "id": "12355"} mock.get(f"{url}/Patient", [{ "status_code": 403 }, { "json": response_text }]) auth_response = { "access_token": "my_access_token", "expires_in": 86400, "token_type": "Bearer", } mock.post( "http://auth", [{ "status_code": 200 }, { "json": auth_response, "status_code": 200 }], ) fhir_client = FhirClient() fhir_client = fhir_client.url(url).resource("Patient") fhir_client = fhir_client.client_credentials( client_id="client_id", client_secret="client_secret") fhir_client = fhir_client.auth_server_url("http://auth").auth_scopes( ["user/*.ready"]) response: FhirGetResponse = fhir_client.get() print(response.responses) assert mock.call_count == 4, ",".join( [r.url for r in mock.request_history]) assert response.responses == json.dumps(response_text)
def test_dev_server_no_auth() -> None: url = "http://fhir:3000/4_0_0" fhir_client = FhirClient() fhir_client = fhir_client.url(url).resource("Patient") response: FhirGetResponse = fhir_client.get() print(response.responses)
def test_fhir_client_bundle() -> None: with requests_mock.Mocker() as mock: url = "http://foo" response_text = { "resourceType": "Bundle", "id": "1437321965", "entry": [ { "link": "https://localhost:3000/4_0_0/Practitioner/1437321965", "resource": { "resourceType": "Practitioner", "id": "1437321965", "meta": { "versionId": "1", "lastUpdated": "2021-01-13T04:37:06+00:00", "source": "http://medstarhealth.org/provider", }, "identifier": [ { "use": "usual", "type": { "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "PRN", }] }, "system": "http://medstarhealth.org", "value": "500524", }, { "use": "official", "type": { "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "NPI", }] }, "system": "http://hl7.org/fhir/sid/us-npi", "value": "1437321965", }, ], "active": True, "name": [{ "use": "usual", "text": "ZOOVIA AMAN MD", "family": "AMAN", "given": ["ZOOVIA", ""], }], "gender": "female", "qualification": [ { "code": { "coding": [{ "system": "http://terminology.hl7.org/ValueSet/v2-2.7-030", "code": "MD", }] } }, { "code": { "coding": [{ "system": "http://terminology.hl7.org/ValueSet/v2-2.7-030", "code": "MD", }] }, "period": { "start": "2011-01-01", "end": "2023-12-31" }, "issuer": { "reference": "Organization/Harpers_Ferry_Family_Medicine" }, }, ], "communication": [{ "coding": [{ "system": "urn:ietf:bcp:47", "code": "en" }] }], }, }, { "link": "https://localhost:3000/4_0_0/PractitionerRole/1437321965-ML1-MLSW", "resource": { "resourceType": "PractitionerRole", "id": "1437321965-ML1-MLSW", "meta": { "versionId": "1", "lastUpdated": "2021-01-13T04:37:24+00:00", "source": "http://medstarhealth.org/provider", }, "practitioner": { "reference": "Practitioner/1437321965" }, "organization": { "reference": "Organization/Medstar-15888213" }, "code": [{ "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/practitioner-role", "code": "doctor", "display": "Doctor", }], "text": "Doctor", }], "specialty": [ { "coding": [{ "system": "https://www.http://medstarhealth.org", "code": "Family Medicine", "display": "Family Medicine", }], "text": "Family Medicine", }, { "coding": [{ "system": "http://nucc.org/provider-taxonomy", "code": "207Q00000X", "display": "Family Medicine", }], "text": "Family Medicine", }, { "coding": [{ "system": "http://nucc.org/provider-taxonomy", "code": "261QP2300X", "display": "Primary Care", }], "text": "Primary Care", }, ], "location": [{ "reference": "Location/Medstar-Alias-ML1-MLSW" }], }, }, ], } expected_response = { "practitioner": [{ "resourceType": "Practitioner", "id": "1437321965", "meta": { "versionId": "1", "lastUpdated": "2021-01-13T04:37:06+00:00", "source": "http://medstarhealth.org/provider", }, "identifier": [ { "use": "usual", "type": { "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "PRN", }] }, "system": "http://medstarhealth.org", "value": "500524", }, { "use": "official", "type": { "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "NPI", }] }, "system": "http://hl7.org/fhir/sid/us-npi", "value": "1437321965", }, ], "active": True, "name": [{ "use": "usual", "text": "ZOOVIA AMAN MD", "family": "AMAN", "given": ["ZOOVIA", ""], }], "gender": "female", "qualification": [ { "code": { "coding": [{ "system": "http://terminology.hl7.org/ValueSet/v2-2.7-030", "code": "MD", }] } }, { "code": { "coding": [{ "system": "http://terminology.hl7.org/ValueSet/v2-2.7-030", "code": "MD", }] }, "period": { "start": "2011-01-01", "end": "2023-12-31" }, "issuer": { "reference": "Organization/Harpers_Ferry_Family_Medicine" }, }, ], "communication": [{ "coding": [{ "system": "urn:ietf:bcp:47", "code": "en" }] }], }], "practitionerrole": [{ "resourceType": "PractitionerRole", "id": "1437321965-ML1-MLSW", "meta": { "versionId": "1", "lastUpdated": "2021-01-13T04:37:24+00:00", "source": "http://medstarhealth.org/provider", }, "practitioner": { "reference": "Practitioner/1437321965" }, "organization": { "reference": "Organization/Medstar-15888213" }, "code": [{ "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/practitioner-role", "code": "doctor", "display": "Doctor", }], "text": "Doctor", }], "specialty": [ { "coding": [{ "system": "https://www.http://medstarhealth.org", "code": "Family Medicine", "display": "Family Medicine", }], "text": "Family Medicine", }, { "coding": [{ "system": "http://nucc.org/provider-taxonomy", "code": "207Q00000X", "display": "Family Medicine", }], "text": "Family Medicine", }, { "coding": [{ "system": "http://nucc.org/provider-taxonomy", "code": "261QP2300X", "display": "Primary Care", }], "text": "Primary Care", }, ], "location": [{ "reference": "Location/Medstar-Alias-ML1-MLSW" }], }], } mock.get(f"{url}/Patient", json=response_text) fhir_client = FhirClient().separate_bundle_resources(True) fhir_client = fhir_client.url(url).resource("Patient") response: FhirGetResponse = fhir_client.get() print(response.responses) assert response.responses == json.dumps(expected_response)