Skip to content

Commit

Permalink
Update for Django 4.1 support. (#43)
Browse files Browse the repository at this point in the history
Django now raises a TypeError when middleware is missing `get_response`.
Removed MiddlewareMixin from SEOBackendBase and added `get_response` argument to tests.
Added django 4.1 to tox for tests.
Fixes issue 42.
  • Loading branch information
sarahboyce authored Sep 26, 2022
1 parent 90b8de6 commit c26df6d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion django_seo_js/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, get_response=None, *args, **kwargs):
self.backend = getattr(backend_module, module_path.split(".")[-1])()


class SEOBackendBase(MiddlewareMixin):
class SEOBackendBase:
"""The base class to inherit for SEO_JS backends"""

def build_absolute_uri(self, request):
Expand Down
6 changes: 3 additions & 3 deletions django_seo_js/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase

from django_seo_js.tests.utils import override_settings
from django_seo_js.tests.utils import override_settings, get_response_empty
from django_seo_js.middleware import EscapedFragmentMiddleware


Expand All @@ -9,11 +9,11 @@ class HelpersTest(TestCase):
@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def test_update_the_render_cache(self):
from django_seo_js.helpers import update_cache_for_url
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.assertEqual(update_cache_for_url("http://example.com"), True)

@override_settings(BACKEND='django_seo_js.backends.TestBackend', ENABLED=False)
def test_update_skips_if_disabled(self):
from django_seo_js.helpers import update_cache_for_url
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.assertEqual(update_cache_for_url("http://example.com"), False)
38 changes: 19 additions & 19 deletions django_seo_js/tests/test_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mock import Mock
from django.test import TestCase

from django_seo_js.tests.utils import override_settings
from django_seo_js.tests.utils import override_settings, get_response_empty
from django_seo_js.middleware import EscapedFragmentMiddleware, UserAgentMiddleware, HashBangMiddleware

print(override_settings)
Expand All @@ -18,7 +18,7 @@ class EscapedFragmentMiddlewareTest(TestCase):
@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def setUp(self):
super(EscapedFragmentMiddlewareTest, self).setUp()
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request = Mock()
self.request.path = "/"
self.request.GET = {}
Expand All @@ -33,19 +33,19 @@ def test_does_not_have_escaped_fragment(self):

@override_settings(BACKEND='django_seo_js.backends.TestBackend', ENABLED=False)
def test_has_escaped_fragment_skips_if_disabled_via_enabled(self):
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request.GET = {}
self.assertEqual(self.middleware.process_request(self.request), None)

@override_settings(BACKEND='django_seo_js.backends.TestServiceDownBackend')
def test_has_escaped_fragment_skips_if_service_is_down(self):
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request.GET = {"_escaped_fragment_": None}
self.assertEqual(self.middleware.process_request(self.request), None)

@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def test_overriding_skips_sitemap_xml_by_default(self):
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request.path = "/sitemap.xml"
self.request.GET = {"_escaped_fragment_": None}
self.assertEqual(self.middleware.process_request(self.request), None)
Expand All @@ -56,7 +56,7 @@ def test_overriding_skips_sitemap_xml_by_default(self):
IGNORE_EXTENSIONS=[],
)
def test_overriding_skips_custom_overrides_xml_by_default(self):
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request.path = "/sitemap.xml"
self.request.GET = {"_escaped_fragment_": None}
self.assertEqual(self.middleware.process_request(self.request).content, b"Test")
Expand All @@ -69,7 +69,7 @@ def test_overriding_skips_custom_overrides_xml_by_default(self):

