341 lines
12 KiB
Python
341 lines
12 KiB
Python
import json
|
|
|
|
import respx
|
|
from fastapi.testclient import TestClient
|
|
from httpx import Response
|
|
|
|
from app import auth as auth_module
|
|
from app.conf_store import config_path
|
|
from app.database import SessionLocal
|
|
from app.main import app
|
|
from app.models import User
|
|
|
|
|
|
def configure_radarr(client: TestClient) -> None:
|
|
response = client.put(
|
|
"/api/settings/radarr",
|
|
json={"url": "http://radarr:7878", "api_key": "test-key"},
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_health() -> None:
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/health")
|
|
assert response.status_code == 200
|
|
assert response.json()["database"] == "ok"
|
|
|
|
|
|
@respx.mock
|
|
def test_radarr_connection() -> None:
|
|
respx.get("http://radarr:7878/api/v3/system/status").mock(
|
|
return_value=Response(200, json={"version": "5.27.0"})
|
|
)
|
|
with TestClient(app) as client:
|
|
configure_radarr(client)
|
|
response = client.post("/api/settings/radarr/test")
|
|
assert response.status_code == 200
|
|
assert response.json()["version"] == "5.27.0"
|
|
|
|
|
|
@respx.mock
|
|
def test_service_status_lifecycle() -> None:
|
|
"""Statut par service: configured -> tested apres un test reussi, puis
|
|
retour a configured quand la configuration change.
|
|
|
|
Utilise une URL distincte des autres tests: conf.json etant partage entre
|
|
les tests, il faut garantir un reset du statut herite.
|
|
"""
|
|
respx.get("http://radarr-lifecycle:7878/api/v3/system/status").mock(
|
|
return_value=Response(200, json={"version": "5.27.0"})
|
|
)
|
|
with TestClient(app) as client:
|
|
response = client.put(
|
|
"/api/settings/radarr",
|
|
json={"url": "http://radarr-lifecycle:7878", "api_key": "test-key"},
|
|
)
|
|
assert response.status_code == 200
|
|
# Configure mais pas encore teste.
|
|
settings = client.get("/api/settings").json()
|
|
assert settings["radarr"]["status"] == "configured"
|
|
assert settings["qbittorrent"]["status"] in {"configured", "unconfigured"}
|
|
|
|
client.post("/api/settings/radarr/test")
|
|
settings = client.get("/api/settings").json()
|
|
assert settings["radarr"]["status"] == "tested"
|
|
assert settings["radarr"]["tested_version"] == "5.27.0"
|
|
assert settings["radarr"]["tested_at"] is not None
|
|
|
|
# Le resultat du test est persiste dans conf.json.
|
|
raw = json.loads(config_path().read_text(encoding="utf-8"))
|
|
assert raw["radarr"]["test"]["ok"] is True
|
|
assert raw["radarr"]["test"]["version"] == "5.27.0"
|
|
|
|
# Modifier l'URL invalide le statut de test.
|
|
client.put(
|
|
"/api/settings/radarr",
|
|
json={"url": "http://radarr-alt:7878", "api_key": None},
|
|
)
|
|
settings = client.get("/api/settings").json()
|
|
assert settings["radarr"]["status"] == "configured"
|
|
|
|
|
|
@respx.mock
|
|
def test_failed_test_marks_service_as_error() -> None:
|
|
respx.get("http://radarr-fail:7878/api/v3/system/status").mock(
|
|
return_value=Response(500, text="boom")
|
|
)
|
|
with TestClient(app) as client:
|
|
client.put(
|
|
"/api/settings/radarr",
|
|
json={"url": "http://radarr-fail:7878", "api_key": "test-key"},
|
|
)
|
|
response = client.post("/api/settings/radarr/test")
|
|
assert response.status_code == 502
|
|
settings = client.get("/api/settings").json()
|
|
assert settings["radarr"]["status"] == "error"
|
|
assert settings["radarr"]["test_error"] is not None
|
|
|
|
|
|
@respx.mock
|
|
def test_qbittorrent_status_lifecycle() -> None:
|
|
respx.get("http://qbit-lifecycle:8080/api/v2/app/version").mock(
|
|
return_value=Response(200, text="v5.0.2")
|
|
)
|
|
with TestClient(app) as client:
|
|
client.put(
|
|
"/api/settings/qbittorrent",
|
|
json={"url": "http://qbit-lifecycle:8080", "port": 8080},
|
|
)
|
|
assert client.post("/api/settings/qbittorrent/test").status_code == 200
|
|
settings = client.get("/api/settings").json()
|
|
assert settings["qbittorrent"]["status"] == "tested"
|
|
assert settings["qbittorrent"]["tested_version"] == "v5.0.2"
|
|
|
|
# Un nouvel enregistrement (URL differente) reinitialise le statut de test.
|
|
client.put(
|
|
"/api/settings/qbittorrent",
|
|
json={"url": "http://qbit-alt:8080", "port": 8080},
|
|
)
|
|
settings = client.get("/api/settings").json()
|
|
assert settings["qbittorrent"]["status"] == "configured"
|
|
|
|
|
|
@respx.mock
|
|
def test_scan_only_imports_iso_files() -> None:
|
|
respx.get("http://radarr:7878/api/v3/movie?includeMovieFile=true").mock(
|
|
return_value=Response(
|
|
200,
|
|
json=[
|
|
{
|
|
"id": 10,
|
|
"title": "ISO Movie",
|
|
"year": 2025,
|
|
"path": "/movies/ISO Movie (2025)",
|
|
"movieFile": {
|
|
"id": 20,
|
|
"relativePath": "ISO Movie (2025).iso",
|
|
"size": 42_000_000_000,
|
|
},
|
|
},
|
|
{
|
|
"id": 11,
|
|
"title": "MKV Movie",
|
|
"year": 2024,
|
|
"path": "/movies/MKV Movie (2024)",
|
|
"movieFile": {
|
|
"id": 21,
|
|
"relativePath": "MKV Movie (2024).mkv",
|
|
"size": 10_000_000_000,
|
|
},
|
|
},
|
|
],
|
|
)
|
|
)
|
|
with TestClient(app) as client:
|
|
configure_radarr(client)
|
|
scan = client.post("/api/scans")
|
|
movies = client.get("/api/movies")
|
|
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_local_address_with_port() -> None:
|
|
# Adresse locale sans schema + port separe: l'URL de base doit etre
|
|
# reconstruite en http://192.168.1.50:8080.
|
|
respx.post("http://192.168.1.50:8080/api/v2/auth/login").mock(
|
|
return_value=Response(200, text="Ok.")
|
|
)
|
|
respx.get("http://192.168.1.50: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": "192.168.1.50",
|
|
"port": 8080,
|
|
"username": "admin",
|
|
"password": "pass",
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json()["port"] == 8080
|
|
test = client.post("/api/settings/qbittorrent/test")
|
|
assert test.status_code == 200
|
|
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"
|
|
|
|
|
|
@respx.mock
|
|
def test_qbittorrent_login_204_modern() -> None:
|
|
"""qBittorrent >= 5.2: le login repond 204 sans corps (plus "200 Ok.")."""
|
|
respx.post("http://192.168.1.50:8080/api/v2/auth/login").mock(
|
|
return_value=Response(204)
|
|
)
|
|
respx.get("http://192.168.1.50:8080/api/v2/app/version").mock(
|
|
return_value=Response(200, text="v5.2.0")
|
|
)
|
|
with TestClient(app) as client:
|
|
client.put(
|
|
"/api/settings/qbittorrent",
|
|
json={
|
|
"url": "192.168.1.50",
|
|
"port": 8080,
|
|
"username": "admin",
|
|
"password": "pass",
|
|
},
|
|
)
|
|
test = client.post("/api/settings/qbittorrent/test")
|
|
assert test.status_code == 200
|
|
assert test.json()["version"] == "v5.2.0"
|
|
|
|
|
|
@respx.mock
|
|
def test_qbittorrent_api_key_mode() -> None:
|
|
"""Cle API native (qBittorrent >= 5.2): en-tete Authorization: Bearer sur
|
|
chaque appel, aucun login."""
|
|
login_route = respx.post("http://192.168.1.50:8080/api/v2/auth/login").mock(
|
|
return_value=Response(404)
|
|
)
|
|
version_route = respx.get("http://192.168.1.50:8080/api/v2/app/version").mock(
|
|
return_value=Response(200, text="v5.2.0")
|
|
)
|
|
with TestClient(app) as client:
|
|
put = client.put(
|
|
"/api/settings/qbittorrent",
|
|
json={"url": "192.168.1.50", "port": 8080, "api_key": "qbt_test_key"},
|
|
)
|
|
assert put.json()["api_key_configured"] is True
|
|
test = client.post("/api/settings/qbittorrent/test")
|
|
raw = json.loads(config_path().read_text(encoding="utf-8"))
|
|
assert test.status_code == 200
|
|
assert test.json()["version"] == "v5.2.0"
|
|
assert not login_route.called
|
|
assert version_route.called
|
|
assert (
|
|
version_route.calls.last.request.headers["authorization"] == "Bearer qbt_test_key"
|
|
)
|
|
assert raw["qbittorrent"]["api_key"] == "qbt_test_key"
|
|
|
|
|
|
def test_settings_persist_to_conf_json() -> None:
|
|
with TestClient(app) as client:
|
|
put = client.put(
|
|
"/api/settings/qbittorrent",
|
|
json={
|
|
"url": "http://qbit:8080/",
|
|
"port": 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"
|
|
assert got.json()["port"] == 8080
|
|
raw = json.loads(config_path().read_text(encoding="utf-8"))
|
|
assert raw["qbittorrent"]["url"] == "http://qbit:8080"
|
|
assert raw["qbittorrent"]["port"] == 8080
|
|
assert raw["qbittorrent"]["username"] == "admin"
|
|
assert raw["qbittorrent"]["password"] == "pass"
|
|
|
|
|
|
def test_setup_link_only_without_existing_account() -> None:
|
|
"""Le lien de premier lancement ne doit pas apparaitre si un compte existe.
|
|
|
|
Verifie aussi que l'endpoint de setup refuse tout second compte, meme avec
|
|
un jeton valide.
|
|
"""
|
|
with TestClient(app) as client:
|
|
client.post("/api/auth/setup", json={"token": "invalide", "username": "x", "password": "y"})
|
|
# Nettoyage: la base de test est partagee entre les tests.
|
|
with SessionLocal() as db:
|
|
db.query(User).delete()
|
|
db.commit()
|
|
# Simule un redemarrage: le lifespan relance bootstrap_setup.
|
|
with TestClient(app):
|
|
assert auth_module.setup_token is not None
|
|
with TestClient(app) as client:
|
|
response = client.post(
|
|
"/api/auth/setup",
|
|
json={
|
|
"token": auth_module.setup_token,
|
|
"username": "admin",
|
|
"password": "pass",
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
# Le jeton est consomme apres creation du compte.
|
|
assert auth_module.setup_token is None
|
|
# Nouveau demarrage: un compte existe -> plus aucun jeton genere,
|
|
# et la creation d'un second compte est refusee.
|
|
with TestClient(app) as client:
|
|
assert auth_module.setup_token is None
|
|
response = client.post(
|
|
"/api/auth/setup",
|
|
json={"token": "peu-importe", "username": "autre", "password": "z"},
|
|
)
|
|
assert response.status_code == 403
|
|
|
|
|
|
def test_startup_logs_conf_json_summary(capsys) -> None:
|
|
"""Au lancement, le log indique si conf.json est charge et ce qu'il contient."""
|
|
from app.conf_store import AppConfig, write_config
|
|
|
|
with TestClient(app) as client:
|
|
# Repart d'une conf vierge: les tests precedents remplissent conf.json.
|
|
write_config(AppConfig())
|
|
client.put(
|
|
"/api/settings/qbittorrent",
|
|
json={"url": "192.168.1.50", "port": 8555},
|
|
)
|
|
with TestClient(app):
|
|
captured = capsys.readouterr()
|
|
assert "conf.json charge" in captured.out
|
|
assert "qbittorrent (bypass d'authentification)" in captured.out
|