From 5ccb499665d63210e44989f350791d931d556b92 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sun, 16 Jun 2019 20:05:53 +0200 Subject: [PATCH] pedagogy: full test suite for UVComment --- pedagogy/templates/pedagogy/uv_detail.jinja | 3 +- pedagogy/tests.py | 89 +++++++++++++++++++++ pedagogy/urls.py | 2 +- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/pedagogy/templates/pedagogy/uv_detail.jinja b/pedagogy/templates/pedagogy/uv_detail.jinja index 9b80efe4..58a389c5 100644 --- a/pedagogy/templates/pedagogy/uv_detail.jinja +++ b/pedagogy/templates/pedagogy/uv_detail.jinja @@ -5,6 +5,7 @@ {% endblock %} {% block content %} +

{% trans %}Back{% endtrans %}

{{ object.code }} - {{ object.title }}

{{ object.objectives|markdown }}

{{ object.program|markdown }}

@@ -23,7 +24,7 @@

{% trans %}Published: {% endtrans %}{{ comment.publish_date }}

{% trans %}Author: {% endtrans %}{{ comment.author }}

{% if user.is_owner(comment) %} -

{% trans %}Edit{% endtrans %}

+

{% trans %}Edit{% endtrans %}

{% trans %}Delete{% endtrans %}

{% endif %} {% endfor %} diff --git a/pedagogy/tests.py b/pedagogy/tests.py index 81407244..5ecf3b9e 100644 --- a/pedagogy/tests.py +++ b/pedagogy/tests.py @@ -476,3 +476,92 @@ class UVCommentDeleteTest(TestCase): # Check that the comment still exists self.assertTrue(UVComment.objects.filter(id=self.comment.id).exists()) + + +class UVCommentUpdateTest(TestCase): + """ + Test UVComment update rights + """ + + def setUp(self): + call_command("populate") + + self.krophil = User.objects.get(username="krophil") + + # Prepare a comment + comment_kwargs = create_uv_comment_template(self.krophil.id) + comment_kwargs["author"] = self.krophil + comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"]) + self.comment = UVComment(**comment_kwargs) + self.comment.save() + + # Prepare edit of this comment for post requests + self.comment_edit = create_uv_comment_template(self.krophil.id) + self.comment_edit["comment"] = "Edited" + + def test_uv_comment_update_root_success(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 302) + self.comment.refresh_from_db() + self.assertEquals(self.comment.comment, self.comment_edit["comment"]) + + def test_uv_comment_update_pedagogy_admin_success(self): + self.client.login(username="tutu", password="plop") + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 302) + self.comment.refresh_from_db() + self.assertEquals(self.comment.comment, self.comment_edit["comment"]) + + def test_uv_comment_update_author_success(self): + self.client.login(username="krophil", password="plop") + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 302) + self.comment.refresh_from_db() + self.assertEquals(self.comment.comment, self.comment_edit["comment"]) + + def test_uv_comment_update_unauthorized_fail(self): + # Anonymous user + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 403) + + # Unsbscribed user + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 403) + + # Subscribed user (not author of the comment) + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 403) + + # Check that the comment hasn't change + self.comment.refresh_from_db() + self.assertNotEquals(self.comment.comment, self.comment_edit["comment"]) + + def test_uv_comment_update_original_author_does_not_change(self): + self.client.login(username="root", password="plop") + self.comment_edit["author"] = User.objects.get(username="root").id + + response = self.client.post( + reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}), + self.comment_edit, + ) + self.assertEquals(response.status_code, 200) + self.assertEquals(self.comment.author, self.krophil) diff --git a/pedagogy/urls.py b/pedagogy/urls.py index 73bd91a5..6bdb99c1 100644 --- a/pedagogy/urls.py +++ b/pedagogy/urls.py @@ -33,7 +33,7 @@ urlpatterns = [ url( r"^comment/(?P[0-9]+)/edit$", UVCommentUpdateView.as_view(), - name="comment_edit", + name="comment_update", ), url( r"^comment/(?P[0-9]+)/delete$",