File: test_connection_01_init.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 (400 lines) | stat: -rw-r--r-- 11,574 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
"""Tests for the connection module / Part 1 / Init & Config."""

import asyncio
from typing import Any
from unittest.mock import AsyncMock, Mock, patch

import pytest

from asusrouter.connection import Connection, generate_credentials
from asusrouter.connection_config import (
    CONNECTION_CONFIG_DEFAULT,
    ARConnectionConfigKey as ARCCKey,
)
from asusrouter.const import (
    DEFAULT_PORT_HTTP,
    DEFAULT_PORT_HTTPS,
    DEFAULT_TIMEOUT,
)
from tests.helpers import (
    TCONST_HOST,
    TCONST_PASS,
    TCONST_USER,
    AsyncPatch,
    ConnectionFactory,
)


@pytest.mark.asyncio
async def test_generate_credentials() -> None:
    """Test the `generate_credentials`."""

    username = TCONST_USER
    password = TCONST_PASS
    payload, headers = generate_credentials(username, password)
    assert payload == "login_authorization=dXNlcjpwYXNz"
    assert headers == {"user-agent": "asusrouter--DUTUtil-"}


class TestConnectionInit:
    """Tests for the Connection class init and config."""

    def _assert_connection(
        self,
        conn: Connection,
        hostname: str,
        username: str,
        password: str,
        port: int | None,
        use_ssl: bool,
        session: Mock | None,
        timeout: int,
        dumpback: Mock | None,
        config: dict[ARCCKey, Any] | None,
        mock_new_session: Mock,
    ) -> None:
        """Test the Connection object."""

        assert conn._hostname == hostname
        assert conn._username == username
        assert conn._password == password
        assert conn.port == port or (
            DEFAULT_PORT_HTTPS if use_ssl else DEFAULT_PORT_HTTP
        )
        if not config:
            config = {}
        for key, value in config.items():
            assert conn.config.get(key) == value
        assert conn.config.get(ARCCKey.USE_SSL) == use_ssl
        assert conn._session == (session or mock_new_session.return_value)
        assert conn._timeout == timeout
        assert conn._dumpback == dumpback
        assert conn._token is None
        assert conn._header is None
        assert conn._connected is False
        assert isinstance(conn._connection_lock, asyncio.Lock)

    @pytest.mark.parametrize(
        (
            "hostname",
            "username",
            "password",
            "port",
            "use_ssl",
            "session",
            "timeout",
            "dumpback",
            "config",
        ),
        [
            (
                TCONST_HOST,
                TCONST_USER,
                TCONST_PASS,
                None,
                False,
                None,
                None,
                None,
                CONNECTION_CONFIG_DEFAULT,
            ),
            (
                TCONST_HOST,
                TCONST_USER,
                TCONST_PASS,
                8080,
                True,
                None,
                None,
                None,
                CONNECTION_CONFIG_DEFAULT,
            ),
            (
                TCONST_HOST,
                TCONST_USER,
                TCONST_PASS,
                8080,
                False,
                Mock(),
                None,
                None,
                CONNECTION_CONFIG_DEFAULT,
            ),
            (
                TCONST_HOST,
                TCONST_USER,
                TCONST_PASS,
                8080,
                True,
                Mock(),
                10,
                Mock(),
                CONNECTION_CONFIG_DEFAULT,
            ),
            (
                "",
                TCONST_USER,
                TCONST_PASS,
                8080,
                True,
                Mock(),
                None,
                Mock(),
                CONNECTION_CONFIG_DEFAULT,
            ),
            (
                TCONST_HOST,
                "",
                TCONST_PASS,
                8080,
                True,
                None,
                10,
                None,
                CONNECTION_CONFIG_DEFAULT,
            ),
            (
                TCONST_HOST,
                TCONST_USER,
                TCONST_PASS,
                -1,
                True,
                None,
                15,
                None,
                CONNECTION_CONFIG_DEFAULT,
            ),
        ],
        ids=[
            "default",
            "custom_port_ssl",
            "custom_session",
            "custom_timeout_dumpback",
            "empty_hostname",
            "empty_username",
            "negative_port",
        ],
    )
    @patch.object(Connection, "_new_session", return_value=Mock())
    def test_init(
        self,
        mock_new_session: Mock,
        hostname: str,
        username: str,
        password: str,
        port: int,
        use_ssl: bool,
        session: Mock | None,
        timeout: int | None,
        dumpback: Mock | None,
        config: dict[ARCCKey, Any] | None,
    ) -> None:
        """Test the initialization of the connection object."""

        # Replace port and use_ssl configs with the ones provided
        if not config:
            config = {}

        config[ARCCKey.PORT] = port or (
            DEFAULT_PORT_HTTPS if use_ssl else DEFAULT_PORT_HTTP
        )
        config[ARCCKey.USE_SSL] = use_ssl

        # Create a Connection
        conn = Connection(
            hostname=hostname,
            username=username,
            password=password,
            port=port,
            use_ssl=use_ssl,
            session=session,
            timeout=timeout,
            dumpback=dumpback,
            config=config,
        )

        # Test the Connection
        self._assert_connection(
            conn,
            hostname=hostname,
            username=username,
            password=password,
            port=port,
            use_ssl=use_ssl,
            session=session,
            timeout=timeout or DEFAULT_TIMEOUT,
            dumpback=dumpback,
            config=config,
            mock_new_session=mock_new_session,
        )

    @patch.object(Connection, "_new_session", return_value=Mock())
    def test_init_multiple_instances(self, mock_new_session: Mock) -> None:
        """Test the initialization of multiple connection objects."""

        # Explicitly set different initial configs
        config1 = {
            ARCCKey.ALLOW_FALLBACK: True,
        }
        config2 = {
            ARCCKey.ALLOW_FALLBACK: False,
        }

        # Create two Connection
        conn1 = Connection(
            hostname="localhost1",
            username="user1",
            password="pass1",
            port=8080,
            use_ssl=True,
            timeout=None,
            config=config1,
        )
        conn2 = Connection(
            hostname="localhost2",
            username="user2",
            password="pass2",
            port=9090,
            use_ssl=False,
            timeout=1,
            config=config2,
        )

        # Test the Connections
        self._assert_connection(
            conn1,
            hostname="localhost1",
            username="user1",
            password="pass1",
            port=8080,
            use_ssl=True,
            session=None,
            timeout=DEFAULT_TIMEOUT,
            dumpback=None,
            config=config1,
            mock_new_session=mock_new_session,
        )
        self._assert_connection(
            conn2,
            hostname="localhost2",
            username="user2",
            password="pass2",
            port=9090,
            use_ssl=False,
            session=None,
            timeout=1,
            dumpback=None,
            config=config2,
            mock_new_session=mock_new_session,
        )

        # Ensure that instances do not interfere with each other
        assert conn1._hostname != conn2._hostname
        assert conn1._username != conn2._username
        assert conn1._password != conn2._password
        assert conn1.port != conn2.port
        assert conn1.config.get(ARCCKey.USE_SSL) != conn2.config.get(
            ARCCKey.USE_SSL
        )
        assert conn1.config.get(ARCCKey.ALLOW_FALLBACK) != conn2.config.get(
            ARCCKey.ALLOW_FALLBACK
        )
        assert conn1._timeout != conn2._timeout

    @pytest.mark.asyncio
    async def test_aenter(
        self,
        connection_factory: ConnectionFactory,
        async_connect: AsyncPatch,
    ) -> None:
        """Test the `__aenter__` method."""

        mock_async_connect = async_connect(Connection)

        # Create a Connection
        connection = connection_factory(port=8080, use_ssl=True)

        # Enter the context manager
        async with connection as conn:
            # Verify that async_connect was called
            mock_async_connect.assert_called_once()

            # Verify that the connection object is returned
            assert conn is connection

    @pytest.mark.asyncio
    async def test_aexit(
        self, connection_factory: ConnectionFactory, async_connect: AsyncPatch
    ) -> None:
        """Test the `__aexit__` method."""

        # Mock the async_connect and async_close methods
        mock_async_connect = async_connect(Connection)
        with patch.object(
            Connection, "async_close", new_callable=AsyncMock
        ) as mock_close:
            # Create a Connection
            connection = connection_factory(port=8080, use_ssl=True)

            # Exit the context manager
            async with connection:
                pass

            # Verify that async_connect was called
            mock_async_connect.assert_called_once()

            # Verify that async_close was called
            mock_close.assert_called_once()

    @pytest.mark.asyncio
    async def test_create(self, async_connect: AsyncPatch) -> None:
        """Test the `create` class method."""

        # Mock the async_connect method
        mock_async_connect = async_connect(Connection)

        # Define test parameters
        hostname = TCONST_HOST
        username = TCONST_USER
        password = TCONST_PASS
        port = 8080
        use_ssl = True
        session = Mock()
        timeout = 15
        dumpback = Mock()
        allow_fallback = True
        config = {ARCCKey.ALLOW_FALLBACK: allow_fallback}

        # Call the create method
        connection = await Connection.create(
            hostname=hostname,
            username=username,
            password=password,
            port=port,
            use_ssl=use_ssl,
            session=session,
            timeout=timeout,
            dumpback=dumpback,
            config=config,
        )

        # Verify that async_connect was called
        mock_async_connect.assert_called_once()

        # Verify that the returned object is an instance of Connection
        assert isinstance(connection, Connection)

        # Verify that the connection object is initialized correctly
        assert connection._hostname == hostname
        assert connection._username == username
        assert connection._password == password
        assert connection.port == port
        assert connection.config.get(ARCCKey.USE_SSL) == use_ssl
        assert connection._session == session
        assert connection._timeout == timeout
        assert connection._dumpback == dumpback
        assert connection.config.get(ARCCKey.ALLOW_FALLBACK) == allow_fallback
        assert connection._token is None
        assert connection._header is None
        assert connection._connected is False
        assert isinstance(connection._connection_lock, asyncio.Lock)