File: test_connection_04_fallback.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 (389 lines) | stat: -rw-r--r-- 12,873 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
"""Tests for the connection module / Part 4 / Fallback."""

import asyncio
import logging
from typing import Any

import pytest

from asusrouter.connection import ConnectionFallback
from asusrouter.connection_config import ARConnectionConfigKey as ARCCKey
from asusrouter.const import DEFAULT_PORT_HTTP, DEFAULT_PORT_HTTPS
from asusrouter.error import (
    AsusRouterFallbackError,
    AsusRouterFallbackForbiddenError,
    AsusRouterFallbackLoopError,
    AsusRouterNotImplementedError,
)
from asusrouter.modules.endpoint import EndpointService
from tests.helpers import AsyncPatch, ConnectionFactory, SyncPatch

CUSTOM_HTTP = DEFAULT_PORT_HTTP + 5
CUSTOM_HTTPS = DEFAULT_PORT_HTTPS + 5


CASES_ASYNC_HANDLE_FALLBACK = [
    # HTTPS @ Custom port -> HTTPS @ Default
    {
        "use_ssl": True,
        "port": CUSTOM_HTTPS,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {},
        "expected_fallback": ConnectionFallback.HTTPS,
        "expect_exception": None,
    },
    # HTTPS @ Custom port, fallback already used -> Loop error
    {
        "use_ssl": True,
        "port": CUSTOM_HTTPS,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {ConnectionFallback.HTTPS: True},
        "expected_fallback": None,
        "expect_exception": AsusRouterFallbackLoopError,
    },
    # HTTPS @ Default -> HTTP @ Default (STRICT_SSL not set)
    {
        "use_ssl": True,
        "port": DEFAULT_PORT_HTTPS,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {},
        "expected_fallback": ConnectionFallback.HTTP,
        "expect_exception": None,
    },
    # HTTPS @ Default, fallback already used -> Loop error
    {
        "use_ssl": True,
        "port": DEFAULT_PORT_HTTPS,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {ConnectionFallback.HTTP: True},
        "expected_fallback": None,
        "expect_exception": AsusRouterFallbackLoopError,
    },
    # HTTPS @ Default -> HTTP @ Default (STRICT_SSL set)
    {
        "use_ssl": True,
        "port": DEFAULT_PORT_HTTPS,
        "strict_ssl": True,
        "allow_upgrade": False,
        "used_fallbacks": {},
        "expected_fallback": None,
        "expect_exception": AsusRouterFallbackForbiddenError,
    },
    # HTTP @ Custom port -> HTTP @ Default
    {
        "use_ssl": False,
        "port": CUSTOM_HTTP,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {},
        "expected_fallback": ConnectionFallback.HTTP,
        "expect_exception": None,
    },
    # HTTP @ Custom port, fallback already used -> Loop error
    {
        "use_ssl": False,
        "port": CUSTOM_HTTP,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {ConnectionFallback.HTTP: True},
        "expected_fallback": None,
        "expect_exception": AsusRouterFallbackLoopError,
    },
    # HTTP @ Default -> HTTPS @ Default (upgrade allowed)
    {
        "use_ssl": False,
        "port": DEFAULT_PORT_HTTP,
        "strict_ssl": False,
        "allow_upgrade": True,
        "used_fallbacks": {},
        "expected_fallback": ConnectionFallback.HTTPS,
        "expect_exception": None,
    },
    # HTTP @ Default, upgrade fallback already used -> Loop error
    {
        "use_ssl": False,
        "port": DEFAULT_PORT_HTTP,
        "strict_ssl": False,
        "allow_upgrade": True,
        "used_fallbacks": {ConnectionFallback.HTTPS: True},
        "expected_fallback": None,
        "expect_exception": AsusRouterFallbackLoopError,
    },
    # HTTP @ Default -> fallback not possible
    {
        "use_ssl": False,
        "port": DEFAULT_PORT_HTTP,
        "strict_ssl": False,
        "allow_upgrade": False,
        "used_fallbacks": {},
        "expected_fallback": None,
        "expect_exception": AsusRouterFallbackError,
    },
]

