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
be3f28d
commit 108668f
Showing
8 changed files
with
270 additions
and
95 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
File renamed without changes.
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
Empty file.
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,91 @@ | ||
"""Identity data related plugins.""" | ||
from datetime import datetime | ||
from typing import Callable, Protocol, TypeAlias, TypedDict, Unpack, final | ||
|
||
import pytest | ||
from django.contrib.auth import get_user_model | ||
from mimesis.locales import Locale | ||
from mimesis.schema import Field, Schema | ||
|
||
|
||
@final | ||
class ProfileData(TypedDict, total=False): | ||
"""Represent the simplified profile data.""" | ||
|
||
first_name: str | ||
last_name: str | ||
date_of_birth: datetime | ||
address: str | ||
job_title: str | ||
phone: str | ||
|
||
|
||
ProfileAssertion: TypeAlias = Callable[[str, ProfileData], None] | ||
|
||
|
||
@final | ||
class ProfileDataFactory(Protocol): | ||
"""Factory for representation of the simplified profile data.""" | ||
|
||
def __call__( | ||
self, | ||
**fields: Unpack[ProfileData], | ||
) -> ProfileData: | ||
"""User data factory protocol.""" | ||
|
||
|
||
@pytest.fixture() | ||
def profile_data_factory( | ||
faker_seed: int, | ||
) -> ProfileDataFactory: | ||
"""Returns factory for fake random profile data.""" | ||
|
||
def factory(**fields: Unpack[ProfileData]) -> ProfileData: | ||
mf = Field(locale=Locale.EN, seed=faker_seed) | ||
schema = Schema( | ||
schema=lambda: { | ||
'first_name': mf('person.first_name'), | ||
'last_name': mf('person.last_name'), | ||
'date_of_birth': mf('datetime.date'), | ||
'address': mf('address.city'), | ||
'job_title': mf('person.occupation'), | ||
'phone': mf('person.telephone'), | ||
}, | ||
iterations=1, | ||
) | ||
return { | ||
**schema.create()[0], # type: ignore[misc] | ||
**fields, | ||
} | ||
|
||
return factory | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def assert_correct_profile() -> ProfileAssertion: | ||
"""All profile fields are equal to reference.""" | ||
|
||
def factory(email: str, expected: ProfileData) -> None: | ||
user = get_user_model().objects.get(email=email) | ||
assert user.id | ||
assert user.is_active | ||
for field_name, data_value in expected.items(): | ||
assert getattr(user, field_name) == data_value | ||
return factory | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def assert_incorrect_profile() -> ProfileAssertion: | ||
"""At least one field does not match.""" | ||
|
||
def factory(email: str, expected: ProfileData) -> None: | ||
user = get_user_model().objects.get(email=email) | ||
assert user.id | ||
assert user.is_active | ||
matches = [] | ||
for field_name, data_value in expected.items(): | ||
matches.append(getattr(user, field_name) == data_value) | ||
|
||
assert not all(matches) | ||
|
||
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
from django.contrib.auth.models import User | ||
from django.test import Client | ||
from plugins.identity.user import ProfileAssertion, ProfileDataFactory | ||
|
||
|
||
@pytest.mark.django_db()() | ||
|
@@ -43,6 +45,65 @@ def test_admin_docs_authorized(admin_client: Client) -> None: | |
assert b'docutils' not in response.content | ||
|
||
|
||
def test_picture_pages_unauthorized(client: Client) -> None: | ||
"""This test ensures that picture management pages require auth.""" | ||
response = client.get('/pictures/dashboard') | ||
assert response.status_code == HTTPStatus.FOUND | ||
|
||
response = client.get('/pictures/favourites') | ||
assert response.status_code == HTTPStatus.FOUND | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_picture_pages_authorized( | ||
client: Client, | ||
django_user_model: User, | ||
) -> None: | ||
"""Ensures picture management pages are accessible for authorized user.""" | ||
password, email = 'password', '[email protected]' | ||
user = django_user_model.objects.create_user( | ||
email, | ||
password, | ||
) | ||
client.force_login(user) | ||
|
||
response = client.get('/pictures/dashboard') | ||
assert response.status_code == HTTPStatus.OK | ||
|
||
response = client.get('/pictures/favourites') | ||
assert response.status_code == HTTPStatus.OK | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_profile_update_authorized( | ||
client: Client, | ||
django_user_model: User, | ||
profile_data_factory: 'ProfileDataFactory', | ||
assert_correct_profile: 'ProfileAssertion', | ||
assert_incorrect_profile: 'ProfileAssertion', | ||
) -> None: | ||
"""This test ensures profile updating for an authorized user.""" | ||
user_data = profile_data_factory() | ||
|
||
password, email = 'password', '[email protected]' | ||
user = django_user_model.objects.create_user( | ||
email, | ||
password, | ||
) | ||
client.force_login(user) | ||
|
||
# there might be a probability of accidental match, but disregard it for now | ||
assert_incorrect_profile(email, user_data) | ||
|
||
response = client.post( | ||
'/identity/update', | ||
data=user_data, | ||
) | ||
assert response.status_code == HTTPStatus.FOUND | ||
assert response.get('Location') == '/identity/update' | ||
assert_correct_profile(email, user_data) | ||
|
||
|
||
@pytest.mark.parametrize('page', [ | ||
'/robots.txt', | ||
'/humans.txt', | ||
|