File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments