diff --git a/fooder/test/fixtures/client.py b/fooder/test/fixtures/client.py index a41ff71..246a9b3 100644 --- a/fooder/test/fixtures/client.py +++ b/fooder/test/fixtures/client.py @@ -1,6 +1,6 @@ from fooder.app import app from fooder.tasks_app import app as tasks_app -from httpx import AsyncClient +from httpx import ASGITransport, AsyncClient import pytest import httpx import os @@ -12,8 +12,12 @@ class Client: username: str | None = None, password: str | None = None, ): - self.client = AsyncClient(app=app, base_url="http://testserver/api") - self.client.headers["Accept"] = "application/json" + self.client = lambda: AsyncClient( + transport=ASGITransport(app=app), + base_url="http://testserver/api", + headers=self.headers, + ) + self.headers = {"Accept": "application/json"} def set_token(self, token: str) -> None: """set_token. @@ -22,7 +26,7 @@ class Client: :type token: str :rtype: None """ - self.client.headers["Authorization"] = "Bearer " + token + self.headers["Authorization"] = "Bearer " + token async def create_user(self, username: str, password: str) -> None: data = {"username": username, "password": password} @@ -57,26 +61,33 @@ class Client: self.set_token(result["access_token"]) async def get(self, path: str, **kwargs) -> httpx.Response: - return await self.client.get(path, **kwargs) + async with self.client() as client: + return await client.get(path, **kwargs) async def delete(self, path: str, **kwargs) -> httpx.Response: - return await self.client.delete(path, **kwargs) + async with self.client() as client: + return await client.delete(path, **kwargs) async def post(self, path: str, **kwargs) -> httpx.Response: - return await self.client.post(path, **kwargs) + async with self.client() as client: + return await client.post(path, **kwargs) async def patch(self, path: str, **kwargs) -> httpx.Response: - return await self.client.patch(path, **kwargs) + async with self.client() as client: + return await client.patch(path, **kwargs) class TasksClient(Client): def __init__(self, authorized: bool = True): super().__init__() - self.client = AsyncClient(app=tasks_app, base_url="http://testserver/api") - self.client.headers["Accept"] = "application/json" + self.client = lambda: AsyncClient( + transport=ASGITransport(app=tasks_app), + base_url="http://testserver/api", + headers=self.headers, + ) if authorized: - self.client.headers["Authorization"] = "Bearer " + self.get_token() + self.headers["Authorization"] = "Bearer " + self.get_token() def get_token(self) -> str: return os.getenv("API_KEY") diff --git a/requirements.txt b/requirements.txt index 7ce385c..d08cf83 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ uvicorn[standard] asyncpg psycopg2-binary==2.9.3 python-jose[cryptography] +bcrypt<5.0.0 passlib[bcrypt] fastapi-users requests diff --git a/requirements_local.txt b/requirements_local.txt index 82188d7..00b80c1 100644 --- a/requirements_local.txt +++ b/requirements_local.txt @@ -4,6 +4,7 @@ pydantic_settings sqlalchemy[postgresql_asyncpg] uvicorn[standard] python-jose[cryptography] +bcrypt<5.0.0 passlib[bcrypt] fastapi-users pytest diff --git a/test.sh b/test.sh index 1e9c904..abf655c 100755 --- a/test.sh +++ b/test.sh @@ -15,13 +15,13 @@ export SECRET_KEY=$(openssl rand -hex 32) export REFRESH_SECRET_KEY=$(openssl rand -hex 32) export API_KEY=$(openssl rand -hex 32) -python -m fooder --create-tables +python3 -m fooder --create-tables # finally run tests if [[ $# -eq 1 ]]; then - python -m pytest fooder --disable-warnings -sv -k "${1}" + python3 -m pytest fooder --disable-warnings -sv -k "${1}" else - python -m pytest fooder --disable-warnings -sv + python3 -m pytest fooder --disable-warnings -sv fi status=$?