From bf4b7db33ebc5493192c1e83ce733e3271ac27d9 Mon Sep 17 00:00:00 2001
From: David Barnes <barnesdavidj@gmail.com>
Date: Fri, 8 Jul 2022 10:28:47 -0400
Subject: [PATCH] Add note about pattern regex and update code to use regular
 string over regex one. Update tests to capture and test for warnings.

---
 .../templatetags/adminlte_filters.py          |   7 +-
 tests/test_middleware.py                      | 106 +++++++++++++-----
 2 files changed, 83 insertions(+), 30 deletions(-)

diff --git a/django_adminlte_2/templatetags/adminlte_filters.py b/django_adminlte_2/templatetags/adminlte_filters.py
index fecc14e..20afe25 100644
--- a/django_adminlte_2/templatetags/adminlte_filters.py
+++ b/django_adminlte_2/templatetags/adminlte_filters.py
@@ -207,6 +207,11 @@ def with_pattern(field, pattern=None):
     regex passed to this filter. Therefore, the regex string needs to be stored
     in a variable that can be sent to the filter.
 
+    NOTE: The default regex in this method is written as a regular string and
+    not a raw string (regex with r prefix) so that the documentation will match.
+    This docstring can not contain a single backslash as python will think it
+    is invalid syntax and raise a warning.
+
     :param field: Form field to add attributes to.
     :param pattern: The JavaScript regex pattern to use.
      Defaults to "\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}" if value not passed.
