86 lines
2.5 KiB
Python
86 lines
2.5 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.core import settings
|
|
|
|
BASE_TOML = """[downloads]
|
|
folder = "/tmp/srip-test-dl"
|
|
max_connections = 2
|
|
requests_per_minute = 60
|
|
concurrency = true
|
|
verify_ssl = true
|
|
|
|
[deezer]
|
|
arl = "test-arl"
|
|
quality = 1
|
|
lower_quality_if_not_available = true
|
|
use_deezloader = true
|
|
deezloader_warnings = true
|
|
|
|
[conversion]
|
|
enabled = false
|
|
codec = "MP3"
|
|
|
|
[database]
|
|
downloads_enabled = true
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_config(tmp_path, monkeypatch):
|
|
import os
|
|
|
|
dl = tmp_path / "dl"
|
|
dl.mkdir()
|
|
cfg = tmp_path / "config.toml"
|
|
cfg.write_text(BASE_TOML.replace("/tmp/srip-test-dl", str(dl)))
|
|
monkeypatch.setattr(settings, "CONFIG_PATH", str(cfg))
|
|
monkeypatch.setattr(settings, "DOWNLOAD_DIR", str(dl))
|
|
os.environ["X"] = "1"
|
|
return cfg
|
|
|
|
|
|
def test_ui_config_get(tmp_config):
|
|
from app.main import app
|
|
|
|
c = TestClient(app)
|
|
r = c.get("/api/config/ui")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["deezer"]["quality"] == 1
|
|
assert body["deezer"]["arl_set"] is True
|
|
assert body["downloads"]["max_connections"] == 2
|
|
assert body["conversion"]["codec"] == "MP3"
|
|
assert body["behavior"]["skip_downloaded"] is True
|
|
|
|
|
|
def test_ui_config_put_roundtrip(tmp_config):
|
|
from app.main import app
|
|
|
|
c = TestClient(app)
|
|
r = c.put("/api/config/ui", json={
|
|
"deezer": {"quality": 2, "lower_quality_if_not_available": False},
|
|
"downloads": {"max_connections": 4},
|
|
"conversion": {"enabled": True, "codec": "FLAC"},
|
|
"behavior": {"skip_downloaded": False},
|
|
})
|
|
assert r.status_code == 200
|
|
body = c.get("/api/config/ui").json()
|
|
assert body["deezer"]["quality"] == 2
|
|
assert body["deezer"]["lower_quality_if_not_available"] is False
|
|
assert body["downloads"]["max_connections"] == 4
|
|
assert body["conversion"] == {"enabled": True, "codec": "FLAC"}
|
|
assert body["behavior"]["skip_downloaded"] is False
|
|
# untouched values preserved
|
|
assert body["deezer"]["use_deezloader"] is True
|
|
|
|
|
|
def test_ui_config_put_rejects_bad_values(tmp_config):
|
|
from app.main import app
|
|
|
|
c = TestClient(app)
|
|
assert c.put("/api/config/ui", json={"deezer": {"quality": 9}}).status_code == 422
|
|
assert c.put("/api/config/ui", json={"conversion": {"codec": "WAV"}}).status_code == 422
|
|
assert c.put("/api/config/ui", json={"downloads": {"folder": "/does/not/exist"}}).status_code == 422
|
|
assert c.put("/api/config/ui", json={"downloads": {"max_connections": 0}}).status_code == 422
|