Files
Athena-Deck/deploy/applio-web/backend/tests/test_uploads.py
T
Mikei386 06cacafbad Add native Applio RVC and preserve the custom studio UI
Pin Applio 3.6.5 and verified basis assets, migrate private voice provenance, and coordinate conversion/live/training with Deck GPU leases. Prepare an inactive voice profile and a restricted studio container. Move CUDA and Torch caches out of the small temporary filesystem to prevent OmniVoice reference failures. Package app updates from the explicit allowlist in one Docker layer.
2026-10-05 10:02:07 +02:00

181 lines
5.6 KiB
Python

"""Tests for secure filename handling and upload validation."""
import pytest
def test_sanitize_normal_filename():
from app.api.uploads import sanitize_filename
assert sanitize_filename("test.wav") == "test.wav"
assert sanitize_filename("my song.mp3") == "my_song.mp3"
def test_sanitize_path_traversal():
from app.api.uploads import sanitize_filename
# Path components are stripped, only the basename remains
result = sanitize_filename("../../etc/passwd")
assert ".." not in result
assert "/" not in result
assert result == "passwd"
result2 = sanitize_filename("../../../root/.ssh/id_rsa")
assert ".." not in result2
assert "/" not in result2
assert result2 == "id_rsa"
result3 = sanitize_filename("/etc/shadow")
assert ".." not in result3
assert result3 == "shadow"
def test_sanitize_hidden_file():
from app.api.uploads import sanitize_filename
result = sanitize_filename(".hidden.wav")
assert not result.startswith(".")
def test_sanitize_special_chars():
from app.api.uploads import sanitize_filename
result = sanitize_filename("file<script>.wav")
assert "<" not in result
assert ">" not in result
def test_sanitize_long_filename():
from app.api.uploads import sanitize_filename
long_name = "a" * 500 + ".wav"
result = sanitize_filename(long_name)
assert len(result) <= 204
def test_sanitize_empty():
from app.api.uploads import sanitize_filename
result = sanitize_filename("")
assert result == "upload"
def test_upload_rejects_disallowed_format(client):
response = client.post(
"/api/uploads",
files={"file": ("test.exe", b"fake exe content", "application/octet-stream")},
)
assert response.status_code == 400
assert "nicht erlaubt" in response.json()["detail"]
def test_upload_rejects_empty_file(client):
response = client.post(
"/api/uploads",
files={"file": ("empty.wav", b"", "audio/wav")},
)
assert response.status_code == 400
assert "leer" in response.json()["detail"]
def test_upload_accepts_wav(client):
response = client.post(
"/api/uploads",
files={"file": ("test.wav", b"RIFF fake wav data", "audio/wav")},
)
assert response.status_code == 200
data = response.json()
assert data["extension"] == "wav"
assert data["size"] > 0
assert "upload_id" in data
def test_upload_accepts_mp3(client):
response = client.post(
"/api/uploads",
files={"file": ("song.mp3", b"ID3 fake mp3", "audio/mpeg")},
)
assert response.status_code == 200
assert response.json()["extension"] == "mp3"
def test_upload_sanitizes_filename(client):
response = client.post(
"/api/uploads",
files={"file": ("../../evil.wav", b"RIFF", "audio/wav")},
)
assert response.status_code == 200
stored = response.json()["stored_name"]
assert ".." not in stored
assert "/" not in stored
def test_create_dataset_accepts_multiple_audio_files(client, tmp_dirs):
response = client.post(
"/api/datasets",
data={"dataset_name": "ElevenLabs-Anna"},
files=[
("files", ("one.wav", b"RIFF one", "audio/wav")),
("files", ("two.mp3", b"ID3 two", "audio/mpeg")),
],
)
assert response.status_code == 200
data = response.json()
assert data["file_count"] == 2
assert data["dataset_path"] == str(tmp_dirs["datasets"] / "ElevenLabs-Anna")
assert (tmp_dirs["datasets"] / "ElevenLabs-Anna" / "one.wav").exists()
assert (tmp_dirs["datasets"] / "ElevenLabs-Anna" / "two.mp3").exists()
@pytest.mark.parametrize("name", ["../anna", "anna/test", ".anna", "anna test"])
def test_create_dataset_rejects_unsafe_name(client, name):
response = client.post(
"/api/datasets",
data={"dataset_name": name},
files={"files": ("one.wav", b"RIFF one", "audio/wav")},
)
assert response.status_code == 400
def test_create_dataset_does_not_overwrite_existing_data(client, tmp_dirs):
existing = tmp_dirs["datasets"] / "Anna"
existing.mkdir()
(existing / "keep.wav").write_bytes(b"keep")
response = client.post(
"/api/datasets",
data={"dataset_name": "Anna"},
files={"files": ("new.wav", b"RIFF new", "audio/wav")},
)
assert response.status_code == 409
assert (existing / "keep.wav").read_bytes() == b"keep"
def test_get_existing_dataset_for_resume(client, tmp_dirs):
existing = tmp_dirs["datasets"] / "Amelia"
existing.mkdir()
(existing / "voice.wav").write_bytes(b"RIFF voice")
(existing / "ignore.txt").write_text("not audio")
response = client.get("/api/datasets/Amelia")
assert response.status_code == 200
data = response.json()
assert data["dataset_name"] == "Amelia"
assert data["dataset_path"] == str(existing)
assert data["file_count"] == 1
assert data["total_size"] == len(b"RIFF voice")
assert data["files"] == [{"stored_name": "voice.wav", "size": len(b"RIFF voice")}]
def test_get_existing_dataset_rejects_empty_directory(client, tmp_dirs):
(tmp_dirs["datasets"] / "Empty").mkdir()
response = client.get("/api/datasets/Empty")
assert response.status_code == 404
def test_create_dataset_cleans_up_after_invalid_file(client, tmp_dirs):
response = client.post(
"/api/datasets",
data={"dataset_name": "Broken"},
files=[
("files", ("one.wav", b"RIFF one", "audio/wav")),
("files", ("bad.exe", b"bad", "application/octet-stream")),
],
)
assert response.status_code == 400
assert not (tmp_dirs["datasets"] / "Broken").exists()
assert not list(tmp_dirs["datasets"].glob(".Broken.upload-*"))