diff --git a/lacommunaute/documentation/tests/tests_category_update_view.py b/lacommunaute/documentation/tests/tests_category_update_view.py new file mode 100644 index 000000000..f4e0bac92 --- /dev/null +++ b/lacommunaute/documentation/tests/tests_category_update_view.py @@ -0,0 +1,75 @@ +import pytest # noqa + +from django.urls import reverse + +from lacommunaute.documentation.factories import CategoryFactory +from lacommunaute.documentation.models import Category +from lacommunaute.users.factories import UserFactory + + +@pytest.fixture(name="category") +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) + + +@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): + if user: + client.force_login(user()) + response = client.get(url) + assert response.status_code == status_code + + +@pytest.fixture(name="expected_context") +def fixture_expected_context(category): + return { + "title": f"Modifier la catégorie {category.name}", + "back_url": reverse("documentation:category_detail", kwargs={"slug": category.slug, "pk": category.pk}), + } + + +def test_view(client, db, url, superuser, expected_context): + client.force_login(superuser) + response = client.get(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"} + + +@pytest.fixture(name="post_data") +def fixture_post_data(): + return { + "name": "New Name", + "short_description": "New Short Description", + "description": "### New Description", + } + + +@pytest.fixture(name="expected_values") +def fixture_expected_values(post_data): + return { + "name": post_data["name"], + "short_description": post_data["short_description"], + "_description_rendered": "

New Description

", + "slug": "new-name", + } + + +def test_update(client, db, url, superuser, post_data, expected_values): + client.force_login(superuser) + response = client.post(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 diff --git a/lacommunaute/documentation/urls.py b/lacommunaute/documentation/urls.py index 59702e6d2..be2a9e17d 100644 --- a/lacommunaute/documentation/urls.py +++ b/lacommunaute/documentation/urls.py @@ -1,6 +1,11 @@ from django.urls import path -from lacommunaute.documentation.views import CategoryCreateView, CategoryDetailView, CategoryListView +from lacommunaute.documentation.views import ( + CategoryCreateView, + CategoryDetailView, + CategoryListView, + CategoryUpdateView, +) app_name = "documentation" @@ -10,4 +15,5 @@ path("", CategoryListView.as_view(), name="category_list"), path("-/", CategoryDetailView.as_view(), name="category_detail"), path("category/create/", CategoryCreateView.as_view(), name="category_create"), + path("-/update/", CategoryUpdateView.as_view(), name="category_update"), ] diff --git a/lacommunaute/documentation/views.py b/lacommunaute/documentation/views.py index 881f43cdb..053b65f17 100644 --- a/lacommunaute/documentation/views.py +++ b/lacommunaute/documentation/views.py @@ -2,7 +2,7 @@ from django.contrib.contenttypes.models import ContentType from django.urls import reverse from django.views.generic import DetailView, ListView -from django.views.generic.edit import CreateView +from django.views.generic.edit import CreateView, UpdateView from taggit.models import Tag from lacommunaute.documentation.models import Category, Document @@ -57,3 +57,12 @@ def get_context_data(self, **kwargs): "back_url": reverse("documentation:category_list"), } return super().get_context_data(**kwargs) | additionnal_context + + +class CategoryUpdateView(CategoryCreateUpdateMixin, UpdateView): + def get_context_data(self, **kwargs): + additionnal_context = { + "title": f"Modifier la catégorie {self.object.name}", + "back_url": self.object.get_absolute_url(), + } + return super().get_context_data(**kwargs) | additionnal_context