From 115819241b9892e6b7381d82e112a05b4384e313 Mon Sep 17 00:00:00 2001 From: Steve Jalim Date: Wed, 2 Oct 2024 14:59:58 +0100 Subject: [PATCH] Expand test coverage: access to view to create a sharing link --- wagtaildraftsharing/tests/test_views.py | 50 +++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/wagtaildraftsharing/tests/test_views.py b/wagtaildraftsharing/tests/test_views.py index f30116d..fa73963 100644 --- a/wagtaildraftsharing/tests/test_views.py +++ b/wagtaildraftsharing/tests/test_views.py @@ -3,8 +3,10 @@ from unittest.mock import patch from django.contrib.auth import get_user_model +from django.contrib.auth.models import AnonymousUser, Group from django.http import Http404 from django.test import RequestFactory, TestCase, override_settings +from django.urls import reverse from django.utils.timezone import now as datetime_now from freezegun import freeze_time from wagtail_factories import PageFactory @@ -22,7 +24,8 @@ class TestViews(TestCase): @classmethod def setUpTestData(cls): super().setUpTestData() - cls.superuser = User.objects.create_superuser(username="test", password="test") + + cls.superuser = User.objects.create_superuser(username="admin", password="test") cls.factory = RequestFactory() def create_revision(self): @@ -44,12 +47,7 @@ def create_revision(self): def test_create_sharing_link_view(self): revision = self.create_revision() - request = self.factory.post( - "/create/", - { - "revision": revision.id, - }, - ) + request = self.factory.post("/create/", {"revision": revision.id}) request.user = self.superuser response = CreateSharingLinkView.as_view()(request) @@ -60,6 +58,44 @@ def test_create_sharing_link_view(self): response_data["url"], WagtaildraftsharingLink.objects.get().url ) + def test_create_sharing_link_view__editor_allowed(self): + # Assumes default Wagtail staff-side user groups are present + editor_group = Group.objects.get(name="Editors") + editor = User.objects.create_user(username="editor", password="test") + editor.groups.add(editor_group) + + revision = self.create_revision() + dest = reverse("wagtaildraftsharing:create") + self.assertEqual(WagtaildraftsharingLink.objects.count(), 0) + self.client.login(username="editor", password="test") + response = self.client.post(dest, data={"revision": revision.id}, follow=True) + self.assertEqual(response.status_code, 200) + self.assertEqual(WagtaildraftsharingLink.objects.count(), 1) + + def test_create_sharing_link_view__moderator_allowed(self): + moderator_group = Group.objects.get(name="Moderators") + moderator = User.objects.create_user(username="moderator", password="test") + moderator.groups.add(moderator_group) + + revision = self.create_revision() + dest = reverse("wagtaildraftsharing:create") + self.assertEqual(WagtaildraftsharingLink.objects.count(), 0) + self.client.login(username="moderator", password="test") + response = self.client.post(dest, data={"revision": revision.id}, follow=True) + self.assertEqual(response.status_code, 200) + self.assertEqual(WagtaildraftsharingLink.objects.count(), 1) + + def test_create_sharing_link_view__anonymous_user_not_allowed(self): + revision = self.create_revision() + dest = reverse("wagtaildraftsharing:create") + self.assertEqual(WagtaildraftsharingLink.objects.count(), 0) + response = self.client.post(dest, data={"revision": revision.id}, follow=True) + self.assertContains( + response, + "Sorry, you do not have permission to access this area", + ) + self.assertEqual(WagtaildraftsharingLink.objects.count(), 0) + @freeze_time(FROZEN_TIME_ISOFORMATTED) def test_create_sharing_link_view__max_age_from_settings(self): frozen_time = datetime.datetime.fromisoformat(FROZEN_TIME_ISOFORMATTED)