From 340c3e107714cc14cabcc4de54ca3402c8798c8a Mon Sep 17 00:00:00 2001 From: IluaAir Date: Sun, 17 Aug 2025 12:55:41 +0300 Subject: [PATCH] add jwt check --- src/repository/base.py | 1 + tests/conftest.py | 13 +++++++++++-- tests/integration_tests/test_auth_db.py | 0 tests/unit_tests/test_auth_jwt.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/integration_tests/test_auth_db.py create mode 100644 tests/unit_tests/test_auth_jwt.py diff --git a/src/repository/base.py b/src/repository/base.py index 52032cc..d3493c1 100644 --- a/src/repository/base.py +++ b/src/repository/base.py @@ -26,6 +26,7 @@ class BaseRepo(Generic[ModelType]): statement = insert(self.model).values(data).returning(self.model) result = await self.session.execute(statement) obj: ModelType = result.scalar_one() + print(obj) return obj async def get_one_or_none(self, **filter_by: Any) -> ModelType | None: diff --git a/tests/conftest.py b/tests/conftest.py index e8120d7..33fe22a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,8 @@ import pytest -from sqlalchemy import NullPool -from sqlalchemy.ext.asyncio import create_async_engine +from sqlalchemy import NullPool, insert +from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine +from src.core.auth_manager import AuthManager from src.core.database import Base from src.models import * # noqa: F403 @@ -10,6 +11,14 @@ engine_null_pool = create_async_engine('sqlite+aiosqlite:///tests/test_db.db', p @pytest.fixture(scope="session", autouse=True) async def setup_database(): + hashed_pass = AuthManager.get_password_hash("admin") + user_admin = {"username": "admin", "hashed_password": hashed_pass, "is_superuser": True} async with engine_null_pool.begin() as conn: await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.create_all) + + async with async_sessionmaker(engine_null_pool, expire_on_commit=False)() as conn: + result = await conn.execute(insert(UsersORM).values(user_admin).returning(UsersORM)) # noqa: F405 + await conn.commit() + admin = result.scalar_one() + assert admin.is_superuser is True diff --git a/tests/integration_tests/test_auth_db.py b/tests/integration_tests/test_auth_db.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit_tests/test_auth_jwt.py b/tests/unit_tests/test_auth_jwt.py new file mode 100644 index 0000000..4da0d02 --- /dev/null +++ b/tests/unit_tests/test_auth_jwt.py @@ -0,0 +1,10 @@ + +from src.core.auth_manager import AuthManager + + +async def test_jwt(): + token = AuthManager.create_access_token(data={"id": 1, "sub": "testuser", "is_active": "True"}) + assert token + encode_token = AuthManager.decode_access_token(token=token) + assert encode_token['id'] == 1 and encode_token['sub'] == 'testuser' +