Browse Source

Add unit tests to support password required template settings

LB Johnston 11 tháng trước cách đây
mục cha
commit
488f338c1c

+ 22 - 0
wagtail/documents/tests/test_collection_privacy.py

@@ -107,6 +107,28 @@ class TestCollectionPrivacyDocument(WagtailTestUtils, TestCase):
             )
             self.assertRedirects(response, "/")
 
+    @override_settings(
+        WAGTAILDOCS_PASSWORD_REQUIRED_TEMPLATE="tests/custom_docs_password_required.html"
+    )
+    def test_anonymous_user_must_authenticate_with_custom_password_required_template(
+        self
+    ):
+        secret_document = Document.objects.create(
+            title="Test document",
+            file=self.fake_file,
+            collection=self.password_collection,
+        )
+        doc_url = reverse(
+            "wagtaildocs_serve", args=(secret_document.id, secret_document.filename)
+        )
+        response = self.client.get(doc_url)
+        self.assertNotEqual(
+            response.templates[0].name, "wagtaildocs/password_required.html"
+        )
+        self.assertEqual(
+            response.templates[0].name, "tests/custom_docs_password_required.html"
+        )
+
     def test_group_restriction_with_anonymous_user(self):
         response, url = self.get_document(self.group_collection)
         self.assertRedirects(response, f"/_util/login/?next={url}")

+ 10 - 0
wagtail/test/testapp/templates/tests/custom_docs_password_required.html

@@ -0,0 +1,10 @@
+{% extends "tests/base.html" %}
+
+{% block content %}
+    <p>Test only.</p>
+    <form action="{{ action_url }}" method="POST">
+        {% csrf_token %}
+        {{ form.as_p }}
+        <input type="submit" value="Continue" class="button" />
+    </form>
+{% endblock %}

+ 10 - 0
wagtail/test/testapp/templates/tests/custom_page_password_required.html

@@ -0,0 +1,10 @@
+{% extends "tests/base.html" %}
+
+{% block content %}
+    <p>Test only.</p>
+    <form action="{{ action_url }}" method="POST">
+        {% csrf_token %}
+        {{ form.as_p }}
+        <input type="submit" value="Continue" class="button" />
+    </form>
+{% endblock %}

+ 18 - 1
wagtail/tests/test_page_privacy.py

@@ -1,5 +1,5 @@
 from django.contrib.auth.models import Group
-from django.test import TestCase
+from django.test import TestCase, override_settings
 
 from wagtail.models import Page, PageViewRestriction
 from wagtail.test.utils import WagtailTestUtils
@@ -79,6 +79,23 @@ class TestPagePrivacy(WagtailTestUtils, TestCase):
             )
             self.assertRedirects(response, "/")
 
+    @override_settings(
+        WAGTAIL_PASSWORD_REQUIRED_TEMPLATE="tests/custom_page_password_required.html"
+    )
+    def test_anonymous_user_must_authenticate_with_custom_password_required_template(
+        self
+    ):
+        response = self.client.get("/secret-plans/")
+
+        self.assertNotEqual(
+            "wagtailcore/password_required.html",
+            response.templates[0].name,
+        )
+        self.assertEqual(
+            "tests/custom_page_password_required.html",
+            response.templates[0].name,
+        )
+
     def test_view_restrictions_apply_to_subpages(self):
         underpants_page = Page.objects.get(
             url_path="/home/secret-plans/steal-underpants/"