diff --git a/app/main.py b/app/main.py index faab72f..4e4ae28 100644 --- a/app/main.py +++ b/app/main.py @@ -204,7 +204,7 @@ async def test_radarr() -> ConnectionResult: def qbittorrent_from_config() -> QbittorrentClient: qb = read_config().qbittorrent url = build_base_url(qb.url, qb.port) - if not url or not qb.username or not qb.password: + if not url: raise HTTPException(status_code=409, detail="La connexion qBittorrent n'est pas configurée") return QbittorrentClient(url, qb.username, qb.password) @@ -228,8 +228,15 @@ def put_qbittorrent_settings(payload: QbittorrentSettingsWrite) -> QbittorrentSe cfg.qbittorrent.port = payload.port if payload.username: cfg.qbittorrent.username = payload.username - if payload.password: + if payload.password: + cfg.qbittorrent.password = payload.password + elif payload.password: + # Mot de passe seul: on conserve l'utilisateur existant. cfg.qbittorrent.password = payload.password + else: + # Champs vides: mode bypass d'authentification qBittorrent, sans identifiants. + cfg.qbittorrent.username = None + cfg.qbittorrent.password = None write_config(cfg) return QbittorrentSettingsRead( url=cfg.qbittorrent.url, diff --git a/app/qbittorrent.py b/app/qbittorrent.py index 4063ace..d570054 100644 --- a/app/qbittorrent.py +++ b/app/qbittorrent.py @@ -6,21 +6,28 @@ import httpx class QbittorrentClient: - def __init__(self, url: str, username: str, password: str) -> None: + """Client WebUI (API v2). Identifiants optionnels: sans eux, aucun login + n'est effectue — qBittorrent doit alors autoriser la plage IP (bypass + d'authentification), car il n'existe pas d'API key cote qBittorrent.""" + + def __init__( + self, url: str, username: str | None = None, password: str | None = None + ) -> None: self._url = url.rstrip("/") self._username = username self._password = password async def _request(self, method: str, endpoint: str, **kwargs) -> httpx.Response: async with httpx.AsyncClient(timeout=30) as client: - login = await client.post( - f"{self._url}/api/v2/auth/login", - data={"username": self._username, "password": self._password}, - headers={"Referer": self._url}, - ) - login.raise_for_status() - if login.text.strip() != "Ok.": - raise ValueError("Authentification qBittorrent refusée") + if self._username and self._password: + login = await client.post( + f"{self._url}/api/v2/auth/login", + data={"username": self._username, "password": self._password}, + headers={"Referer": self._url}, + ) + login.raise_for_status() + if login.text.strip() != "Ok.": + raise ValueError("Authentification qBittorrent refusée") response = await client.request( method, f"{self._url}{endpoint}", diff --git a/app/schemas.py b/app/schemas.py index 3ce94d8..3fdac2e 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -37,7 +37,8 @@ class QbittorrentSettingsRead(BaseModel): class QbittorrentSettingsWrite(BaseModel): url: str | None = None port: int | None = Field(default=None, ge=1, le=65535) - username: str | None = Field(default=None, min_length=1) + # Utilisateur vide = bypass d'authentification qBittorrent. + username: str | None = None password: str | None = Field(default=None, min_length=1) diff --git a/tests/test_api.py b/tests/test_api.py index a16fe2e..2664647 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -102,6 +102,28 @@ def test_qbittorrent_connection_local_address_with_port() -> None: assert test.json()["version"] == "v5.0.2" +@respx.mock +def test_qbittorrent_connection_without_credentials() -> None: + """Sans identifiants (bypass d'authentification qBittorrent): aucun login, + l'API est appelee directement.""" + respx.post("http://192.168.1.50:8555/api/v2/auth/login").mock( + return_value=Response(404) + ) + respx.get("http://192.168.1.50:8555/api/v2/app/version").mock( + return_value=Response(200, text="v5.0.2") + ) + with TestClient(app) as client: + put = client.put( + "/api/settings/qbittorrent", + json={"url": "192.168.1.50", "port": 8555, "username": "", "password": None}, + ) + assert put.status_code == 200 + assert put.json()["password_configured"] is False + test = client.post("/api/settings/qbittorrent/test") + assert test.status_code == 200 + assert test.json()["version"] == "v5.0.2" + + def test_settings_persist_to_conf_json() -> None: with TestClient(app) as client: put = client.put( diff --git a/web/src/App.tsx b/web/src/App.tsx index da2c4c7..ef6df97 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -987,21 +987,23 @@ export default function App() { />