CASES_FALLBACK = [
    # No fallback provided
    {
        "fallback_type": None,
        "expected_ssl": False,
        "expected_port": DEFAULT_PORT_HTTP,
        "expected_verify_ssl": None,  # Should not change
        "expect_exception": None,
    },
    # HTTP fallback
    {
        "fallback_type": ConnectionFallback.HTTP,
        "expected_ssl": False,
        "expected_port": DEFAULT_PORT_HTTP,
        "expected_verify_ssl": None,  # Should not change
        "expect_exception": None,
    },
    # HTTPS fallback
    {
        "fallback_type": ConnectionFallback.HTTPS,
        "expected_ssl": True,
        "expected_port": DEFAULT_PORT_HTTPS,
        "expected_verify_ssl": None,  # Should not change
        "expect_exception": None,
    },
    # HTTPS_UNSAFE fallback
    {
        "fallback_type": ConnectionFallback.HTTPS_UNSAFE,
        "expected_ssl": True,
        "expected_port": DEFAULT_PORT_HTTPS,
        "expected_verify_ssl": False,
        "expect_exception": None,
    },
    # Not implemented fallback
    {
        "fallback_type": "not_implemented",
        "expected_ssl": None,
        "expected_port": None,
        "expected_verify_ssl": None,
        "expect_exception": AsusRouterNotImplementedError,
    },
]


