allow return detail user on registration

This commit is contained in:
Egor Poderyagin 2017-05-12 14:12:44 +03:00
parent f48d19b3ba
commit a08155a481
2 changed files with 18 additions and 3 deletions

View File

@ -14,7 +14,8 @@ from allauth.account.views import ConfirmEmailView
from allauth.account.utils import complete_signup from allauth.account.utils import complete_signup
from allauth.account import app_settings as allauth_settings from allauth.account import app_settings as allauth_settings
from rest_auth.app_settings import (TokenSerializer, from rest_auth.app_settings import (UserDetailsSerializer,
TokenSerializer,
JWTSerializer, JWTSerializer,
create_token) create_token)
from rest_auth.models import TokenModel from rest_auth.models import TokenModel
@ -49,8 +50,9 @@ class RegisterView(CreateAPIView):
'token': self.token 'token': self.token
} }
return JWTSerializer(data).data return JWTSerializer(data).data
else: elif getattr(settings, 'REST_USE_TOKEN', True):
return TokenSerializer(user.auth_token).data return TokenSerializer(user.auth_token).data
return UserDetailsSerializer(user).data
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
@ -66,12 +68,13 @@ class RegisterView(CreateAPIView):
user = serializer.save(self.request) user = serializer.save(self.request)
if getattr(settings, 'REST_USE_JWT', False): if getattr(settings, 'REST_USE_JWT', False):
self.token = jwt_encode(user) self.token = jwt_encode(user)
else: elif getattr(settings, 'REST_USE_TOKEN', True):
create_token(self.token_model, user, serializer) create_token(self.token_model, user, serializer)
complete_signup(self.request._request, user, complete_signup(self.request._request, user,
allauth_settings.EMAIL_VERIFICATION, allauth_settings.EMAIL_VERIFICATION,
None) None)
return user return user

View File

@ -426,6 +426,18 @@ class APITestCase1(TestCase, BaseAPITestCase):
self._login() self._login()
self._logout() self._logout()
@override_settings(REST_USE_TOKEN=False)
def test_registration_without_token(self):
user_count = get_user_model().objects.all().count()
self.post(self.register_url, data=self.REGISTRATION_DATA_WITH_EMAIL, status_code=201)
self.assertEqual(self.response.json['username'], self.USERNAME)
self.assertEqual(self.response.json['email'], self.EMAIL)
self.assertEqual(get_user_model().objects.all().count(), user_count + 1)
self._login()
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'