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

Add tests for user preferences #950

wants to merge 2 commits into from

Conversation

klmp200
Copy link
Contributor

@klmp200 klmp200 commented Dec 16, 2024

No description provided.

Comment on lines +196 to +293
@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
)
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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants