async def test_fhir_client_patient_list_auth_fail_retry_async() -> None:
    test_name = "test_fhir_client_patient_list_auth_fail_retry_async"

    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 = await fhir_client.get_async()

    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)