diff --git a/tests/test_upload_limits_archive.py b/tests/test_upload_limits_archive.py new file mode 100644 index 0000000..edf3a67 --- /dev/null +++ b/tests/test_upload_limits_archive.py @@ -0,0 +1,165 @@ +"""Свои пределы загрузки для архивов (распоряжение Хозяина 08.10.2026). + +Повод: сметчица ведёт весь объект в одном диалоге — сметы, конъюнктурный анализ, прайсы-сканы +архивом — и упирается в 20 МБ на диалог. Обычные файлы остаются как были (на файл и на сумму +обычных в диалоге); архив и тома многотомника получают свой предел на файл, а всё +загруженное в диалог вместе — свой общий предел. Архивы не съедают квоту обычных файлов. +""" + +import importlib +import os +import sys + +import pytest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +for p in (ROOT, os.path.join(ROOT, "tests")): + if p not in sys.path: + sys.path.insert(0, p) + +from test_upload_limits_readonly import MB, TAB, _hdr, _tus_create, _up # noqa: E402 + + +@pytest.fixture() +def env(tmp_path, monkeypatch): + monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db")) + monkeypatch.setenv("DATA_DIR", str(tmp_path / "data")) + monkeypatch.setenv("AI_BACKEND", "claude") + monkeypatch.setenv("SESSION_SECRET", "test-secret") + monkeypatch.setenv("USER_MAX_FILE_MB", "1") + monkeypatch.setenv("USER_MAX_DIALOG_MB", "2") + monkeypatch.setenv("USER_MAX_ARCHIVE_MB", "3") + monkeypatch.setenv("USER_MAX_ARCHIVE_DIALOG_MB", "5") + monkeypatch.delenv("AGENT_OWNER_EMAILS", raising=False) + monkeypatch.chdir(ROOT) + import db, auth, claude_bridge, main + importlib.reload(db) + importlib.reload(auth) + importlib.reload(claude_bridge) + importlib.reload(main) + db.init_db() + from fastapi.testclient import TestClient + return db, auth, main, TestClient(main.app) + + +def test_archive_above_file_limit_accepted(env): + db, auth, main, client = env + assert _up(client, _hdr(auth, db, "u@x"), "смета.zip", 2 * MB).status_code == 200 + + +def test_archive_over_archive_limit_refused(env): + db, auth, main, client = env + r = _up(client, _hdr(auth, db, "u@x"), "смета.rar", 3 * MB + 1) + assert r.status_code == 413 + assert "Архив «смета.rar» больше 3 МБ" in r.json()["detail"] + + +@pytest.mark.parametrize("name", ["a.z01", "a.7z.001", "a.r00", "a.7z"]) +def test_split_volume_counts_as_archive(env, name): + db, auth, main, client = env + assert _up(client, _hdr(auth, db, "u@x"), name, 2 * MB).status_code == 200 + + +def test_archive_extension_case_insensitive(env): + db, auth, main, client = env + assert _up(client, _hdr(auth, db, "u@x"), "СМЕТА.ZIP", 2 * MB).status_code == 200 + + +def test_zip_inside_name_is_regular_file(env): + db, auth, main, client = env + r = _up(client, _hdr(auth, db, "u@x"), "смета.zip.xlsx", 2 * MB) + assert r.status_code == 413 + assert "больше 1 МБ" in r.json()["detail"] + + +def test_regular_refusal_tells_about_archive_limit(env): + db, auth, main, client = env + r = _up(client, _hdr(auth, db, "u@x"), "смета.pdf", MB + 1) + assert r.status_code == 413 + assert "Архив (zip, rar, 7z) можно до 3 МБ" in r.json()["detail"] + + +def test_archive_does_not_eat_regular_quota(env): + db, auth, main, client = env + h = _hdr(auth, db, "u@x") + assert _up(client, h, "a.zip", 2 * MB).status_code == 200 + assert _up(client, h, "b.xlsx", MB - 10).status_code == 200 + assert _up(client, h, "c.xlsx", MB - 10).status_code == 200 + + +def test_regular_files_count_toward_total(env): + db, auth, main, client = env + h = _hdr(auth, db, "u@x") + assert _up(client, h, "a.xlsx", MB - 10).status_code == 200 + assert _up(client, h, "b.xlsx", MB - 10).status_code == 200 + assert _up(client, h, "c.zip", 3 * MB).status_code == 200 + r = _up(client, h, "d.zip", 100) + assert r.status_code == 413 + assert "из 5 МБ" in r.json()["detail"] + + +def test_archives_total_over_limit_refused(env): + db, auth, main, client = env + h = _hdr(auth, db, "u@x") + assert _up(client, h, "a.zip", 3 * MB).status_code == 200 + assert _up(client, h, "b.rar", 2 * MB).status_code == 200 + r = _up(client, h, "c.7z", 100) + assert r.status_code == 413 + assert "вместе с архивами" in r.json()["detail"] + + +def test_tus_create_archive_uses_archive_limit(env): + db, auth, main, client = env + assert _tus_create(client, _hdr(auth, db, "u@x"), "смета.zip", 2 * MB).status_code == 201 + + +def test_tus_create_archive_over_archive_limit(env): + db, auth, main, client = env + r = _tus_create(client, _hdr(auth, db, "u@x"), "смета.zip", 3 * MB + 1) + assert r.status_code == 413 + assert "больше 3 МБ" in r.json()["detail"] + + +def test_tus_pending_archive_does_not_eat_regular_quota(env): + db, auth, main, client = env + h = _hdr(auth, db, "u@x") + assert _tus_create(client, h, "a.zip", 2 * MB).status_code == 201 + assert _tus_create(client, h, "b.xlsx", MB - 10).status_code == 201 + assert _tus_create(client, h, "c.xlsx", MB - 10).status_code == 201 + + +def test_tus_pending_archives_count_toward_total(env): + db, auth, main, client = env + h = _hdr(auth, db, "u@x") + assert _tus_create(client, h, "a.zip", 3 * MB).status_code == 201 + assert _tus_create(client, h, "b.zip", 2 * MB).status_code == 201 + r = _tus_create(client, h, "c.z01", 100) + assert r.status_code == 413 + assert "из 5 МБ" in r.json()["detail"] + + +def test_uploaded_bytes_split_classifies_by_real_name(env): + db, auth, main, client = env + db.log_action("u@x", "upload", "POST", "/api/upload", f"смета (1B) tab=q.zip (5000B) tab={TAB}") + assert db.uploaded_bytes_split("u@x", TAB) == (0, 5000) + db.log_action("v@x", "upload", "POST", "/api/upload", f"смета.zip (1B) tab=q.xlsx (7B) tab={TAB}") + assert db.uploaded_bytes_split("v@x", TAB) == (7, 0) + + +def test_archive_limit_from_env(env, monkeypatch): + db, auth, main, client = env + monkeypatch.setenv("USER_MAX_ARCHIVE_MB", "4") + assert _up(client, _hdr(auth, db, "u@x"), "a.zip", 3 * MB + MB // 2).status_code == 200 + + +def test_admin_archive_not_limited(env): + db, auth, main, client = env + assert _up(client, _hdr(auth, db, "boss@x", "admin"), "big.zip", 6 * MB).status_code == 200 + + +def test_is_archive_file(): + import archive_host as ah + for name in ("a.zip", "a.RAR", "a.7z", "a.z01", "a.001", "a.r00"): + assert ah.is_archive_file(name), name + for name in ("a.xlsx", "смета.zip.xlsx", "a.pdf"): + assert not ah.is_archive_file(name), name