mirror of
https://github.com/ae-utbm/sith.git
synced 2026-10-23 10:49:31 +00:00
add tests
This commit is contained in:
1 parent
3312a6214e
commit
2c77ed3c54
2 files changed
+58
-10
No files matched your search
+1
-1
@@ -238,7 +238,7 @@ class Club(models.Model):
|
|||||||
|
|
||||||
def can_be_edited_by(self, user: User) -> bool:
|
def can_be_edited_by(self, user: User) -> bool:
|
||||||
"""Method to see if that object can be edited by the given user."""
|
"""Method to see if that object can be edited by the given user."""
|
||||||
return self.has_rights_in_club(user)
|
return user.has_perm("club.change_club") or self.has_rights_in_club(user)
|
||||||
|
|
||||||
def can_roles_be_edited_by(self, user: User) -> bool:
|
def can_roles_be_edited_by(self, user: User) -> bool:
|
||||||
"""Return True if the given user can edit the roles of this club"""
|
"""Return True if the given user can edit the roles of this club"""
|
||||||
|
|||||||
+57
-9
@@ -1,8 +1,9 @@
|
|||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Callable
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from aemark import markdown
|
from aemark import markdown
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from django.contrib.auth.models import Permission
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from model_bakery import baker
|
from model_bakery import baker
|
||||||
from pytest_django.asserts import assertHTMLEqual, assertRedirects
|
from pytest_django.asserts import assertHTMLEqual, assertRedirects
|
||||||
@@ -16,13 +17,20 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_page_display_on_club_main_page(client: Client):
|
@pytest.mark.parametrize("user_factory", [lambda: None, subscriber_user.make])
|
||||||
|
def test_page_display_on_club_main_page(
|
||||||
|
client: Client, user_factory: Callable[[], User | None]
|
||||||
|
):
|
||||||
"""Test the club Page is properly displayed on the club main view"""
|
"""Test the club Page is properly displayed on the club main view"""
|
||||||
club = baker.make(Club)
|
club = baker.make(Club)
|
||||||
|
# create another previous revision, to test that we display the most recent one
|
||||||
|
baker.make(PageRev, page=club.page, revision=1)
|
||||||
content = "# foo\nLorem ipsum dolor sit amet"
|
content = "# foo\nLorem ipsum dolor sit amet"
|
||||||
baker.make(PageRev, page=club.page, revision=1, content=content)
|
baker.make(PageRev, page=club.page, revision=2, content=content)
|
||||||
res = client.get(reverse("club:club_view", kwargs={"club_id": club.id}))
|
|
||||||
|
|
||||||
|
if user := user_factory():
|
||||||
|
client.force_login(user)
|
||||||
|
res = client.get(reverse("club:club_view", kwargs={"club_id": club.id}))
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
soup = BeautifulSoup(res.text, "lxml")
|
soup = BeautifulSoup(res.text, "lxml")
|
||||||
detail_html = soup.find(id="club-page").find(class_="markdown")
|
detail_html = soup.find(id="club-page").find(class_="markdown")
|
||||||
@@ -30,20 +38,60 @@ def test_page_display_on_club_main_page(client: Client):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_club_main_page_without_content(client: Client):
|
@pytest.mark.parametrize(
|
||||||
"""Test the club view works, even if the club page is empty"""
|
("user_factory", "can_view_tabs", "can_view_edit_button"),
|
||||||
|
[
|
||||||
|
(lambda _: None, False, False),
|
||||||
|
(lambda _: baker.make(User), False, False),
|
||||||
|
(lambda _: subscriber_user.make(), True, False),
|
||||||
|
(
|
||||||
|
# admin user
|
||||||
|
lambda _: subscriber_user.make(
|
||||||
|
user_permissions=[Permission.objects.get(codename="change_club")]
|
||||||
|
),
|
||||||
|
True,
|
||||||
|
True,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
# club board member
|
||||||
|
lambda club: (
|
||||||
|
baker.make(
|
||||||
|
Membership,
|
||||||
|
club=club,
|
||||||
|
role=baker.make(ClubRole, is_board=True),
|
||||||
|
user=subscriber_user.make(),
|
||||||
|
).user
|
||||||
|
),
|
||||||
|
True,
|
||||||
|
True,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
ids=["anonymous", "public", "subscriber", "admin", "club board"],
|
||||||
|
)
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_club_main_page_displayed_elements(
|
||||||
|
client: Client, user_factory, can_view_tabs, can_view_edit_button
|
||||||
|
):
|
||||||
|
"""Test that the appropriate elements are displayed depending on user status."""
|
||||||
club = baker.make(Club)
|
club = baker.make(Club)
|
||||||
club.page.revisions.all().delete()
|
if user := user_factory(club):
|
||||||
|
client.force_login(user)
|
||||||
res = client.get(reverse("club:club_view", kwargs={"club_id": club.id}))
|
res = client.get(reverse("club:club_view", kwargs={"club_id": club.id}))
|
||||||
|
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
soup = BeautifulSoup(res.text, "lxml")
|
soup = BeautifulSoup(res.text, "lxml")
|
||||||
detail_html = soup.find(id="club-page")
|
tabs = soup.find(class_="tool_bar")
|
||||||
assert detail_html.find_all("markdown") == []
|
assert tabs is not None if can_view_tabs else tabs is None
|
||||||
|
edit_btn = soup.find(
|
||||||
|
"a",
|
||||||
|
href=reverse("core:page_edit", kwargs={"page_name": club.page.get_full_name()}),
|
||||||
|
)
|
||||||
|
assert edit_btn is not None if can_view_edit_button else edit_btn is None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_page_revision(client: Client):
|
def test_page_revision(client: Client):
|
||||||
|
"""Test that accessing a previous revision of the club page works."""
|
||||||
club = baker.make(Club)
|
club = baker.make(Club)
|
||||||
revisions = baker.make(
|
revisions = baker.make(
|
||||||
PageRev, page=club.page, _quantity=3, content=iter(["foo", "bar", "baz"])
|
PageRev, page=club.page, _quantity=3, content=iter(["foo", "bar", "baz"])
|
||||||
|
|||||||
Reference in new issue
Block a user