|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | # Standard Library |
6 | | -from unittest.mock import MagicMock, PropertyMock, patch |
| 6 | +from unittest.mock import MagicMock, patch |
7 | 7 |
|
8 | 8 | # Third Party |
9 | | -import requests_mock |
| 9 | +import httpx |
10 | 10 |
|
11 | 11 | # Django |
12 | 12 | from django.conf import settings |
13 | | -from django.test import TestCase |
14 | 13 |
|
15 | 14 | # Alliance Auth |
16 | 15 | from esi import __url__ as esi_url |
17 | 16 | from esi import __version__ as esi_version |
| 17 | +from esi.openapi_clients import ESIClientProvider |
| 18 | +from esi.tests.test_openapi import SPEC_PATH |
18 | 19 |
|
19 | 20 | # AA Sovereignty Timer |
20 | | -from sovtimer import __app_name_useragent__, __github_url__, __version__ |
21 | | -from sovtimer.providers import esi |
| 21 | +from sovtimer import ( |
| 22 | + __app_name_verbose__, |
| 23 | + __esi_compatibility_date__, |
| 24 | + __github_url__, |
| 25 | + __version__, |
| 26 | +) |
| 27 | +from sovtimer.tests import BaseTestCase |
22 | 28 |
|
23 | 29 |
|
24 | | -class TestUserAgent(TestCase): |
| 30 | +class TestUserAgent(BaseTestCase): |
25 | 31 | """ |
26 | 32 | Test suite for verifying the `User-Agent` header in ESI requests. |
27 | 33 | """ |
28 | 34 |
|
29 | | - @requests_mock.Mocker() |
30 | | - def test_user_agent_header(self, requests_mocker): |
| 35 | + @patch.object(httpx.Client, "send") |
| 36 | + def test_user_agent_header(self, send: MagicMock): |
31 | 37 | """ |
32 | | - Test the `User-Agent` header for ESI requests. |
| 38 | + Test that the `User-Agent` header is correctly set in ESI requests. |
33 | 39 |
|
34 | | - This test ensures that the `User-Agent` header in the request matches |
35 | | - the expected format. It uses mocking to avoid making live API calls |
36 | | - and verifies the header construction. |
| 40 | + This test verifies that the `User-Agent` header in HTTP requests made by the ESI client |
| 41 | + is constructed correctly based on the provided application name, version, and other metadata. |
37 | 42 |
|
38 | | - :param requests_mocker: A mocker object provided by the `requests_mock` library |
39 | | - to intercept HTTP requests. |
40 | | - :type requests_mocker: requests_mock.mocker.Mocker |
41 | | - :return: None |
| 43 | + Args: |
| 44 | + send (MagicMock): A mocked `httpx.Client.send` method to intercept HTTP requests and provide a controlled response. |
| 45 | +
|
| 46 | + Assertions: |
| 47 | + - The `User-Agent` header in the HTTP request matches the expected format. |
| 48 | + - The `players` field in the response JSON is correctly parsed and matches the expected value. |
42 | 49 | """ |
43 | 50 |
|
44 | | - # Create a mock response object with the expected `User-Agent` header |
45 | | - mock_response = MagicMock() |
46 | | - mock_response.request.headers = { |
47 | | - "User-Agent": ( |
48 | | - f"{__app_name_useragent__}/{__version__} " |
| 51 | + # Initialize the ESI client provider with test-specific metadata |
| 52 | + esi = ESIClientProvider( |
| 53 | + ua_appname=__app_name_verbose__, # Application name for the User-Agent header |
| 54 | + ua_url=__github_url__, # Application URL for the User-Agent header |
| 55 | + ua_version=__version__, # Application version for the User-Agent header |
| 56 | + compatibility_date=__esi_compatibility_date__, # Compatibility date for the ESI spec |
| 57 | + spec_file=SPEC_PATH, # Path to the OpenAPI specification file |
| 58 | + ) |
| 59 | + |
| 60 | + # Mock the HTTP response returned by the `send` method |
| 61 | + send.return_value = httpx.Response( |
| 62 | + status_code=200, # HTTP status code for the response |
| 63 | + json={ # Mocked JSON response body |
| 64 | + "players": 1234, |
| 65 | + "server_version": "1234", |
| 66 | + "start_time": "2029-09-19T11:02:08Z", |
| 67 | + }, |
| 68 | + request=httpx.Request(method="GET", url="test"), # Mocked HTTP request |
| 69 | + ) |
| 70 | + |
| 71 | + # Perform the ESI client request and retrieve the status |
| 72 | + status = esi.client.Status.GetStatus().result() |
| 73 | + |
| 74 | + # Retrieve the arguments passed to the mocked `send` method |
| 75 | + call_args, call_kwargs = send.call_args |
| 76 | + |
| 77 | + # Assert that the `User-Agent` header matches the expected format |
| 78 | + self.assertEqual( |
| 79 | + first=call_args[0].headers["user-agent"], |
| 80 | + second=( |
| 81 | + f"AaSovereigntyTimer/{__version__} " |
49 | 82 | f"({settings.ESI_USER_CONTACT_EMAIL}; +{__github_url__}) " |
50 | | - f"Django-ESI/{esi_version} (+{esi_url})" |
51 | | - ) |
52 | | - } |
53 | | - # Mock the result of the API call |
54 | | - mock_result = (None, mock_response) |
55 | | - |
56 | | - # Create a mock `Status` object and configure its `GetStatus` method |
57 | | - mock_status = MagicMock() |
58 | | - mock_status.GetStatus.return_value.result.return_value = mock_result |
59 | | - |
60 | | - # Create a mock `client` object and assign the mock `Status` object to it |
61 | | - mock_client = MagicMock() |
62 | | - mock_client.Status = mock_status |
63 | | - |
64 | | - # Patch the `client` property of the `ESIClientProvider` class to use the mock client |
65 | | - with patch( |
66 | | - target="esi.openapi_clients.ESIClientProvider.client", |
67 | | - new_callable=PropertyMock, |
68 | | - ) as mock_client_prop: |
69 | | - mock_client_prop.return_value = mock_client |
70 | | - |
71 | | - # Call the mocked `GetStatus` method and retrieve the response |
72 | | - _, response = esi.client.Status.GetStatus().result(return_response=True) |
73 | | - |
74 | | - # Assert that the `User-Agent` header in the response matches the expected value |
75 | | - self.assertEqual( |
76 | | - first=response.request.headers["User-Agent"], |
77 | | - second=( |
78 | | - f"{__app_name_useragent__}/{__version__} " |
79 | | - f"({settings.ESI_USER_CONTACT_EMAIL}; +{__github_url__}) " |
80 | | - f"Django-ESI/{esi_version} (+{esi_url})" |
81 | | - ), |
82 | | - ) |
| 83 | + f"DjangoEsi/{esi_version} (+{esi_url})" |
| 84 | + ), |
| 85 | + ) |
| 86 | + |
| 87 | + # Assert that the `players` field in the response matches the expected value |
| 88 | + self.assertEqual(first=status.players, second=1234) |
0 commit comments