diff --git a/tests/conftest.py b/tests/conftest.py index 66d530cc..dbcc257b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,5 +10,7 @@ # Should be the first custom one: 'plugins.django_settings', # TODO: add your own plugins here! + 'plugins.common', 'plugins.identity.user', + 'plugins.pictures.favourite_picture', ] diff --git a/tests/plugins/common.py b/tests/plugins/common.py new file mode 100644 index 00000000..86849061 --- /dev/null +++ b/tests/plugins/common.py @@ -0,0 +1,17 @@ +from random import SystemRandom + +import pytest +from mimesis import Field, Locale + + +@pytest.fixture() +def faker_seed() -> int: + """Returns seed for generating fake data.""" + cryptogen = SystemRandom() + return cryptogen.randrange(100) + + +@pytest.fixture() +def fake(faker_seed: int) -> Field: + """Returns mimesis field.""" + return Field(locale=Locale.RU, seed=faker_seed) diff --git a/tests/plugins/identity/user.py b/tests/plugins/identity/user.py index 9d55fa4c..5ca64ca3 100644 --- a/tests/plugins/identity/user.py +++ b/tests/plugins/identity/user.py @@ -1,36 +1,17 @@ from dataclasses import asdict, dataclass -from random import SystemRandom -from typing import Callable, Protocol, TypeAlias, Unpack +from typing import Callable, Protocol, TypeAlias import pytest -from mimesis.locales import Locale from mimesis.schema import Field 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(AsDictMixin): - """Stores user essential data.""" +class UserData: + """User essential data model.""" email: str first_name: str @@ -39,10 +20,14 @@ class UserData(AsDictMixin): address: str phone: str + def as_dict(self) -> dict[str, str]: + """Represents user data in a dictionary form.""" + return asdict(self) + @dataclass(slots=True) class RegistrationData(UserData): - """Stores user essential data with passwords for registration.""" + """User essential data with passwords for registration model.""" password1: str password2: str @@ -57,11 +42,10 @@ def __call__(self) -> RegistrationData: @pytest.fixture() -def registration_data_factory(faker_seed: int) -> RegistrationDataFactory: +def registration_data_factory(fake: Field) -> RegistrationDataFactory: """Returns registration data with fake data.""" def factory(**fields: OptionalFields) -> RegistrationData: - fake = Field(locale=Locale.RU, seed=faker_seed) password = fake('password') email = str(fields.get('email', fake('email'))) diff --git a/tests/plugins/pictures/.gitkeep b/tests/plugins/pictures/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/plugins/pictures/favourite_picture.py b/tests/plugins/pictures/favourite_picture.py new file mode 100644 index 00000000..8c373a24 --- /dev/null +++ b/tests/plugins/pictures/favourite_picture.py @@ -0,0 +1,45 @@ +from dataclasses import asdict, dataclass + +import pytest +from mimesis import Field + +from server.apps.identity.models import User +from server.apps.pictures.models import FavouritePicture + + +@dataclass(slots=True) +class FavouritePictureData: + """Essential favourite picture data model.""" + + foreign_id: int + url: str + + def as_dict(self) -> dict[str, str | int]: + """Return as a dictionary.""" + return asdict(self) + + +@pytest.fixture() +def picture_data(fake: Field) -> FavouritePictureData: + """Returns favourite picture data.""" + return FavouritePictureData( + foreign_id=fake('numeric.increment'), + url=fake('internet.uri'), + ) + + +@pytest.mark.django_db() +@pytest.fixture() +def created_fav_picture( + created_new_user: User, + picture_data: 'FavouritePictureData', +) -> FavouritePicture: + """Create new favourite picture.""" + created_picture = FavouritePicture.objects.create( + user=created_new_user, + **picture_data.as_dict(), + ) + assert str(created_picture).startswith( + ' 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 diff --git a/tests/test_apps/test_identity/test_user.py b/tests/test_apps/test_identity/test_registration.py similarity index 67% rename from tests/test_apps/test_identity/test_user.py rename to tests/test_apps/test_identity/test_registration.py index e07e7faa..bca7baa2 100644 --- a/tests/test_apps/test_identity/test_user.py +++ b/tests/test_apps/test_identity/test_registration.py @@ -52,25 +52,3 @@ 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 diff --git a/tests/test_apps/test_pictures/.gitkeep b/tests/test_apps/test_pictures/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/test_apps/test_pictures/test_favourites.py b/tests/test_apps/test_pictures/test_favourites.py new file mode 100644 index 00000000..3f748888 --- /dev/null +++ b/tests/test_apps/test_pictures/test_favourites.py @@ -0,0 +1,49 @@ +import pytest +from django.test import Client +from django.urls import reverse + +from server.apps.identity.models import User +from server.apps.pictures.logic.usecases.favourites_list import FavouritesList +from server.apps.pictures.models import FavouritePicture + + +@pytest.mark.django_db() +def test_fetch_favourite_picture_if_exists( + created_new_user: User, + created_fav_picture: FavouritePicture, +) -> None: + """Test getting correct favourite pictures.""" + favourite_pictures = FavouritesList() + user_fav_pictures = favourite_pictures(user_id=created_new_user.id) + + assert len(user_fav_pictures) == 1 + assert created_fav_picture in user_fav_pictures + + +@pytest.mark.django_db() +def test_logged_in_user_access_dashboard( + created_new_user: User, + client: Client, +) -> None: + """Test that logged in user can access all views.""" + client.force_login(created_new_user) + + response = client.get(reverse('pictures:dashboard')) + assert 'Профиль'.encode() in response.content + + +@pytest.mark.django_db() +def test_logged_in_user_access_favourite_pictures( + created_new_user: User, + client: Client, + created_fav_picture: FavouritePicture, +) -> None: + """Test that logged in user can access all views.""" + client.force_login(created_new_user) + + response = client.get(reverse('pictures:favourites')) + assert 'Список любимых картинок'.encode() in response.content + assert ( + 'Номер {0}'.format(created_fav_picture.foreign_id).encode() + in response.content + )