189 lines
6.5 KiB
Python
189 lines
6.5 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_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"
|
|
|
|
|
|
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
|