File: test_devicemap.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (434 lines) | stat: -rw-r--r-- 13,448 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""Test AsusRouter devicemap endpoint module."""

from collections.abc import Generator
from datetime import datetime, timedelta, timezone
from typing import Any
from unittest.mock import ANY, MagicMock, patch

import pytest

from asusrouter.config import ARConfigKey as ARConfKey
from asusrouter.modules.data import AsusData, AsusDataState
from asusrouter.modules.endpoint import devicemap


def generate_xml_content(groups: dict[str, Any]) -> str:
    """Generate XML content based on input parameters."""

    content = "<devicemap>\n"
    for group, keys in groups.items():
        content += f"    <{group}>\n"
        for key, value in keys.items():
            content += f"        <{key}>{value}</{key}>\n"
        content += f"    </{group}>\n"
    content += "</devicemap>\n"
    return content


@pytest.mark.parametrize(
    "content",
    [
        "<devicemap></devicemap>",  # Empty devicemap
        "non-xml",  # Invalid XML
    ],
)
def test_read_invalid(content: str) -> None:
    """Test read function with empty devicemap."""

    assert devicemap.read(content) == {}


@pytest.fixture
def common_group() -> dict[str, dict[str, str]]:
    """Return the common test data intermediate."""

    return {
        "group1": {"key1": "value1"},
        "group2": {"key2": "value2"},
        "group3": {"key3": "value3_test"},
    }


@pytest.fixture
def common_test_data_result() -> dict[str, dict[str, str]]:
    """Return the common test data result."""

    return {
        "group1": {"key1": "value1"},
        "group2": {"key2": "value2"},
        "group3": {"key3": "value3"},
    }


@pytest.fixture
def mock_functions() -> Generator[dict[str, MagicMock | Any], None, None]:
    """Return the mock functions."""

    with (
        patch(
            "asusrouter.modules.endpoint.devicemap.read_index",
            return_value={
                "group1": {"key1": "value1"},
                "group3": {"key3": "value3_test"},
            },
        ) as mock_read_index,
        patch(
            "asusrouter.modules.endpoint.devicemap.read_key",
            return_value={"group2": {"key2": "value2"}},
        ) as mock_read_key,
        patch(
            "asusrouter.modules.endpoint.devicemap.merge_dicts",
            side_effect=lambda x, y: {**x, **y},
        ) as mock_merge_dicts,
        patch(
            "asusrouter.modules.endpoint.devicemap.clean_dict",
            side_effect=lambda x: x,
        ) as mock_clean_dict,
        patch(
            "asusrouter.modules.endpoint.devicemap.clean_dict_key_prefix",
            side_effect=lambda x, _: x,
        ) as mock_clean_dict_key_prefix,
        patch(
            "asusrouter.modules.endpoint.devicemap.DEVICEMAP_CLEAR",
            new={
                "group3": {"key3": "_test", "key4": "_test"},
                "group4": {"key5": "_test"},
            },
        ) as mock_devicemap_clear,
    ):
        yield {
            "read_index": mock_read_index,
            "read_key": mock_read_key,
            "merge_dicts": mock_merge_dicts,
            "clean_dict": mock_clean_dict,
            "clean_dict_key_prefix": mock_clean_dict_key_prefix,
            "devicemap_clear": mock_devicemap_clear,
        }


def test_read_with_data(
    mock_functions: dict[str, MagicMock | Any],  # pylint: disable=redefined-outer-name
    common_test_data_result: dict[str, dict[str, str]],  # pylint: disable=redefined-outer-name
    common_group: dict[str, dict[str, str]],  # pylint: disable=redefined-outer-name
) -> None:
    """Test read function."""

    # Test data
    content = generate_xml_content(common_group)
    expected_devicemap = common_test_data_result

    # Call the function
    result = devicemap.read(content)

    # Check the result
    assert result == expected_devicemap

    # Check the calls to the mocked functions
    mock_functions["read_index"].assert_called_with(common_group)
    mock_functions["read_key"].assert_called_with(common_group)
    assert mock_functions["merge_dicts"].call_count == 2  # noqa: PLR2004
    mock_functions["clean_dict"].assert_called_with(expected_devicemap)
    assert mock_functions["clean_dict_key_prefix"].call_count == 3  # noqa: PLR2004


@pytest.fixture
def const_devicemap() -> list[tuple[str, str, list[str]]]:
    """Return the const devicemap."""

    return [
        ("output_group1", "group1", ["input_value1"]),
        ("output_group2", "group2", ["input_value3", "input_value4"]),
        ("output_group3", "group3", ["input_value5"]),
        ("output_group4", "group4", ["input_value6"]),
        ("output_group5", "group5", ["input_value2"]),
        ("output_group6", "group6", ["input_value7"]),
    ]


@pytest.fixture
def const_devicemap_result() -> dict[str, dict[str, str]]:
    """Return the const devicemap result."""

    return {
        "output_group1": {"input_value1": "value1"},
        "output_group2": {"input_value3": "value3", "input_value4": "value4"},
        "output_group3": {"input_value5": "value5"},
        "output_group4": {"input_value6": "value6"},
        "output_group5": {},
        "output_group6": {},
    }


@pytest.fixture
def input_data() -> dict[str, list[str]]:
    """Return the input data for the tests."""

    return {
        "group1": ["value1"],
        "group2": ["value3", "value4"],
        "group3": ["value5"],
        "group4": ["value6"],
        # "group5": [],
        "group6": [],
    }


@pytest.fixture
def input_data_key() -> dict[str, Any]:
    """Return the input data for the read_key test."""

    return {
        "group1": ["input_value1=value1"],
        "group2": ["input_value3=value3", "input_value4=value4"],
        "group3": "input_value5=value5",
        "group4": "input_value6=value6",
        "group5": None,
        "group6": [],
    }


def test_read_index(
    const_devicemap: list[tuple[str, str, list[str]]],  # pylint: disable=redefined-outer-name
    const_devicemap_result: dict[str, dict[str, str]],  # pylint: disable=redefined-outer-name
    input_data: dict[str, list[str]],  # pylint: disable=redefined-outer-name
) -> None:
    """Test read_index function."""

    with patch.object(devicemap, "DEVICEMAP_BY_INDEX", new=const_devicemap):
        # Call the function
        result = devicemap.read_index(input_data)

        # Check the result
        assert result == const_devicemap_result


def test_read_key(
    const_devicemap: list[tuple[str, str, list[str]]],  # pylint: disable=redefined-outer-name
    const_devicemap_result: dict[str, dict[str, str]],  # pylint: disable=redefined-outer-name
    input_data_key: dict[str, Any],  # pylint: disable=redefined-outer-name
) -> None:
    """Test read_key function."""

    with patch.object(devicemap, "DEVICEMAP_BY_KEY", new=const_devicemap):
        # Call the function
        result = devicemap.read_key(input_data_key)

        # Check the result
        assert result == const_devicemap_result


def test_read_special(
    input_data: dict[str, list[str]],  # pylint: disable=redefined-outer-name
) -> None:
    """Test read_special function."""

    result = devicemap.read_special(input_data)
    assert result == {}  # pylint: disable=C1803


@pytest.mark.parametrize(
    ("content", "result"),
    [
        # Test with a valid content string
        (
            "Thu, 16 Nov 2023 07:17:45 +0100(219355 secs since boot)",
            (
                datetime(
                    2023,
                    11,
                    16,
                    7,
                    17,
                    45,
                    tzinfo=timezone(timedelta(hours=1)),
                )
                - timedelta(seconds=219355),
                219355,
            ),
        ),
        # Test with an invalid content string (no seconds)
        ("Thu, 16 Nov 2023 07:17:45 +0100(no secs since boot)", (None, None)),
        # Test with an invalid content string (bad format)
        ("bad format", (None, None)),
        # Test with a content string that has an invalid date
        ("Not a date (219355 secs since boot)", (None, 219355)),
        # Test with a content string that has an invalid number of seconds
        (
            "Thu, 16 Nov 2023 07:17:45 +0100(not a number secs since boot)",
            (None, None),
        ),
    ],
)
def test_read_uptime_string(
    content: str, result: tuple[datetime | None, int | None]
) -> None:
    """Test read_uptime_string function."""

    assert devicemap.read_uptime_string(content) == result


@pytest.mark.parametrize(
    ("raw_seconds", "diff_seconds", "result_seconds"),
    [
        (15, 0, 14),
        (15, 1, 14),
        (15, 2, 12),
        (16, 0, 16),
        (16, 1, 14),
        (16, 2, 14),
    ],
)
def test_read_uptime_string_robust(
    raw_seconds: int,
    diff_seconds: int,
    result_seconds: int,
) -> None:
    """Test read_uptime_string with robust boottime enabled."""

    # Mock the ARConfig to enable robust boottime
    with patch(
        "asusrouter.modules.endpoint.devicemap.ARConfig"
    ) as mock_config:
        mock_config.get.side_effect = (
            lambda key: key == ARConfKey.ROBUST_BOOTTIME
        )
        # Test with a valid content string
        content = f"Sat, 8 Aug 2025 08:08:{raw_seconds} "
        content += f"+0100({diff_seconds} secs since boot)"
        result = devicemap.read_uptime_string(content)
        assert result == (
            datetime(
                2025,
                8,
                8,
                8,
                8,
                result_seconds,
                tzinfo=timezone(timedelta(hours=1)),
            ),
            diff_seconds,
        )


@pytest.mark.parametrize(
    ("boottime_return", "expected_flags"),
    [
        (("boottime", False), {}),
        (("boottime", True), {"reboot": True}),
    ],
)
@patch("asusrouter.modules.endpoint.devicemap.process_boottime")
@patch("asusrouter.modules.endpoint.devicemap.process_ovpn")
def test_process(
    mock_process_ovpn: MagicMock,
    mock_process_boottime: MagicMock,
    boottime_return: tuple[str, bool],
    expected_flags: dict[str, bool],
) -> None:
    """Test process function."""

    # Prepare the mock functions
    mock_process_boottime.return_value = boottime_return
    mock_process_ovpn.return_value = "openvpn"

    # Prepare the test data
    data = {
        "history": {AsusData.BOOTTIME: AsusDataState(data="prev_boottime")}
    }

    # Call the function with the test data
    result = devicemap.process(data)

    # Check the result
    assert result == {
        AsusData.DEVICEMAP: data,
        AsusData.BOOTTIME: boottime_return[0],
        AsusData.OPENVPN: "openvpn",
        AsusData.FLAGS: expected_flags,
    }

    # Check that the mock functions were called with the correct arguments
    mock_process_boottime.assert_called_once_with(data, "prev_boottime")
    mock_process_ovpn.assert_called_once_with(data)


@pytest.mark.parametrize(
    ("prev_boottime_delta", "expected_result"),
    [
        (timedelta(seconds=1), ({"datetime": ANY, "uptime": 2}, False)),
        (timedelta(seconds=3), ({"datetime": ANY, "uptime": 2}, True)),
    ],
)
@patch("asusrouter.modules.endpoint.devicemap.read_uptime_string")
def test_process_boottime(
    mock_read_uptime_string: MagicMock,
    prev_boottime_delta: timedelta,
    expected_result: tuple[dict[str, Any], bool],
) -> None:
    """Test process_boottime function."""

    # Prepare the mock function
    mock_read_uptime_string.return_value = (datetime.now(), 2)

    # Prepare the test data
    devicemap_data = {"sys": {"uptimeStr": "uptime string"}}
    prev_boottime = {"datetime": datetime.now() - prev_boottime_delta}

    # Call the function with the test data
    result = devicemap.process_boottime(devicemap_data, prev_boottime)

    # Check the result
    assert result == expected_result

    # Check that the mock function was called with the correct argument
    mock_read_uptime_string.assert_called_once_with("uptime string")


@patch("asusrouter.modules.endpoint.devicemap.AsusOVPNClient")
@patch("asusrouter.modules.endpoint.devicemap.AsusOVPNServer")
@patch("asusrouter.modules.endpoint.devicemap.safe_int")
def test_process_ovpn(
    mock_safe_int: MagicMock,
    mock_asusovpnserver: MagicMock,
    mock_asusovnclient: MagicMock,
) -> None:
    """Test process_ovpn function."""

    # Prepare the mock functions
    mock_asusovnclient.return_value = MagicMock()
    mock_asusovpnserver.return_value = MagicMock()
    mock_safe_int.return_value = 0

    # Prepare the test data
    devicemap_data = {
        "vpn": {
            "client1_state": "state",
            "client1_errno": "errno",
            "server1_state": "state",
        }
    }

    # Call the function with the test data
    result = devicemap.process_ovpn(devicemap_data)

    # Check the result
    expected_result = {
        "client": {
            1: {
                "state": mock_asusovnclient.return_value,
                "errno": 0,
            }
        },
        "server": {
            1: {
                "state": mock_asusovpnserver.return_value,
            }
        },
    }
    assert result == expected_result

    # Check that the mock functions were called with the correct arguments
    mock_asusovnclient.assert_called_once_with(0)
    mock_asusovpnserver.assert_called_once_with(0)
    mock_safe_int.assert_any_call("state", default=0)
    mock_safe_int.assert_any_call("errno")