Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added support for Django 4.2. #353

Merged
merged 13 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ jobs:
matrix:
os: [ubuntu-20.04]
python-version: ['3.8']
toxenv: [quality, docs, django32-drf312, django32-drflatest]
toxenv: [
quality,
docs,
django32-drf314,
UsamaSadiq marked this conversation as resolved.
Show resolved Hide resolved
django32-drflatest,
django42-drflatest
]

steps:
- uses: actions/checkout@v3
Expand All @@ -34,7 +40,7 @@ jobs:
run: tox

- name: Run coverage
if: matrix.python-version == '3.8' && matrix.toxenv == 'django32-drflatest'
if: matrix.python-version == '3.8' && matrix.toxenv == 'django42-drflatest'
uses: codecov/codecov-action@v3
with:
flags: unittests
Expand Down
4 changes: 2 additions & 2 deletions csrf/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
URL definitions for the CSRF API endpoints.
"""

from django.urls import include, re_path
from django.urls import include, path


urlpatterns = [
re_path(r'^v1/', include('csrf.api.v1.urls'), name='csrf_api_v1'),
path('v1/', include('csrf.api.v1.urls'), name='csrf_api_v1'),
]
4 changes: 2 additions & 2 deletions csrf/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
URL definitions for version 1 of the CSRF API.
"""

from django.urls import re_path
from django.urls import path

from .views import CsrfTokenView


urlpatterns = [
re_path(r'^token$', CsrfTokenView.as_view(), name='csrf_token'),
path('token', CsrfTokenView.as_view(), name='csrf_token'),
]
4 changes: 2 additions & 2 deletions csrf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
URLs for the CSRF application.
"""

from django.urls import include, re_path
from django.urls import include, path


urlpatterns = [
re_path(r'^csrf/api/', include('csrf.api.urls'), name='csrf_api'),
path('csrf/api/', include('csrf.api.urls'), name='csrf_api'),
]
6 changes: 5 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

from datetime import datetime
import os
import sys
from datetime import datetime


# on_rtd is whether we are on readthedocs.org
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
Expand All @@ -33,6 +34,8 @@
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'docs_settings')

import django


django.setup()

# -- General configuration -----------------------------------------------------
Expand Down Expand Up @@ -66,6 +69,7 @@
#
import edx_rest_framework_extensions


# The short X.Y version.
version = edx_rest_framework_extensions.__version__

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Unit tests for jwt authentication middlewares.
"""
from itertools import product
from unittest.mock import ANY, patch
from unittest.mock import ANY, Mock, patch

import ddt
from django.http.cookie import SimpleCookie
Expand Down Expand Up @@ -69,7 +69,8 @@ class TestEnsureJWTAuthSettingsMiddleware(TestCase):
def setUp(self):
super().setUp()
self.request = RequestFactory().get('/')
self.middleware = EnsureJWTAuthSettingsMiddleware() # pylint: disable=no-value-for-parameter
self.mock_response = Mock()
self.middleware = EnsureJWTAuthSettingsMiddleware(self.mock_response)

def _assert_included(self, item, iterator, should_be_included):
if should_be_included:
Expand Down Expand Up @@ -395,7 +396,8 @@ def setUp(self):
super().setUp()
self.request = RequestFactory().get('/')
self.request.session = 'mock session'
self.middleware = JwtAuthCookieMiddleware() # pylint: disable=no-value-for-parameter
self.mock_response = Mock()
self.middleware = JwtAuthCookieMiddleware(self.mock_response)

@patch('edx_django_utils.monitoring.set_custom_attribute')
def test_do_not_use_jwt_cookies(self, mock_set_custom_attribute):
Expand Down
12 changes: 6 additions & 6 deletions edx_rest_framework_extensions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def _set_request_referer_attribute(self, request):
"""
Add custom attribute 'request_referer' for http referer.
"""
if 'HTTP_REFERER' in request.META and request.META['HTTP_REFERER']:
monitoring.set_custom_attribute('request_referer', request.META['HTTP_REFERER'])
if 'referer' in request.headers and request.headers['referer']:
monitoring.set_custom_attribute('request_referer', request.headers['referer'])

