File: test_ddns.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 (218 lines) | stat: -rw-r--r-- 6,111 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
"""Tests for the DDNS module."""

from __future__ import annotations

from typing import Any
from unittest.mock import patch

import pytest

from asusrouter.modules.ddns import (
    AsusDDNS,
    DDNSStatusCode,
    process_ddns,
    read_ddns_status_code,
)

ALL_KEYS = [
    "enabled",
    "hostname",
    "ip_address",
    "old_name",
    "replace_status",
    "return_code",
    "server",
    "state",
    "status",
    "status_hint",
    "updated",
]


@pytest.mark.parametrize(
    ("raw", "expected"),
    [
        (None, DDNSStatusCode.NONE),
        ("", DDNSStatusCode.NONE),
        ("Time-out", DDNSStatusCode.TIMEOUT),
        ("unknown_error", DDNSStatusCode.UNKNOWN_ERROR),
        ("connect_fail", DDNSStatusCode.CONNECT_FAIL),
        ("no_change", DDNSStatusCode.NO_CHANGE),
        ("ddns_query", DDNSStatusCode.QUERY),
        ("auth_fail", DDNSStatusCode.AUTH_FAIL),
        ("-1", DDNSStatusCode.ERROR),
        ("200", DDNSStatusCode.SUCCESS),
        ("203", DDNSStatusCode.DOMAIN_TAKEN),
        ("220", DDNSStatusCode.REGISTERED_ORIGINAL),
        ("230", DDNSStatusCode.REGISTERED_NEW),
        ("233", DDNSStatusCode.NEW_DOMAIN_TAKEN),
        ("296", DDNSStatusCode.NOT_REGISTERED),
        ("297", DDNSStatusCode.CANNOT_START_WITH_NUMBER),
        ("298", DDNSStatusCode.INVALID_DOMAIN),
        ("299", DDNSStatusCode.INVALID_IP),
        ("390", DDNSStatusCode.SERVER_ERROR),
        ("401", DDNSStatusCode.UNAUTHORIZED),
        ("402", DDNSStatusCode.FIRMWARE_UPDATE_REQUIRED),
        ("407", DDNSStatusCode.PROXY_AUTH_REQUIRED),
        ("some_unknown_code", DDNSStatusCode.OTHER),
        ("foobar", DDNSStatusCode.OTHER),
    ],
)
def test_read_ddns_status_code(
    raw: str | None,
    expected: DDNSStatusCode,
) -> None:
    """Test the read_ddns_status_code function."""

    assert read_ddns_status_code(raw) == expected


@pytest.mark.parametrize(
    ("input_data", "expected"),
    [
        # Disabled DDNS, should be INACTIVE regardless of status
        (
            {
                "ddns_enable_x": "0",
                "ddns_return_code_chk": "200",
            },
            {
                "enabled": False,
                "state": AsusDDNS.INACTIVE,
                "status": DDNSStatusCode.SUCCESS,
            },
        ),
        # Enabled, success status
        (
            {
                "ddns_enable_x": "1",
                "ddns_return_code_chk": "200",
            },
            {
                "enabled": True,
                "state": AsusDDNS.ACTIVE,
                "status": DDNSStatusCode.SUCCESS,
            },
        ),
        # Enabled, error status
        (
            {
                "ddns_enable_x": "1",
                "ddns_return_code_chk": "-1",
            },
            {
                "enabled": True,
                "state": AsusDDNS.INACTIVE,
                "status": DDNSStatusCode.ERROR,
            },
        ),
        # Enabled, unknown status
        (
            {
                "ddns_enable_x": "1",
                "ddns_return_code_chk": "foobar",
            },
            {
                "enabled": True,
                "state": AsusDDNS.INACTIVE,
                "status": DDNSStatusCode.OTHER,
            },
        ),
        # Disabled, unknown status
        (
            {
                "ddns_enable_x": "0",
                "ddns_return_code_chk": "foobar",
            },
            {
                "enabled": False,
                "state": AsusDDNS.INACTIVE,
                "status": DDNSStatusCode.OTHER,
            },
        ),
    ],
)
def test_process_ddns(
    input_data: dict[str, str],
    expected: dict[str, Any],
) -> None:
    """Test the process_ddns function."""

    result = process_ddns(input_data)
    assert result["enabled"] == expected["enabled"]
    assert result["state"] == expected["state"]
    assert result["status"] == expected["status"]


@pytest.mark.parametrize(
    ("input_data", "expected"),
    [
        (
            {
                "ddns_enable_x": "1",
                "ddns_hostname_x": "host",
                "ddns_ipaddr": "ip",
                "ddns_old_name": "old",
                "ddns_replace_status": 1,
                "ddns_return_code": "rc",
                "ddns_server_x": "server",
                "ddns_updated": "1",
                "ddns_return_code_chk": "200",
            },
            {
                "enabled": "1",
                "hostname": "host",
                "ip_address": "ip",
                "old_name": "old",
                "replace_status": 1,
                "return_code": "rc",
                "server": "server",
                "updated": "1",
            },
        ),
        (
            # Only one field present
            {"ddns_enable_x": "0"},
            {
                "enabled": "0",
                "hostname": None,
                "ip_address": None,
                "old_name": None,
                "replace_status": None,
                "return_code": None,
                "server": None,
                "updated": None,
            },
        ),
        (
            # No fields present
            {},
            {
                "enabled": None,
                "hostname": None,
                "ip_address": None,
                "old_name": None,
                "replace_status": None,
                "return_code": None,
                "server": None,
                "updated": None,
            },
        ),
    ],
)
def test_process_ddns_fields(
    input_data: dict[str, str],
    expected: dict[str, str | None],
) -> None:
    """Test that process_ddns copies or defaults all fields as expected."""

    with (
        patch("asusrouter.modules.ddns.safe_bool", side_effect=lambda x: x),
        patch("asusrouter.modules.ddns.clean_string", side_effect=lambda x: x),
    ):
        result = process_ddns(input_data)
        for key, value in expected.items():
            assert result[key] == value
        # Also check that all expected keys are present
        for key in ALL_KEYS:
            assert key in result