unit & interga tests
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
HOST="change_me"
|
||||||
|
PORT="change_me"
|
||||||
|
TEST_USERNAME="change_me"
|
||||||
|
TEST_PASSWORD="change_me"
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import allure
|
import allure
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import pytest_asyncio
|
|||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
|
|
||||||
from src.service.auth.auth import CurrentUserService
|
from src.service.auth.auth import CurrentUserService
|
||||||
|
from src.service.users_crud.users_crud import CrudService
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
@@ -13,4 +14,9 @@ async def current_user_service()->CurrentUserService:
|
|||||||
async def requests(mocker):
|
async def requests(mocker):
|
||||||
fake_request = mocker.MagicMock(spec=Request)
|
fake_request = mocker.MagicMock(spec=Request)
|
||||||
fake_request.headers = {"user-agent": "pytest-agent", "x-forwarded-for":"127.0.0.1"}
|
fake_request.headers = {"user-agent": "pytest-agent", "x-forwarded-for":"127.0.0.1"}
|
||||||
return fake_request
|
return fake_request
|
||||||
|
|
||||||
|
@pytest_asyncio.fixture
|
||||||
|
async def crud_service()->CrudService:
|
||||||
|
crud_service=CrudService()
|
||||||
|
return crud_service
|
||||||
@@ -41,6 +41,8 @@ class TestAuth:
|
|||||||
assert test_result.email==user_data.email
|
assert test_result.email==user_data.email
|
||||||
assert test_result.direct_permissions==user_data.direct_permissions
|
assert test_result.direct_permissions==user_data.direct_permissions
|
||||||
assert test_result.group==user_data.group
|
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",[
|
@pytest.mark.parametrize("user_data, uuid, expected_exception,expected_status",[
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user