Skip to content

Commit

Permalink
Drop support for Django 3.1 and 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-schilling committed Jan 13, 2022
1 parent 11e7b96 commit a812897
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 110 deletions.
3 changes: 1 addition & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

package = "strawberry_django_jwt"
python_versions = ["3.10", "3.9", "3.8", "3.7"]
django_versions = ["4.0", "3.2", "3.1"]
django_versions = ["4.0", "3.2"]
invalid_sessions = [
("3.7", "4.0"),
("3.10", "3.1"),
]
pyjwt_versions = ["1.7.1", "2.1.0"]
strawberry_graphql_versions = ["0.69.0", "latest"]
Expand Down
157 changes: 86 additions & 71 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ classifiers = [
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Framework :: Django',
'Framework :: Django :: 3.0',
'Framework :: Django :: 3.1',
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.0',
]
Expand All @@ -25,8 +23,8 @@ packages = [
[tool.poetry.dependencies]
python = "^3.7"
Django = [
{version = ">=3.1,<4", markers = "python_version < '3.8'", optional = true},
{version = ">=3.1,<5", markers = "python_version >= '3.8'", optional = true}
{version = ">=3.2,<4", markers = "python_version < '3.8'", optional = true},
{version = ">=3.2,<5", markers = "python_version >= '3.8'", optional = true}
]
PyJWT = ">=1.7.1,<3.0"
strawberry-graphql = ">=0.69.0,<1.0.0"
Expand Down
6 changes: 1 addition & 5 deletions strawberry_django_jwt/testcases.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import django
import strawberry
from django.contrib.auth.models import AnonymousUser
from django.core.handlers.asgi import ASGIRequest
Expand Down Expand Up @@ -117,10 +116,7 @@ def credentials(self, **kwargs):
self._credentials = kwargs

def execute(self, query, variables=None, **extra):
if django.VERSION[:2] == (3, 1):
context = self.post(query, custom_headers=self._credentials, **extra)
else:
context = self.post(query, **self._credentials, **extra)
context = self.post(query, **self._credentials, **extra)

return super().execute(
query,
Expand Down
8 changes: 7 additions & 1 deletion tests/example_app/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import strawberry_django_jwt.mutations as jwt_mutations
from strawberry_django_jwt.decorators import login_required
from strawberry_django_jwt.middleware import AsyncJSONWebTokenMiddleware
from strawberry_django_jwt.middleware import (
AsyncJSONWebTokenMiddleware,
JSONWebTokenMiddleware,
)


@strawberry.type
Expand Down Expand Up @@ -35,3 +38,6 @@ class Mutation:
schema = Schema(
query=Query, mutation=Mutation, extensions=[AsyncJSONWebTokenMiddleware]
)
sync_schema = Schema(
query=Query, mutation=Mutation, extensions=[JSONWebTokenMiddleware]
)
8 changes: 6 additions & 2 deletions tests/example_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
from rest_framework.routers import DefaultRouter

from strawberry_django_jwt.decorators import jwt_cookie
from tests.example_app.schema import schema
from strawberry_django_jwt.views import AsyncStatusHandlingGraphQLView as AGQLView
from tests.example_app.schema import schema, sync_schema
from strawberry_django_jwt.views import (
AsyncStatusHandlingGraphQLView as AGQLView,
StatusHandlingGraphQLView as GQLView,
)


class UserSerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -31,6 +34,7 @@ class UserViewSet(viewsets.ModelViewSet):
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
re_path(r"^graphql/?$", jwt_cookie(AGQLView.as_view(schema=schema))),
re_path(r"^sync-graphql/?$", jwt_cookie(GQLView.as_view(schema=sync_schema))),
path("users", include(router.urls)),
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
]
15 changes: 15 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ def _authenticate(_=None, **__):

self.assertEqual(result, self.user)

async def test_sync_asgi_authenticate(self):
settings.AUTHENTICATION_BACKENDS = ["tests.test_auth.SyncDefaultAuth"]
request = self.request_factory.post("/")

def _authenticate(_=None, **__):
return self.user

with patch(
"tests.test_auth.SyncDefaultAuth.authenticate",
side_effect=_authenticate,
):
result = await authenticate(request)

self.assertEqual(result, self.user)

async def test_permission_denied(self):
settings.AUTHENTICATION_BACKENDS = ["tests.test_auth.PermissionDeniedAuth"]
request = self.request_factory.post("/")
Expand Down
18 changes: 3 additions & 15 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import base64

import django
import pytest
from django.contrib.auth.models import User
from django.test import Client, TestCase as DjangoTestCase
Expand Down Expand Up @@ -84,35 +83,24 @@ def setUp(self):
self.backend = JSONWebTokenBackend()

async def test_authenticate_async(self):
name = (
jwt_settings.JWT_AUTH_HEADER_NAME
if django.VERSION[:2] < (3, 2)
else jwt_settings.JWT_AUTH_HEADER_NAME.replace("HTTP_", "")
)
name = jwt_settings.JWT_AUTH_HEADER_NAME.replace("HTTP_", "")
headers = {
name: f"{jwt_settings.JWT_AUTH_HEADER_PREFIX} {self.token}",
}

request = self.request_factory.get("/", **headers)
if django.VERSION[:2] == (3, 1):
request.META.update(headers)

user = await self.backend.authenticate_async(request=request)

self.assertEqual(user, self.user)

async def test_authenticate_fail_async(self):
name = (
jwt_settings.JWT_AUTH_HEADER_NAME
if django.VERSION[:2] < (3, 2)
else jwt_settings.JWT_AUTH_HEADER_NAME.replace("HTTP_", "")
)
name = jwt_settings.JWT_AUTH_HEADER_NAME.replace("HTTP_", "")
headers = {
name: f"{jwt_settings.JWT_AUTH_HEADER_PREFIX} invalid",
}

request = self.request_factory.get("/", **headers)
if django.VERSION[:2] == (3, 1):
request.META.update(headers)

with self.assertRaises(JSONWebTokenError):
await self.backend.authenticate_async(request=request)
Expand Down
6 changes: 1 addition & 5 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json

import django
import strawberry
from django.contrib.auth import get_user_model

Expand Down Expand Up @@ -158,10 +157,7 @@ async def test_login_async(self):
): f"{jwt_settings.JWT_AUTH_HEADER_PREFIX} {self.token}",
}

if django.VERSION[:2] == (3, 1):
response = await self.client.execute(query, custom_headers=headers)
else:
response = await self.client.execute(query, **headers)
response = await self.client.execute(query, **headers)
data = response.data

self.assertEqual(data["test"], "TEST")
Expand Down
5 changes: 0 additions & 5 deletions tests/testcases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from unittest import mock

import django
import strawberry
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission, User
Expand Down Expand Up @@ -45,8 +44,6 @@ def setUp(self):

def info(self, user=None, **headers):
request = self.request_factory.post("/", **headers)
if django.VERSION[:2] == (3, 1):
request.META.update({f"HTTP_{k}": v for k, v in headers.items()})

if user is not None:
request.user = user
Expand Down Expand Up @@ -133,8 +130,6 @@ def setUp(self):

def info(self, user=None, **headers):
request = self.request_factory.post("/", **headers)
if django.VERSION[:2] == (3, 1):
request.META.update({f"HTTP_{k}": v for k, v in headers.items()})

if user is not None:
request.user = user
Expand Down

0 comments on commit a812897

Please sign in to comment.