This repository was archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Lesson1 updates #68
Open
AlexKupreev
wants to merge
2
commits into
tough-dev-school:master
Choose a base branch
from
AlexKupreev:lesson1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lesson1 updates #68
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,87 @@ | ||
| """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] | ||
|
|
||
|
|
||
| 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[typeddict-item] | ||
| **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 hidden or 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', | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Минорный момент: лучше не продуктовые зависимости добавлять в
devгруппу.Например, это можно сделать командой
poetry add --group dev xxxили вручную переставить в файле.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
оно вроде и есть в dev-зависимостях?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Точно, проглядел.
My bad :(