@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def test_overriding_skips_gifs_by_default(self):
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request.path = "/sitemap.xml"
self.request.GET = {"_escaped_fragment_": None}
self.assertEqual(self.middleware.process_request(self.request), None)
Expand All @@ -79,7 +79,7 @@ def test_overriding_skips_gifs_by_default(self):
IGNORE_EXTENSIONS=[".html", ".txt", ]
)
def test_overriding_skips_custom_overrides_gifs_by_default(self):
self.middleware = EscapedFragmentMiddleware()
self.middleware = EscapedFragmentMiddleware(get_response_empty)
self.request.path = "/foo.gif"
self.request.GET = {"_escaped_fragment_": None}
self.assertEqual(self.middleware.process_request(self.request).content, b"Test")
Expand All @@ -96,7 +96,7 @@ class HashBangMiddlewareTest(EscapedFragmentMiddlewareTest):
@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def setUp(self):
super(HashBangMiddlewareTest, self).setUp()
self.middleware = HashBangMiddleware()
self.middleware = HashBangMiddleware(get_response_empty)
self.request = Mock()
self.request.path = "/"
self.request.GET = {}
Expand All @@ -107,7 +107,7 @@ class UserAgentMiddlewareTest(TestCase):
@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def setUp(self):
super(UserAgentMiddlewareTest, self).setUp()
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request = Mock()
self.request.path = "/"
self.request.META = {}
Expand All @@ -130,7 +130,7 @@ def test_does_not_match_one_of_the_default_user_agents(self):
BACKEND='django_seo_js.backends.TestBackend'
)
def test_overriding_matches(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.META = {
"HTTP_USER_AGENT": "The TestUserAgent v1.0"
}
Expand All @@ -141,7 +141,7 @@ def test_overriding_matches(self):
BACKEND='django_seo_js.backends.TestBackend'
)
def test_overriding_does_not_match_properly(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.META = {
"HTTP_USER_AGENT":
"Mozilla/2.0 (compatible; Ask Jeeves/Teoma; +http://about.ask.com/en/docs/about/webmasters.shtml)"
Expand All @@ -153,21 +153,21 @@ def test_overriding_does_not_match_properly(self):
BACKEND='django_seo_js.backends.TestBackend'
)
def test_missing_user_agent_still_works(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.META = {}
self.assertEqual(self.middleware.process_request(self.request), None)

@override_settings(BACKEND='django_seo_js.backends.TestBackend', ENABLED=False)
def test_overriding_matches_skips_if_disabled_via_enabled(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.META = {
"HTTP_USER_AGENT": "The TestUserAgent v1.0"
}
self.assertEqual(self.middleware.process_request(self.request), None)

@override_settings(BACKEND='django_seo_js.backends.TestServiceDownBackend')
def test_overriding_matches_skips_if_service_is_down(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.META = {
"HTTP_USER_AGENT":
"Mozilla/2.0 (compatible; Ask Jeeves/Teoma; +http://about.ask.com/en/docs/about/webmasters.shtml)"
Expand All @@ -176,7 +176,7 @@ def test_overriding_matches_skips_if_service_is_down(self):

@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def test_overriding_skips_sitemap_xml_by_default(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.path = "/sitemap.xml"
self.request.META = {
"HTTP_USER_AGENT":
Expand All @@ -190,7 +190,7 @@ def test_overriding_skips_sitemap_xml_by_default(self):
IGNORE_EXTENSIONS=[],
)
def test_overriding_skips_custom_overrides_xml_by_default(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.path = "/sitemap.xml"
self.request.META = {
"HTTP_USER_AGENT":
Expand All @@ -206,7 +206,7 @@ def test_overriding_skips_custom_overrides_xml_by_default(self):

@override_settings(BACKEND='django_seo_js.backends.TestBackend')
def test_overriding_skips_gifs_by_default(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.path = "/foo.gif"
self.request.META = {
"HTTP_USER_AGENT":
Expand All @@ -219,7 +219,7 @@ def test_overriding_skips_gifs_by_default(self):
IGNORE_EXTENSIONS=[".html", ".txt", ]
)
def test_overriding_skips_custom_overrides_gifs_by_default(self):
self.middleware = UserAgentMiddleware()
self.middleware = UserAgentMiddleware(get_response_empty)
self.request.path = "/foo.gif"
self.request.META = {
"HTTP_USER_AGENT":
Expand Down
5 changes: 5 additions & 0 deletions django_seo_js/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import wraps
from django.http.response import HttpResponse
from django_seo_js import settings


Expand Down Expand Up @@ -28,3 +29,7 @@ def enable(self):
def disable(self):
for k, v in self.originals.items():
setattr(settings, k, v)


def get_response_empty(request):
return HttpResponse()
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
# and then run "tox" from this directory.

[tox]
envlist = python{37}-dj{2,3}
envlist = python{37,38,39}-dj{2,3,4}

[testenv]
deps =
-rrequirements.tests.txt

dj2: Django ~=2.2
dj3: Django ~=3.2.9
dj3: Django ~=3.2.15
dj4: Django ~=4.1.1

commands =
{envpython} manage.py test

0 comments on commit c26df6d

Please sign in to comment.