Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
64 lines (52 loc) · 1.94 KB
/
Copy pathconftest.py
File metadata and controls
64 lines (52 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Project conftest"""
# pylint: disable=wildcard-import, unused-wildcard-import
from types import SimpleNamespace
import pytest
from fixtures.common import * # noqa: F403
from fixtures.users import * # noqa: F403
from main.exceptions import DoNotUseRequestException
@pytest.fixture(autouse=True)
def mock_apisix_auth(mocker):
"""
Neutralize the APISIX middleware login/logout in tests.
The middleware treats APISIX as the source of truth and logs out any Django
session lacking the gateway's user header, which would otherwise drop the
sessions established by ``client.force_login``. Tests that exercise the
middleware itself patch these targets again locally.
"""
return SimpleNamespace(
login=mocker.patch("main.middleware.apisix_user.login"),
logout=mocker.patch("main.middleware.apisix_user.logout"),
)
@pytest.fixture(autouse=True)
def prevent_requests(mocker, request):
"""Patch requests to error on request by default"""
if "mocked_responses" in request.fixturenames:
return
mocker.patch(
"requests.sessions.Session.request",
autospec=True,
side_effect=DoNotUseRequestException,
)
@pytest.fixture(autouse=True)
def _use_dummy_redis_cache_backend(settings):
new_cache_settings = settings.CACHES.copy()
new_cache_settings["redis"] = {
"BACKEND": "django.core.cache.backends.dummy.DummyCache",
}
settings.CACHES = new_cache_settings
@pytest.fixture
def mock_http_consumer_send(mocker):
"""Mock the AsyncHttpConsumer class send functions"""
mock_send_body = mocker.patch(
"channels.generic.http.AsyncHttpConsumer.send_body",
new_callable=mocker.AsyncMock,
)
mock_send_headers = mocker.patch(
"channels.generic.http.AsyncHttpConsumer.send_headers",
new_callable=mocker.AsyncMock,
)
return SimpleNamespace(
send_body=mock_send_body,
send_headers=mock_send_headers,
)