class TestConnectionFallback:
    """Tests for the Connection class fallbacks."""

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        "case",
        CASES_ASYNC_HANDLE_FALLBACK,
        ids=[
            "https_custom_to_default",
            "https_custom_loop",
            "https_default_to_http",
            "https_default_loop",
            "https_default_strict_ssl",
            "http_custom_to_default",
            "http_custom_loop",
            "http_default_upgrade",
            "http_default_upgrade_loop",
            "http_default_no_fallback",
        ],
    )
    async def test_async_handle_fallback_matrix(
        self,
        case: dict[str, Any],
        connection_factory: ConnectionFactory,
        fallback: AsyncPatch,
        send_request: AsyncPatch,
    ) -> None:
        """Test _async_handle_fallback method."""

        # Create a connection
        connection = connection_factory()
        # Prepare and set configs
        config = {
            ARCCKey.PORT: case["port"],
            ARCCKey.USE_SSL: case["use_ssl"],
            ARCCKey.STRICT_SSL: case["strict_ssl"],
            ARCCKey.ALLOW_UPGRADE_HTTP_TO_HTTPS: case["allow_upgrade"],
        }
        for key, value in config.items():
            connection.config.set(key, value)
        # Set used fallbacks
        connection._used_fallbacks = case["used_fallbacks"].copy()

        # Fallback failed (exception expected)
        if case["expect_exception"] is not None:
            with pytest.raises(case["expect_exception"]):
                await connection._async_handle_fallback(
                    connection._send_request, endpoint=EndpointService.LOGIN
                )
        # Fallback successful
        else:
            mock_send_request = send_request(connection)
            mock_fallback = fallback(connection)

            await connection._async_handle_fallback(
                mock_send_request,
                endpoint=EndpointService.LOGIN,
            )
            mock_fallback.assert_awaited_once_with(
                fallback_type=case["expected_fallback"]
            )
            mock_send_request.assert_awaited_once_with(
                endpoint=EndpointService.LOGIN
            )

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        "case",
        CASES_FALLBACK,
        ids=[
            "no_fallback",
            "http",
            "https",
            "https_unsafe",
            "not_implemented",
        ],
    )
    async def test_fallback(
        self,
        case: dict[str, Any],
        connection_factory: ConnectionFactory,
        reset_connection: SyncPatch,
        async_connect: AsyncPatch,
    ) -> None:
        """Test _fallback method."""

        connection = connection_factory()
        # Set initial config values
        connection.config.set(
            ARCCKey.USE_SSL, not case.get("expected_ssl", False)
        )
        connection.config.set(ARCCKey.PORT, 9999)
        connection.config.set(ARCCKey.VERIFY_SSL, True)

        mock_reset = reset_connection(connection)
        mock_connect = async_connect(connection)

        if case["expect_exception"]:
            with pytest.raises(case["expect_exception"]):
                await connection._fallback(case["fallback_type"])
        else:
            await connection._fallback(case["fallback_type"])
            # Check that we handle empty fallback types with a default value
            fallback_type = case["fallback_type"] or ConnectionFallback.HTTP
            # Fallback type should be marked as used
            assert connection._used_fallbacks.get(fallback_type) is True
            # Config values should be updated
            assert (
                connection.config.get(ARCCKey.USE_SSL) == case["expected_ssl"]
            )
            assert connection.config.get(ARCCKey.PORT) == case["expected_port"]
            # VERIFY_SSL only for HTTPS_UNSAFE
            if case["expected_verify_ssl"] is not None:
                assert (
                    connection.config.get(ARCCKey.VERIFY_SSL)
                    == case["expected_verify_ssl"]
                )
            # Should call reset_connection and async_connect
            mock_reset.assert_called_once()
            mock_connect.assert_awaited_once()

    @pytest.mark.asyncio
    async def test_fallback_cancels_inflight_connect(
        self,
        connection_factory: ConnectionFactory,
        reset_connection: SyncPatch,
        async_connect: AsyncPatch,
    ) -> None:
        """When a connect task is in-flight.

        _fallback should cancel and await it.
        """

        connection = connection_factory()

        started = asyncio.Event()
        cancelled = asyncio.Event()

        async def long_running() -> None:
            # signal we've started and then sleep until cancelled
            started.set()
            try:
                await asyncio.sleep(60)
            except asyncio.CancelledError:
                cancelled.set()
                raise

        # attach a long-running in-flight connect task
        connection._connect_task = asyncio.create_task(long_running())
        await started.wait()

        mock_reset = reset_connection(connection)
        async_connect(connection)

        # run fallback which should cancel & await the in-flight task,
        # then reset & reconnect
        await connection._fallback(ConnectionFallback.HTTP)

        # confirm the old task was cancelled and its cancellation was observed
        assert cancelled.is_set()
        # ensure reset_connection and async_connect were invoked
        # as part of fallback
        mock_reset.assert_called_once()

    @pytest.mark.asyncio
    async def test_fallback_consumes_exception_from_cancelled_inflight_task(
        self,
        connection_factory: ConnectionFactory,
        async_connect: AsyncPatch,
        reset_connection: SyncPatch,
        caplog: pytest.LogCaptureFixture,
    ) -> None:
        """If an in-flight connect is cancelled and raises.

        _fallback should consume/log the exception.
        """
        connection = connection_factory()

        # Create a task that raises a RuntimeError when cancelled
        # (to hit the logging branch)
        async def old_task_coro() -> None:
            try:
                await asyncio.sleep(60)
            except asyncio.CancelledError as exc:
                # translate cancel into other exception
                # to exercise the logging branch
                raise RuntimeError("boom") from exc

        connection._connect_task = asyncio.create_task(old_task_coro())
        # ensure the task has started
        await asyncio.sleep(0)

        mock_reset = reset_connection(connection)
        mock_connect = async_connect(connection)

        caplog.set_level(logging.DEBUG, logger="asusrouter.connection")

        # Run fallback; it should cancel the old task, await it,
        # consume the RuntimeError and log it
        await connection._fallback(ConnectionFallback.HTTP)

        # reset_connection and async_connect must have been called
        mock_reset.assert_called_once()
        mock_connect.assert_awaited_once()

        # Ensure we logged the message that the cancelled task finished
        # with an exception
        found = any(
            record.levelno == logging.DEBUG
            and record.getMessage().startswith(
                "In-flight connect task finished after cancel with:"
            )
            for record in caplog.records
        )
        assert found, (
            "Expected debug log about in-flight task"
            " finishing after cancel with exception"
        )