Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for user preferences #950

Open
wants to merge 2 commits into
base: taiste
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions core/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

import pytest
from django.conf import settings
from django.core.cache import cache
from django.core.management import call_command
from django.test import Client, TestCase
from django.urls import reverse
from django.utils.timezone import now
from model_bakery import baker, seq
from model_bakery.recipe import Recipe, foreign_key

from club.models import Club, Membership
from core.baker_recipes import (
board_user,
old_subscriber_user,
subscriber_user,
very_old_subscriber_user,
)
from core.models import User
from counter.models import Counter, Refilling, Selling
from eboutic.models import Invoice, InvoiceItem
from trombi.models import Trombi, TrombiUser


class TestSearchUsers(TestCase):
Expand Down Expand Up @@ -187,3 +191,103 @@ def test_generate_username(first_name: str, last_name: str, expected: str):
new_user = User(first_name=first_name, last_name=last_name, email="[email protected]")
new_user.generate_username()
assert new_user.username == expected


@pytest.mark.django_db
class TestUserPreferences:
@pytest.fixture
def subscriber(self) -> User:
return subscriber_user.make()

@pytest.fixture
def other_subscriber(self) -> User:
return subscriber_user.make()

@pytest.fixture
def trombi_user(self) -> User:
user = subscriber_user.make()
club = baker.make(Club)
baker.make(
Membership,
club=club,
start_date=now() - timedelta(days=30),
role=settings.SITH_CLUB_ROLES_ID["Curious"],
user=user,
)
trombi = baker.make(Trombi, club=club)
baker.make(TrombiUser, user=user, trombi=trombi)
return user

@pytest.fixture
def non_subscriber(self) -> User:
return baker.make(User)

@pytest.fixture
def club_admin(self) -> User:
user = baker.make(User)
baker.make(
Membership,
start_date=now() - timedelta(days=30),
role=settings.SITH_CLUB_ROLES_ID["Board member"],
user=user,
)
return user

@pytest.fixture
def board_member(self) -> User:
return board_user.make()

@pytest.fixture
def admin(self) -> User:
return baker.make(User, is_superuser=True)

@pytest.mark.parametrize(
("tested_user", "accessing_user", "expected_code"),
[
("subscriber", None, 403), # Anonymous user
("subscriber", "non_subscriber", 403),
("subscriber", "club_admin", 403),
("subscriber", "other_subscriber", 403),
("subscriber", "trombi_user", 403),
("subscriber", "subscriber", 200),
("subscriber", "board_member", 200),
("subscriber", "admin", 200),
("non_subscriber", None, 403), # Anonymous user
("non_subscriber", "club_admin", 403),
("non_subscriber", "subscriber", 403),
("non_subscriber", "other_subscriber", 403),
("non_subscriber", "trombi_user", 403),
("non_subscriber", "non_subscriber", 200),
("non_subscriber", "board_member", 200),
("non_subscriber", "admin", 200),
("trombi_user", None, 403), # Anonymous user
("trombi_user", "club_admin", 403),
("trombi_user", "subscriber", 403),
("trombi_user", "other_subscriber", 403),
("trombi_user", "non_subscriber", 403),
("trombi_user", "trombi_user", 200),
("trombi_user", "board_member", 200),
("trombi_user", "admin", 200),
],
)
@pytest.mark.django_db
def test_user_preferences_access(
self,
client: Client,
request: pytest.FixtureRequest,
tested_user: str,
accessing_user: str | None,
expected_code: int,
):
cache.clear()
if accessing_user is not None:
client.force_login(request.getfixturevalue(accessing_user))
assert (
client.get(
reverse(
"core:user_prefs",
kwargs={"user_id": request.getfixturevalue(tested_user).pk},
)
).status_code
== expected_code
)
Comment on lines +196 to +293
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu utilises un seul test pour tester trois choses distinctes (accès à ses préférences, accès aux préférences d'un autre utilisateur, accès aux préférence d'un utilisateur appartenant à un trombi).

Ca fait que tu balances pas mal de boilerplate et de techniques qui devraient relever plus de l'exception que de la manière générale d'écrire des tests. Au final, ça fait pas mal de code pour quelque chose dont on ne comprend pas exactement ce que c'est censé tester.

Tu ne veux pas découper ça en trois tests distincts et un peu plus simples ?

Loading