71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
"""Регрессия: setup guard отключается через TestingConfig, не через test API."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from app import create_app, db
|
|
from config import ProductionConfig, TestingConfig
|
|
|
|
|
|
class _GuardBypassTestConfig(TestingConfig):
|
|
_TMP_DIR = tempfile.mkdtemp(prefix="wesp-setup-guard-bypass-")
|
|
SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(_TMP_DIR, 'recipes_test.db')}"
|
|
SQLALCHEMY_BINDS = {
|
|
"reports": f"sqlite:///{os.path.join(_TMP_DIR, 'reports_test.db')}",
|
|
}
|
|
|
|
|
|
class SetupGuardConfigTests(unittest.TestCase):
|
|
def test_testing_config_allows_api_without_setup_completed(self) -> None:
|
|
app = create_app(_GuardBypassTestConfig)
|
|
with app.app_context():
|
|
db.create_all()
|
|
client = app.test_client()
|
|
resp = client.post(
|
|
"/api/auth/login",
|
|
json={"login": "wrong", "password": "wrong"},
|
|
)
|
|
self.assertNotEqual(resp.status_code, 302)
|
|
self.assertIn(resp.status_code, (400, 401, 500))
|
|
|
|
def test_setup_config_redirects_to_setup_when_incomplete(self) -> None:
|
|
tmp = tempfile.mkdtemp(prefix="wesp-setup-guard-")
|
|
try:
|
|
base = Path(tmp) / "wesp_home"
|
|
base.mkdir()
|
|
data = base / "data"
|
|
data.mkdir()
|
|
recipes = data / "recipes.db"
|
|
reports = data / "reports.db"
|
|
|
|
class SetupGuardConfig(ProductionConfig):
|
|
BASE_DIR = str(base)
|
|
DATA_DIR = str(data)
|
|
SQLALCHEMY_DATABASE_URI = f"sqlite:///{recipes}"
|
|
SQLALCHEMY_BINDS = {"reports": f"sqlite:///{reports}"}
|
|
SECRET_KEY = "setup-guard-test-secret"
|
|
TESTING = True
|
|
WESP_TESTING_BYPASS_SETUP_GUARD = False
|
|
SYNC_BACKGROUND_REQUEUE = False
|
|
SYNC_CLIENT_AUTOSTART = False
|
|
WESP_ADMIN_LOG_PATH = ""
|
|
WESP_LLM_AUTOSTART = False
|
|
|
|
app = create_app(SetupGuardConfig, run_migrations=True)
|
|
with app.app_context():
|
|
client = app.test_client()
|
|
resp = client.get("/")
|
|
self.assertEqual(resp.status_code, 302)
|
|
self.assertIn("/setup", resp.headers.get("Location", ""))
|
|
finally:
|
|
shutil.rmtree(tmp, ignore_errors=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|