83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from core.models import AppUser, BackupSchedule, Library, MediaSource, MediaItem
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_user(db):
|
|
return AppUser.objects.create_superuser("backup_admin", "backup@example.com", "secret-pass-123")
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_backup_schedule_requires_admin(client):
|
|
response = client.get("/api/backups/settings")
|
|
assert response.status_code == 401
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_backup_schedule_get_and_update(client, admin_user):
|
|
client.login(username="backup_admin", password="secret-pass-123")
|
|
|
|
response = client.get("/api/backups/settings")
|
|
assert response.status_code == 200
|
|
|
|
payload = {
|
|
"enabled": True,
|
|
"frequency": "weekly",
|
|
"minute": 30,
|
|
"hour": 3,
|
|
"day_of_week": 6,
|
|
"day_of_month": 1,
|
|
"retention_count": 5,
|
|
}
|
|
update = client.put(
|
|
"/api/backups/settings",
|
|
data=json.dumps(payload),
|
|
content_type="application/json",
|
|
)
|
|
assert update.status_code == 200
|
|
|
|
schedule = BackupSchedule.get_solo()
|
|
assert schedule.enabled is True
|
|
assert schedule.frequency == "weekly"
|
|
assert schedule.retention_count == 5
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_backup_now_writes_json_and_strips_cached_paths(client, admin_user, settings, tmp_path):
|
|
settings.BACKUP_ROOT = str(tmp_path)
|
|
client.login(username="backup_admin", password="secret-pass-123")
|
|
|
|
library = Library.objects.create(owner_user=admin_user, name="Backup Lib")
|
|
source = MediaSource.objects.create(
|
|
library=library,
|
|
name="YT",
|
|
source_type="youtube_playlist",
|
|
uri="https://example.com/list",
|
|
)
|
|
MediaItem.objects.create(
|
|
media_source=source,
|
|
title="Cached Item",
|
|
item_kind="movie",
|
|
runtime_seconds=60,
|
|
file_path="https://youtube.com/watch?v=abc",
|
|
cached_file_path="/tmp/pytv_cache/abc.mp4",
|
|
)
|
|
|
|
resp = client.post("/api/backups/run")
|
|
assert resp.status_code == 200
|
|
filename = resp.json()["filename"]
|
|
|
|
backup_path = Path(settings.BACKUP_ROOT) / filename
|
|
assert backup_path.exists()
|
|
|
|
payload = json.loads(backup_path.read_text(encoding="utf-8"))
|
|
media_entries = [x for x in payload["items"] if x["model"] == "core.mediaitem"]
|
|
assert media_entries
|
|
assert media_entries[0]["fields"]["cached_file_path"] is None
|
|
|
|
|