-
Notifications
You must be signed in to change notification settings - Fork 1
[GPCAPIM-265] Generate X-Request-Id header #85
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
|
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. I would parameterise the header test so that only a single assertion is made per test. You could also patch the uuid module to return a yest value and assert for that. |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,6 @@ | |||||||||||
| from dataclasses import dataclass | ||||||||||||
| from datetime import date | ||||||||||||
| from typing import TYPE_CHECKING, Any, cast | ||||||||||||
| from uuid import uuid4 | ||||||||||||
|
|
||||||||||||
| import pytest | ||||||||||||
| import requests | ||||||||||||
|
|
@@ -270,7 +269,8 @@ def test_search_patient_by_nhs_number_sends_expected_headers( | |||||||||||
| * Authorization header | ||||||||||||
| * NHSD-End-User-Organisation-ODS header | ||||||||||||
| * Accept header | ||||||||||||
| * caller-provided X-Request-ID and X-Correlation-ID headers | ||||||||||||
| * auto-generated X-Request-ID (UUID format) | ||||||||||||
| * caller-provided X-Correlation-ID header | ||||||||||||
|
|
||||||||||||
| :param stub: Stub backend fixture. | ||||||||||||
| :param mock_requests_get: Patched ``requests.get`` fixture capturing outbound | ||||||||||||
|
|
@@ -291,12 +291,10 @@ def test_search_patient_by_nhs_number_sends_expected_headers( | |||||||||||
| base_url="https://example.test/personal-demographics/FHIR/R4", | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| req_id = str(uuid4()) | ||||||||||||
| corr_id = "corr-123" | ||||||||||||
|
|
||||||||||||
| result = client.search_patient_by_nhs_number( | ||||||||||||
| "9000000009", | ||||||||||||
| request_id=req_id, | ||||||||||||
| correlation_id=corr_id, | ||||||||||||
| ) | ||||||||||||
| assert result is not None | ||||||||||||
|
|
@@ -305,7 +303,14 @@ def test_search_patient_by_nhs_number_sends_expected_headers( | |||||||||||
| assert headers["Authorization"] == "Bearer test-token" | ||||||||||||
| assert headers["NHSD-End-User-Organisation-ODS"] == "A12345" | ||||||||||||
| assert headers["Accept"] == "application/fhir+json" | ||||||||||||
| assert headers["X-Request-ID"] == req_id | ||||||||||||
| # X-Request-ID should be auto-generated as a UUID | ||||||||||||
| assert "X-Request-ID" in headers | ||||||||||||
| assert isinstance(headers["X-Request-ID"], str) | ||||||||||||
| assert len(headers["X-Request-ID"]) >= 32 | ||||||||||||
| # Verify it's a valid UUID by trying to parse it | ||||||||||||
|
Comment on lines
+306
to
+310
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. IMO, these are unnecessary. The final assertion, |
||||||||||||
| from uuid import UUID | ||||||||||||
|
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. Move to import block at the top of the file. |
||||||||||||
|
|
||||||||||||
| UUID(headers["X-Request-ID"]) # Should not raise | ||||||||||||
|
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. To produce nicer error messages if/when the test fails:
Suggested change
|
||||||||||||
| assert headers["X-Correlation-ID"] == corr_id | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
@@ -314,10 +319,10 @@ def test_search_patient_by_nhs_number_generates_request_id( | |||||||||||
| mock_requests_get: dict[str, Any], | ||||||||||||
| ) -> None: | ||||||||||||
| """ | ||||||||||||
| Verify that the client generates an X-Request-ID when not provided. | ||||||||||||
| Verify that the client generates a new X-Request-ID for each request. | ||||||||||||
|
|
||||||||||||
| The stub is in strict mode, so a missing or invalid X-Request-ID would cause a 400. | ||||||||||||
| This test confirms a request ID is present and looks UUID-like. | ||||||||||||
| This test confirms a request ID is present and is a valid UUID. | ||||||||||||
|
|
||||||||||||
| :param stub: Stub backend fixture. | ||||||||||||
| :param mock_requests_get: Patched ``requests.get`` fixture capturing outbound | ||||||||||||
|
|
||||||||||||
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.
You've stated this twice. I'm not sure it's need once.