-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifie le lien canonique dans un formulaire dédié (#6611)
* Supprime le lien canonique du formulaire de contenu * Ajoute un formulaire dédié au lien canonique * Formulaire modal spécifique * Enregistrement dans le journal d'événements * Ajoute des tests
- Loading branch information
Showing
10 changed files
with
229 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
zds/tutorialv2/tests/tests_views/tests_editcanonicallinkview.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
from django.urls import reverse | ||
from django.utils.html import escape | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from zds.tutorialv2.views.canonical import EditCanonicalLinkForm, EditCanonicalLinkView | ||
from zds.tutorialv2.tests import TutorialTestMixin, override_for_contents | ||
from zds.tutorialv2.tests.factories import PublishableContentFactory | ||
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory | ||
|
||
|
||
@override_for_contents() | ||
class PermissionTests(TutorialTestMixin, TestCase): | ||
"""Test permissions and associated behaviors, such as redirections and status codes.""" | ||
|
||
def setUp(self): | ||
# Create users | ||
self.author = ProfileFactory().user | ||
self.staff = StaffProfileFactory().user | ||
self.outsider = ProfileFactory().user | ||
|
||
# Create a content | ||
self.content = PublishableContentFactory(author_list=[self.author]) | ||
|
||
# Get information to be reused in tests | ||
self.form_url = reverse("content:edit-canonical-link", kwargs={"pk": self.content.pk}) | ||
self.form_data = {"source": "https://example.com"} | ||
self.content_data = {"pk": self.content.pk, "slug": self.content.slug} | ||
self.content_url = reverse("content:view", kwargs=self.content_data) | ||
self.login_url = reverse("member-login") + "?next=" + self.form_url | ||
|
||
def test_not_authenticated(self): | ||
self.client.logout() # ensure no user is authenticated | ||
response = self.client.post(self.form_url, self.form_data) | ||
self.assertRedirects(response, self.login_url) | ||
|
||
def test_authenticated_author(self): | ||
self.client.force_login(self.author) | ||
response = self.client.post(self.form_url, self.form_data) | ||
self.assertRedirects(response, self.content_url) | ||
|
||
def test_authenticated_staff(self): | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.form_url, self.form_data) | ||
self.assertRedirects(response, self.content_url) | ||
|
||
def test_authenticated_outsider(self): | ||
self.client.force_login(self.outsider) | ||
response = self.client.post(self.form_url, self.form_data) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
|
||
@override_for_contents() | ||
class WorkflowTests(TutorialTestMixin, TestCase): | ||
"""Test the workflow of the form, such as validity errors and success messages.""" | ||
|
||
def setUp(self): | ||
# Create a user | ||
self.author = ProfileFactory() | ||
|
||
# Create a content | ||
self.content = PublishableContentFactory(author_list=[self.author.user]) | ||
|
||
# Get information to be reused in tests | ||
self.form_url = reverse("content:edit-canonical-link", kwargs={"pk": self.content.pk}) | ||
self.error_messages = EditCanonicalLinkForm.declared_fields["source"].error_messages | ||
self.error_messages["too_long"] = _("Assurez-vous que cette valeur comporte au plus") | ||
self.success_message = EditCanonicalLinkView.success_message | ||
|
||
# Log in with an authorized user (e.g the author of the content) to perform the tests | ||
self.client.force_login(self.author.user) | ||
|
||
def get_test_cases(self): | ||
return { | ||
"no_field": {"inputs": {}, "expected_outputs": [self.success_message]}, | ||
"empty": {"inputs": {"source": ""}, "expected_outputs": [self.success_message]}, | ||
"valid_1": {"inputs": {"source": "example.com"}, "expected_outputs": [self.success_message]}, | ||
"valid_2": {"inputs": {"source": "https://example.com"}, "expected_outputs": [self.success_message]}, | ||
"invalid": {"inputs": {"source": "invalid_url"}, "expected_outputs": [self.error_messages["invalid"]]}, | ||
} | ||
|
||
def test_form_workflow(self): | ||
test_cases = self.get_test_cases() | ||
for case_name, case in test_cases.items(): | ||
with self.subTest(msg=case_name): | ||
response = self.client.post(self.form_url, case["inputs"], follow=True) | ||
for msg in case["expected_outputs"]: | ||
self.assertContains(response, escape(msg)) | ||
|
||
|
||
@override_for_contents() | ||
class FunctionalTests(TutorialTestMixin, TestCase): | ||
"""Test the detailed behavior of the feature, such as updates of the database or repositories.""" | ||
|
||
def setUp(self): | ||
self.author = ProfileFactory() | ||
self.content = PublishableContentFactory(author_list=[self.author.user]) | ||
self.form_url = reverse("content:edit-canonical-link", kwargs={"pk": self.content.pk}) | ||
|
||
# Log in with an authorized user (e.g the author of the content) to perform the tests | ||
self.client.force_login(self.author.user) | ||
|
||
@patch("zds.tutorialv2.signals.canonical_link_management") | ||
def test_normal(self, canonical_link_management): | ||
valid_url = "https://example.com" | ||
self.client.post(self.form_url, data={"source": valid_url}, follow=True) | ||
expected = {"source": valid_url, "call_count": 1} | ||
self.check_effects(expected, canonical_link_management) | ||
|
||
@patch("zds.tutorialv2.signals.canonical_link_management") | ||
def test_empty(self, canonical_link_management): | ||
self.client.post(self.form_url, data={"source": ""}, follow=True) | ||
expected = {"source": "", "call_count": 1} | ||
self.check_effects(expected, canonical_link_management) | ||
|
||
def check_effects(self, expected_outputs, canonical_link_management): | ||
self.content.refresh_from_db() | ||
self.assertEqual(self.content.source, expected_outputs["source"]) | ||
self.assertEqual(canonical_link_management.send.call_count, expected_outputs["call_count"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from crispy_forms.bootstrap import StrictButton | ||
from crispy_forms.helper import FormHelper | ||
from crispy_forms.layout import Layout, Field, ButtonHolder | ||
from django import forms | ||
from django.contrib import messages | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.shortcuts import get_object_or_404 | ||
from django.urls import reverse | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from zds.tutorialv2 import signals | ||
from zds.tutorialv2.mixins import SingleContentFormViewMixin | ||
from zds.tutorialv2.models.database import PublishableContent | ||
from zds.utils import get_current_user | ||
|
||
|
||
class EditCanonicalLinkForm(forms.Form): | ||
source = forms.URLField( | ||
label=_( | ||
"""Si votre contenu est publié en dehors de Zeste de Savoir (blog, site personnel, etc.), | ||
indiquez le lien de la publication originale :""" | ||
), | ||
max_length=PublishableContent._meta.get_field("source").max_length, | ||
required=False, | ||
widget=forms.TextInput(attrs={"placeholder": _("https://...")}), | ||
error_messages={"invalid": _("Entrez un lien valide.")}, | ||
) | ||
|
||
def __init__(self, content, *args, **kwargs): | ||
kwargs["initial"] = {"source": content.source} | ||
super().__init__(*args, **kwargs) | ||
|
||
self.helper = FormHelper() | ||
self.helper.form_method = "post" | ||
self.helper.form_action = reverse("content:edit-canonical-link", kwargs={"pk": content.pk}) | ||
self.helper.form_class = "modal modal-flex" | ||
self.helper.form_id = "edit-canonical-link" | ||
|
||
self.helper.layout = Layout( | ||
Field("source"), | ||
ButtonHolder( | ||
StrictButton(_("Valider"), type="submit"), | ||
), | ||
) | ||
|
||
self.previous_page_url = reverse("content:view", kwargs={"pk": content.pk, "slug": content.slug}) | ||
|
||
|
||
class EditCanonicalLinkView(LoginRequiredMixin, SingleContentFormViewMixin): | ||
model = PublishableContent | ||
form_class = EditCanonicalLinkForm | ||
success_message = _("Le lien canonique a bien été modifié.") | ||
modal_form = True | ||
http_method_names = ["post"] | ||
|
||
def dispatch(self, request, *args, **kwargs): | ||
content = get_object_or_404(PublishableContent, pk=self.kwargs["pk"]) | ||
success_url_kwargs = {"pk": content.pk, "slug": content.slug} | ||
self.success_url = reverse("content:view", kwargs=success_url_kwargs) | ||
return super().dispatch(request, *args, **kwargs) | ||
|
||
def get_form_kwargs(self): | ||
kwargs = super().get_form_kwargs() | ||
kwargs["content"] = self.object | ||
return kwargs | ||
|
||
def form_invalid(self, form): | ||
form.previous_page_url = self.success_url | ||
return super().form_invalid(form) | ||
|
||
def form_valid(self, form): | ||
self.object.source = form.cleaned_data["source"] | ||
self.object.save() | ||
messages.success(self.request, self.success_message) | ||
signals.canonical_link_management.send(sender=self.__class__, performer=get_current_user(), content=self.object) | ||
return super().form_valid(form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters