-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathtest_qb_downloader.py
More file actions
530 lines (427 loc) · 19.4 KB
/
test_qb_downloader.py
File metadata and controls
530 lines (427 loc) · 19.4 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
"""Tests for QbDownloader: constructor, SSL/scheme logic, auth, and error handling.
The implementation keeps _use_https as a local variable computed inside auth()
from self.host, so SSL/HTTPS behaviour is validated by observing auth() side-effects
(log messages) rather than reading an instance attribute directly.
"""
import logging
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
from module.downloader.client.qb_downloader import QbDownloader
# ---------------------------------------------------------------------------
# Constructor / URL building
# ---------------------------------------------------------------------------
class TestQbDownloaderConstructor:
"""Verify host URL normalisation at construction time."""
def test_ssl_true_no_scheme_uses_https(self):
"""ssl=True with bare host prepends https://."""
qb = QbDownloader(
host="192.168.1.10:8080", username="admin", password="pass", ssl=True
)
assert qb.host == "https://192.168.1.10:8080"
def test_ssl_false_no_scheme_uses_http(self):
"""ssl=False with bare host prepends http://."""
qb = QbDownloader(
host="192.168.1.10:8080", username="admin", password="pass", ssl=False
)
assert qb.host == "http://192.168.1.10:8080"
def test_explicit_http_scheme_preserved_when_ssl_true(self):
"""Explicit http:// scheme is kept even if ssl=True."""
qb = QbDownloader(
host="http://192.168.1.10:8080", username="admin", password="pass", ssl=True
)
assert qb.host == "http://192.168.1.10:8080"
def test_explicit_https_scheme_preserved_when_ssl_false(self):
"""Explicit https:// scheme is kept even if ssl=False."""
qb = QbDownloader(
host="https://192.168.1.10:8080",
username="admin",
password="pass",
ssl=False,
)
assert qb.host == "https://192.168.1.10:8080"
def test_explicit_http_scheme_preserved_ssl_false(self):
"""Explicit http:// URL with ssl=False stays as http://."""
qb = QbDownloader(
host="http://nas.local:8080", username="u", password="p", ssl=False
)
assert qb.host == "http://nas.local:8080"
def test_explicit_https_scheme_preserved_ssl_true(self):
"""Explicit https:// URL with ssl=True stays as https://."""
qb = QbDownloader(
host="https://nas.local:8080", username="u", password="p", ssl=True
)
assert qb.host == "https://nas.local:8080"
def test_credentials_stored(self):
"""Constructor stores username, password, and ssl flag as-is."""
qb = QbDownloader(
host="localhost:8080", username="admin", password="secret", ssl=False
)
assert qb.username == "admin"
assert qb.password == "secret"
assert qb.ssl is False
def test_client_initially_none(self):
"""_client starts as None before any auth call."""
qb = QbDownloader(
host="localhost:8080", username="admin", password="pass", ssl=False
)
assert qb._client is None
# ---------------------------------------------------------------------------
# Scheme selection: parametrised matrix
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"host,ssl,expected_prefix",
[
# Bare host - scheme derived from ssl flag
("192.168.1.1:8080", True, "https://"),
("192.168.1.1:8080", False, "http://"),
("qb.home", True, "https://"),
("qb.home", False, "http://"),
# Explicit http:// always wins regardless of ssl
("http://192.168.1.1:8080", True, "http://"),
("http://192.168.1.1:8080", False, "http://"),
# Explicit https:// always wins regardless of ssl
("https://192.168.1.1:8080", True, "https://"),
("https://192.168.1.1:8080", False, "https://"),
],
)
def test_scheme_selection_matrix(host: str, ssl: bool, expected_prefix: str):
"""Constructor resolves host scheme correctly for all input combinations."""
qb = QbDownloader(host=host, username="u", password="p", ssl=ssl)
assert qb.host.startswith(expected_prefix), (
f"host={host!r} ssl={ssl} -> expected prefix {expected_prefix!r}, got {qb.host!r}"
)
# ---------------------------------------------------------------------------
# auth: AsyncClient is created with verify=False
# ---------------------------------------------------------------------------
class TestAuthClientCreation:
"""auth() must create the httpx.AsyncClient with verify=False unconditionally."""
async def test_auth_creates_client_with_verify_false_when_ssl_true(self):
"""verify=False is used even when ssl=True (self-signed certs are common)."""
qb = QbDownloader(
host="192.168.1.10:8080", username="admin", password="pass", ssl=True
)
captured: list[dict] = []
class _FakeClient:
def __init__(self, **kwargs):
captured.append(kwargs)
async def post(self, url, data=None):
resp = MagicMock()
resp.status_code = 200
resp.text = "Ok."
return resp
async def aclose(self):
pass
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient", _FakeClient
):
result = await qb.auth()
assert result is True
assert len(captured) == 1
assert captured[0].get("verify") is False
async def test_auth_creates_client_with_verify_false_when_ssl_false(self):
"""verify=False is used even when ssl=False."""
qb = QbDownloader(
host="192.168.1.10:8080", username="admin", password="pass", ssl=False
)
captured: list[dict] = []
class _FakeClient:
def __init__(self, **kwargs):
captured.append(kwargs)
async def post(self, url, data=None):
resp = MagicMock()
resp.status_code = 200
resp.text = "Ok."
return resp
async def aclose(self):
pass
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient", _FakeClient
):
result = await qb.auth()
assert result is True
assert captured[0].get("verify") is False
async def test_auth_uses_5_second_connect_timeout(self):
"""auth() sets connect timeout to 5.0 seconds."""
qb = QbDownloader(host="localhost:8080", username="u", password="p", ssl=False)
captured_timeouts: list[httpx.Timeout] = []
class _FakeClient:
def __init__(self, **kwargs):
captured_timeouts.append(kwargs.get("timeout"))
async def post(self, url, data=None):
resp = MagicMock()
resp.status_code = 200
resp.text = "Ok."
return resp
async def aclose(self):
pass
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient", _FakeClient
):
await qb.auth()
assert len(captured_timeouts) == 1
assert captured_timeouts[0].connect == pytest.approx(5.0)
async def test_auth_sets_connection_limits_for_keepalive(self):
"""Regression for #984: qB client must cap keepalive so idle TCP
sockets don't linger past proxy / NAS idle-reap timeouts, otherwise
parallel renamer calls cascade into 'Server disconnected' errors."""
qb = QbDownloader(host="localhost:8080", username="u", password="p", ssl=False)
captured: list[dict] = []
class _FakeClient:
def __init__(self, **kwargs):
captured.append(kwargs)
async def post(self, url, data=None):
resp = MagicMock()
resp.status_code = 200
resp.text = "Ok."
return resp
async def aclose(self):
pass
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient", _FakeClient
):
await qb.auth()
limits = captured[0].get("limits")
assert limits is not None
assert limits.keepalive_expiry is not None
assert limits.keepalive_expiry > 0
assert limits.max_connections is not None
# ---------------------------------------------------------------------------
# auth: success / failure paths
# ---------------------------------------------------------------------------
class TestAuthSuccessFailure:
"""auth() return value reflects qBittorrent server responses."""
async def test_auth_returns_true_on_ok_response(self):
"""Returns True when server responds 200 + 'Ok.'."""
qb = QbDownloader(
host="localhost:8080", username="admin", password="pass", ssl=False
)
mock_client = AsyncMock()
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.text = "Ok."
mock_client.post = AsyncMock(return_value=mock_resp)
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
result = await qb.auth()
assert result is True
async def test_auth_returns_false_on_403(self):
"""Returns False and stops retrying immediately on 403 Forbidden."""
qb = QbDownloader(
host="localhost:8080", username="admin", password="pass", ssl=False
)
mock_client = AsyncMock()
mock_resp = MagicMock()
mock_resp.status_code = 403
mock_resp.text = "Forbidden"
mock_client.post = AsyncMock(return_value=mock_resp)
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
result = await qb.auth(retry=3)
assert result is False
# Should break immediately on 403, not exhaust all retries
assert mock_client.post.call_count == 1
async def test_auth_retries_up_to_limit_on_server_error(self):
"""Retries up to the retry limit on non-200/non-403 responses."""
qb = QbDownloader(
host="localhost:8080", username="admin", password="pass", ssl=False
)
mock_client = AsyncMock()
mock_resp = MagicMock()
mock_resp.status_code = 500
mock_resp.text = "Internal Server Error"
mock_client.post = AsyncMock(return_value=mock_resp)
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
result = await qb.auth(retry=2)
assert result is False
assert mock_client.post.call_count == 2
# ---------------------------------------------------------------------------
# auth: ConnectError logging - HTTPS vs HTTP message branching
# ---------------------------------------------------------------------------
class TestAuthConnectErrorLogging:
"""On ConnectError, the log message depends on whether the URL uses https://."""
async def test_https_url_logs_https_specific_guidance(self, caplog):
"""HTTPS-specific guidance is logged when host uses https:// and ConnectError occurs."""
# Use explicit https:// URL so the local use_https flag is True
qb = QbDownloader(
host="https://192.168.1.10:8080", username="u", password="p", ssl=True
)
mock_client = AsyncMock()
mock_client.post = AsyncMock(
side_effect=httpx.ConnectError("Connection refused")
)
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
with caplog.at_level(
logging.ERROR, logger="module.downloader.client.qb_downloader"
):
result = await qb.auth(retry=1)
assert result is False
error_messages = [
r.message for r in caplog.records if r.levelno == logging.ERROR
]
assert any("HTTPS" in msg for msg in error_messages)
assert any(
"disable SSL" in msg or "plain HTTP" in msg for msg in error_messages
)
async def test_https_url_derived_from_ssl_flag_logs_https_guidance(self, caplog):
"""HTTPS guidance also fires when scheme comes from ssl=True (bare host)."""
# Bare host + ssl=True -> self.host becomes https://... -> use_https=True in auth()
qb = QbDownloader(
host="192.168.1.10:8080", username="u", password="p", ssl=True
)
assert qb.host.startswith("https://")
mock_client = AsyncMock()
mock_client.post = AsyncMock(side_effect=httpx.ConnectError("refused"))
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
with caplog.at_level(
logging.ERROR, logger="module.downloader.client.qb_downloader"
):
await qb.auth(retry=1)
error_messages = [
r.message for r in caplog.records if r.levelno == logging.ERROR
]
assert any("HTTPS" in msg for msg in error_messages)
async def test_http_url_logs_generic_message_without_ssl_hint(self, caplog):
"""Generic connection error is logged when host uses http:// and ConnectError occurs."""
qb = QbDownloader(
host="http://192.168.1.10:8080", username="u", password="p", ssl=False
)
mock_client = AsyncMock()
mock_client.post = AsyncMock(
side_effect=httpx.ConnectError("Connection refused")
)
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
with caplog.at_level(
logging.ERROR, logger="module.downloader.client.qb_downloader"
):
result = await qb.auth(retry=1)
assert result is False
error_messages = [
r.message for r in caplog.records if r.levelno == logging.ERROR
]
assert any(
"Cannot connect to qBittorrent Server" in msg for msg in error_messages
)
# SSL-disable hint must NOT appear for plain HTTP connections
assert not any("disable SSL" in msg for msg in error_messages)
async def test_http_url_derived_from_ssl_flag_false_no_ssl_hint(self, caplog):
"""SSL-disable hint is absent when scheme comes from ssl=False (bare host)."""
qb = QbDownloader(
host="192.168.1.10:8080", username="u", password="p", ssl=False
)
assert qb.host.startswith("http://")
mock_client = AsyncMock()
mock_client.post = AsyncMock(side_effect=httpx.ConnectError("refused"))
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
with caplog.at_level(
logging.ERROR, logger="module.downloader.client.qb_downloader"
):
await qb.auth(retry=1)
all_messages = " ".join(r.message for r in caplog.records)
assert "disable SSL" not in all_messages
assert "plain HTTP" not in all_messages
async def test_connect_error_logs_check_ip_port_info(self, caplog):
"""Both HTTPS and HTTP paths log an info message about checking IP/port."""
qb = QbDownloader(
host="https://192.168.1.10:8080", username="u", password="p", ssl=True
)
mock_client = AsyncMock()
mock_client.post = AsyncMock(side_effect=httpx.ConnectError("refused"))
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
with caplog.at_level(
logging.INFO, logger="module.downloader.client.qb_downloader"
):
await qb.auth(retry=1)
info_messages = [r.message for r in caplog.records if r.levelno == logging.INFO]
assert any("IP" in msg or "port" in msg for msg in info_messages)
async def test_explicit_http_with_ssl_true_still_uses_generic_message(self, caplog):
"""Explicit http:// URL overrides ssl=True: generic error message, no HTTPS hint."""
# ssl=True but explicit http:// -> use_https=False inside auth()
qb = QbDownloader(
host="http://192.168.1.10:8080", username="u", password="p", ssl=True
)
assert qb.host.startswith("http://")
mock_client = AsyncMock()
mock_client.post = AsyncMock(side_effect=httpx.ConnectError("refused"))
with patch(
"module.downloader.client.qb_downloader.httpx.AsyncClient",
return_value=mock_client,
):
with patch(
"module.downloader.client.qb_downloader.asyncio.sleep",
new_callable=AsyncMock,
):
with caplog.at_level(
logging.ERROR, logger="module.downloader.client.qb_downloader"
):
await qb.auth(retry=1)
error_messages = [
r.message for r in caplog.records if r.levelno == logging.ERROR
]
assert not any("disable SSL" in msg for msg in error_messages)
assert not any("HTTPS" in msg for msg in error_messages)
# ---------------------------------------------------------------------------
# _url helper
# ---------------------------------------------------------------------------
class TestUrlHelper:
"""_url() builds the correct API endpoint path."""
def test_url_format_with_http(self):
"""_url returns host + /api/v2/ + endpoint for http hosts."""
qb = QbDownloader(host="localhost:8080", username="u", password="p", ssl=False)
assert qb._url("auth/login") == "http://localhost:8080/api/v2/auth/login"
def test_url_format_with_https(self):
"""_url includes https:// when SSL is used."""
qb = QbDownloader(host="nas.local:8080", username="u", password="p", ssl=True)
assert qb._url("app/version") == "https://nas.local:8080/api/v2/app/version"
def test_url_with_explicit_http_scheme_overriding_ssl_true(self):
"""_url works correctly when explicit http:// scheme overrides ssl=True."""
qb = QbDownloader(
host="http://nas.local:8080", username="u", password="p", ssl=True
)
assert qb._url("torrents/info") == "http://nas.local:8080/api/v2/torrents/info"