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()
+38 -4
View File
@@ -1,14 +1,48 @@
import allure
import pytest
from httpx import HTTPStatusError
from tests.e2e.conftest import MySession, e2e_settings
@pytest.mark.integra
class TestCrud:
@pytest.mark.parametrize("email",[
("Test_Email@test.com")
("test@d.d")
])
async def test_get_user_by_email_positive(self):
async def test_get_user_by_email_positive(self, email:str, auth_fixture:MySession)->None:
with allure.step(""):
pass
with allure.step("Get user by email"):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
response = await auth_fixture.get(f"{target_url}/user/get_by_email",params={"email":email})
response.raise_for_status()
response=response.json()
with allure.step("Validate response"):
assert response.get("email")==email
assert "TEST_" in response.get("first_name")
assert "TEST_" in response.get("last_name")
assert "TEST_" in response.get("middle_name")
assert response.get("direct_permissions")==[]
assert response.get("group")==[]
assert not response.get("hashed_password") or not response.get("plain_password") or not response.get("password")
@pytest.mark.parametrize("email, expected_exception", [
("test@test.test", HTTPStatusError),
("test", HTTPStatusError),
("@d", HTTPStatusError)
])
async def test_get_user_by_email_negative(self, email:str, expected_exception, auth_fixture:MySession)->None:
with allure.step("Get user by email"), pytest.raises(expected_exception):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
response = await auth_fixture.get(f"{target_url}/user/get_by_email",params={"email":email})
response.raise_for_status()