Skip to content

Commit

Permalink
Flake8 style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-jaworski committed Apr 28, 2015
1 parent ece4c01 commit a60df71
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 57 deletions.
6 changes: 4 additions & 2 deletions rest_auth/registration/serializers.py
Expand Up @@ -16,8 +16,10 @@ def validate(self, attrs):
request = request._request

if not view:
raise serializers.ValidationError('View is not defined, pass it ' +
'as a context variable')
raise serializers.ValidationError(
'View is not defined, pass it as a context variable'
)

self.adapter_class = getattr(view, 'adapter_class', None)

if not self.adapter_class:
Expand Down
4 changes: 2 additions & 2 deletions rest_auth/registration/urls.py
Expand Up @@ -3,7 +3,8 @@

from .views import Register, VerifyEmail

urlpatterns = patterns('',
urlpatterns = patterns(
'',
url(r'^$', Register.as_view(), name='rest_register'),
url(r'^verify-email/$', VerifyEmail.as_view(), name='rest_verify_email'),

Expand All @@ -21,4 +22,3 @@
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
name='account_confirm_email'),
)

25 changes: 16 additions & 9 deletions rest_auth/serializers.py
Expand Up @@ -78,7 +78,6 @@ def save(self):


class PasswordResetConfirmSerializer(serializers.Serializer):

"""
Serializer for requesting a password reset e-mail.
"""
Expand Down Expand Up @@ -107,8 +106,9 @@ def validate(self, attrs):

self.custom_validation(attrs)
# Construct SetPasswordForm instance
self.set_password_form = self.set_password_form_class(user=self.user,
data=attrs)
self.set_password_form = self.set_password_form_class(
user=self.user, data=attrs
)
if not self.set_password_form.is_valid():
raise serializers.ValidationError(self.set_password_form.errors)
if not default_token_generator.check_token(self.user, attrs['token']):
Expand All @@ -129,8 +129,9 @@ class PasswordChangeSerializer(serializers.Serializer):
set_password_form_class = SetPasswordForm

def __init__(self, *args, **kwargs):
self.old_password_field_enabled = getattr(settings,
'OLD_PASSWORD_FIELD_ENABLED', False)
self.old_password_field_enabled = getattr(
settings, 'OLD_PASSWORD_FIELD_ENABLED', False
)
super(PasswordChangeSerializer, self).__init__(*args, **kwargs)

if not self.old_password_field_enabled:
Expand All @@ -140,14 +141,20 @@ def __init__(self, *args, **kwargs):
self.user = getattr(self.request, 'user', None)

def validate_old_password(self, value):
if self.old_password_field_enabled and self.user and \
not self.user.check_password(value):
invalid_password_conditions = (
self.old_password_field_enabled,
self.user,
not self.user.check_password(value)
)

if all(invalid_password_conditions):
raise serializers.ValidationError('Invalid password')
return value

def validate(self, attrs):
self.set_password_form = self.set_password_form_class(user=self.user,
data=attrs)
self.set_password_form = self.set_password_form_class(
user=self.user, data=attrs
)

if not self.set_password_form.is_valid():
raise serializers.ValidationError(self.set_password_form.errors)
Expand Down
106 changes: 75 additions & 31 deletions rest_auth/tests.py
Expand Up @@ -35,7 +35,7 @@ class BaseAPITestCase(object):
def send_request(self, request_method, *args, **kwargs):
request_func = getattr(self.client, request_method)
status_code = None
if not 'content_type' in kwargs and request_method != 'get':
if 'content_type' not in kwargs and request_method != 'get':
kwargs['content_type'] = 'application/json'
if 'data' in kwargs and request_method != 'get' and kwargs['content_type'] == 'application/json':
data = kwargs.get('data', '')
Expand Down Expand Up @@ -216,8 +216,11 @@ def test_password_change(self):
"new_password1": "new_person",
"new_password2": "new_person"
}
self.post(self.password_change_url, data=new_password_payload,
status_code=200)
self.post(
self.password_change_url,
data=new_password_payload,
status_code=200
)

# user should not be able to login using old password
self.post(self.login_url, data=login_payload, status_code=400)
Expand All @@ -231,8 +234,11 @@ def test_password_change(self):
"new_password1": "new_person1",
"new_password2": "new_person"
}
self.post(self.password_change_url, data=new_password_payload,
status_code=400)
self.post(
self.password_change_url,
data=new_password_payload,
status_code=400
)

# send empty payload
self.post(self.password_change_url, data={}, status_code=400)
Expand All @@ -252,16 +258,22 @@ def test_password_change_with_old_password(self):
"new_password1": "new_person",
"new_password2": "new_person"
}
self.post(self.password_change_url, data=new_password_payload,
status_code=400)
self.post(
self.password_change_url,
data=new_password_payload,
status_code=400
)

new_password_payload = {
"old_password": self.PASS,
"new_password1": "new_person",
"new_password2": "new_person"
}
self.post(self.password_change_url, data=new_password_payload,
status_code=200)
self.post(
self.password_change_url,
data=new_password_payload,
status_code=200
)

# user should not be able to login using old password
self.post(self.login_url, data=login_payload, status_code=400)
Expand Down Expand Up @@ -364,11 +376,17 @@ def test_registration_with_email_verification(self):
mail_count = len(mail.outbox)

# test empty payload
self.post(self.register_url, data={},
status_code=status.HTTP_400_BAD_REQUEST)
self.post(
self.register_url,
data={},
status_code=status.HTTP_400_BAD_REQUEST
)

