File: test_init.py

package info (click to toggle)
python-nextdns 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: python: 1,548; sh: 10; makefile: 3
file content (506 lines) | stat: -rw-r--r-- 15,659 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
"""Tests for nextdns package."""

import json
import re
from http import HTTPStatus
from pathlib import Path
from typing import Any
from unittest.mock import Mock, patch

import aiohttp
import pytest
from aioresponses import aioresponses
from syrupy import SnapshotAssertion
from tenacity import RetryError

from nextdns import (
    ATTR_ANALYTICS,
    ATTR_CLEAR_LOGS,
    ATTR_GET_LOGS,
    ATTR_LOGS,
    ATTR_PARENTAL_CONTROL_CATEGORIES,
    ATTR_PARENTAL_CONTROL_SERVICES,
    ATTR_PROFILE,
    ATTR_PROFILES,
    ATTR_TEST,
    ENDPOINTS,
    MAP_SETTING,
    ApiError,
    InvalidApiKeyError,
    NextDns,
    ProfileIdNotFoundError,
    ProfileNameNotFoundError,
    SettingNotSupportedError,
)
from nextdns.const import ATTR_BLOCK_PAGE

PROFILE_ID = "fakepr"


@pytest.mark.asyncio
async def test_valid_data(
    snapshot: SnapshotAssertion, profiles_data: dict[str, Any]
) -> None:
    """Test with valid data."""
    with Path.open("tests/fixtures/dnssec.json", encoding="utf-8") as file:
        dnssec_data = json.load(file)
    with Path.open("tests/fixtures/encryption.json", encoding="utf-8") as file:
        encryption_data = json.load(file)
    with Path.open("tests/fixtures/ip_versions.json", encoding="utf-8") as file:
        ip_versions_data = json.load(file)
    with Path.open("tests/fixtures/protocols.json", encoding="utf-8") as file:
        protocols_data = json.load(file)
    with Path.open("tests/fixtures/status.json", encoding="utf-8") as file:
        status_data = json.load(file)
    with Path.open("tests/fixtures/test.json", encoding="utf-8") as file:
        test_data = json.load(file)
    with Path.open("tests/fixtures/profile.json", encoding="utf-8") as file:
        profile_data = json.load(file)

    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.get(
            ENDPOINTS[ATTR_ANALYTICS].format(profile_id=PROFILE_ID, type="dnssec"),
            payload=dnssec_data,
        )
        session_mock.get(
            ENDPOINTS[ATTR_ANALYTICS].format(profile_id=PROFILE_ID, type="encryption"),
            payload=encryption_data,
        )
        session_mock.get(
            ENDPOINTS[ATTR_ANALYTICS].format(profile_id=PROFILE_ID, type="ipVersions"),
            payload=ip_versions_data,
        )
        session_mock.get(
            ENDPOINTS[ATTR_ANALYTICS].format(profile_id=PROFILE_ID, type="protocols"),
            payload=protocols_data,
        )
        session_mock.get(
            ENDPOINTS[ATTR_ANALYTICS].format(profile_id=PROFILE_ID, type="status"),
            payload=status_data,
        )
        session_mock.get(
            ENDPOINTS[ATTR_TEST].format(profile_id=PROFILE_ID), payload=test_data
        )
        session_mock.get(
            ENDPOINTS[ATTR_PROFILE].format(profile_id=PROFILE_ID), payload=profile_data
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        analytics = await nextdns.get_all_analytics(PROFILE_ID)
        connection_status = await nextdns.connection_status(PROFILE_ID)
        settings = await nextdns.get_settings(PROFILE_ID)

    await session.close()

    assert nextdns == snapshot
    assert analytics == snapshot
    assert connection_status == snapshot
    assert settings == snapshot

    assert nextdns.get_profile_name(PROFILE_ID) == snapshot
    assert nextdns.get_profile_id("Fake Profile") == snapshot


@pytest.mark.asyncio
async def test_profile_id_not_found(profiles_data: dict[str, Any]) -> None:
    """Test with wrong profile id."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        nextdns = await NextDns.create(session, "fakeapikey")

    await session.close()

    with pytest.raises(ProfileIdNotFoundError):
        nextdns.get_profile_name("xxyyxx")


@pytest.mark.asyncio
async def test_profile_name_not_found(profiles_data: dict[str, Any]) -> None:
    """Test with wrong name id."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        nextdns = await NextDns.create(session, "fakeapikey")

    await session.close()

    with pytest.raises(ProfileNameNotFoundError):
        nextdns.get_profile_id("Profile Name")


@pytest.mark.asyncio
async def test_clear_logs(profiles_data: dict[str, Any]) -> None:
    """Test clear_logs() method."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.delete(
            ENDPOINTS[ATTR_CLEAR_LOGS].format(profile_id=PROFILE_ID),
            status=HTTPStatus.NO_CONTENT.value,
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.clear_logs(PROFILE_ID)

    await session.close()

    assert result is True


@pytest.mark.asyncio
async def test_get_logs(profiles_data: dict[str, Any]) -> None:
    """Test get_logs() method."""
    with Path.open("tests/fixtures/logs.csv", encoding="utf-8") as file:
        logs = file.read()

    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.get(
            ENDPOINTS[ATTR_GET_LOGS].format(profile_id=PROFILE_ID),
            status=HTTPStatus.OK.value,
            payload=logs,
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.get_logs(PROFILE_ID)

    await session.close()

    assert result == logs


@pytest.mark.asyncio
@pytest.mark.parametrize(
    ("setting", "url"),
    [
        ("block_page", ENDPOINTS[ATTR_BLOCK_PAGE].format(profile_id=PROFILE_ID)),
        (
            "block_tinder",
            MAP_SETTING["block_tinder"].url.format(
                profile_id=PROFILE_ID, service=MAP_SETTING["block_tinder"].name
            ),
        ),
        (
            "block_piracy",
            MAP_SETTING["block_piracy"].url.format(
                profile_id=PROFILE_ID, category=MAP_SETTING["block_piracy"].name
            ),
        ),
    ],
)
async def test_set_setting(
    setting: str, url: str, profiles_data: dict[str, Any]
) -> None:
    """Test set_setting() method."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.patch(url, status=HTTPStatus.NO_CONTENT.value)

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.set_setting(PROFILE_ID, setting, True)

    await session.close()

    assert result is True


@pytest.mark.asyncio
async def test_set_parental_contrl_service(profiles_data: dict[str, Any]) -> None:
    """Test set_setting() method for parental control service."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.patch(
            MAP_SETTING["block_tinder"].url.format(
                profile_id=PROFILE_ID, service=MAP_SETTING["block_tinder"].name
            ),
            status=HTTPStatus.NOT_FOUND.value,
            payload={"errors": [{"code": "notFound"}]},
        )
        session_mock.post(
            ENDPOINTS[ATTR_PARENTAL_CONTROL_SERVICES].format(profile_id=PROFILE_ID),
            status=HTTPStatus.NO_CONTENT.value,
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.set_setting(PROFILE_ID, "block_tinder", True)

    await session.close()

    assert result is True


@pytest.mark.asyncio
async def test_set_parental_contrl_category(profiles_data: dict[str, Any]) -> None:
    """Test set_setting() method for parental control category."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.patch(
            MAP_SETTING["block_piracy"].url.format(
                profile_id=PROFILE_ID, category=MAP_SETTING["block_piracy"].name
            ),
            status=HTTPStatus.NOT_FOUND.value,
            payload={"errors": [{"code": "notFound"}]},
        )
        session_mock.post(
            ENDPOINTS[ATTR_PARENTAL_CONTROL_CATEGORIES].format(profile_id=PROFILE_ID),
            status=HTTPStatus.NO_CONTENT.value,
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.set_setting(PROFILE_ID, "block_piracy", True)

    await session.close()

    assert result is True


@pytest.mark.asyncio
async def test_set_not_supported_setting(profiles_data: dict[str, Any]) -> None:
    """Test set_setting() method with not supported setting."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        nextdns = await NextDns.create(session, "fakeapikey")

        with pytest.raises(SettingNotSupportedError):
            await nextdns.set_setting(PROFILE_ID, "unsupported_setting", True)

    await session.close()


@pytest.mark.asyncio
async def test_invalid_api_key() -> None:
    """Test error when provided API key is invalid."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], status=HTTPStatus.FORBIDDEN.value)

        with pytest.raises(InvalidApiKeyError):
            await NextDns.create(session, "fakeapikey")

    await session.close()


@pytest.mark.parametrize(
    "exception", [TimeoutError, aiohttp.ClientConnectorError(Mock(), Mock())]
)
@pytest.mark.asyncio
async def test_retry_error(exception: Exception) -> None:
    """Test retry error."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock, patch("asyncio.sleep") as sleep_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], exception=exception, repeat=True)

        with pytest.raises(RetryError) as exc:
            await NextDns.create(session, "fakeapikey")

        assert "RetryError" in str(exc.value)

    assert sleep_mock.call_count == 2
    assert sleep_mock.call_args_list[0][0][0] == 2
    assert sleep_mock.call_args_list[1][0][0] == 4

    await session.close()


@pytest.mark.parametrize(
    "exception", [TimeoutError, aiohttp.ClientConnectorError(Mock(), Mock())]
)
@pytest.mark.asyncio
async def test_retry_success(
    profiles_data: dict[str, Any], exception: Exception
) -> None:
    """Test retry which succeeded."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock, patch("asyncio.sleep") as sleep_mock:
        session_mock.get(
            ENDPOINTS[ATTR_PROFILES],
            exception=exception,
        )
        session_mock.get(
            ENDPOINTS[ATTR_PROFILES],
            exception=exception,
        )
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        await NextDns.create(session, "fakeapikey")

    assert sleep_mock.call_count == 2
    assert sleep_mock.call_args_list[0][0][0] == 2
    assert sleep_mock.call_args_list[1][0][0] == 4

    await session.close()


@pytest.mark.asyncio
async def test_retry_after_524(profiles_data: dict[str, Any]) -> None:
    """Test retry after HTTP status code 524."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock, patch("asyncio.sleep") as sleep_mock:
        session_mock.get(
            ENDPOINTS[ATTR_PROFILES],
            status=524,
            payload="Timeout Error",
        )
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        await NextDns.create(session, "fakeapikey")

    assert sleep_mock.call_count == 1
    assert sleep_mock.call_args_list[0][0][0] == 2

    await session.close()


@pytest.mark.asyncio
async def test_too_many_requests() -> None:
    """Test too many requests."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(
            ENDPOINTS[ATTR_PROFILES],
            status=429,
        )

        with pytest.raises(ApiError, match=re.escape("Too many requests")):
            await NextDns.create(session, "fakeapikey")

    await session.close()


