feat: 11. Тесты
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import os
|
||||
|
||||
os.environ["DATABASE_URL"] = "sqlite:///./test_vault.db"
|
||||
os.environ["JWT_SECRET"] = "test-secret"
|
||||
os.environ["PASSWORD_PEPPER"] = "test-pepper"
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.core.passwords import hash_password
|
||||
from app.db import SessionLocal, engine
|
||||
from app.db.base import Base
|
||||
import app.models # noqa: F401,E402 register models on Base.metadata
|
||||
from app.main import app as fastapi_app
|
||||
from app.models import User
|
||||
|
||||
STRONG_PASSWORD = "Kz8#Fj2!Lm9@Qw4$xR"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clean_database():
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
yield
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with TestClient(fastapi_app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_user():
|
||||
def _make_user(email, password=STRONG_PASSWORD, is_admin=False):
|
||||
db = SessionLocal()
|
||||
try:
|
||||
user = User(
|
||||
email=email,
|
||||
hashed_password=hash_password(password),
|
||||
is_admin=is_admin,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
return user
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
return _make_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_admin(make_user):
|
||||
def _make_admin(email="admin@vault.test", password=STRONG_PASSWORD):
|
||||
return make_user(email=email, password=password, is_admin=True)
|
||||
|
||||
return _make_admin
|
||||
|
||||
Reference in New Issue
Block a user