2024-05-20 12:10:17 +02:00
|
|
|
from fooder.app import app
|
|
|
|
from httpx import AsyncClient
|
2023-09-03 15:43:24 +02:00
|
|
|
import pytest
|
2024-05-20 12:10:17 +02:00
|
|
|
import httpx
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
username: str | None = None,
|
|
|
|
password: str | None = None,
|
|
|
|
):
|
2024-05-20 12:10:17 +02:00
|
|
|
self.client = AsyncClient(app=app, base_url="http://testserver/api")
|
|
|
|
self.client.headers["Accept"] = "application/json"
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
def set_token(self, token: str) -> None:
|
|
|
|
"""set_token.
|
|
|
|
|
|
|
|
:param token:
|
|
|
|
:type token: str
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2024-05-20 12:10:17 +02:00
|
|
|
self.client.headers["Authorization"] = "Bearer " + token
|
2023-09-03 15:43:24 +02:00
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
async def create_user(self, username: str, password: str) -> None:
|
2023-09-03 15:43:24 +02:00
|
|
|
data = {"username": username, "password": password}
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await self.post("user", json=data)
|
2023-09-03 15:43:24 +02:00
|
|
|
response.raise_for_status()
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
async def login(self, username: str, password: str, force_login: bool) -> None:
|
2023-09-03 15:43:24 +02:00
|
|
|
"""login.
|
|
|
|
|
|
|
|
:param username:
|
|
|
|
:type username: str
|
|
|
|
:param password:
|
|
|
|
:type password: str
|
|
|
|
:param force_login:
|
|
|
|
:type password: bool
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
data = {"username": username, "password": password}
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await self.post("token", data=data)
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
if force_login:
|
2024-05-20 12:10:17 +02:00
|
|
|
await self.create_user(username, password)
|
|
|
|
return await self.login(username, password, False)
|
2023-09-03 15:43:24 +02:00
|
|
|
else:
|
2024-05-20 12:10:17 +02:00
|
|
|
raise Exception(
|
|
|
|
f"Could not login as {username}! Detail: {response.text}"
|
|
|
|
)
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
result = response.json()
|
|
|
|
self.set_token(result["access_token"])
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
async def get(self, path: str, **kwargs) -> httpx.Response:
|
|
|
|
return await self.client.get(path, **kwargs)
|
2023-09-03 15:43:24 +02:00
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
async def delete(self, path: str, **kwargs) -> httpx.Response:
|
|
|
|
return await self.client.delete(path, **kwargs)
|
2023-09-03 15:43:24 +02:00
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
async def post(self, path: str, **kwargs) -> httpx.Response:
|
|
|
|
return await self.client.post(path, **kwargs)
|
2023-09-03 15:43:24 +02:00
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
async def patch(self, path: str, **kwargs) -> httpx.Response:
|
|
|
|
return await self.client.patch(path, **kwargs)
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def unauthorized_client() -> Client:
|
2024-05-20 12:10:17 +02:00
|
|
|
return Client()
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-20 12:10:17 +02:00
|
|
|
async def client(user_payload) -> Client:
|
|
|
|
client = Client()
|
|
|
|
await client.login(user_payload["username"], user_payload["password"], True)
|
|
|
|
return client
|