self.post(self.register_url, data=self.REGISTRATION_DATA_WITH_EMAIL,
status_code=status.HTTP_201_CREATED)
self.post(
self.register_url,
data=self.REGISTRATION_DATA_WITH_EMAIL,
status_code=status.HTTP_201_CREATED
)
self.assertEqual(get_user_model().objects.all().count(), user_count + 1)
self.assertEqual(len(mail.outbox), mail_count + 1)
new_user = get_user_model().objects.latest('id')
Expand All @@ -379,14 +397,20 @@ def test_registration_with_email_verification(self):
"username": self.USERNAME,
"password": self.PASS
}
self.post(self.login_url, data=payload,
status=status.HTTP_400_BAD_REQUEST)
self.post(
self.login_url,
data=payload,
status=status.HTTP_400_BAD_REQUEST
)

# veirfy email
# verify email
email_confirmation = new_user.emailaddress_set.get(email=self.EMAIL)\
.emailconfirmation_set.order_by('-created')[0]
self.post(self.veirfy_email_url, data={"key": email_confirmation.key},
status_code=status.HTTP_200_OK)
self.post(
self.veirfy_email_url,
data={"key": email_confirmation.key},
status_code=status.HTTP_200_OK
)

# try to login again
self._login()
Expand Down Expand Up @@ -423,8 +447,13 @@ def setUp(self):
@responses.activate
def test_failed_social_auth(self):
# fake response
responses.add(responses.GET, self.graph_api_url, body='', status=400,
content_type='application/json')
responses.add(
responses.GET,
self.graph_api_url,
body='',
status=400,
content_type='application/json'
)

payload = {
'access_token': 'abc123'
Expand All @@ -434,9 +463,14 @@ def test_failed_social_auth(self):
@responses.activate
def test_social_auth(self):
# fake response for facebook call
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true}'
responses.add(responses.GET, self.graph_api_url, body=resp_body,
status=200, content_type='application/json')
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true}' # noqa
responses.add(
responses.GET,
self.graph_api_url,
body=resp_body,
status=200,
content_type='application/json'
)

users_count = get_user_model().objects.all().count()
payload = {
Expand All @@ -459,24 +493,34 @@ def test_social_auth(self):
REST_SESSION_LOGIN=False
)
def test_edge_case(self):
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}'
responses.add(responses.GET, self.graph_api_url,
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}' # noqa
responses.add(
responses.GET,
self.graph_api_url,
body=resp_body % self.EMAIL,
status=200, content_type='application/json')
status=200,
content_type='application/json'
)

# test empty payload
self.post(self.register_url, data={}, status_code=400)

self.post(self.register_url, data=self.REGISTRATION_DATA,
status_code=201)
self.post(
self.register_url,
data=self.REGISTRATION_DATA,
status_code=201
)
new_user = get_user_model().objects.latest('id')
self.assertEqual(new_user.username, self.REGISTRATION_DATA['username'])

# veirfy email
# verify email
email_confirmation = new_user.emailaddress_set.get(email=self.EMAIL)\
.emailconfirmation_set.order_by('-created')[0]
self.post(self.veirfy_email_url, data={"key": email_confirmation.key},
status_code=status.HTTP_200_OK)
self.post(
self.veirfy_email_url,
data={"key": email_confirmation.key},
status_code=status.HTTP_200_OK
)

self._login()
self._logout()
Expand Down
34 changes: 21 additions & 13 deletions rest_auth/views.py
Expand Up @@ -9,9 +9,11 @@
from rest_framework.authtoken.models import Token
from rest_framework.generics import RetrieveUpdateAPIView

from .app_settings import (TokenSerializer, UserDetailsSerializer,
LoginSerializer, PasswordResetSerializer, PasswordResetConfirmSerializer,
PasswordChangeSerializer)
from .app_settings import (
TokenSerializer, UserDetailsSerializer, LoginSerializer,
PasswordResetSerializer, PasswordResetConfirmSerializer,
PasswordChangeSerializer
)


class Login(GenericAPIView):
Expand All @@ -38,12 +40,14 @@ def login(self):
login(self.request, self.user)

def get_response(self):
return Response(self.response_serializer(self.token).data,
status=status.HTTP_200_OK)
return Response(
self.response_serializer(self.token).data, status=status.HTTP_200_OK
)

def get_error_response(self):
return Response(self.serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
return Response(
self.serializer.errors, status=status.HTTP_400_BAD_REQUEST
)

def post(self, request, *args, **kwargs):
self.serializer = self.get_serializer(data=self.request.DATA)
Expand Down Expand Up @@ -114,8 +118,10 @@ def post(self, request, *args, **kwargs):
status=status.HTTP_400_BAD_REQUEST)
serializer.save()
# Return the success message with OK HTTP status
return Response({"success": "Password reset e-mail has been sent."},
status=status.HTTP_200_OK)
return Response(
{"success": "Password reset e-mail has been sent."},
status=status.HTTP_200_OK
)


class PasswordResetConfirm(GenericAPIView):
Expand All @@ -134,8 +140,9 @@ class PasswordResetConfirm(GenericAPIView):
def post(self, request):
serializer = self.get_serializer(data=request.DATA)
if not serializer.is_valid():
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST
)
serializer.save()
return Response({"success": "Password has been reset with the new password."})

Expand All @@ -155,7 +162,8 @@ class PasswordChange(GenericAPIView):
def post(self, request):
serializer = self.get_serializer(data=request.DATA)
if not serializer.is_valid():
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST
)
serializer.save()
return Response({"success": "New password has been saved."})
1 change: 1 addition & 0 deletions runtests.py
@@ -1,4 +1,5 @@
# This file mainly exists to allow python setup.py test to work.
# flake8: noqa
import os
import sys

Expand Down

0 comments on commit a60df71

Please sign in to comment.