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

Commit 1ec0e1f

Browse files
committed
Updates to lesson 1 tests
1 parent a010229 commit 1ec0e1f

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
image: "postgres:15-alpine"
1414
volumes:
1515
- pgdata:/var/lib/postgresql/data
16+
ports:
17+
- "5432:5432"
1618
networks:
1719
- webnet
1820
- postgresnet

tests/plugins/identity/user.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66
from django.contrib.auth import get_user_model
7+
from django.test import Client
8+
from django.contrib.auth.models import User
79
from mimesis.locales import Locale
810
from mimesis.schema import Field, Schema
911

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

8789
return factory
90+
91+
92+
@pytest.fixture(scope='function')
93+
def logged_user_client(client: Client, django_user_model: User):
94+
"""Client for a logged in user."""
95+
password, email = 'password', '[email protected]'
96+
user = django_user_model.objects.create_user(
97+
email,
98+
password,
99+
)
100+
client.force_login(user)
101+
return client

tests/test_server/test_urls.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from http import HTTPStatus
22

33
import pytest
4-
from django.contrib.auth.models import User
54
from django.test import Client
6-
from plugins.identity.user import ProfileAssertion, ProfileDataFactory
5+
from plugins.identity.user import (
6+
ProfileAssertion,
7+
ProfileDataFactory,
8+
logged_user_client
9+
)
710

811

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

4750

48-
def test_picture_pages_unauthorized(client: Client) -> None:
51+
@pytest.mark.parametrize("url_found", [
52+
'/pictures/dashboard',
53+
'/pictures/favourites'
54+
])
55+
def test_picture_pages_unauthorized(client: Client, url_found: str) -> None:
4956
"""This test ensures that picture management pages require auth."""
50-
response = client.get('/pictures/dashboard')
51-
assert response.status_code == HTTPStatus.FOUND
52-
53-
response = client.get('/pictures/favourites')
57+
response = client.get(url_found)
5458
assert response.status_code == HTTPStatus.FOUND
5559

5660

5761
@pytest.mark.django_db()
62+
@pytest.mark.parametrize("url_accessible", [
63+
'/pictures/dashboard',
64+
'/pictures/favourites'
65+
])
5866
def test_picture_pages_authorized(
59-
client: Client,
60-
django_user_model: User,
67+
logged_user_client: Client,
68+
url_accessible: str
6169
) -> None:
6270
"""Ensures picture management pages are accessible for authorized user."""
63-
password, email = 'password', '[email protected]'
64-
user = django_user_model.objects.create_user(
65-
email,
66-
password,
67-
)
68-
client.force_login(user)
69-
70-
response = client.get('/pictures/dashboard')
71-
assert response.status_code == HTTPStatus.OK
7271

73-
response = client.get('/pictures/favourites')
72+
response = logged_user_client.get(url_accessible)
7473
assert response.status_code == HTTPStatus.OK
7574

7675

7776
@pytest.mark.django_db()
7877
def test_profile_update_authorized(
79-
client: Client,
80-
django_user_model: User,
78+
logged_user_client: Client,
8179
profile_data_factory: 'ProfileDataFactory',
8280
assert_correct_profile: 'ProfileAssertion',
8381
assert_incorrect_profile: 'ProfileAssertion',
8482
) -> None:
8583
"""This test ensures profile updating for an authorized user."""
8684
user_data = profile_data_factory()
8785

88-
password, email = 'password', '[email protected]'
89-
user = django_user_model.objects.create_user(
90-
email,
91-
password,
92-
)
93-
client.force_login(user)
86+
# that is an email for `logged_user_client` fixture
87+
# maybe add indirect parametrization?
88+
9489

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

98-
response = client.post(
93+
response = logged_user_client.post(
9994
'/identity/update',
10095
data=user_data,
10196
)

0 commit comments

Comments
 (0)