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

Commit

Permalink
Homework 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve-Bupyc committed Sep 16, 2023
1 parent 3144a56 commit 2f72425
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 6 deletions.
93 changes: 89 additions & 4 deletions poetry.lock

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

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ dennis = "^1.1"
dump-env = "^1.3"
ipython = "^8.15"
import-linter = "^1.11"
faker = "^19.6.1"
factory-boy = "^3.3.0"
pytest-factoryboy = "^2.5.1"

[tool.poetry.group.docs]
optional = true
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

"plugins.django_settings",
"plugins.identity.user"
# TODO: add your own plugins here!
]
89 changes: 89 additions & 0 deletions tests/plugins/identity/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import factory
import pytest
from django.contrib.auth.hashers import make_password
from factory.base import FactoryMetaClass
from pytest_factoryboy import register

from server.apps.identity.models import User

locale = "RU_ru"


@register
class UserDataFactory(factory.DictFactory):
email = factory.Faker("email")
first_name = factory.Faker("first_name", locale=locale)
last_name = factory.Faker("last_name", locale=locale)
date_of_birth = factory.Faker("date_object")
address = factory.Faker("address", locale=locale)
job_title = factory.Faker("job", locale=locale)
phone = factory.Faker("phone_number")
password = make_password("pass")


@register
class UserUpdateDataFactory(factory.DictFactory):
first_name = factory.Faker("first_name", locale=locale)
last_name = factory.Faker("last_name", locale=locale)
date_of_birth = factory.Faker("date_object")
address = factory.Faker("address", locale=locale)
job_title = factory.Faker("job", locale=locale)
phone = factory.Faker("phone_number")


@pytest.fixture()
def registration_data(user_data_factory: FactoryMetaClass) -> dict:
data = user_data_factory()
data["password1"] = data["password"]
data["password2"] = data["password"]

return data


@pytest.fixture()
def expected_user_data() -> dict:
def factory(data: dict) -> dict:
return {
key_name: value_part
for key_name, value_part in data.items()
if not key_name.startswith("password")
}

return factory


@pytest.fixture()
def user(user_data_factory: FactoryMetaClass) -> User:
return User.objects.create_user(**user_data_factory())


@pytest.fixture(scope="session")
def assert_correct_user() -> None:
def factory(email: str, expected: dict) -> None:
user = User.objects.get(email=email)
# Special fields:
assert user.id
assert user.is_active
assert not user.is_superuser
assert not user.is_staff
# All other fields:
for field_name, data_value in expected.items():
assert getattr(user, field_name) == data_value

return factory


@pytest.fixture(scope="session")
def assert_correct_superuser() -> None:
def factory(email: str, expected: dict) -> None:
user = User.objects.get(email=email)
# Special fields:
assert user.id
assert user.is_active
assert user.is_superuser
assert user.is_staff
# All other fields:
for field_name, data_value in expected.items():
assert getattr(user, field_name) == data_value

return factory
10 changes: 10 additions & 0 deletions tests/test_apps/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
from django.test import Client

from server.apps.identity.models import User


@pytest.fixture()
def authenticated_client(client: Client, user: User) -> Client:
client.force_login(user)
return client
38 changes: 38 additions & 0 deletions tests/test_apps/test_identity/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest
from factory.base import FactoryMetaClass

from server.apps.identity.models import User

pytestmark = pytest.mark.django_db


def test_create_user(
user_data_factory: FactoryMetaClass, expected_user_data, assert_correct_user
) -> None:
user_data = user_data_factory()

User.objects.create_user(**user_data)

assert_correct_user(user_data["email"], expected_user_data(user_data))


def test_create_superuser(
user_data_factory: FactoryMetaClass, expected_user_data, assert_correct_superuser
) -> None:
user_data = user_data_factory()

User.objects.create_superuser(**user_data)

assert_correct_superuser(user_data["email"], expected_user_data(user_data))


def test_create_user_missing_email(
user_data_factory: FactoryMetaClass,
) -> None:
user_data = user_data_factory(email="")

with pytest.raises(ValueError) as excinfo:
User.objects.create_user(**user_data)

assert "Users must have an email address" in str(excinfo.value)
assert not User.objects.filter(email=user_data["email"]).exists()
66 changes: 66 additions & 0 deletions tests/test_apps/test_identity/views/test_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from http import HTTPStatus

import pytest
from django.test import Client
from django.urls import reverse
from factory.base import FactoryMetaClass

from server.apps.identity.models import User


@pytest.mark.django_db()
def test_login_page_renders(client: Client) -> None:
response = client.get(reverse("identity:login"))

assert response.status_code == HTTPStatus.OK
assert response.get("Content-Type") == "text/html; charset=utf-8"


@pytest.mark.django_db()
def test_invalid_login(client: Client, user: User) -> None:
response = client.post(
reverse("identity:login"),
data={"username": "user", "password": "pass"},
)

assert response.status_code == HTTPStatus.OK
assert response.get("Content-Type") == "text/html; charset=utf-8"


@pytest.mark.django_db()
def test_valid_login(client: Client, user: User) -> None:
response = client.post(
reverse("identity:login"),
data={"username": user.email, "password": "pass"},
)

# assert response.status_code == HTTPStatus.FOUND
# assert response.get("Location", "") == reverse("pictures:dashboard")


@pytest.mark.django_db()
def test_registration_missing_required_field(
client: Client, user_data_factory: FactoryMetaClass
) -> None:
post_data = user_data_factory(email="")
response = client.post(reverse("identity:registration"), data=user_data_factory())

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


@pytest.mark.django_db()
def test_valid_registration(
client: Client,
registration_data: dict,
expected_user_data: dict,
assert_correct_user,
) -> None:
response = client.post(reverse("identity:registration"), data=registration_data)

assert response.status_code == HTTPStatus.FOUND
assert response.get("Location", "") == reverse("identity:login")

assert_correct_user(
registration_data["email"], expected_user_data(registration_data)
)
Loading

0 comments on commit 2f72425

Please sign in to comment.