Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ dennis = "^1.1"
dump-env = "^1.3"
ipython = "^8.15"
import-linter = "^1.11"
factory-boy = "^3.3.0"
httpretty = "^1.1.4"

[tool.poetry.group.docs]
optional = true
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ filterwarnings =
# but, we want to list them here:
ignore::DeprecationWarning:password_reset.*:
ignore::DeprecationWarning:pytest_freezegun:
# update pydantic
ignore::DeprecationWarning


[coverage:run]
Expand All @@ -113,6 +115,9 @@ plugins =
omit =
# Is not reported, because is imported during setup:
server/settings/components/logging.py
server/apps/identity/templates/*
server/apps/pictures/templates/*
server/common/django/templates/*


[mypy]
Expand Down
26 changes: 26 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@
1. https://docs.python.org/3/library/doctest.html
2. https://docs.pytest.org/en/latest/doctest.html
"""
from typing import TYPE_CHECKING

import pytest

from server.apps.pictures.container import container
from server.common.django.types import Settings

if TYPE_CHECKING:
from django.test import Client

from tests.plugins.identity.user_factory import UserFactory

pytest_plugins = [
# Should be the first custom one:
'plugins.django_settings',
'plugins.identity.user_factory',
'plugins.identity.mock_http',

# TODO: add your own plugins here!
]


@pytest.fixture()
def settings() -> Settings:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Плагин pytest-django предоставляет фикстуру settings, которая возвращает настройки, поэтому по идее такая фикстура не нужна

"""Django settings."""
return container.resolve(Settings)


@pytest.fixture()
def authorized_user(client: 'Client', user: 'UserFactory') -> 'UserFactory':
"""Authorized_user user."""
client.force_login(user)
return user
Empty file removed tests/plugins/identity/.gitkeep
Empty file.
43 changes: 43 additions & 0 deletions tests/plugins/identity/mock_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
from typing import Iterator
from urllib.parse import urljoin

import factory
import httpretty
import pytest

from server.apps.identity.intrastructure.services.placeholder import (
UserResponse,
)
from server.common.django.types import Settings


class UserResponseFactory(factory.Factory):
"""User response factory for create lead."""

id = factory.Sequence(int)

class Meta(object):
model = UserResponse


@pytest.fixture()
def create_lead_api_user_response() -> UserResponseFactory:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Фикстуру если она просто что-то возвращает лучше назвать без глагола, то есть lead_api_user_response

"""Generate random user response data."""
return UserResponseFactory.create()


@pytest.fixture()
def mock_api_create_lead(
create_lead_api_user_response: UserResponse,
settings: Settings,
) -> Iterator[UserResponse]:
"""Mock external `/users` calls."""
with httpretty.httprettized():
httpretty.register_uri(
method=httpretty.POST,
body=json.dumps(create_lead_api_user_response.model_dump()),
uri=urljoin(settings.PLACEHOLDER_API_URL, 'users'),
)
yield create_lead_api_user_response
assert httpretty.has_request()
47 changes: 47 additions & 0 deletions tests/plugins/identity/user_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Any, Callable, TypeAlias

import factory
import pytest

from server.apps.identity.models import User


class UserFactory(factory.django.DjangoModelFactory):
"""Factory model User."""

class Meta(object):
"""Indication Django model."""

model = User

class Params(object): # noqa: WPS110
superuser = factory.Trait(
is_superuser=True,
is_staff=True,
)
enabled = True

email = factory.Faker('email')
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
date_of_birth = factory.Faker('date_object')
address = factory.Faker('address')
job_title = factory.Faker('job')
phone = factory.Faker('phone_number')


UserFactoryType: TypeAlias = Callable[[], Callable[[], UserFactory]]


@pytest.fixture()
def user_model_factory() -> UserFactoryType:
"""Returns factory for fake random data for user model."""
def factory(**fields: dict[str, Any]) -> Callable[[], UserFactory]:
return UserFactory.create(**fields)
return factory


@pytest.fixture()
def user(user_model_factory: UserFactory) -> UserFactory:
"""Model User."""
return user_model_factory()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import TYPE_CHECKING

import pytest

from server.apps.identity.container import container
from server.apps.identity.logic.usecases.user_create_new import UserCreateNew

if TYPE_CHECKING:
from server.apps.identity.intrastructure.services.placeholder import (
UserResponse,
)
from tests.plugins.identity.user_factory import UserFactory

pytestmark = pytest.mark.django_db


def test_user_create_new(
user: 'UserFactory',
mock_api_create_lead: 'UserResponse',
) -> None:
"""Test create user in external service."""
user_create_new = container.instantiate(UserCreateNew)
user_create_new(user)

user.refresh_from_db()
assert user.lead_id is not None
Loading