Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Add tests for a few django common templates
Browse files Browse the repository at this point in the history
  • Loading branch information
balancy committed Sep 15, 2023
1 parent a7cb840 commit 6b8f26a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
12 changes: 11 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import-linter = "^1.11"

# homework dependencies
mimesis = "^11.1.0"
httpretty = "^1.1.4"

[tool.poetry.group.docs]
optional = true
Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ per-file-ignores =
# Allow to have magic numbers and wrong module names inside migrations:
server/*/migrations/*.py: WPS102, WPS114, WPS432
# Tests have some more freedom:
tests/*.py: S101, WPS201, WPS202, WPS218, WPS226, WPS306, WPS428, WPS436, WPS442
tests/*.py: S101, WPS201, WPS202, WPS218, WPS226, WPS306, WPS428, WPS436, WPS442, WPS465


[isort]
Expand Down Expand Up @@ -115,7 +115,6 @@ omit =
# Is not reported, because is imported during setup:
server/settings/components/logging.py
server/common/django/templates/common/text/*.txt
server/common/django/templates/common/includes/footer.html

[mypy]
# Mypy configuration:
Expand Down
51 changes: 34 additions & 17 deletions tests/plugins/identity/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import asdict, dataclass
from random import SystemRandom
from typing import Callable, Protocol, TypeAlias
from typing import Callable, Protocol, TypeAlias, Unpack

import pytest
from mimesis.locales import Locale
Expand All @@ -9,8 +9,27 @@
from server.apps.identity.models import User


@pytest.fixture()
def faker_seed() -> int:
"""Returns seed for generating fake data."""
cryptogen = SystemRandom()
return cryptogen.randrange(100)


OptionalFields: TypeAlias = dict[str, str] | None


@dataclass
class AsDictMixin:
"""Mixin to convert dataclass to dictionary."""

def as_dict(self) -> dict[str, str]:
"""Represents user data in a dictionary form."""
return asdict(self)


@dataclass(slots=True)
class UserData:
class UserData(AsDictMixin):
"""Stores user essential data."""

email: str
Expand All @@ -28,10 +47,6 @@ class RegistrationData(UserData):
password1: str
password2: str

def as_dict(self) -> dict[str, str]:
"""Represents registration data in a dictionary form."""
return asdict(self)


class RegistrationDataFactory(Protocol):
"""Registration data factory protocol."""
Expand All @@ -41,22 +56,14 @@ def __call__(self) -> RegistrationData:
...


@pytest.fixture()
def faker_seed() -> int:
"""Returns seed for generating fake data."""
cryptogen = SystemRandom()
return cryptogen.randrange(100)


@pytest.fixture()
def registration_data_factory(faker_seed: int) -> RegistrationDataFactory:
"""Returns registration data with fake data."""

def factory(email: str | None = None) -> RegistrationData:
def factory(**fields: OptionalFields) -> RegistrationData:
fake = Field(locale=Locale.RU, seed=faker_seed)
password = fake('password')
if email is None:
email = fake('email')
email = str(fields.get('email', fake('email')))

return RegistrationData(
email=email,
Expand Down Expand Up @@ -99,7 +106,10 @@ def user_data(registration_data: RegistrationData) -> UserData:
def assert_correct_user() -> UserAssertion:
"""Asserts that user with given email exists and has expected data."""

def factory(email: str, expected: UserData) -> None:
def factory(
email: str,
expected: UserData,
) -> None:
user = User.objects.get(email=email)
assert user.is_active
assert not user.is_superuser
Expand All @@ -109,3 +119,10 @@ def factory(email: str, expected: UserData) -> None:
assert getattr(user, field_name) == field_value

return factory


@pytest.mark.django_db()
@pytest.fixture()
def created_new_user(user_data: UserData) -> User:
"""Creates a new user in the database."""
return User.objects.create(**asdict(user_data))
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,25 @@ def test_registration_missing_email_field(

assert response.status_code == HTTPStatus.OK
assert not User.objects.filter(email=post_data['email'])


@pytest.mark.django_db()
def test_valid_login(
client: Client,
created_new_user: User,
) -> None:
"""Tests that login works with correct user data."""
client.force_login(created_new_user)

response = client.get(reverse('index'))
assert 'Выход'.encode() in response.content


@pytest.mark.django_db()
def test_login_view_uses_login_template(client: Client) -> None:
"""Tests that login view uses correct template."""
response = client.get(reverse('identity:login'))

assert response.status_code == HTTPStatus.OK
assert 'Войти в личный кабинет'.encode() in response.content
assert 'Have fun!'.encode() in response.content

0 comments on commit 6b8f26a

Please sign in to comment.