conftest e2e auth/test user create

This commit is contained in:
2026-08-17 19:33:23 +03:00
parent 0a4a21f2e7
commit 1eb9935a15
5 changed files with 61 additions and 21 deletions
+17 -14
View File
@@ -21,14 +21,14 @@ class MySession(requests_async.AsyncSession):
self.headers = {}
self.token = None
async def get(self, url:str, **kwargs):
async def request(self, method:str, url:str, **kwargs):
if self.token:
self.headers['Authorization'] = f"Bearer {self.token}"
kwargs.setdefault('headers', self.headers)
return await super().get(url, **kwargs)
return await super().request(method, url, **kwargs)
@pytest_asyncio.fixture(scope="session", autouse=True)
async def auth_fixture()->None:
@pytest_asyncio.fixture(scope="function", autouse=True)
async def auth_fixture():
payload = {"username": e2e_settings.TEST_USERNAME, "password": e2e_settings.TEST_PASSWORD}
url = f'http://{e2e_settings.HOST}:{e2e_settings.PORT}'
@@ -37,25 +37,28 @@ async def auth_fixture()->None:
response = await session.post(url + "/protected/token", data=payload)
response.raise_for_status()
session.token = response.json()["access_token"]
yield session
@pytest_asyncio.fixture(scope="function", autouse=True)
async def test_user_fixture():
async def test_user_fixture(auth_fixture: MySession):
url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
test_id=uuid4
test_id=uuid4()
new_user_record={
"first_name":f"TEST_{test_id}",
"last_name":f"TEST_{test_id}",
"middle_name":f"TEST_{test_id}",
"email":"TEST@TEST.TEST",
"plain_password":"1234",
"email":"test@d.d",
"plain_password":"Test1234!",
"direct_permissions":[],
"group":[]
}
response=await requests_async.post(f"{url}/create_user", data=new_user_record)
print(response.json())
yield
new_user_to_delete={"email":"TEST@TEST.TEST"}
response=await requests_async.post(f"{url}/delete_user_hard", data=new_user_to_delete)
print(response.json())
response=await auth_fixture.post(f"{url}/user/create_user", json=new_user_record)
response.raise_for_status()
yield auth_fixture
response=await auth_fixture.post(f"{url}/user/delete_user_hard", params={"email":new_user_record.get("email")})
response.raise_for_status()