From 25724759719bb08dd49cc0956757160e97c7b009 Mon Sep 17 00:00:00 2001 From: mjaworski Date: Tue, 1 Mar 2016 11:51:01 +0000 Subject: [PATCH] fixed code quality --- rest_auth/registration/views.py | 1 + rest_auth/serializers.py | 3 +++ rest_auth/tests/test_api.py | 9 +++------ rest_auth/tests/test_social.py | 3 +-- rest_auth/utils.py | 3 ++- rest_auth/views.py | 3 +-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index d1d8d37..41ca856 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -23,6 +23,7 @@ from .app_settings import RegisterSerializer from rest_auth.utils import jwt_encode + class RegisterView(CreateAPIView): serializer_class = RegisterSerializer permission_classes = (AllowAny, ) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 7daf17c..3d4faae 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -118,6 +118,7 @@ class TokenSerializer(serializers.ModelSerializer): model = TokenModel fields = ('key',) + class UserDetailsSerializer(serializers.ModelSerializer): """ @@ -128,6 +129,7 @@ class UserDetailsSerializer(serializers.ModelSerializer): fields = ('username', 'email', 'first_name', 'last_name') read_only_fields = ('email', ) + class JWTSerializer(serializers.Serializer): """ Serializer for JWT authentication. @@ -135,6 +137,7 @@ class JWTSerializer(serializers.Serializer): token = serializers.CharField() user = UserDetailsSerializer() + class PasswordResetSerializer(serializers.Serializer): """ diff --git a/rest_auth/tests/test_api.py b/rest_auth/tests/test_api.py index f75322c..a74e409 100644 --- a/rest_auth/tests/test_api.py +++ b/rest_auth/tests/test_api.py @@ -97,13 +97,12 @@ class APITestCase1(TestCase, BaseAPITestCase): "username": self.USERNAME, "password": self.PASS } - user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS) + get_user_model().objects.create_user(self.USERNAME, '', self.PASS) self.post(self.login_url, data=payload, status_code=200) self.assertEqual('token' in self.response.json.keys(), True) self.token = self.response.json['token'] - def test_login_by_email(self): # starting test without allauth app settings.INSTALLED_APPS.remove('allauth') @@ -335,7 +334,6 @@ class APITestCase1(TestCase, BaseAPITestCase): user = get_user_model().objects.get(pk=user.pk) self.assertEqual(user.email, self.response.json['email']) - def test_registration(self): user_count = get_user_model().objects.all().count() @@ -365,7 +363,6 @@ class APITestCase1(TestCase, BaseAPITestCase): self._login() self._logout() - def test_registration_with_invalid_password(self): data = self.REGISTRATION_DATA.copy() data['password2'] = 'foobar' @@ -430,7 +427,7 @@ class APITestCase1(TestCase, BaseAPITestCase): } # create user - user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS) + get_user_model().objects.create_user(self.USERNAME, '', self.PASS) self.post(self.login_url, data=payload, status_code=200) self.get(self.logout_url, status=status.HTTP_200_OK) @@ -443,7 +440,7 @@ class APITestCase1(TestCase, BaseAPITestCase): } # create user - user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS) + get_user_model().objects.create_user(self.USERNAME, '', self.PASS) self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK) self.get(self.logout_url, status_code=status.HTTP_405_METHOD_NOT_ALLOWED) diff --git a/rest_auth/tests/test_social.py b/rest_auth/tests/test_social.py index 34c912e..a06e365 100644 --- a/rest_auth/tests/test_social.py +++ b/rest_auth/tests/test_social.py @@ -148,6 +148,5 @@ class TestSocialAuth(TestCase, BaseAPITestCase): self.post(self.fb_login_url, data=payload, status_code=200) self.assertIn('token', self.response.json.keys()) self.assertIn('user', self.response.json.keys()) - - self.assertEqual(get_user_model().objects.all().count(), users_count + 1) + self.assertEqual(get_user_model().objects.all().count(), users_count + 1) diff --git a/rest_auth/utils.py b/rest_auth/utils.py index 5cb94d7..99d80b8 100644 --- a/rest_auth/utils.py +++ b/rest_auth/utils.py @@ -15,8 +15,9 @@ def default_create_token(token_model, user, serializer): token, _ = token_model.objects.get_or_create(user=user) return token + def jwt_encode(user): - try: + try: from rest_framework_jwt.settings import api_settings except ImportError: raise ImportError('rest_framework_jwt needs to be installed') diff --git a/rest_auth/views.py b/rest_auth/views.py index 25f47f2..5e168cd 100644 --- a/rest_auth/views.py +++ b/rest_auth/views.py @@ -46,7 +46,7 @@ class LoginView(GenericAPIView): def login(self): self.user = self.serializer.validated_data['user'] - + if getattr(settings, 'REST_USE_JWT', False): self.token = jwt_encode(self.user) else: @@ -55,7 +55,6 @@ class LoginView(GenericAPIView): if getattr(settings, 'REST_SESSION_LOGIN', True): login(self.request, self.user) - def get_response(self): serializer_class = self.get_response_serializer()