@@ -229,7 +234,7 @@ def with_pattern(field, pattern=None):
         <input type="tel" name="field" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" id="id_field" />
     """
     if pattern is None:
-        pattern = r"\([0-9]{3}\) [0-9]{3}-[0-9]{4}"
+        pattern = "\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}"
 
     attrs = field.field.widget.attrs
     attrs['pattern'] = pattern
diff --git a/tests/test_middleware.py b/tests/test_middleware.py
index 5d2d125..15aed32 100644
--- a/tests/test_middleware.py
+++ b/tests/test_middleware.py
@@ -87,12 +87,24 @@ class MiddlewareTestCase(TestCase):
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
     def test_middleware_blocks_when_user_anonymous_login_off_strict_on_login_wl_off_strict_wl_off(self):
         """test_middleware_blocks_when_user_anonymous_login_off_strict_on_login_wl_off_strict_wl_off"""
-        response = self.client.get(
-            reverse('django_adminlte_2:demo-css'),
-            follow=True
-        )
-        self.assertEqual(response.status_code, 200)
-        self.assertContains(response, "Home")
+        with warnings.catch_warnings(record=True) as wa:
+            warning_message = (
+                "The view 'demo_css' does not have the"
+                " permission_required, one_of_permission, or login_required"
+                " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is"
+                " set to True. This means that this view is inaccessible until"
+                " either permissions are set on the view or the url_name for the"
+                " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting."
+            )
+
+            response = self.client.get(
+                reverse('django_adminlte_2:demo-css'),
+                follow=True
+            )
+            self.assertEqual(response.status_code, 200)
+            self.assertContains(response, "Home")
+            self.assertEqual(len(wa), 1)
+            self.assertIn(warning_message, str(wa[-1].message))
 
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
     @patch('django_adminlte_2.middleware.STRICT_POLICY_WHITELIST', UPDATED_STRICT_POLICY_WHITELIST)
@@ -123,12 +135,24 @@ class MiddlewareTestCase(TestCase):
         """test_middleware_blocks_when_user_anonymous_login_on_strict_on_login_wl_on_strict_wl_off"""
         # NOTE: This test goes to demo-css, fails the strict policy, then goes to home.
         # Home is a new request that fails the login required being on and thus redirect to login page.
-        response = self.client.get(
-            reverse('django_adminlte_2:demo-css'),
-            follow=True
-        )
-        self.assertEqual(response.status_code, 200)
-        self.assertContains(response, "Login")
+        with warnings.catch_warnings(record=True) as wa:
+            warning_message = (
+                "The view 'demo_css' does not have the"
+                " permission_required, one_of_permission, or login_required"
+                " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is"
+                " set to True. This means that this view is inaccessible until"
+                " either permissions are set on the view or the url_name for the"
+                " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting."
+            )
+
+            response = self.client.get(
+                reverse('django_adminlte_2:demo-css'),
+                follow=True
+            )
+            self.assertEqual(response.status_code, 200)
+            self.assertContains(response, "Login")
+            self.assertEqual(len(wa), 1)
+            self.assertIn(warning_message, str(wa[-1].message))
 
     @patch('django_adminlte_2.middleware.LOGIN_REQUIRED', True)
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
@@ -196,13 +220,25 @@ class MiddlewareTestCase(TestCase):
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
     def test_middleware_blocks_when_user_logged_in_login_off_strict_on_login_wl_off_strict_wl_off(self):
         """test_middleware_blocks_when_user_logged_in_login_off_strict_on_login_wl_off_strict_wl_off"""
-        self.client.force_login(self.test_user_w_perms)
-        response = self.client.get(
-            reverse('django_adminlte_2:demo-css'),
-            follow=True
-        )
-        self.assertEqual(response.status_code, 200)
-        self.assertContains(response, "Home")
+        with warnings.catch_warnings(record=True) as wa:
+            self.client.force_login(self.test_user_w_perms)
+            warning_message = (
+                "The view 'demo_css' does not have the"
+                " permission_required, one_of_permission, or login_required"
+                " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is"
+                " set to True. This means that this view is inaccessible until"
+                " either permissions are set on the view or the url_name for the"
+                " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting."
+            )
+
+            response = self.client.get(
+                reverse('django_adminlte_2:demo-css'),
+                follow=True
+            )
+            self.assertEqual(response.status_code, 200)
+            self.assertContains(response, "Home")
+            self.assertEqual(len(wa), 1)
+            self.assertIn(warning_message, str(wa[-1].message))
 
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
     @patch('django_adminlte_2.middleware.STRICT_POLICY_WHITELIST', UPDATED_STRICT_POLICY_WHITELIST)
@@ -220,20 +256,32 @@ class MiddlewareTestCase(TestCase):
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
     def test_middleware_blocks_when_user_logged_in_login_on_strict_on_login_wl_off_strict_wl_off(self):
         """test_middleware_blocks_when_user_logged_in_login_on_strict_on_login_wl_off_strict_wl_off"""
-        self.client.force_login(self.test_user_w_perms)
-        response = self.client.get(
-            reverse('django_adminlte_2:demo-css'),
-            follow=True
-        )
-        self.assertEqual(response.status_code, 200)
-        self.assertContains(response, "Home")
+        with warnings.catch_warnings(record=True) as wa:
+            self.client.force_login(self.test_user_w_perms)
+            warning_message = (
+                "The view 'demo_css' does not have the"
+                " permission_required, one_of_permission, or login_required"
+                " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is"
+                " set to True. This means that this view is inaccessible until"
+                " either permissions are set on the view or the url_name for the"
+                " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting."
+            )
+
+            response = self.client.get(
+                reverse('django_adminlte_2:demo-css'),
+                follow=True
+            )
+            self.assertEqual(response.status_code, 200)
+            self.assertContains(response, "Home")
+            self.assertEqual(len(wa), 1)
+            self.assertIn(warning_message, str(wa[-1].message))
 
     @patch('django_adminlte_2.middleware.LOGIN_REQUIRED', True)
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
     @patch('django_adminlte_2.middleware.LOGIN_EXEMPT_WHITELIST', UPDATED_LOGIN_EXEMPT_WHITELIST)
     def test_middleware_blocks_when_user_logged_in_login_on_strict_on_login_wl_on_strict_wl_off(self):
         """test_middleware_blocks_when_user_logged_in_login_on_strict_on_login_wl_on_strict_wl_off"""
-        with warnings.catch_warnings(record=True) as w:
+        with warnings.catch_warnings(record=True) as wa:
             self.client.force_login(self.test_user_w_perms)
             warning_message = (
                 "The view 'demo_css' does not have the"
@@ -250,8 +298,8 @@ class MiddlewareTestCase(TestCase):
             )
             self.assertEqual(response.status_code, 200)
             self.assertContains(response, "Home")
-            self.assertEqual(len(w), 1)
-            self.assertIn(warning_message, str(w[-1].message))
+            self.assertEqual(len(wa), 1)
+            self.assertIn(warning_message, str(wa[-1].message))
 
     @patch('django_adminlte_2.middleware.LOGIN_REQUIRED', True)
     @patch('django_adminlte_2.middleware.STRICT_POLICY', True)
-- 
GitLab