Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.8 KB
Raw Blame
"""Backup-status API tests ported from ``tests/web/routes/test_backup_status_api.py``.

The Flask original was deleted by the FastAPI migration. On this branch the
endpoint lives at ``GET /settings/api/backup-status``
(``web/routers/settings.py::api_get_backup_status``) and the size formatter
at ``utilities/formatting.human_size``.

The only successor is ``test_endpoint_coverage.py::test_settings_backup_status``,
which asserts nothing but ``status_code == 200`` — deleting the symlink guard,
the ``.tmp`` exclusion or the whole size formatter leaves it green.

The original's "response shape" block replicated the route's glob in the test
body (pathlib asserting on pathlib, never touching the SUT). Those are ported
as real requests against the endpoint so the property they were meant to pin
— what the handler actually reports — is now the thing under test.
``get_user_backup_directory`` is patched to a tmp dir so a background backup
landing a real file can't make the counts flaky.
"""

from unittest.mock import patch

import pytest

from local_deep_research.utilities.formatting import human_size

PATHS = "local_deep_research.config.paths"


class TestHumanSize:
    """Tests for the shared human_size formatter."""

    def test_zero_bytes(self):
        assert human_size(0) == "0.0 B"

    def test_bytes(self):
        assert human_size(500) == "500.0 B"

    def test_kilobytes(self):
        assert human_size(1536) == "1.5 KB"

    def test_megabytes(self):
        result = human_size(258_179_072)
        assert "MB" in result
        assert result == "246.2 MB"

    def test_gigabytes(self):
        result = human_size(2_147_483_648)
        assert result == "2.0 GB"

    def test_terabytes(self):
        result = human_size(1_099_511_627_776)
        assert result == "1.0 TB"

    def test_petabytes(self):
        result = human_size(1_125_899_906_842_624)  # 1 PB = 1024^5
        assert result == "1.0 PB"

    def test_exabytes_fallback(self):
        result = human_size(1_152_921_504_606_846_976)  # 1 EB = 1024^6
        assert result == "1.0 EB"

    def test_negative_petabytes(self):
        result = human_size(-1_125_899_906_842_624)  # -1 PB
        assert result == "-1.0 PB"


@pytest.fixture()
def backup_dir(tmp_path):
    """Redirect the endpoint's per-user backup directory at a tmp dir."""
    directory = tmp_path / "backups"
    directory.mkdir()
    with patch(f"{PATHS}.get_user_backup_directory", return_value=directory):
        yield directory


def _get_status(client):
    resp = client.get("/settings/api/backup-status")
    assert resp.status_code == 200, resp.text
    return resp.json()


class TestBackupStatusResponseShape:
    """The listing the endpoint actually reports for a given directory."""

    def test_no_backups_returns_empty(self, authenticated_client, backup_dir):
        """When the backup directory is empty, the response has count=0."""
        data = _get_status(authenticated_client)

        assert data["count"] == 0
        assert data["backups"] == []
        assert data["total_size_bytes"] == 0

    def test_single_backup_detected(self, authenticated_client, backup_dir):
        """A single backup file is found and sized correctly."""
        (backup_dir / "ldr_backup_20260326_120000.db").write_bytes(b"x" * 4096)

        data = _get_status(authenticated_client)

        assert data["count"] == 1
        assert data["backups"][0]["filename"] == "ldr_backup_20260326_120000.db"
        assert data["backups"][0]["size_bytes"] == 4096

    def test_multiple_backups_sorted_newest_first(
        self, authenticated_client, backup_dir
    ):
        """Multiple backups are listed newest first."""
        old = backup_dir / "ldr_backup_20260325_120000.db"
        new = backup_dir / "ldr_backup_20260326_120000.db"
        old.write_bytes(b"old")
        new.write_bytes(b"new!")
        # The handler orders by mtime, so make mtime agree with the
        # timestamps encoded in the filenames.
        import os

        os.utime(old, (1_700_000_000, 1_700_000_000))
        os.utime(new, (1_700_086_400, 1_700_086_400))

        data = _get_status(authenticated_client)

        names = [b["filename"] for b in data["backups"]]
        assert names == [
            "ldr_backup_20260326_120000.db",
            "ldr_backup_20260325_120000.db",
        ]

    def test_tmp_files_not_included(self, authenticated_client, backup_dir):
        """Temporary .tmp files must not appear in the backup listing."""
        (backup_dir / "ldr_backup_20260326_120000.db").write_bytes(b"real")
        (backup_dir / "ldr_backup_20260326_130000.db.tmp").write_bytes(b"temp")

        data = _get_status(authenticated_client)

        names = [b["filename"] for b in data["backups"]]
        assert names == ["ldr_backup_20260326_120000.db"]

    def test_total_size_calculation(self, authenticated_client, backup_dir):
        """total_size_bytes sums every listed backup file's size."""
        (backup_dir / "ldr_backup_20260325_120000.db").write_bytes(b"x" * 1000)
        (backup_dir / "ldr_backup_20260326_120000.db").write_bytes(b"x" * 2000)

        data = _get_status(authenticated_client)

        assert data["total_size_bytes"] == 3000


class TestBackupStatusEndpointGlobHardening:
    """Real endpoint tests that drive GET /settings/api/backup-status.

    Unlike the response-shape tests above, these verify the symlink
    hardening is wired into the handler, not just that pathlib behaves.
    """

    def test_symlinked_backup_entry_is_excluded(
        self, authenticated_client, backup_dir, tmp_path
    ):
        """A symlink planted in the backup dir must not appear in the API
        response, so an external target's metadata is never reported."""
        # A legitimate backup.
        real = backup_dir / "ldr_backup_20260101_120000.db"
        real.write_bytes(b"x" * 2048)

        # A malicious symlink whose name matches the glob but targets a file
        # outside the backup directory.
        outside = tmp_path / "outside_secret.db"
        outside.write_bytes(b"secret-data-of-known-size")
        evil = backup_dir / "ldr_backup_29991231_235959.db"
        evil.symlink_to(outside)

        data = _get_status(authenticated_client)
        names = [b["filename"] for b in data["backups"]]

        assert "ldr_backup_20260101_120000.db" in names
        assert "ldr_backup_29991231_235959.db" not in names
        # The aggregate must never count more than what is listed — guards a
        # future regression where an entry is filtered from the list but its
        # (external) target size still leaks into total_size_bytes.
        assert data["total_size_bytes"] == sum(
            b["size_bytes"] for b in data["backups"]
        )
        # Listing must be non-destructive: the symlink and its target survive.
        assert evil.is_symlink()
        assert outside.exists()