@pytest.mark.asyncio
async def test_error_with_html() -> None:
    """Test error status code with text/html error response."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(
            ENDPOINTS[ATTR_PROFILES],
            status=523,
            content_type="text/html",
            payload="origin is unreachable",
        )

        with pytest.raises(ApiError, match=re.escape("Error code: 523")):
            await NextDns.create(session, "fakeapikey")

    await session.close()


@pytest.mark.asyncio
async def test_set_logs_retention(profiles_data: dict[str, Any]) -> None:
    """Test set_logs_retention() method."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.patch(
            ENDPOINTS[ATTR_LOGS].format(profile_id=PROFILE_ID),
            status=HTTPStatus.NO_CONTENT.value,
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.set_logs_retention(PROFILE_ID, 1)

    await session.close()

    assert result is True


@pytest.mark.asyncio
async def test_set_logs_retention_with_invalid_value(
    profiles_data: dict[str, Any],
) -> None:
    """Test set_logs_retention() method with invalid value."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        nextdns = await NextDns.create(session, "fakeapikey")

    await session.close()

    with pytest.raises(
        ValueError,
        match=re.escape(
            "Invalid logs retention value. "
            "Allowed values are: (1, 6, 24, 168, 720, 2160, 4320, 8760, 17520)"
        ),
    ):
        await nextdns.set_logs_retention(PROFILE_ID, 999)


@pytest.mark.asyncio
async def test_set_logs_location(profiles_data: dict[str, Any]) -> None:
    """Test set_logs_location() method."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
        session_mock.patch(
            ENDPOINTS[ATTR_LOGS].format(profile_id=PROFILE_ID),
            status=HTTPStatus.NO_CONTENT.value,
        )

        nextdns = await NextDns.create(session, "fakeapikey")

        result = await nextdns.set_logs_location(PROFILE_ID, "us")

    await session.close()

    assert result is True


@pytest.mark.asyncio
async def test_set_logs_location_with_invalid_value(
    profiles_data: dict[str, Any],
) -> None:
    """Test set_logs_location() method with invalid value."""
    session = aiohttp.ClientSession()

    with aioresponses() as session_mock:
        session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

        nextdns = await NextDns.create(session, "fakeapikey")

    await session.close()

    with pytest.raises(
        ValueError,
        match=re.escape(
            "Invalid logs location value. Allowed values are: ('ch', 'eu', 'gb', 'us')"
        ),
    ):
        await nextdns.set_logs_location(PROFILE_ID, "pl")