From cfc0c54181cd378582d9a445287c632aff9ce507 Mon Sep 17 00:00:00 2001 From: Orionis312050 Date: Sun, 11 Oct 2026 17:13:32 +0000 Subject: [PATCH] :sparkles: Endpoints qBittorrent (GET/PUT/test) persistant dans conf.json --- app/main.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test_api.py | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/app/main.py b/app/main.py index a93486b..276d10e 100644 --- a/app/main.py +++ b/app/main.py @@ -16,6 +16,7 @@ from app import auth from app.conf_store import read_config, write_config from app.database import Base, engine, get_db from app.models import IsoMovie +from app.qbittorrent import QbittorrentClient from app.radarr import RadarrClient, movie_file_path from app.schemas import ( ApiServiceSettingsRead, @@ -26,6 +27,7 @@ from app.schemas import ( HealthRead, IsoMovieRead, QbittorrentSettingsRead, + QbittorrentSettingsWrite, RadarrSettingsRead, RadarrSettingsWrite, ScanResult, @@ -172,6 +174,52 @@ async def test_radarr() -> ConnectionResult: return ConnectionResult(success=True, version=version, message=f"Radarr {version} accessible") +def qbittorrent_from_config() -> QbittorrentClient: + qb = read_config().qbittorrent + if not qb.url or not qb.username or not qb.password: + raise HTTPException(status_code=409, detail="La connexion qBittorrent n'est pas configurée") + return QbittorrentClient(qb.url, qb.username, qb.password) + + +@app.get("/api/settings/qbittorrent", response_model=QbittorrentSettingsRead) +def get_qbittorrent_settings() -> QbittorrentSettingsRead: + qb = read_config().qbittorrent + return QbittorrentSettingsRead( + url=qb.url, username=qb.username, password_configured=qb.password is not None + ) + + +@app.put("/api/settings/qbittorrent", response_model=QbittorrentSettingsRead) +def put_qbittorrent_settings(payload: QbittorrentSettingsWrite) -> QbittorrentSettingsRead: + cfg = read_config() + if payload.url is not None: + cfg.qbittorrent.url = str(payload.url).rstrip("/") + if payload.username: + cfg.qbittorrent.username = payload.username + if payload.password: + cfg.qbittorrent.password = payload.password + write_config(cfg) + return QbittorrentSettingsRead( + url=cfg.qbittorrent.url, + username=cfg.qbittorrent.username, + password_configured=cfg.qbittorrent.password is not None, + ) + + +@app.post("/api/settings/qbittorrent/test", response_model=ConnectionResult) +async def test_qbittorrent() -> ConnectionResult: + client = qbittorrent_from_config() + try: + version = await client.version() + except (httpx.HTTPError, ValueError) as error: + raise HTTPException( + status_code=502, detail=f"Connexion qBittorrent impossible: {error}" + ) from error + return ConnectionResult( + success=True, version=version, message=f"qBittorrent {version} accessible" + ) + + @app.get("/api/movies", response_model=list[IsoMovieRead]) def list_movies(db: DbSession) -> list[IsoMovie]: return list(db.scalars(select(IsoMovie).order_by(IsoMovie.title)).all()) diff --git a/tests/test_api.py b/tests/test_api.py index 25f1d0d..50fb88f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,7 +1,10 @@ +import json + import respx from fastapi.testclient import TestClient from httpx import Response +from app.conf_store import config_path from app.main import app @@ -70,3 +73,38 @@ def test_scan_only_imports_iso_files() -> None: assert scan.status_code == 200 assert scan.json()["iso_files"] == 1 assert [movie["title"] for movie in movies.json()] == ["ISO Movie"] + + +@respx.mock +def test_qbittorrent_connection() -> None: + respx.post("http://qbit:8080/api/v2/auth/login").mock( + return_value=Response(200, text="Ok.") + ) + respx.get("http://qbit:8080/api/v2/app/version").mock( + return_value=Response(200, text="v5.0.2") + ) + with TestClient(app) as client: + response = client.put( + "/api/settings/qbittorrent", + json={"url": "http://qbit:8080", "username": "admin", "password": "pass"}, + ) + assert response.status_code == 200 + 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( + "/api/settings/qbittorrent", + json={"url": "http://qbit:8080/", "username": "admin", "password": "pass"}, + ) + got = client.get("/api/settings/qbittorrent") + assert put.status_code == 200 + assert put.json()["password_configured"] is True + assert got.json()["url"] == "http://qbit:8080" + raw = json.loads(config_path().read_text(encoding="utf-8")) + assert raw["qbittorrent"]["url"] == "http://qbit:8080" + assert raw["qbittorrent"]["username"] == "admin" + assert raw["qbittorrent"]["password"] == "pass"