diff --git a/configs/.e2e.env.example b/configs/.e2e.env.example new file mode 100644 index 0000000..d689630 --- /dev/null +++ b/configs/.e2e.env.example @@ -0,0 +1,4 @@ +HOST="change_me" +PORT="change_me" +TEST_USERNAME="change_me" +TEST_PASSWORD="change_me" \ No newline at end of file diff --git a/tests/e2e/test_users_crud.py b/tests/e2e/test_users_crud.py index d30ca64..6795bda 100644 --- a/tests/e2e/test_users_crud.py +++ b/tests/e2e/test_users_crud.py @@ -1,4 +1,3 @@ - from uuid import uuid4 import allure diff --git a/tests/integrated/conftest.py b/tests/integrated/conftest.py index 7d8a7d2..635927b 100644 --- a/tests/integrated/conftest.py +++ b/tests/integrated/conftest.py @@ -2,6 +2,7 @@ import pytest_asyncio from fastapi import Request from src.service.auth.auth import CurrentUserService +from src.service.users_crud.users_crud import CrudService @pytest_asyncio.fixture @@ -13,4 +14,9 @@ async def current_user_service()->CurrentUserService: async def requests(mocker): fake_request = mocker.MagicMock(spec=Request) fake_request.headers = {"user-agent": "pytest-agent", "x-forwarded-for":"127.0.0.1"} - return fake_request \ No newline at end of file + return fake_request + +@pytest_asyncio.fixture +async def crud_service()->CrudService: + crud_service=CrudService() + return crud_service \ No newline at end of file diff --git a/tests/integrated/test_auth.py b/tests/integrated/test_auth.py index d8efa94..46ec0a5 100644 --- a/tests/integrated/test_auth.py +++ b/tests/integrated/test_auth.py @@ -41,6 +41,8 @@ class TestAuth: assert test_result.email==user_data.email assert test_result.direct_permissions==user_data.direct_permissions assert test_result.group==user_data.group + assert not hasattr(test_result, "password") or not hasattr(test_result, "plain_password") or not hasattr(test_result, "hashed_password") + assert not hasattr(test_result, "status") @pytest.mark.parametrize("user_data, uuid, expected_exception,expected_status",[ diff --git a/tests/integrated/test_crud.py b/tests/integrated/test_crud.py new file mode 100644 index 0000000..48e887c --- /dev/null +++ b/tests/integrated/test_crud.py @@ -0,0 +1,16 @@ +from types import SimpleNamespace +from unittest.mock import AsyncMock + +import allure +import pytest + +from src.service.users_crud.users_crud import CrudService + + +@pytest.mark.integra +class TestCrud: + + async def test_create_user_positive(self, crud_service:CrudService, monkeypatch)->None: + + with allure.step("Patching functions"): + pass \ No newline at end of file diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 139597f..3ee8c0e 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -1,2 +1,9 @@ +import pytest_asyncio + +from src.service.users_crud.users_crud import CrudService +@pytest_asyncio.fixture +async def crud_service()->CrudService: + crud_service=CrudService() + return crud_service \ No newline at end of file diff --git a/tests/unit/test_crud.py b/tests/unit/test_crud.py new file mode 100644 index 0000000..6198808 --- /dev/null +++ b/tests/unit/test_crud.py @@ -0,0 +1,48 @@ +from types import SimpleNamespace +from unittest.mock import AsyncMock + +import allure +import pytest +from fastapi import HTTPException + +from src.service.users_crud.users_crud import CrudService + + +@pytest.mark.unit +class TestCrud: + + @pytest.mark.parametrize("user_data",[ + pytest.param(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True),id="Get_user_by_email_positive") + ]) + async def test_get_user_by_email_positive(self, monkeypatch, user_data:SimpleNamespace, crud_service:CrudService)->None: + + with allure.step("Patching functions"): + monkeypatch.setattr(crud_service.crud_db_actions, "get_user_by_email",AsyncMock(return_value=user_data)) + + with allure.step("Test get_by_email"): + test_result = await crud_service.get_user_by_email(user_data.email) + + assert test_result.email==user_data.email + assert test_result.first_name==user_data.first_name + assert test_result.last_name==user_data.last_name + assert test_result.middle_name==user_data.middle_name + assert test_result.email==user_data.email + assert test_result.email==user_data.email + assert not hasattr(test_result, "password") or not hasattr(test_result, "plain_password") or not hasattr(test_result, "hashed_password") + assert test_result.direct_permissions==user_data.direct_permissions + assert test_result.group==user_data.group + assert not hasattr(test_result, "status") + + + @pytest.mark.parametrize("email, expected_exception, expected_status",[ + pytest.param("Wrong_email", HTTPException, 404,id="Wrong_email"), + pytest.param("",HTTPException, 404,id="Empty_email"), + ]) + async def test_get_user_by_email_negative(self, email, crud_service:CrudService, expected_exception, expected_status:int)->None: + + with allure.step("Test get_by_email"), pytest.raises(expected_exception) as exc_info: + + await crud_service.get_user_by_email(email) + + if expected_exception is HTTPException: + assert exc_info.value.status_code==expected_status \ No newline at end of file