From 438fe19cc6539121049d54c4e16b24c0973af272 Mon Sep 17 00:00:00 2001 From: Arnaud-D <35631001+Arnaud-D@users.noreply.github.com> Date: Sun, 15 Sep 2024 20:51:30 +0200 Subject: [PATCH] Ajoute des tests pour les vues intro et conclusion --- .../tests_views/tests_editconclusionview.py | 81 +++++++++++++++++++ .../tests_views/tests_editintroductionview.py | 81 +++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 zds/tutorialv2/tests/tests_views/tests_editconclusionview.py create mode 100644 zds/tutorialv2/tests/tests_views/tests_editintroductionview.py diff --git a/zds/tutorialv2/tests/tests_views/tests_editconclusionview.py b/zds/tutorialv2/tests/tests_views/tests_editconclusionview.py new file mode 100644 index 0000000000..70ec404010 --- /dev/null +++ b/zds/tutorialv2/tests/tests_views/tests_editconclusionview.py @@ -0,0 +1,81 @@ +from datetime import datetime + +from django.test import TestCase +from django.urls import reverse + +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-conclusion", kwargs={"pk": self.content.pk}) + self.form_data = {"conclusion": "conclusion content"} # avoids changing the slug + 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): + """Test that on form submission, unauthenticated users are redirected to the login page.""" + 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): + """Test that on form submission, authors are redirected to the content page.""" + 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): + """Test that on form submission, staffs are redirected to the content page.""" + 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): + """Test that on form submission, unauthorized users get a 403.""" + 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 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-conclusion", kwargs={"pk": self.content.pk}) + self.client.force_login(self.author.user) + + def test_normal(self): + start_date = datetime.now() + self.assertTrue(self.content.update_date < start_date) + + new_conclusion = "Ceci n'est pas l'ancienne conclusion" + response = self.client.post(self.form_url, {"conclusion": new_conclusion}, follow=True) + self.assertEqual(response.status_code, 200) + + self.content.refresh_from_db() + + # Database update + self.assertTrue(self.content.update_date > start_date) + + # Update in repository + versioned_content = self.content.load_version() + self.assertEqual(versioned_content.get_conclusion(), new_conclusion) diff --git a/zds/tutorialv2/tests/tests_views/tests_editintroductionview.py b/zds/tutorialv2/tests/tests_views/tests_editintroductionview.py new file mode 100644 index 0000000000..24bf992056 --- /dev/null +++ b/zds/tutorialv2/tests/tests_views/tests_editintroductionview.py @@ -0,0 +1,81 @@ +from datetime import datetime + +from django.test import TestCase +from django.urls import reverse + +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-introduction", kwargs={"pk": self.content.pk}) + self.form_data = {"introduction": "introduction content"} # avoids changing the slug + 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): + """Test that on form submission, unauthenticated users are redirected to the login page.""" + 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): + """Test that on form submission, authors are redirected to the content page.""" + 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): + """Test that on form submission, staffs are redirected to the content page.""" + 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): + """Test that on form submission, unauthorized users get a 403.""" + 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 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-introduction", kwargs={"pk": self.content.pk}) + self.client.force_login(self.author.user) + + def test_normal(self): + start_date = datetime.now() + self.assertTrue(self.content.update_date < start_date) + + new_introduction = "Ceci n'est pas l'ancienne introduction" + response = self.client.post(self.form_url, {"introduction": new_introduction}, follow=True) + self.assertEqual(response.status_code, 200) + + self.content.refresh_from_db() + + # Database update + self.assertTrue(self.content.update_date > start_date) + + # Update in repository + versioned_content = self.content.load_version() + self.assertEqual(versioned_content.get_introduction(), new_introduction)