Skip to content

Commit b043837

Browse files
authored
Merge pull request #1366 from major/RSPEED-2691/consolidate-rlsapi-v1-tests
RSPEED-2691: Consolidate rlsapi_v1 unit tests to reduce duplication
2 parents 962193c + 6f6c00e commit b043837

2 files changed

Lines changed: 194 additions & 187 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Shared pytest fixtures for endpoint unit tests."""
2+
3+
from collections.abc import Callable
4+
from typing import Any
5+
6+
import pytest
7+
from pytest_mock import MockerFixture
8+
9+
10+
@pytest.fixture(name="mock_request_factory")
11+
def mock_request_factory_fixture(mocker: MockerFixture) -> Callable[..., Any]:
12+
"""Create a mock FastAPI Request with optional RH Identity.
13+
14+
Returns:
15+
Callable that accepts an optional rh_identity argument and returns a Mock request.
16+
"""
17+
18+
def _create(rh_identity: Any = None) -> Any:
19+
mock_request = mocker.Mock()
20+
mock_request.headers = {"User-Agent": "CLA/0.4.2"}
21+
22+
if rh_identity is not None:
23+
mock_request.state = mocker.Mock()
24+
mock_request.state.rh_identity_data = rh_identity
25+
else:
26+
# Use spec=[] to create a Mock with no attributes, simulating absent rh_identity_data
27+
mock_request.state = mocker.Mock(spec=[])
28+
29+
return mock_request
30+
31+
return _create
32+
33+
34+
@pytest.fixture(name="mock_background_tasks")
35+
def mock_background_tasks_fixture(mocker: MockerFixture) -> Any:
36+
"""Create a mock BackgroundTasks object.
37+
38+
Returns:
39+
A Mock object representing FastAPI BackgroundTasks.
40+
"""
41+
return mocker.Mock()

0 commit comments

Comments
 (0)