Skip to content

Commit 8f8dfde

Browse files
committed
[FIX] Don't do live ESI calls for tests
1 parent c5f680c commit 8f8dfde

1 file changed

Lines changed: 46 additions & 11 deletions

File tree

‎sovtimer/tests/test_user_agent.py‎

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Test user agent header for ESI requests
33
"""
44

5+
# Standard Library
6+
from unittest.mock import MagicMock, PropertyMock, patch
7+
58
# Third Party
69
import requests_mock
710

@@ -28,6 +31,13 @@ class TestUserAgent(TestCase):
2831

2932
@classmethod
3033
def setUpClass(cls) -> None:
34+
"""
35+
Setup the test class
36+
37+
:return:
38+
:rtype:
39+
"""
40+
3141
super().setUpClass()
3242

3343
cls.status_response = {
@@ -36,18 +46,43 @@ def setUpClass(cls) -> None:
3646
"start_time": "2017-01-02T12:34:56Z",
3747
}
3848

39-
# @patch(MODULE_PATH + ".app_settings.ESI_USER_CONTACT_EMAIL", "email@example.com")
4049
def test_user_agent_header(self, requests_mocker):
41-
requests_mocker.register_uri(
42-
"GET", url="http://localhost", json=self.status_response
43-
)
44-
_, response = esi.client.Status.GetStatus().result(return_response=True)
45-
46-
self.assertEqual(
47-
first=response.request.headers["User-Agent"],
48-
second=(
50+
"""
51+
Test the user agent header for ESI requests
52+
53+
:param requests_mocker:
54+
:type requests_mocker:
55+
:return:
56+
:rtype:
57+
"""
58+
59+
mock_response = MagicMock()
60+
mock_response.request.headers = {
61+
"User-Agent": (
4962
f"{__app_name_useragent__}/{__version__} "
5063
f"({settings.ESI_USER_CONTACT_EMAIL}; +{__github_url__}) "
5164
f"Django-ESI/{esi_version} (+{esi_url})"
52-
),
53-
)
65+
)
66+
}
67+
mock_result = (None, mock_response)
68+
69+
mock_status = MagicMock()
70+
mock_status.GetStatus.return_value.result.return_value = mock_result
71+
mock_client = MagicMock()
72+
mock_client.Status = mock_status
73+
74+
with patch(
75+
target="esi.openapi_clients.ESIClientProvider.client",
76+
new_callable=PropertyMock,
77+
) as mock_client_prop:
78+
mock_client_prop.return_value = mock_client
79+
_, response = esi.client.Status.GetStatus().result(return_response=True)
80+
81+
self.assertEqual(
82+
first=response.request.headers["User-Agent"],
83+
second=(
84+
f"{__app_name_useragent__}/{__version__} "
85+
f"({settings.ESI_USER_CONTACT_EMAIL}; +{__github_url__}) "
86+
f"Django-ESI/{esi_version} (+{esi_url})"
87+
),
88+
)

0 commit comments

Comments
 (0)