Skip to content

Commit 51fd804

Browse files
committed
test(utils): add tests for RH identity utility
Signed-off-by: Major Hayden <major@redhat.com>
1 parent c441be2 commit 51fd804

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

tests/unit/utils/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Unit tests for utils/query.py functions.
3939
## [test_responses.py](test_responses.py)
4040
Unit tests for utils/responses.py functions.
4141

42+
## [test_rh_identity.py](test_rh_identity.py)
43+
Unit tests for utils/rh_identity module.
44+
4245
## [test_shields.py](test_shields.py)
4346
Unit tests for utils/shields.py functions.
4447

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Unit tests for utils/rh_identity module."""
2+
3+
import pytest
4+
from pytest_mock import MockerFixture
5+
6+
from authentication.rh_identity import RHIdentityData
7+
from utils.rh_identity import AUTH_DISABLED, get_rh_identity_context
8+
9+
10+
def test_auth_disabled_constant() -> None:
11+
"""Verify AUTH_DISABLED constant value."""
12+
assert AUTH_DISABLED == "auth_disabled"
13+
14+
15+
@pytest.mark.parametrize(
16+
("rh_identity_setup", "expected_org_id", "expected_system_id"),
17+
[
18+
pytest.param(
19+
{"org_id": "org123", "user_id": "sys456"},
20+
"org123",
21+
"sys456",
22+
id="identity_present",
23+
),
24+
pytest.param(
25+
None,
26+
AUTH_DISABLED,
27+
AUTH_DISABLED,
28+
id="identity_absent",
29+
),
30+
pytest.param(
31+
{"org_id": "", "user_id": "sys456"},
32+
AUTH_DISABLED,
33+
"sys456",
34+
id="empty_org_id",
35+
),
36+
pytest.param(
37+
{"org_id": "org123", "user_id": ""},
38+
"org123",
39+
AUTH_DISABLED,
40+
id="empty_user_id",
41+
),
42+
],
43+
)
44+
def test_get_rh_identity_context(
45+
mocker: MockerFixture,
46+
rh_identity_setup: dict[str, str] | None,
47+
expected_org_id: str,
48+
expected_system_id: str,
49+
) -> None:
50+
"""Test get_rh_identity_context extracts or defaults org/system IDs."""
51+
mock_request = mocker.Mock()
52+
53+
if rh_identity_setup is not None:
54+
mock_rh_identity = mocker.Mock(spec=RHIdentityData)
55+
mock_rh_identity.get_org_id.return_value = rh_identity_setup["org_id"]
56+
mock_rh_identity.get_user_id.return_value = rh_identity_setup["user_id"]
57+
mock_request.state = mocker.Mock()
58+
mock_request.state.rh_identity_data = mock_rh_identity
59+
else:
60+
mock_request.state = mocker.Mock(spec=[])
61+
62+
org_id, system_id = get_rh_identity_context(mock_request)
63+
64+
assert org_id == expected_org_id
65+
assert system_id == expected_system_id

0 commit comments

Comments
 (0)