fixed code quality

This commit is contained in:
mjaworski 2016-03-01 11:51:01 +00:00
parent eda3d7b6d2
commit 2572475971
6 changed files with 11 additions and 11 deletions

View File

@ -23,6 +23,7 @@ from .app_settings import RegisterSerializer
from rest_auth.utils import jwt_encode from rest_auth.utils import jwt_encode
class RegisterView(CreateAPIView): class RegisterView(CreateAPIView):
serializer_class = RegisterSerializer serializer_class = RegisterSerializer
permission_classes = (AllowAny, ) permission_classes = (AllowAny, )

View File

@ -118,6 +118,7 @@ class TokenSerializer(serializers.ModelSerializer):
model = TokenModel model = TokenModel
fields = ('key',) fields = ('key',)
class UserDetailsSerializer(serializers.ModelSerializer): class UserDetailsSerializer(serializers.ModelSerializer):
""" """
@ -128,6 +129,7 @@ class UserDetailsSerializer(serializers.ModelSerializer):
fields = ('username', 'email', 'first_name', 'last_name') fields = ('username', 'email', 'first_name', 'last_name')
read_only_fields = ('email', ) read_only_fields = ('email', )
class JWTSerializer(serializers.Serializer): class JWTSerializer(serializers.Serializer):
""" """
Serializer for JWT authentication. Serializer for JWT authentication.
@ -135,6 +137,7 @@ class JWTSerializer(serializers.Serializer):
token = serializers.CharField() token = serializers.CharField()
user = UserDetailsSerializer() user = UserDetailsSerializer()
class PasswordResetSerializer(serializers.Serializer): class PasswordResetSerializer(serializers.Serializer):
""" """

View File

@ -97,13 +97,12 @@ class APITestCase1(TestCase, BaseAPITestCase):
"username": self.USERNAME, "username": self.USERNAME,
"password": self.PASS "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.post(self.login_url, data=payload, status_code=200)
self.assertEqual('token' in self.response.json.keys(), True) self.assertEqual('token' in self.response.json.keys(), True)
self.token = self.response.json['token'] self.token = self.response.json['token']
def test_login_by_email(self): def test_login_by_email(self):
# starting test without allauth app # starting test without allauth app
settings.INSTALLED_APPS.remove('allauth') settings.INSTALLED_APPS.remove('allauth')
@ -335,7 +334,6 @@ class APITestCase1(TestCase, BaseAPITestCase):
user = get_user_model().objects.get(pk=user.pk) user = get_user_model().objects.get(pk=user.pk)
self.assertEqual(user.email, self.response.json['email']) self.assertEqual(user.email, self.response.json['email'])
def test_registration(self): def test_registration(self):
user_count = get_user_model().objects.all().count() user_count = get_user_model().objects.all().count()
@ -365,7 +363,6 @@ class APITestCase1(TestCase, BaseAPITestCase):
self._login() self._login()
self._logout() self._logout()
def test_registration_with_invalid_password(self): def test_registration_with_invalid_password(self):
data = self.REGISTRATION_DATA.copy() data = self.REGISTRATION_DATA.copy()
data['password2'] = 'foobar' data['password2'] = 'foobar'
@ -430,7 +427,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
} }
# create user # 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.post(self.login_url, data=payload, status_code=200)
self.get(self.logout_url, status=status.HTTP_200_OK) self.get(self.logout_url, status=status.HTTP_200_OK)
@ -443,7 +440,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
} }
# create user # 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.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) self.get(self.logout_url, status_code=status.HTTP_405_METHOD_NOT_ALLOWED)

View File

@ -148,6 +148,5 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
self.post(self.fb_login_url, data=payload, status_code=200) self.post(self.fb_login_url, data=payload, status_code=200)
self.assertIn('token', self.response.json.keys()) self.assertIn('token', self.response.json.keys())
self.assertIn('user', 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)

View File

@ -15,8 +15,9 @@ def default_create_token(token_model, user, serializer):
token, _ = token_model.objects.get_or_create(user=user) token, _ = token_model.objects.get_or_create(user=user)
return token return token
def jwt_encode(user): def jwt_encode(user):
try: try:
from rest_framework_jwt.settings import api_settings from rest_framework_jwt.settings import api_settings
except ImportError: except ImportError:
raise ImportError('rest_framework_jwt needs to be installed') raise ImportError('rest_framework_jwt needs to be installed')

View File

@ -46,7 +46,7 @@ class LoginView(GenericAPIView):
def login(self): def login(self):
self.user = self.serializer.validated_data['user'] self.user = self.serializer.validated_data['user']
if getattr(settings, 'REST_USE_JWT', False): if getattr(settings, 'REST_USE_JWT', False):
self.token = jwt_encode(self.user) self.token = jwt_encode(self.user)
else: else:
@ -55,7 +55,6 @@ class LoginView(GenericAPIView):
if getattr(settings, 'REST_SESSION_LOGIN', True): if getattr(settings, 'REST_SESSION_LOGIN', True):
login(self.request, self.user) login(self.request, self.user)
def get_response(self): def get_response(self):
serializer_class = self.get_response_serializer() serializer_class = self.get_response_serializer()