| 1 |
"""Test for the main endpoint module.""" |
| 2 |
|
| 3 |
from unittest.mock import AsyncMock, MagicMock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from asusrouter.const import RequestType |
| 8 |
from asusrouter.error import AsusRouter404Error, AsusRouterRequestFormatError |
| 9 |
from asusrouter.modules.endpoint import ( |
| 10 |
SENSITIVE_ENDPOINTS, |
| 11 |
Endpoint, |
| 12 |
EndpointService, |
| 13 |
EndpointTools, |
| 14 |
EndpointType, |
| 15 |
_get_module, |
| 16 |
check_available, |
| 17 |
data_get, |
| 18 |
data_set, |
| 19 |
get_request_type, |
| 20 |
is_sensitive_endpoint, |
| 21 |
process, |
| 22 |
read, |
| 23 |
) |
| 24 |
|
| 25 |
TEST_SENSITIVE_ENDPOINTS: tuple[EndpointType, ...] = (EndpointService.LOGIN,) |
| 26 |
IDS_SENSITIVE_ENDPOINTS = ("login",) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
("endpoint"), TEST_SENSITIVE_ENDPOINTS, ids=IDS_SENSITIVE_ENDPOINTS |
| 31 |
) |
| 32 |
def test_marked_sensitive_endpoint(endpoint: EndpointType) -> None: |
| 33 |
"""Test if the endpoint is marked as sensitive.""" |
| 34 |
|
| 35 |
assert endpoint in SENSITIVE_ENDPOINTS |
| 36 |
assert is_sensitive_endpoint(endpoint) is True |
| 37 |
|
| 38 |
|
| 39 |
def test_sensitive_endpoints_immutable() -> None: |
| 40 |
"""SENSITIVE_ENDPOINTS should be an immutable frozenset.""" |
| 41 |
|
| 42 |
assert isinstance(SENSITIVE_ENDPOINTS, frozenset) |
| 43 |
# Attempting to call add should raise AttributeError |
| 44 |
with pytest.raises(AttributeError): |
| 45 |
SENSITIVE_ENDPOINTS.add(EndpointService.LOGOUT) # type: ignore[attr-defined] |
| 46 |
|
| 47 |
# union returns a new frozenset; original remains unchanged |
| 48 |
new_set = SENSITIVE_ENDPOINTS | frozenset({EndpointService.LOGOUT}) |
| 49 |
assert isinstance(new_set, frozenset) |
| 50 |
assert EndpointService.LOGOUT not in SENSITIVE_ENDPOINTS |
| 51 |
assert EndpointService.LOGOUT in new_set |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize( |
| 55 |
("endpoint", "forced_request"), |
| 56 |
[ |
| 57 |
(Endpoint.PORT_STATUS, RequestType.GET), |
| 58 |
(EndpointTools.NETWORK, RequestType.GET), |
| 59 |
(EndpointTools.TRAFFIC_BACKHAUL, RequestType.GET), |
| 60 |
(EndpointTools.TRAFFIC_ETHERNET, RequestType.GET), |
| 61 |
(EndpointTools.TRAFFIC_WIFI, RequestType.GET), |
| 62 |
(EndpointService.LOGIN, RequestType.POST), |
| 63 |
], |
| 64 |
) |
| 65 |
def test_get_request_type( |
| 66 |
endpoint: EndpointType, forced_request: RequestType |
| 67 |
) -> None: |
| 68 |
"""Test get_request_type function.""" |
| 69 |
|
| 70 |
assert get_request_type(endpoint) == forced_request |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.parametrize( |
| 74 |
("endpoint", "expected"), |
| 75 |
[ |
| 76 |
(EndpointService.LOGIN, True), |
| 77 |
(Endpoint.PORT_STATUS, False), |
| 78 |
(EndpointTools.NETWORK, False), |
| 79 |
(EndpointService.LOGOUT, False), |
| 80 |
], |
| 81 |
) |
| 82 |
def test_is_sensitive_endpoint( |
| 83 |
endpoint: EndpointType, |
| 84 |
expected: bool, |
| 85 |
) -> None: |
| 86 |
"""is_sensitive_endpoint returns True only for sensitive endpoints.""" |
| 87 |
|
| 88 |
assert is_sensitive_endpoint(endpoint) is expected |
| 89 |
|
| 90 |
|
| 91 |
def test_get_module() -> None: |
| 92 |
"""Test _get_module method.""" |
| 93 |
|
| 94 |
# Test valid endpoint |
| 95 |
with patch( |
| 96 |
"importlib.import_module", return_value="mocked_module" |
| 97 |
) as mock_import: |
| 98 |
result = _get_module(Endpoint.PORT_STATUS) |
| 99 |
assert result == "mocked_module" # type: ignore[comparison-overlap] |
| 100 |
mock_import.assert_called_once_with( |
| 101 |
"asusrouter.modules.endpoint.port_status" |
| 102 |
) |
| 103 |
|
| 104 |
# Test invalid endpoint |
| 105 |
with patch("importlib.import_module") as mock_import: |
| 106 |
mock_import.side_effect = ModuleNotFoundError |
| 107 |
result = _get_module(Endpoint.FIRMWARE) |
| 108 |
assert result is None |
| 109 |
mock_import.assert_called_once_with( |
| 110 |
"asusrouter.modules.endpoint.firmware" |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
def test_read() -> None: |
| 115 |
"""Test read method.""" |
| 116 |
|
| 117 |
# Mock the module and its read method |
| 118 |
mock_module = MagicMock() |
| 119 |
mock_module.read.return_value = {"mocked": "data"} |
| 120 |
|
| 121 |
# Test valid endpoint |
| 122 |
with patch( |
| 123 |
"asusrouter.modules.endpoint._get_module", return_value=mock_module |
| 124 |
) as mock_get_module: |
| 125 |
result = read(Endpoint.FIRMWARE, "content") |
| 126 |
assert result == {"mocked": "data"} |
| 127 |
mock_get_module.assert_called_once_with(Endpoint.FIRMWARE) |
| 128 |
mock_module.read.assert_called_once_with("content") |
| 129 |
|
| 130 |
# Test invalid endpoint |
| 131 |
with patch( |
| 132 |
"asusrouter.modules.endpoint._get_module", return_value=None |
| 133 |
) as mock_get_module: |
| 134 |
result = read(Endpoint.PORT_STATUS, "content") |
| 135 |
assert result == {} |
| 136 |
mock_get_module.assert_called_once_with(Endpoint.PORT_STATUS) |
| 137 |
|
| 138 |
|
| 139 |
def test_read_module_return_fail() -> None: |
| 140 |
"""Test read method when the module returns wrong result.""" |
| 141 |
|
| 142 |
mock_module = MagicMock() |
| 143 |
mock_module.read.return_value = "not_a_dict" |
| 144 |
|
| 145 |
with patch( |
| 146 |
"asusrouter.modules.endpoint._get_module", return_value=mock_module |
| 147 |
) as mock_get_module: |
| 148 |
result = read(Endpoint.FIRMWARE, "content") |
| 149 |
assert result == {} |
| 150 |
mock_get_module.assert_called_once_with(Endpoint.FIRMWARE) |
| 151 |
|
| 152 |
|
| 153 |
@pytest.mark.parametrize( |
| 154 |
("require_history", "require_firmware", "require_wlan", "call_count"), |
| 155 |
[ |
| 156 |
(True, False, False, 1), |
| 157 |
(False, True, False, 1), |
| 158 |
(False, False, True, 1), |
| 159 |
(False, False, False, 0), |
| 160 |
(True, True, True, 3), |
| 161 |
], |
| 162 |
) |
| 163 |
def test_process( |
| 164 |
require_history: bool, |
| 165 |
require_firmware: bool, |
| 166 |
require_wlan: bool, |
| 167 |
call_count: int, |
| 168 |
) -> None: |
| 169 |
"""Test process method.""" |
| 170 |
|
| 171 |
# Mock the module and its process method |
| 172 |
mock_module = MagicMock() |
| 173 |
mock_module.process.return_value = {"mocked": "data"} |
| 174 |
|
| 175 |
# Mock the data_set function |
| 176 |
mock_data_set = MagicMock() |
| 177 |
|
| 178 |
# Define a side effect function for getattr |
| 179 |
def getattr_side_effect( |
| 180 |
_: object, attr: str, default: bool | None = None |
| 181 |
) -> bool | None: |
| 182 |
"""Return the value of the attribute if it exists.""" |
| 183 |
|
| 184 |
if attr == "REQUIRE_HISTORY": |
| 185 |
return require_history |
| 186 |
if attr == "REQUIRE_FIRMWARE": |
| 187 |
return require_firmware |
| 188 |
if attr == "REQUIRE_WLAN": |
| 189 |
return require_wlan |
| 190 |
return bool(default) if default is not None else None |
| 191 |
|
| 192 |
# Test valid endpoint |
| 193 |
with ( |
| 194 |
patch( |
| 195 |
"asusrouter.modules.endpoint._get_module", return_value=mock_module |
| 196 |
), |
| 197 |
patch("asusrouter.modules.endpoint.data_set", mock_data_set), |
| 198 |
patch( |
| 199 |
"asusrouter.modules.endpoint.getattr", |
| 200 |
side_effect=getattr_side_effect, |
| 201 |
), |
| 202 |
): |
| 203 |
result = process(Endpoint.DEVICEMAP, {"key": "value"}) |
| 204 |
assert result == {"mocked": "data"} |
| 205 |
mock_module.process.assert_called_once_with({"key": "value"}) |
| 206 |
assert mock_data_set.call_count == call_count |
| 207 |
|
| 208 |
|
| 209 |
def test_process_no_module() -> None: |
| 210 |
"""Test process method when no module is found.""" |
| 211 |
|
| 212 |
# Mock the _get_module function to return None |
| 213 |
with patch( |
| 214 |
"asusrouter.modules.endpoint._get_module", return_value=None |
| 215 |
) as mock_get_module: |
| 216 |
result = process(Endpoint.PORT_STATUS, {"key": "value"}) |
| 217 |
assert result == {} |
| 218 |
mock_get_module.assert_called_once_with(Endpoint.PORT_STATUS) |
| 219 |
|
| 220 |
|
| 221 |
def test_process_module_return_fail() -> None: |
| 222 |
"""Test process method when the module returns wrong result.""" |
| 223 |
|
| 224 |
mock_module = MagicMock() |
| 225 |
mock_module.process.return_value = "not_a_dict" |
| 226 |
|
| 227 |
with patch( |
| 228 |
"asusrouter.modules.endpoint._get_module", return_value=mock_module |
| 229 |
) as mock_get_module: |
| 230 |
result = process(Endpoint.DEVICEMAP, {"key": "value"}) |
| 231 |
assert result == {} |
| 232 |
mock_get_module.assert_called_once_with(Endpoint.DEVICEMAP) |
| 233 |
|
| 234 |
|
| 235 |
@pytest.mark.parametrize( |
| 236 |
("error"), |
| 237 |
[ |
| 238 |
AttributeError, |
| 239 |
ValueError, |
| 240 |
], |
| 241 |
) |
| 242 |
def test_process_module_raises(error: type[Exception]) -> None: |
| 243 |
"""Test process method when an exception is raised.""" |
| 244 |
|
| 245 |
mock_module = MagicMock() |
| 246 |
mock_module.process.side_effect = error |
| 247 |
|
| 248 |
with patch( |
| 249 |
"asusrouter.modules.endpoint._get_module", return_value=mock_module |
| 250 |
) as mock_get_module: |
| 251 |
result = process(Endpoint.DEVICEMAP, {"key": "value"}) |
| 252 |
assert result == {} |
| 253 |
mock_get_module.assert_called_once_with(Endpoint.DEVICEMAP) |
| 254 |
|
| 255 |
|
| 256 |
def test_data_set() -> None: |
| 257 |
"""Test data_set function.""" |
| 258 |
|
| 259 |
# Test data |
| 260 |
data = {"key1": "value1"} |
| 261 |
kwargs = {"key2": "value2", "key3": "value3"} |
| 262 |
|
| 263 |
# Call the function |
| 264 |
result = data_set(data, **kwargs) |
| 265 |
|
| 266 |
# Check the result |
| 267 |
assert result == {"key1": "value1", "key2": "value2", "key3": "value3"} |
| 268 |
|
| 269 |
|
| 270 |
@pytest.mark.parametrize( |
| 271 |
("data", "key", "expected", "data_left"), |
| 272 |
[ |
| 273 |
# Key exists |
| 274 |
( |
| 275 |
{"key1": "value1", "key2": "value2"}, |
| 276 |
"key1", |
| 277 |
"value1", |
| 278 |
{"key2": "value2"}, |
| 279 |
), |
| 280 |
# Key does not exist |
| 281 |
( |
| 282 |
{"key1": "value1", "key2": "value2"}, |
| 283 |
"key3", |
| 284 |
None, |
| 285 |
{"key1": "value1", "key2": "value2"}, |
| 286 |
), |
| 287 |
# Empty data |
| 288 |
({}, "key1", None, {}), |
| 289 |
], |
| 290 |
) |
| 291 |
def test_data_get( |
| 292 |
data: dict[str, str], |
| 293 |
key: str, |
| 294 |
expected: str | None, |
| 295 |
data_left: dict[str, str], |
| 296 |
) -> None: |
| 297 |
"""Test data_get function.""" |
| 298 |
|
| 299 |
# Call the function |
| 300 |
result = data_get(data, key) |
| 301 |
|
| 302 |
# Check the result |
| 303 |
assert result == expected |
| 304 |
assert data == data_left |
| 305 |
|
| 306 |
|
| 307 |
@pytest.mark.asyncio |
| 308 |
@pytest.mark.parametrize( |
| 309 |
("api_query_return", "expected_result"), |
| 310 |
[ |
| 311 |
# Test case: status 200 |
| 312 |
((200, None, None), (True, None)), |
| 313 |
# Test case: status 200 and content |
| 314 |
((200, None, "content"), (True, "content")), |
| 315 |
# Test case: status not 200 |
| 316 |
((403, None, None), (False, None)), |
| 317 |
# Test case: AsusRouterRequestFormatError is raised |
| 318 |
(AsusRouterRequestFormatError(), (True, None)), |
| 319 |
# Test case: AsusRouter404Error is raised |
| 320 |
(AsusRouter404Error(), (False, None)), |
| 321 |
], |
| 322 |
) |
| 323 |
async def test_check_available( |
| 324 |
api_query_return: tuple[int, str | None, str | None], |
| 325 |
expected_result: tuple[bool, str | None], |
| 326 |
) -> None: |
| 327 |
"""Test check_available function.""" |
| 328 |
|
| 329 |
# Mock the api_query function |
| 330 |
api_query = AsyncMock() |
| 331 |
if isinstance(api_query_return, Exception): |
| 332 |
api_query.side_effect = api_query_return |
| 333 |
else: |
| 334 |
api_query.return_value = api_query_return |
| 335 |
|
| 336 |
# Call the function |
| 337 |
result = await check_available(Endpoint.DEVICEMAP, api_query) |
| 338 |
|
| 339 |
# Check the result |
| 340 |
assert result == expected_result |