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

Commit

Permalink
Updates to lesson 1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKupreev committed Sep 23, 2023
1 parent a010229 commit 1ec0e1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
image: "postgres:15-alpine"
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- webnet
- postgresnet
Expand Down
14 changes: 14 additions & 0 deletions tests/plugins/identity/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest
from django.contrib.auth import get_user_model
from django.test import Client
from django.contrib.auth.models import User
from mimesis.locales import Locale
from mimesis.schema import Field, Schema

Expand Down Expand Up @@ -85,3 +87,15 @@ def factory(email: str, expected: ProfileData) -> None:
assert not all(matches)

return factory


@pytest.fixture(scope='function')
def logged_user_client(client: Client, django_user_model: User):
"""Client for a logged in user."""
password, email = 'password', '[email protected]'
user = django_user_model.objects.create_user(
email,
password,
)
client.force_login(user)
return client
51 changes: 23 additions & 28 deletions tests/test_server/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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
from plugins.identity.user import (
ProfileAssertion,
ProfileDataFactory,
logged_user_client
)


@pytest.mark.django_db()()
Expand Down Expand Up @@ -45,57 +48,49 @@ def test_admin_docs_authorized(admin_client: Client) -> None:
assert b'docutils' not in response.content


def test_picture_pages_unauthorized(client: Client) -> None:
@pytest.mark.parametrize("url_found", [
'/pictures/dashboard',
'/pictures/favourites'
])
def test_picture_pages_unauthorized(client: Client, url_found: str) -> 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')
response = client.get(url_found)
assert response.status_code == HTTPStatus.FOUND


@pytest.mark.django_db()
@pytest.mark.parametrize("url_accessible", [
'/pictures/dashboard',
'/pictures/favourites'
])
def test_picture_pages_authorized(
client: Client,
django_user_model: User,
logged_user_client: Client,
url_accessible: str
) -> 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')
response = logged_user_client.get(url_accessible)
assert response.status_code == HTTPStatus.OK


@pytest.mark.django_db()
def test_profile_update_authorized(
client: Client,
django_user_model: User,
logged_user_client: Client,
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)
# that is an email for `logged_user_client` fixture
# maybe add indirect parametrization?
email = '[email protected]'

# there might be a probability of accidental match, but disregard it for now
assert_incorrect_profile(email, user_data)

response = client.post(
response = logged_user_client.post(
'/identity/update',
data=user_data,
)
Expand Down

0 comments on commit 1ec0e1f

Please sign in to comment.