File: test_wireguard.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 (112 lines) | stat: -rw-r--r-- 2,709 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
"""Tests for the wireguard module."""

from typing import Any
from unittest.mock import AsyncMock

import pytest

from asusrouter.modules.wireguard import (
    AsusWireGuardClient,
    AsusWireGuardServer,
    _get_arguments,
    set_state,
)


@pytest.mark.parametrize(
    ("kwargs", "expected"),
    [
        # Correct arguments
        ({"arguments": {"id": 1}}, 1),
        ({"id": 2}, 2),
        ({"arguments": {"id": 3}, "id": 4}, 3),
        # Missing arguments
        ({}, 1),
    ],
)
def test_get_arguments(kwargs: Any, expected: int | None) -> None:
    """Test get_arguments."""

    assert _get_arguments(**kwargs) == expected


@pytest.mark.asyncio
@pytest.mark.parametrize(
    (
        "state",
        "arguments",
        "expect_modify",
        "expected_service",
        "expected_args",
        "expected_result",
    ),
    [
        # Correct states
        (
            AsusWireGuardClient.ON,
            {"id": 1},
            False,
            "start_wgc 1",
            {"id": 1, "wgc_enable": 1, "wgc_unit": 1},
            True,
        ),
        (
            AsusWireGuardClient.OFF,
            {"id": 2},
            True,
            "stop_wgc 2",
            {"id": 2, "wgc_enable": 0, "wgc_unit": 2},
            True,
        ),
        (
            AsusWireGuardServer.ON,
            {"id": 3},
            False,
            "restart_wgs;restart_dnsmasq",
            {"id": 3, "wgs_enable": 1, "wgs_unit": 3},
            True,
        ),
        (
            AsusWireGuardServer.OFF,
            {"id": 4},
            True,
            "restart_wgs;restart_dnsmasq",
            {"id": 4, "wgs_enable": 0, "wgs_unit": 4},
            True,
        ),
        # Wrong states
        (None, {"id": 5}, False, None, None, False),
        (None, None, False, None, None, False),
    ],
)
async def test_set_state(  # noqa: PLR0913
    state: AsusWireGuardClient | AsusWireGuardServer,
    arguments: dict[str, int | None] | None,
    expect_modify: bool,
    expected_service: str | None,
    expected_args: dict[str, int | None] | None,
    expected_result: bool,
) -> None:
    """Test set_state."""

    # Prepare the callback
    callback = AsyncMock(return_value=True)

    # Get the result
    result = await set_state(
        callback, state, arguments=arguments, expect_modify=expect_modify
    )

    # Check the result
    assert result is expected_result

    # Check the callback
    if expected_result:
        callback.assert_called_once_with(
            service=expected_service,
            arguments=expected_args,
            apply=True,
            expect_modify=expect_modify,
        )
    else:
        callback.assert_not_called()