Skip to content

Commit 29d3329

Browse files
authored
Merge pull request #1448 from tisnik/lcore-1608-updated-docstrings-in-auth-unit-tests
LCORE-1608: Updated docstrings in auth unit tests
2 parents 8974907 + fa5bacb commit 29d3329

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

tests/unit/authentication/test_api_key_token.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
def default_api_key_token_configuration() -> APIKeyTokenConfiguration:
1616
"""Default APIKeyTokenConfiguration for testing.
1717
18-
Provide a default APIKeyTokenConfiguration for tests.
18+
Pytest fixture providing a default APIKeyTokenConfiguration for
19+
tests.
1920
2021
Returns:
2122
APIKeyTokenConfiguration: configuration with `api_key` set to

tests/unit/authentication/test_jwk_token.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ def token_payload() -> dict[str, Any]:
5757

5858

5959
def make_key() -> dict[str, Any]:
60-
"""Generate a key pair for testing purposes."""
60+
"""Generate a key pair for testing purposes.
61+
62+
Create an RSA test key pair and return the private key, public key, and key identifier.
63+
64+
Returns:
65+
dict: A dictionary with the following entries:
66+
- "private_key": the generated private JsonWebKey instance.
67+
- "public_key": the corresponding public JsonWebKey instance.
68+
- "kid": the key identifier (thumbprint) as a string.
69+
"""
6170
key = JsonWebKey.generate_key("RSA", 2048, is_private=True)
6271
return {
6372
"private_key": key,
@@ -79,8 +88,10 @@ def another_single_key_set() -> list[dict[str, Any]]:
7988
Create a single-key JWK set using a newly generated RSA key.
8089
8190
Returns:
82-
list[dict[str, Any]]: A list containing one key dict with keys
83-
`private_key`, `public_key`, and `kid`.
91+
list[dict[str, Any]]: A list containing one dict with keys:
92+
- `private_key`: the generated private JsonWebKey
93+
- `public_key`: the corresponding public JsonWebKey
94+
- `kid`: the key identifier (thumbprint)
8495
"""
8596
return [make_key()]
8697

