This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3144a56
commit 2f72425
Showing
8 changed files
with
335 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
Oops, something went wrong.