Skip to content

Commit

Permalink
show link to documentation:category_update for superuser only in docu…
Browse files Browse the repository at this point in the history
…mentation:category_detail
  • Loading branch information
vincentporte committed Sep 25, 2024
1 parent 0fee9d9 commit 7e484e8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lacommunaute/documentation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __str__(self):
def get_absolute_url(self):
return reverse("documentation:category_detail", kwargs={"pk": self.pk, "slug": self.slug})

def get_update_url(self):
return reverse("documentation:category_update", kwargs={"pk": self.pk, "slug": self.slug})


class Document(Publication):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="documents")
Expand Down
13 changes: 13 additions & 0 deletions lacommunaute/documentation/tests/tests_category_detail_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest # noqa

from lacommunaute.documentation.factories import CategoryFactory, DocumentFactory
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.testing import parse_response_to_soup


Expand All @@ -24,3 +25,15 @@ def test_category_detail_view_with_tagged_documents(client, db, category, active
assert response.status_code == 200
content = parse_response_to_soup(response, selector="main", replace_img_src=True, replace_in_href=[category])
assert str(content) == snapshot(name=snapshot_name)


@pytest.mark.parametrize(
"user,link_is_visible",
[(None, False), (lambda: UserFactory(), False), (lambda: UserFactory(is_superuser=True), True)],
)
def test_link_to_update_view(client, db, url, category, user, link_is_visible):
if user:
client.force_login(user())
response = client.get(url)
assert response.status_code == 200
assert bool(category.get_update_url() in response.content.decode()) == link_is_visible
17 changes: 6 additions & 11 deletions lacommunaute/documentation/tests/tests_category_update_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ def fixture_category(db):
return CategoryFactory(for_snapshot=True)


@pytest.fixture(name="url")
def fixture_url(category):
return reverse("documentation:category_update", kwargs={"slug": category.slug, "pk": category.pk})


@pytest.fixture(name="superuser")
def fixture_superuser(db):
return UserFactory(is_superuser=True)
Expand All @@ -25,10 +20,10 @@ def fixture_superuser(db):
@pytest.mark.parametrize(
"user,status_code", [(None, 302), (lambda: UserFactory(), 403), (lambda: UserFactory(is_superuser=True), 200)]
)
def test_user_passes_test_mixin(client, db, url, user, status_code):
def test_user_passes_test_mixin(client, db, category, user, status_code):
if user:
client.force_login(user())
response = client.get(url)
response = client.get(category.get_update_url())
assert response.status_code == status_code


Expand All @@ -40,9 +35,9 @@ def fixture_expected_context(category):
}


def test_view(client, db, url, superuser, expected_context):
def test_view(client, db, category, superuser, expected_context):
client.force_login(superuser)
response = client.get(url)
response = client.get(category.get_update_url())
assert response.status_code == 200
assert {k: response.context[k] for k in expected_context.keys()} == expected_context
assert response.context["form"].fields.keys() == {"name", "short_description", "description", "image"}
Expand All @@ -67,9 +62,9 @@ def fixture_expected_values(post_data):
}


def test_update(client, db, url, superuser, post_data, expected_values):
def test_update(client, db, category, superuser, post_data, expected_values):
client.force_login(superuser)
response = client.post(url, post_data)
response = client.post(category.get_update_url(), post_data)
assert response.status_code == 302
category = Category.objects.get(slug=expected_values["slug"])
assert {k: getattr(category, k) for k in expected_values.keys()} == expected_values
4 changes: 4 additions & 0 deletions lacommunaute/documentation/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def test_get_absolute_url(self, db):
category = CategoryFactory()
assert category.get_absolute_url() == f"/documentation/{category.slug}-{category.pk}/"

def test_get_update_url(self, db):
category = CategoryFactory()
assert category.get_update_url() == f"/documentation/{category.slug}-{category.pk}/update/"


class TestDocument:
def test_slug(self, db):
Expand Down

0 comments on commit 7e484e8

Please sign in to comment.