@@ -114,7 +125,11 @@ def valid_token(
114125

115126
@pytest.fixture(autouse=True)
116127
def clear_jwk_cache() -> Generator:
117-
"""Clear the global JWK cache before each test."""
128+
"""Clear the global JWK cache before each test.
129+
130+
This autouse fixture ensures the module-level `_jwk_cache` is emptied at
131+
setup and teardown to prevent cross-test interference.
132+
"""
118133
_jwk_cache.clear()
119134
yield
120135
_jwk_cache.clear()
@@ -497,6 +512,9 @@ def no_user_id_token(
497512
`single_key_set`; the supplied `token_payload` is modified in-place to
498513
remove `user_id`.
499514
515+
Parameters:
516+
token_payload (dict): Payload to encode; will be mutated to remove `user_id`.
517+
500518
Returns:
501519
jwt (str): Encoded JWT as a string that does not contain the `user_id` claim.
502520
"""
@@ -637,7 +655,11 @@ async def test_custom_claims(
637655
mocked_signing_keys_server: Any,
638656
custom_claims_token: str,
639657
) -> None:
640-
"""Test with a token that has custom claims."""
658+
"""Test with a token that has custom claims.
659+
660+
Asserts the returned auth tuple matches the expected user id, username,
661+
skip flag, and original token.
662+
"""
641663
_ = mocked_signing_keys_server
642664

643665
dependency = JwkTokenAuthDependency(custom_claims_configuration)
@@ -650,7 +672,15 @@ async def test_custom_claims(
650672

651673
@pytest.fixture
652674
def token_header_256_1(multi_key_set: list[dict[str, Any]]) -> dict[str, Any]:
653-
"""A sample token header for RS256 using multi_key_set."""
675+
"""A sample token header for RS256 using multi_key_set.
676+
677+
Parameters:
678+
multi_key_set (list[dict[str, Any]]): List of JWK dictionaries; the
679+
header's `kid` is taken from multi_key_set[0]["kid"].
680+
681+
Returns:
682+
dict[str, Any]: JWT header containing "alg", "typ", and "kid".
683+
"""
654684
return {"alg": "RS256", "typ": "JWT", "kid": multi_key_set[0]["kid"]}
655685

656686

@@ -727,7 +757,7 @@ def multi_key_set() -> list[dict[str, Any]]:
727757
by tests (e.g., `private_key`, `public_key`, and `kid`).
728758
729759
Returns:
730-
key_set (list[dict]): A list of three signing key dictionaries.
760+
list[dict[str, Any]]: A list of three signing key dictionaries.
731761
"""
732762
return [make_key(), make_key(), make_key()]
733763

tests/unit/authentication/test_k8s.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,15 @@ async def test_kube_admin_cluster_version_not_found_returns_500(
836836
async def test_kube_admin_cluster_version_permission_error_returns_500(
837837
mocker: MockerFixture,
838838
) -> None:
839-
"""Test kube:admin flow returns 500 when permission to ClusterVersion is denied."""
839+
"""Test kube:admin flow returns 500 when permission to ClusterVersion is denied.
840+
841+
Sets up a request with a valid Bearer token, mocks subject access review to
842+
allow the action and mocks the authenticated user as `kube:admin`. Patches
843+
cluster-id retrieval to raise ClusterVersionPermissionError and asserts
844+
that the dependency raises an HTTPException with status code 500,
845+
`detail["response"] == "Internal server error"`, and that `detail["cause"]`
846+
contains an "Insufficient permissions" message.
847+
"""
840848
dependency = K8SAuthDependency()
841849
mock_authz_api = mocker.patch("authentication.k8s.K8sClientSingleton.get_authz_api")
842850
mock_authz_api.return_value.create_subject_access_review.return_value = (

tests/unit/authentication/test_rh_identity.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def user_identity_data() -> dict:
2121
2222
Provide a valid Red Hat identity payload for a User, suitable for unit tests.
2323
24+
The payload contains two top-level keys:
25+
- `identity`: includes `account_number`, `org_id`, `type` (set to
26+
`"User"`), and `user` with `user_id`, `username`, and `is_org_admin`.
27+
- `entitlements`: maps service names (for example, `"rhel"`, `"ansible"`,
28+
`"openshift"`) to objects with `is_entitled` and `is_trial`.
29+
2430
Returns:
2531
identity_data (dict): A dictionary with two top-level keys:
2632
- "identity": contains "account_number", "org_id", "type" (set to
@@ -485,7 +491,13 @@ class TestRHIdentityHealthProbeSkip:
485491
def _mock_configuration(
486492
mocker: MockerFixture, skip_for_health_probes: bool
487493
) -> None:
488-
"""Patch the configuration singleton with a mock for probe skip tests."""
494+
"""Patch the configuration singleton with a mock for probe skip tests.
495+
496+
Parameters:
497+
skip_for_health_probes (bool): Value to assign to the mocked
498+
configuration's authentication_configuration.skip_for_health_probes
499+
flag.
500+
"""
489501
mock_config = mocker.MagicMock()
490502
mock_config.authentication_configuration.skip_for_health_probes = (
491503
skip_for_health_probes
@@ -714,7 +726,13 @@ def test_org_id_valid_accepted(self, user_identity_data: dict) -> None:
714726
RHIdentityData(user_identity_data)
715727

716728
def test_org_id_oversized_rejected(self, user_identity_data: dict) -> None:
717-
"""Reject oversized org_id."""
729+
"""Reject oversized org_id.
730+
731+
Verifies that an identity with an org_id longer than 256 characters is rejected.
732+
733+
Expects RHIdentityData to raise an HTTPException with status_code 400
734+
when identity.org_id length exceeds 256 characters.
735+
"""
718736
user_identity_data["identity"]["org_id"] = "a" * 257
719737
with pytest.raises(HTTPException) as exc_info:
720738
RHIdentityData(user_identity_data)

0 commit comments

Comments
 (0)