-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add X-Permit-Consistent-Update header on facts proxy requests #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -353,7 +353,7 @@ async def forward_request_then_wait_for_update( | |
| query_params: dict[str, Any] | None = None, | ||
| ) -> Response: | ||
| _update_id = update_id or uuid4() | ||
| response = await client.send_forward_request(request, path, query_params=query_params) | ||
| response = await client.send_forward_request(request, path, query_params=query_params, is_consistent_update=True) | ||
|
omer9564 marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Router integration is untested The only place Suggestion: Add a router-level test that mocks
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added router-level coverage in |
||
| body = client.extract_body(response) | ||
| if body is None: | ||
| return client.convert_response(response) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
| from horizon.facts.client import CONSISTENT_UPDATE_HEADER, FactsClient | ||
| from starlette.requests import Request as FastApiRequest | ||
|
|
||
|
|
||
| def _make_request(headers: dict[str, str] | None = None) -> FastApiRequest: | ||
| scope = { | ||
| "type": "http", | ||
| "method": "POST", | ||
| "path": "/facts/users", | ||
| "raw_path": b"/facts/users", | ||
| "query_string": b"", | ||
| "headers": [(k.lower().encode(), v.encode()) for k, v in (headers or {}).items()], | ||
| } | ||
|
|
||
| async def receive(): | ||
| return {"type": "http.request", "body": b"", "more_body": False} | ||
|
|
||
| return FastApiRequest(scope, receive) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_build_forward_request_adds_header_when_consistent_update(): | ||
| """When is_consistent_update=True, request should carry the X-Permit-Consistent-Update header with value 'true'.""" | ||
| client = FactsClient() | ||
|
|
||
| mock_remote_config = MagicMock() | ||
| mock_remote_config.context = {"project_id": "proj1", "env_id": "env1"} | ||
|
|
||
| with ( | ||
| patch("horizon.facts.client.get_remote_config", return_value=mock_remote_config), | ||
| patch("horizon.facts.client.get_env_api_key", return_value="test_api_key"), | ||
| ): | ||
| request = _make_request(headers={"authorization": "Bearer user_token", "content-type": "application/json"}) | ||
| forward_request = await client.build_forward_request(request, "/users", is_consistent_update=True) | ||
|
|
||
| # Check the literal header name (not the constant) so a constant rename is caught by tests. | ||
| assert "X-Permit-Consistent-Update" in forward_request.headers | ||
| assert forward_request.headers["X-Permit-Consistent-Update"] == "true" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_build_forward_request_omits_header_by_default(): | ||
| """By default (fallback proxy path), the request should NOT carry the consistent-update header.""" | ||
| client = FactsClient() | ||
|
|
||
| mock_remote_config = MagicMock() | ||
| mock_remote_config.context = {"project_id": "proj1", "env_id": "env1"} | ||
|
|
||
| with ( | ||
| patch("horizon.facts.client.get_remote_config", return_value=mock_remote_config), | ||
| patch("horizon.facts.client.get_env_api_key", return_value="test_api_key"), | ||
| ): | ||
| request = _make_request(headers={"authorization": "Bearer user_token", "content-type": "application/json"}) | ||
| forward_request = await client.build_forward_request(request, "/anything") | ||
|
|
||
| assert forward_request.headers.get(CONSISTENT_UPDATE_HEADER) is None | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_send_forward_request_propagates_consistent_update_kwarg(): | ||
| """send_forward_request must plumb is_consistent_update into the built request's headers.""" | ||
| client = FactsClient() | ||
|
|
||
| mock_remote_config = MagicMock() | ||
| mock_remote_config.context = {"project_id": "proj1", "env_id": "env1"} | ||
|
|
||
| with ( | ||
| patch("horizon.facts.client.get_remote_config", return_value=mock_remote_config), | ||
| patch("horizon.facts.client.get_env_api_key", return_value="test_api_key"), | ||
| patch.object(FactsClient, "send", new_callable=AsyncMock) as mock_send, | ||
| ): | ||
| request = _make_request(headers={"authorization": "Bearer user_token", "content-type": "application/json"}) | ||
| await client.send_forward_request(request, "/users", is_consistent_update=True) | ||
|
|
||
| assert mock_send.await_count == 1 | ||
| assert mock_send.call_args is not None | ||
| sent_request = mock_send.call_args.args[0] | ||
| assert sent_request.headers.get("X-Permit-Consistent-Update") == "true" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
| from horizon.facts.client import FactsClient | ||
| from horizon.facts.router import forward_remaining_requests, forward_request_then_wait_for_update | ||
| from httpx import Response as HttpxResponse | ||
| from starlette.requests import Request as FastApiRequest | ||
|
|
||
|
|
||
| def _make_request(headers: dict[str, str] | None = None) -> FastApiRequest: | ||
| scope = { | ||
| "type": "http", | ||
| "method": "POST", | ||
| "path": "/facts/users", | ||
| "raw_path": b"/facts/users", | ||
| "query_string": b"", | ||
| "headers": [(k.lower().encode(), v.encode()) for k, v in (headers or {}).items()], | ||
| } | ||
|
|
||
| async def receive(): | ||
| return {"type": "http.request", "body": b"", "more_body": False} | ||
|
|
||
| return FastApiRequest(scope, receive) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_forward_request_then_wait_for_update_sets_consistent_update_flag(): | ||
| """The wait-for-update proxy path MUST pass is_consistent_update=True to the client.""" | ||
| client = MagicMock(spec=FactsClient) | ||
| client.send_forward_request = AsyncMock(return_value=HttpxResponse(status_code=204)) | ||
| client.extract_body = MagicMock(return_value=None) | ||
| client.convert_response = MagicMock(return_value=MagicMock()) | ||
|
|
||
| update_subscriber = MagicMock() | ||
| request = _make_request(headers={"authorization": "Bearer token"}) | ||
|
|
||
| await forward_request_then_wait_for_update( | ||
| client, | ||
| request, | ||
| update_subscriber, | ||
| wait_timeout=0, | ||
| path="/users", | ||
| entries_callback=lambda _r, _body, _update_id: [], | ||
| ) | ||
|
|
||
| assert client.send_forward_request.await_count == 1 | ||
| _, kwargs = client.send_forward_request.await_args | ||
| assert kwargs.get("is_consistent_update") is True | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_forward_remaining_requests_does_not_set_consistent_update_header(): | ||
| """The fallback proxy route MUST NOT mark the request as a consistent update.""" | ||
| client = FactsClient() | ||
|
|
||
| mock_remote_config = MagicMock() | ||
| mock_remote_config.context = {"project_id": "proj1", "env_id": "env1"} | ||
|
|
||
| captured = {} | ||
|
|
||
| async def fake_send(request, *, stream=False): # noqa: ARG001 | ||
| captured["headers"] = dict(request.headers) | ||
| return HttpxResponse(status_code=204) | ||
|
|
||
| with ( | ||
| patch("horizon.facts.client.get_remote_config", return_value=mock_remote_config), | ||
| patch("horizon.facts.client.get_env_api_key", return_value="test_api_key"), | ||
| patch.object(FactsClient, "send", side_effect=fake_send), | ||
| ): | ||
| request = _make_request(headers={"authorization": "Bearer token", "content-type": "application/json"}) | ||
| await forward_remaining_requests(request, client, full_path="some/other/path") | ||
|
|
||
| assert "X-Permit-Consistent-Update" not in captured["headers"] | ||
| assert "x-permit-consistent-update" not in captured["headers"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
send_forward_requestkwarg passthrough is untestedThe kwarg plumbing from
send_forward_requestintobuild_forward_requesthas no direct test. The PR's purpose is propagating this flag across that boundary, so the boundary itself should be covered.Suggestion: Add a test that patches
FactsClient.sendand asserts the built request carriesX-Permit-Consistent-Update: truewhensend_forward_request(..., is_consistent_update=True)is called.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
test_send_forward_request_propagates_consistent_update_kwargwhich patchesFactsClient.send, callssend_forward_request(..., is_consistent_update=True), and asserts the builtHttpxRequestcarriesX-Permit-Consistent-Update: true. Fixed in d02054a.