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

django-allauth v64 migration #655

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
django-version: ['3.2', '4.2', '5.0']
django-version: ['4.2', '5.0']
exclude:
- python-version: '3.8'
django-version: '5.0'
Expand All @@ -67,7 +67,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -r dj_rest_auth/tests/requirements.pip
pip install -r dj_rest_auth/tests/requirements.txt
pip install "Django~=${{ matrix.django-version }}.0"
- name: Run Tests
run: |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Drop-in API endpoints for handling authentication securely in Django Rest Framew
with SPAs (e.g., React, Vue, Angular), and Mobile applications.

## Requirements
- Django 3, 4 and 5 (See Unit Test Coverage in CI)
- Django 4.2 and 5 (See Unit Test Coverage in CI)
- Python >= 3.8

## Quick Setup
Expand Down Expand Up @@ -47,7 +47,7 @@ REST_AUTH = {

### Testing

Install required modules with `pip install -r dj_rest_auth/tests/requirements.pip`
Install required modules with `pip install -r dj_rest_auth/tests/requirements.txt`

To run the tests within a virtualenv, run `python runtests.py` from the repository directory.
The easiest way to run test coverage is with [`coverage`](https://pypi.org/project/coverage/),
Expand Down
8 changes: 0 additions & 8 deletions demo/requirements.pip

This file was deleted.

12 changes: 12 additions & 0 deletions demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
django>=5.0.0
djangorestframework>=3.11.0
djangorestframework-simplejwt==5.3.1
django-allauth>=64.0.0
drf-yasg==1.21.7
django-cors-headers==4.4.0
coreapi==2.3.3
PyJWT~=2.9.0
responses~=0.12.1
requests~=2.32.3
setuptools==75.1.0
-e ./..
2 changes: 1 addition & 1 deletion dj_rest_auth/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__title__ = 'dj-rest-auth'
__description__ = 'Authentication and Registration in Django Rest Framework.'
__url__ = 'http://github.com/iMerica/dj-rest-auth'
__version__ = '6.0.0'
__version__ = '7.0.0'
__author__ = '@iMerica https://github.com/iMerica'
__author_email__ = '[email protected]'
__license__ = 'MIT'
Expand Down
10 changes: 5 additions & 5 deletions dj_rest_auth/jwt_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.utils import timezone
from datetime import datetime
from django.utils.translation import gettext_lazy as _
from rest_framework import status
from rest_framework import exceptions, serializers
Expand All @@ -12,7 +12,7 @@
def set_jwt_access_cookie(response, access_token):
from rest_framework_simplejwt.settings import api_settings as jwt_settings
cookie_name = api_settings.JWT_AUTH_COOKIE
access_token_expiration = (timezone.now() + jwt_settings.ACCESS_TOKEN_LIFETIME)
access_token_expiration = (datetime.utcnow() + jwt_settings.ACCESS_TOKEN_LIFETIME)
cookie_secure = api_settings.JWT_AUTH_SECURE
cookie_httponly = api_settings.JWT_AUTH_HTTPONLY
cookie_samesite = api_settings.JWT_AUTH_SAMESITE
Expand All @@ -32,7 +32,7 @@ def set_jwt_access_cookie(response, access_token):

def set_jwt_refresh_cookie(response, refresh_token):
from rest_framework_simplejwt.settings import api_settings as jwt_settings
refresh_token_expiration = (timezone.now() + jwt_settings.REFRESH_TOKEN_LIFETIME)
refresh_token_expiration = (datetime.utcnow() + jwt_settings.REFRESH_TOKEN_LIFETIME)
refresh_cookie_name = api_settings.JWT_AUTH_REFRESH_COOKIE
refresh_cookie_path = api_settings.JWT_AUTH_REFRESH_COOKIE_PATH
cookie_secure = api_settings.JWT_AUTH_SECURE
Expand Down Expand Up @@ -101,13 +101,13 @@ class RefreshViewWithCookieSupport(TokenRefreshView):
def finalize_response(self, request, response, *args, **kwargs):
if response.status_code == status.HTTP_200_OK and 'access' in response.data:
set_jwt_access_cookie(response, response.data['access'])
response.data['access_expiration'] = (timezone.now() + jwt_settings.ACCESS_TOKEN_LIFETIME)
response.data['access_expiration'] = (datetime.utcnow() + jwt_settings.ACCESS_TOKEN_LIFETIME)
if response.status_code == status.HTTP_200_OK and 'refresh' in response.data:
set_jwt_refresh_cookie(response, response.data['refresh'])
if api_settings.JWT_AUTH_HTTPONLY:
del response.data['refresh']
else:
response.data['refresh_expiration'] = (timezone.now() + jwt_settings.REFRESH_TOKEN_LIFETIME)
response.data['refresh_expiration'] = (datetime.utcnow() + jwt_settings.REFRESH_TOKEN_LIFETIME)
return super().finalize_response(request, response, *args, **kwargs)
return RefreshViewWithCookieSupport

Expand Down
1 change: 0 additions & 1 deletion dj_rest_auth/registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def get_response_data(self, user):
return api_settings.JWT_SERIALIZER(data, context=self.get_serializer_context()).data
elif self.token_model:
return api_settings.TOKEN_SERIALIZER(user.auth_token, context=self.get_serializer_context()).data

return None

def create(self, request, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coveralls==1.11.1
django-allauth==0.61.1
django-allauth>=64.0.0
djangorestframework-simplejwt>=5.3.1
flake8==3.8.4
responses==0.12.1
unittest-xml-reporting==3.0.4
requests-oauthlib==2.0.0
31 changes: 1 addition & 30 deletions dj_rest_auth/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
try:
from django.urls import reverse
except ImportError: # pragma: no cover
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse # noqa

from jwt import decode as decode_jwt
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
Expand Down Expand Up @@ -527,35 +527,6 @@ def test_registration_with_jwt(self):
self._login()
self._logout()

@override_api_settings(SESSION_LOGIN=True)
@override_api_settings(TOKEN_MODEL=None)
def test_registration_with_session(self):
import sys
from importlib import reload
from django.contrib.sessions.middleware import SessionMiddleware
from django.contrib.messages.middleware import MessageMiddleware
reload(sys.modules['dj_rest_auth.models'])
reload(sys.modules['dj_rest_auth.registration.views'])
from dj_rest_auth.registration.views import RegisterView

user_count = get_user_model().objects.all().count()

self.post(self.register_url, data={}, status_code=400)

factory = APIRequestFactory()
request = factory.post(self.register_url, self.REGISTRATION_DATA)

for middleware_class in (SessionMiddleware, MessageMiddleware):
middleware = middleware_class(lambda request: None)
middleware.process_request(request)

response = RegisterView.as_view()(request)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(response.data, None)
self.assertEqual(get_user_model().objects.all().count(), user_count + 1)

self._login(status.HTTP_204_NO_CONTENT)
self._logout()

def test_registration_with_invalid_password(self):
data = self.REGISTRATION_DATA.copy()
Expand Down
17 changes: 2 additions & 15 deletions dj_rest_auth/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from allauth.socialaccount.providers.facebook.views import FacebookProvider
from allauth.socialaccount.providers.facebook.provider import FacebookProvider
from allauth.socialaccount.models import SocialApp
from allauth.core.exceptions import ImmediateHttpResponse
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.core.exceptions import ValidationError
from django.test import TestCase, modify_settings, override_settings
from django.contrib.sites.models import Site
from django.http import HttpResponseBadRequest
from rest_framework.exceptions import ErrorDetail
from rest_framework.test import APIRequestFactory, force_authenticate
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -142,23 +140,12 @@ def test_validate_no_view_submit(self):
serializer.is_valid()
self.assertDictEqual(serializer.errors, self.NO_VIEW_SUBMIT_ERROR)

def test_validate_no_adpapter_class_present(self):
def test_validate_no_adapter_class_present(self):
dummy_view = SocialLoginView()
serializer = SocialLoginSerializer(data=self.request_data, context={'request': self.request, 'view': dummy_view})
serializer.is_valid()
self.assertDictEqual(serializer.errors, self.NO_ADAPTER_CLASS_PRESENT)

@patch('allauth.socialaccount.providers.facebook.views.fb_complete_login')
@patch('allauth.socialaccount.adapter.DefaultSocialAccountAdapter.pre_social_login')
def test_immediate_http_response_error(self, mock_pre_social_login, mock_fb_complete_login):
dummy_view = SocialLoginView()
dummy_view.adapter_class = FacebookOAuth2Adapter
mock_pre_social_login.side_effect = lambda request, social_login: exec('raise ImmediateHttpResponse(HttpResponseBadRequest("Bad Request"))')
mock_fb_complete_login.return_value = FacebookProvider(self.request, app=FacebookOAuth2Adapter).sociallogin_from_response(self.request, self.fb_response)
serializer = SocialLoginSerializer(data=self.request_data, context={'request': self.request, 'view': dummy_view})
serializer.is_valid()
self.assertDictEqual(serializer.errors, self.HTTP_BAD_REQUEST_MESSAGE)

def test_http_error(self):
dummy_view = SocialLoginView()
dummy_view.adapter_class = FacebookOAuth2Adapter
Expand Down
2 changes: 1 addition & 1 deletion dj_rest_auth/tests/test_social.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse # noqa


@override_settings(ROOT_URLCONF='tests.urls')
Expand Down
6 changes: 3 additions & 3 deletions dj_rest_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth import login as django_login
from django.contrib.auth import logout as django_logout
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone
from datetime import datetime
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views.decorators.debug import sensitive_post_parameters
Expand Down Expand Up @@ -81,8 +81,8 @@ def get_response(self):
from rest_framework_simplejwt.settings import (
api_settings as jwt_settings,
)
access_token_expiration = (timezone.now() + jwt_settings.ACCESS_TOKEN_LIFETIME)
refresh_token_expiration = (timezone.now() + jwt_settings.REFRESH_TOKEN_LIFETIME)
access_token_expiration = (datetime.utcnow() + jwt_settings.ACCESS_TOKEN_LIFETIME)
refresh_token_expiration = (datetime.utcnow() + jwt_settings.REFRESH_TOKEN_LIFETIME)
return_expiration_times = api_settings.JWT_AUTH_RETURN_EXPIRATION
auth_httponly = api_settings.JWT_AUTH_HTTPONLY

Expand Down
2 changes: 1 addition & 1 deletion docs/demo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To run this locally follow the steps below.
cd /tmp
git clone https://github.com/iMerica/dj-rest-auth.git
cd dj-rest-auth/demo/
pip install -r requirements.pip
pip install -r requirements.txt
python manage.py migrate --settings=demo.settings --noinput
python manage.py runserver --settings=demo.settings

Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
keywords='django rest auth registration rest-framework django-registration api',
zip_safe=False,
install_requires=[
'Django>=3.2,<6.0',
'Django>=4.2,<6.0',
'djangorestframework>=3.13.0',
],
extras_require={
'with-social': ['django-allauth>=0.56.0,<0.62.0'],
'with-social': ['django-allauth>=64.0.0'],
},
tests_require=[
'coveralls>=1.11.1',
'django-allauth>=0.57.0',
'django-allauth>=64.0.0',
'djangorestframework-simplejwt==4.6.0',
'responses==0.12.1',
'unittest-xml-reporting==3.0.4',
Expand Down
7 changes: 3 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ python =
commands =
python ./runtests.py
deps =
-rdj_rest_auth/tests/requirements.pip
django3: Django>=3.2,<4.0
django4: Django>=4.0,<5.0
-r dj_rest_auth/tests/requirements.txt
django4: Django>=4.2,<5.0
django5: Django>=5.0,<6.0

# Configuration for coverage and flake8 is being set in `./setup.cfg`
Expand All @@ -35,7 +34,7 @@ commands =
coverage run ./runtests.py
coverage report
deps =
-rdj_rest_auth/tests/requirements.pip
-r dj_rest_auth/tests/requirements.txt

[testenv:flake8]
changedir = {toxinidir}/dj_rest_auth
Expand Down