Files
site/WESP_REL/tests/test_route_auth_guards.py
2026-07-17 12:57:18 +03:00

49 lines
1.5 KiB
Python

import os
import tempfile
import unittest
from app import create_app, db
from config import TestingConfig
class RouteAuthGuardTestConfig(TestingConfig):
_TMP_DIR = tempfile.mkdtemp(prefix="wesp-route-guards-tests-")
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')}"}
AUTH_LOGIN = "guard-admin"
AUTH_PASSWORD = "guard-secret"
class RouteAuthGuardsTests(unittest.TestCase):
def setUp(self) -> None:
self.app = create_app(RouteAuthGuardTestConfig)
self.client = self.app.test_client()
self.ctx = self.app.app_context()
self.ctx.push()
db.create_all()
def tearDown(self) -> None:
db.session.remove()
db.drop_all()
self.ctx.pop()
def test_components_create_requires_auth(self) -> None:
payload = {"name": "Corn", "type": "Грубые корма", "dry_matter": 10, "price": 0}
unauthorized = self.client.post("/api/components", json=payload)
self.assertEqual(unauthorized.status_code, 401)
login = self.client.post(
"/api/auth/login",
json={"login": "guard-admin", "password": "guard-secret"},
)
self.assertEqual(login.status_code, 200)
authorized = self.client.post("/api/components", json=payload)
self.assertEqual(authorized.status_code, 201)
self.assertTrue(authorized.get_json().get("success"))
if __name__ == "__main__":
unittest.main()