e2e crud 0.2

This commit is contained in:
2026-08-18 19:33:41 +03:00
parent 1eb9935a15
commit b3083b0e82
8 changed files with 333 additions and 72 deletions
+17 -6
View File
@@ -40,25 +40,36 @@ async def auth_fixture():
yield session
@pytest_asyncio.fixture(scope="function", autouse=True)
async def test_user_fixture(auth_fixture: MySession):
@pytest_asyncio.fixture(scope="function")
async def test_user_fixture(request, auth_fixture: MySession):
url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
test_id=uuid4()
direct_permission_param, group_param = request.param
new_user_record={
"first_name":f"TEST_{test_id}",
"last_name":f"TEST_{test_id}",
"middle_name":f"TEST_{test_id}",
"email":"test@d.d",
"email":f"TEST_{test_id}@d.d",
"plain_password":"Test1234!",
"direct_permissions":[],
"group":[]
"direct_permissions":direct_permission_param,
"group":group_param
}
response=await auth_fixture.post(f"{url}/user/create_user", json=new_user_record)
response.raise_for_status()
yield auth_fixture
async with MySession() as session:
payload={"username": new_user_record.get("email"), "password": new_user_record.get("plain_password")}
response = await session.post(url + "/protected/token", data=payload)
response.raise_for_status()
session.token = response.json()["access_token"]
yield (session, new_user_record)
response=await auth_fixture.post(f"{url}/user/delete_user_hard", params={"email":new_user_record.get("email")})
response.raise_for_status()
+252 -16
View File
@@ -1,3 +1,6 @@
from uuid import uuid4
import allure
import pytest
from httpx import HTTPStatusError
@@ -8,15 +11,17 @@ from tests.e2e.conftest import MySession, e2e_settings
@pytest.mark.integra
class TestCrud:
@pytest.mark.parametrize("email",[
("test@d.d")
])
async def test_get_user_by_email_positive(self, email:str, auth_fixture:MySession)->None:
@pytest.mark.parametrize("test_user_fixture", [(["admin"], ["admin_group"])], indirect=True)
async def test_get_user_by_email_positive(self,test_user_fixture)->None:
session, new_user_record=test_user_fixture
with allure.step("Get user by email"):
email = new_user_record.get("email") #get email from the fixture in yield sector
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 = await session.get(f"{target_url}/user/get_by_email",params={"email":email})
response.raise_for_status()
response=response.json()
@@ -26,23 +31,254 @@ class TestCrud:
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 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_status", [
pytest.param("test@test.test", 404, id="non_existed_email"),
pytest.param("test",404, id="wrong_email"),
pytest.param("@d", 404, id="wrong_email")
])
async def test_get_user_by_email_negative(self, email:str, expected_status:int, auth_fixture:MySession)->None:
with allure.step("Get user by email"):
target_url = f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
with pytest.raises(HTTPStatusError) as exc_info:
response = await auth_fixture.get(f"{target_url}/user/get_by_email", params={"email": email})
response.raise_for_status()
assert exc_info.value.response.status_code == expected_status
@pytest.mark.parametrize("new_user_record",[
pytest.param({ "first_name":"TEST",
"last_name":"TEST",
"middle_name":"TEST",
"email":f"TEST_{uuid4()}@d.d",
"plain_password":"Test1234!",
"direct_permissions":[],
"group":[]}, id="Positive_user_creation_with_all_the_fields"),
pytest.param({ "first_name":"TEST",
"last_name":"TEST",
"middle_name":"TEST",
"email":f"TEST_{uuid4()}@d.d",
"plain_password":"Test1234!",
"direct_permissions":["WRONG_PERMISSIONS"],
"group":["WRONG_GROUP"]},id="Positive_wrong_permissions"),
])
async def test_create_delete_user_hard_positive(self, new_user_record:dict,auth_fixture:MySession)->None:
with allure.step("Setting target_url"):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
with allure.step("Create new test user and check for the new user"):
response=await auth_fixture.post(f"{target_url}/user/create_user", json=new_user_record)
response.raise_for_status()
try:
with allure.step("Check for the new user"):
response = await auth_fixture.get(f"{target_url}/user/get_by_email",params={"email":new_user_record.get("email")})
response.raise_for_status()
response=response.json()
assert response.get("email")==new_user_record["email"]
assert response.get("first_name")==new_user_record["first_name"]
assert response.get("last_name")==new_user_record["last_name"]
assert response.get("middle_name")==new_user_record["middle_name"]
assert response.get("direct_permissions") == new_user_record["direct_permissions"] or response.get("direct_permissions") == []
assert response.get("group") == new_user_record["group"] or response.get("group") == []
assert not response.get("hashed_password") or not response.get("plain_password") or not response.get("password")
finally:
with allure.step("delete new user"):
response=await auth_fixture.post(f"{target_url}/user/delete_user_hard", params={"email":new_user_record["email"]})
response.raise_for_status()
@pytest.mark.parametrize("new_user_record, expected_status", [
pytest.param({ "first_name":"TEST",
"last_name":"TEST",
"middle_name":"TEST",
"email":"WRONGEMAIL",
"plain_password":"Test1234!",
"direct_permissions":[],
"group":[]},422,id="Non_existed_email"),
pytest.param({ "first_name":"TEST",
"last_name":"TEST",
"middle_name":"TEST",
"email":"TEST1@d.d",
"plain_password":"1234",
"direct_permissions":[],
"group":[]},422,id="Wrong_password"),
pytest.param({ "first_name":"TEST",
"email":"TEST1@d.d",
"plain_password":"Test1234!",
},422,id="Not_all_the_fields"),
])
async def test_create_user_negative(self, new_user_record:dict, auth_fixture:MySession, expected_status:int):
with allure.step("Preparing data to create new user negative"):
user_created=False
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
try:
with allure.step("Create new test user and check for the new user"):
response=await auth_fixture.post(f"{target_url}/user/create_user", json=new_user_record)
if response.status_code < 400:
user_created = True
with pytest.raises(HTTPStatusError) as exc_info:
response.raise_for_status()
assert exc_info.value.response.status_code == expected_status
finally:
if user_created:
with allure.step("delete new user"):
response = await auth_fixture.post(
f"{target_url}/user/delete_user_hard",
params={"email": new_user_record["email"]}
)
response.raise_for_status()
@pytest.mark.parametrize("new_user_record",[
pytest.param({ "first_name":"TEST",
"last_name":"TEST",
"middle_name":"TEST",
"email":f"TEST_{uuid4()}@d.d",
"plain_password":"Test1234!",
"direct_permissions":[],
"group":[]}, id="Positive_user_delete_soft"),
])
async def test_user_create_delete_soft_positive(self, new_user_record:dict, auth_fixture:MySession)->None:
with allure.step("Setting target_url"):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
with allure.step("Create new test user and check for the new user"):
response=await auth_fixture.post(f"{target_url}/user/create_user", json=new_user_record)
response.raise_for_status()
try:
with allure.step("Check for the new user"):
response = await auth_fixture.get(f"{target_url}/user/get_by_email",params={"email":new_user_record.get("email")})
response.raise_for_status()
with allure.step("Delete user soft"):
response=await auth_fixture.post(f"{target_url}/user/delete_user_soft", params={"email":new_user_record["email"]})
response.raise_for_status()
finally:
with allure.step("delete new user"):
response=await auth_fixture.post(f"{target_url}/user/delete_user_hard", params={"email":new_user_record["email"]})
response.raise_for_status()
@pytest.mark.parametrize("email, expected_status, ",[
pytest.param("Test", 404,id="Wrong_email")
])
async def test_user_delete_soft_negative(self, email:str,expected_status:int, auth_fixture:MySession)->None:
with allure.step("Delete user soft"):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
with pytest.raises(HTTPStatusError) as exc_info:
response=await auth_fixture.post(f"{target_url}/user/delete_user_soft", params={"email":email})
response.raise_for_status()
assert exc_info.value.response.status_code == expected_status
@pytest.mark.parametrize("email, expected_status, ",[
pytest.param("Test", 404,id="Wrong_email")
])
async def test_user_delete_hard_negative(self, email:str,expected_status:int, auth_fixture:MySession)->None:
with allure.step("Delete user hard"):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
with pytest.raises(HTTPStatusError) as exc_info:
response=await auth_fixture.post(f"{target_url}/user/delete_user_soft", params={"email":email})
response.raise_for_status()
assert exc_info.value.response.status_code == expected_status
@pytest.mark.parametrize("test_user_fixture", [
([], [])
], indirect=True)
@pytest.mark.parametrize("user_record_to_update",[
pytest.param({"first_name": "Test_New"},id="Positive_user_update_partially")
])
async def test_user_update_partially_positive(self, test_user_fixture, user_record_to_update:dict)->None:
session, new_user_record=test_user_fixture
with allure.step("Setting target_url"):
target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}"
with allure.step("Update user"):
response= await session.patch(f"{target_url}/user/patch_user", json=user_record_to_update, params={"email":new_user_record["email"]})
response.raise_for_status()
with allure.step("Check for the updated user"):
response=await session.get(f"{target_url}/user/get_by_email", params={"email":new_user_record["email"]})
response.raise_for_status()
response=response.json()
actual_permissions = [item.get("direct_permissions") for item in response.get("direct_permissions")] #unpacking json like {group:[{},{}]}
actual_groups =[item.get("group") for item in response.get("group")]
assert response.get("email")==new_user_record["email"]
assert response.get("first_name")==user_record_to_update["first_name"]
assert response.get("last_name")==new_user_record["last_name"]
assert response.get("middle_name")==new_user_record["middle_name"]
assert actual_permissions == new_user_record["direct_permissions"] or actual_permissions == [None]
assert actual_groups == new_user_record["group"] or actual_groups == [None]
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)
@pytest.mark.parametrize("test_user_fixture", [
([], [])
], indirect=True)
@pytest.mark.parametrize("user_record_to_update, expected_exception, expected_status",[
pytest.param({"plain_password": "Wrong_pass"},HTTPStatusError,422,id="Wrong_password"),
pytest.param({"email": "Wrong_email"},HTTPStatusError,422,id="Wrong_email"),
pytest.param({},HTTPStatusError, 400,id="Positive_user_update_nothing")
])
async def test_get_user_by_email_negative(self, email:str, expected_exception, auth_fixture:MySession)->None:
async def test_user_update_partially_negative(self, test_user_fixture, user_record_to_update:dict, expected_exception, expected_status:int)->None:
with allure.step("Get user by email"), pytest.raises(expected_exception):
session, new_user_record=test_user_fixture
with allure.step("Setting target_url"):
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})
with allure.step("Update user"), pytest.raises(expected_exception) as exc_info:
response= await session.patch(f"{target_url}/user/patch_user", json=user_record_to_update, params={"email":new_user_record["email"]})
response.raise_for_status()
assert exc_info.value.response.status_code == expected_status