def _set_request_user_agent_attributes(self, request):
"""
Expand All @@ -133,8 +133,8 @@ def _set_request_user_agent_attributes(self, request):
request_user_agent
request_client_name: The client name from edx-rest-api-client calls.
"""
if 'HTTP_USER_AGENT' in request.META and request.META['HTTP_USER_AGENT']:
user_agent = request.META['HTTP_USER_AGENT']
if 'user-agent' in request.headers and request.headers['user-agent']:
user_agent = request.headers['user-agent']
monitoring.set_custom_attribute('request_user_agent', user_agent)
if user_agent:
# Example agent string from edx-rest-api-client:
Expand All @@ -152,8 +152,8 @@ def _set_request_auth_type_guess_attribute(self, request):
auth_type = 'no-user'
elif not request.user.is_authenticated:
auth_type = 'unauthenticated'
elif 'HTTP_AUTHORIZATION' in request.META and request.META['HTTP_AUTHORIZATION']:
token_parts = request.META['HTTP_AUTHORIZATION'].split()
elif 'authorization' in request.headers and request.headers['authorization']:
token_parts = request.headers['authorization'].split()
# Example: "JWT eyJhbGciO..."
if len(token_parts) == 2:
auth_type = token_parts[0].lower() # 'jwt' or 'bearer' (for example)
Expand Down
8 changes: 5 additions & 3 deletions edx_rest_framework_extensions/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Unit tests for middlewares.
"""
import re
from unittest.mock import call, patch
from unittest.mock import Mock, call, patch

import ddt
from django.contrib.auth.models import AnonymousUser
Expand All @@ -24,7 +24,8 @@ def setUp(self):
super().setUp()
RequestCache.clear_all_namespaces()
self.request = RequestFactory().get('/')
self.middleware = RequestCustomAttributesMiddleware() # pylint: disable=no-value-for-parameter
self.mock_response = Mock()
self.middleware = RequestCustomAttributesMiddleware(self.mock_response)

@patch('edx_django_utils.monitoring.set_custom_attribute')
def test_edx_drf_extensions_version_attribute(self, mock_set_custom_attribute):
Expand Down Expand Up @@ -230,7 +231,8 @@ def setUp(self):
super().setUp()
RequestCache.clear_all_namespaces()
self.request = RequestFactory().get('/')
self.middleware = RequestMetricsMiddleware()
self.mock_response = Mock()
self.middleware = RequestMetricsMiddleware(self.mock_response)

@patch('edx_django_utils.monitoring.set_custom_attribute')
def test_request_auth_type_guess_anonymous_attribute(self, mock_set_custom_attribute):
Expand Down
3 changes: 2 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_settings')
sys.path.append(PWD)
try:
from django.core.management import execute_from_command_line # pylint: disable=wrong-import-position
from django.core.management import \
execute_from_command_line # pylint: disable=wrong-import-position
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def get_version(*file_paths):
'Programming Language :: Python :: 3.8',
'Framework :: Django',
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.0',
'Framework :: Django :: 4.1',
'Framework :: Django :: 4.2',
],
packages=find_packages(exclude=["tests"]),
install_requires=load_requirements('requirements/base.in'),
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[tox]
envlist = py38-django{32}-drf{312,latest}, quality, docs
envlist = py38-django{32,40,41,42}-drf{312,latest}, quality, docs

[testenv]
setenv =
PYTHONPATH = {toxinidir}
deps =
-r{toxinidir}/requirements/test.txt
django32: Django>=3.2,<4.0
drf312: djangorestframework>=3.12,<3.13
django42: Django>=4.2,<4.3
drf314: djangorestframework<3.15.0
drflatest: djangorestframework
commands =
python -m pytest --cov {posargs}
Expand Down