Skip to content

Commit

Permalink
Allow registration if no token model is configured (#343)
Browse files Browse the repository at this point in the history
* Fix error during registration if REST_AUTH_TOKEN_MODEL is None

* Improve registration test to check if it works without token model

* Add missing mandatory registration url
  • Loading branch information
tgamauf committed Dec 20, 2021
1 parent 26ecb1a commit a2638b4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
22 changes: 16 additions & 6 deletions dj_rest_auth/registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def get_response_data(self, user):
'refresh_token': self.refresh_token,
}
return JWTSerializer(data, context=self.get_serializer_context()).data
elif getattr(settings, 'REST_SESSION_LOGIN', False):
return None
else:
return TokenSerializer(user.auth_token, context=self.get_serializer_context()).data

Expand All @@ -66,20 +68,28 @@ def create(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
user = self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
data = self.get_response_data(user)

if data:
response = Response(
data,
status=status.HTTP_201_CREATED,
headers=headers,
)
else:
response = Response(status=status.HTTP_204_NO_CONTENT, headers=headers)

return Response(
self.get_response_data(user),
status=status.HTTP_201_CREATED,
headers=headers,
)
return response

def perform_create(self, serializer):
user = serializer.save(self.request)
if allauth_settings.EMAIL_VERIFICATION != \
allauth_settings.EmailVerificationMethod.MANDATORY:
if getattr(settings, 'REST_USE_JWT', False):
self.access_token, self.refresh_token = jwt_encode(user)
else:
elif not getattr(settings, 'REST_SESSION_LOGIN', False):
# Session authentication isn't active either, so this has to be
# token authentication
create_token(self.token_model, user, serializer)

complete_signup(
Expand Down
4 changes: 2 additions & 2 deletions dj_rest_auth/tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ def init(self):
self.social_account_list_url = reverse('social_account_list')
self.resend_email_url = reverse("rest_resend_email")

def _login(self):
def _login(self, expected_status_code=status.HTTP_200_OK):
payload = {
'username': self.USERNAME,
'password': self.PASS,
}
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
self.post(self.login_url, data=payload, status_code=expected_status_code)

def _logout(self):
self.post(self.logout_url, status=status.HTTP_200_OK)
14 changes: 14 additions & 0 deletions dj_rest_auth/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,20 @@ def test_registration_with_jwt(self):
self._login()
self._logout()

@override_settings(REST_SESSION_LOGIN=True)
@override_settings(REST_AUTH_TOKEN_MODEL=None)
def test_registration_with_session(self):
user_count = get_user_model().objects.all().count()

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

result = self.post(self.register_url, data=self.REGISTRATION_DATA, status_code=204)
self.assertEqual(result.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()
data['password2'] = 'foobar'
Expand Down

0 comments on commit a2638b4

Please sign in to comment.