diff --git a/pedagogy/tests.py b/pedagogy/tests.py index 89874ca6..fad2b1d7 100644 --- a/pedagogy/tests.py +++ b/pedagogy/tests.py @@ -28,7 +28,7 @@ from django.core.management import call_command from core.models import User -from pedagogy.models import UV, UVComment +from pedagogy.models import UV, UVComment, UVCommentReport def create_uv_template(user_id, code="IFC1", exclude_list=[]): @@ -727,3 +727,232 @@ class UVSearchTest(TestCase): # Search with credit type response = self.client.get(reverse("pedagogy:guide"), {"credit_type": "TM"}) self.assertNotContains(response, text="PA00") + + +class UVModerationFormTest(TestCase): + """ + Test moderation view + Assert access rights and if the form works well + """ + + 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_1 = UVComment(**comment_kwargs) + self.comment_1.save() + + # Prepare another 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_2 = UVComment(**comment_kwargs) + self.comment_2.save() + + # Prepare a comment report for comment 1 + self.report_1 = UVCommentReport( + comment=self.comment_1, reporter=self.krophil, reason="C'est moche" + ) + self.report_1.save() + self.report_1_bis = UVCommentReport( + comment=self.comment_1, reporter=self.krophil, reason="C'est moche 2" + ) + self.report_1_bis.save() + + # Prepare a comment report for comment 2 + self.report_2 = UVCommentReport( + comment=self.comment_2, reporter=self.krophil, reason="C'est moche" + ) + self.report_2.save() + + def test_access_authorized_success(self): + # Test with root + self.client.login(username="root", password="plop") + response = self.client.get(reverse("pedagogy:moderation")) + self.assertEquals(response.status_code, 200) + + # Test with pedagogy admin + self.client.login(username="tutu", password="plop") + response = self.client.get(reverse("pedagogy:moderation")) + self.assertEquals(response.status_code, 200) + + def test_access_unauthorized_fail(self): + # Test with anonymous user + response = self.client.get(reverse("pedagogy:moderation")) + self.assertEquals(response.status_code, 403) + + # Test with unsubscribed user + self.client.login(username="guy", password="plop") + response = self.client.get(reverse("pedagogy:moderation")) + self.assertEquals(response.status_code, 403) + + # Test with subscribed user + self.client.login(username="sli", password="plop") + response = self.client.get(reverse("pedagogy:moderation")) + self.assertEquals(response.status_code, 403) + + def test_do_nothing(self): + self.client.login(username="root", password="plop") + response = self.client.post(reverse("pedagogy:moderation")) + self.assertEquals(response.status_code, 302) + + # Test that nothing has changed + self.assertTrue(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_1.id).exists()) + self.assertTrue( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + self.assertTrue(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_2.id).exists()) + + def test_delete_comment(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), {"accepted_reports": [self.report_1.id]} + ) + self.assertEquals(response.status_code, 302) + + # Test that the comment and it's associated report has been deleted + self.assertFalse(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertFalse(UVComment.objects.filter(id=self.comment_1.id).exists()) + # Test that the bis report has been deleted + self.assertFalse( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + + # Test that the other comment and report still exists + self.assertTrue(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_2.id).exists()) + + def test_delete_comment_bulk(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), + {"accepted_reports": [self.report_1.id, self.report_2.id]}, + ) + self.assertEquals(response.status_code, 302) + + # Test that comments and their associated reports has been deleted + self.assertFalse(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertFalse(UVComment.objects.filter(id=self.comment_1.id).exists()) + self.assertFalse(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + self.assertFalse(UVComment.objects.filter(id=self.comment_2.id).exists()) + # Test that the bis report has been deleted + self.assertFalse( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + + def test_delete_comment_with_bis(self): + # Test case if two reports targets the same comment and are both deleted + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), + {"accepted_reports": [self.report_1.id, self.report_1_bis.id]}, + ) + self.assertEquals(response.status_code, 302) + + # Test that the comment and it's associated report has been deleted + self.assertFalse(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertFalse(UVComment.objects.filter(id=self.comment_1.id).exists()) + # Test that the bis report has been deleted + self.assertFalse( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + + def test_delete_report(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), {"denied_reports": [self.report_1.id]} + ) + self.assertEquals(response.status_code, 302) + + # Test that the report has been deleted and that the comment still exists + self.assertFalse(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_1.id).exists()) + # Test that the bis report is still there + self.assertTrue( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + + # Test that the other comment and report still exists + self.assertTrue(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_2.id).exists()) + + def test_delete_report_bulk(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), + { + "denied_reports": [ + self.report_1.id, + self.report_1_bis.id, + self.report_2.id, + ] + }, + ) + self.assertEquals(response.status_code, 302) + + # Test that every reports has been deleted + self.assertFalse(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertFalse( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + self.assertFalse(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + + # Test that comments still exists + self.assertTrue(UVComment.objects.filter(id=self.comment_1.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_2.id).exists()) + + def test_delete_mixed(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), + { + "accepted_reports": [self.report_2.id], + "denied_reports": [self.report_1.id], + }, + ) + self.assertEquals(response.status_code, 302) + + # Test that report 2 and his comment has been deleted + self.assertFalse(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + self.assertFalse(UVComment.objects.filter(id=self.comment_2.id).exists()) + + # Test that report 1 has been deleted and it's comment still exists + self.assertFalse(UVCommentReport.objects.filter(id=self.report_1.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_1.id).exists()) + + # Test that report 1 bis is still there + self.assertTrue( + UVCommentReport.objects.filter(id=self.report_1_bis.id).exists() + ) + + def test_delete_mixed_with_bis(self): + self.client.login(username="root", password="plop") + response = self.client.post( + reverse("pedagogy:moderation"), + { + "accepted_reports": [self.report_1.id], + "denied_reports": [self.report_1_bis.id], + }, + ) + self.assertEquals(response.status_code, 302) + + # Test that report 1 and 1 bis has been deleted + self.assertFalse( + UVCommentReport.objects.filter( + id__in=[self.report_1.id, self.report_1_bis.id] + ).exists() + ) + + # Test that comment 1 has been deleted + self.assertFalse(UVComment.objects.filter(id=self.comment_1.id).exists()) + + # Test that report and comment 2 still exists + self.assertTrue(UVCommentReport.objects.filter(id=self.report_2.id).exists()) + self.assertTrue(UVComment.objects.filter(id=self.comment_2.id).exists()) diff --git a/pedagogy/views.py b/pedagogy/views.py index 21feb15e..dd727126 100644 --- a/pedagogy/views.py +++ b/pedagogy/views.py @@ -34,7 +34,7 @@ from django.views.generic import ( from django.core import serializers from django.utils import html from django.http import HttpResponse -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, ObjectDoesNotExist from django.core.urlresolvers import reverse_lazy from django.shortcuts import get_object_or_404 @@ -248,9 +248,17 @@ class UVModerationFormView(FormView): def form_valid(self, form): form_clean = form.clean() for report in form_clean.get("accepted_reports", []): - report.comment.delete() # Delete the related comment + try: + report.comment.delete() # Delete the related comment + except ObjectDoesNotExist: + # To avoid errors when two reports points the same comment + pass for report in form_clean.get("denied_reports", []): - report.delete() # Delete the report itself + try: + report.delete() # Delete the report itself + except ObjectDoesNotExist: + # To avoid errors when two reports points the same comment + pass return super(UVModerationFormView, self).form_valid(form) def get_success_url(self):