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
|
"""Tests for the openvpn module."""
from unittest.mock import AsyncMock
import pytest
from asusrouter.modules.firmware import Firmware
from asusrouter.modules.identity import AsusDevice
from asusrouter.modules.openvpn import (
AsusOVPNClient,
AsusOVPNServer,
set_state,
)
FW_MAJOR = "3.0.0.4"
FW_MINOR_OLD = 386
FW_MINOR_NEW = 388
identity_mock = {
"merlin_new": AsusDevice(
merlin=True,
firmware=Firmware(major=FW_MAJOR, minor=FW_MINOR_NEW, build=0),
),
"merlin_old": AsusDevice(
merlin=True,
firmware=Firmware(major=FW_MAJOR, minor=FW_MINOR_OLD, build=0),
),
"stock_new": AsusDevice(
merlin=False,
firmware=Firmware(major=FW_MAJOR, minor=FW_MINOR_NEW, build=0),
),
"stock_old": AsusDevice(
merlin=False,
firmware=Firmware(major=FW_MAJOR, minor=FW_MINOR_OLD, build=0),
),
}
@pytest.mark.asyncio
@pytest.mark.parametrize(
(
"state",
"vpn_id",
"identity",
"expect_modify",
"expect_call",
"expected_args",
"expected_service",
),
[
# Correct states
(
AsusOVPNClient.ON,
1,
"merlin_new",
True,
True,
{"id": 1},
"start_vpnclient1",
),
(
AsusOVPNClient.OFF,
1,
"merlin_new",
False,
True,
{"id": 1},
"stop_vpnclient1",
),
(
AsusOVPNServer.ON,
1,
"merlin_new",
True,
True,
{"id": 1},
"start_vpnserver1",
),
(
AsusOVPNServer.OFF,
1,
"merlin_new",
False,
True,
{"id": 1},
"stop_vpnserver1",
),
# Different identity - merlin old
(
AsusOVPNClient.ON,
1,
"merlin_old",
True,
True,
{"id": 1},
"start_vpnclient1",
),
(
AsusOVPNClient.OFF,
1,
"merlin_old",
False,
True,
{"id": 1},
"stop_vpnclient1",
),
# Different identity - stock old
(
AsusOVPNClient.ON,
1,
"stock_old",
True,
True,
{"id": 1},
"start_vpnclient1",
),
(
AsusOVPNClient.OFF,
1,
"stock_old",
False,
True,
{"id": 1},
"stop_vpnclient1",
),
# Get to modern API - stock new
(
AsusOVPNServer.ON,
1,
"stock_new",
True,
True,
{"id": 1, "VPNServer_enable": "1"},
"restart_openvpnd;restart_chpass;restart_samba;restart_dnsmasq;",
),
(
AsusOVPNServer.OFF,
1,
"stock_new",
False,
True,
{"id": 1, "VPNServer_enable": "0"},
"stop_openvpnd;restart_samba;restart_dnsmasq;",
),
# Modern API with unknown state - this should not happen but is handled
(
AsusOVPNClient.ON,
1,
"stock_new",
True,
False,
{},
None,
),
# Wrong states
(AsusOVPNClient.UNKNOWN, 1, "merlin_new", False, False, {}, None),
(None, 1, "merlin_new", False, False, {}, None),
# No ID
(AsusOVPNClient.ON, None, "merlin_new", True, False, {}, None),
# No identity - should use legacy service
(
AsusOVPNClient.ON,
1,
None,
True,
True,
{"id": 1},
"start_vpnclient1",
),
],
)
async def test_set_state( # noqa: PLR0913
state: AsusOVPNClient | AsusOVPNServer | None,
vpn_id: int | None,
identity: str | None,
expect_modify: bool,
expect_call: bool,
expected_args: dict[str, int],
expected_service: str | None,
) -> None:
"""Test set_state."""
# Create a mock callback function
callback = AsyncMock()
# Compile the kwargs
kwargs = {
"id": vpn_id,
"identity": identity_mock[identity] if identity else None,
}
# Call the set_state function
await set_state(
callback=callback, state=state, expect_modify=expect_modify, **kwargs
)
# Check if the callback function was called
if expect_call:
callback.assert_called_once_with(
service=expected_service,
arguments=expected_args,
apply=True,
expect_modify=expect_modify,
)
else:
callback.assert_not_called()
|