|
| 1 | +import pytest |
| 2 | + |
| 3 | +from requests.cookies import RequestsCookieJar |
| 4 | +from src.settings._download_clients_qbit import QbitClient, QbitError |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize( |
| 8 | + "cookie_name, cookie_value, expected", |
| 9 | + [ |
| 10 | + # Legacy format |
| 11 | + ("SID", "abc", {"SID": "abc"}), |
| 12 | + # New dynamic port format (qBit 5.2+) |
| 13 | + ("QBIT_SID_8080", "xyz", {"QBIT_SID_8080": "xyz"}), |
| 14 | + ("QBIT_SID_12345", "token123", {"QBIT_SID_12345": "token123"}), |
| 15 | + ], |
| 16 | +) |
| 17 | +def test_extract_sid_success(cookie_name, cookie_value, expected): |
| 18 | + """Test successful extraction for various valid cookie names.""" |
| 19 | + jar = RequestsCookieJar() |
| 20 | + jar.set(cookie_name, cookie_value) |
| 21 | + |
| 22 | + assert QbitClient.extract_sid(jar) == expected |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize( |
| 26 | + "cookies", |
| 27 | + [ |
| 28 | + {}, # Empty jar |
| 29 | + {"WRONG_NAME": "value"}, # Incorrect name |
| 30 | + {"sid": "lowercase_fails"}, # Case sensitivity check |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_extract_sid_failures(cookies): |
| 34 | + """Test that invalid cookies properly raise QbitError.""" |
| 35 | + jar = RequestsCookieJar() |
| 36 | + for name, val in cookies.items(): |
| 37 | + jar.set(name, val) |
| 38 | + |
| 39 | + with pytest.raises(QbitError, match="No qBit cookie found"): |
| 40 | + QbitClient.extract_sid(jar) |
| 41 | + |
| 42 | + |
| 43 | +def test_extract_sid_priority(): |
| 44 | + """Verify it returns the first valid match it encounters.""" |
| 45 | + jar = RequestsCookieJar() |
| 46 | + jar.set("SID", "first") |
| 47 | + jar.set("QBIT_SID_9090", "second") |
| 48 | + |
| 49 | + result = QbitClient.extract_sid(jar) |
| 50 | + # Since it's a loop over the jar, it returns the first match found |
| 51 | + assert list(result.values())[0] in ["first", "second"] |
0 commit comments