def test_validate_registration_token(self): registration_token = Registrations.generate_registration_token('*****@*****.**') token_email = Registrations.validate_registration_token(registration_token) self.assertEqual(token_email, '*****@*****.**') registration_token = Registrations.generate_registration_token('*****@*****.**') token_email = Registrations.validate_registration_token(registration_token) self.assertNotEqual(token_email, '*****@*****.**')
def list(self, request): """ Validates a registration token. The request errors out if the token has expired or is invalid. Request URL: GET /api/v1/users/registration/?token=<token> """ if not request.GET.get('token', False): raise ValidationError(detail='Registration token is required.') token_email = Registrations.validate_registration_token(request.GET.get('token', False)) if token_email is None: raise ValidationError(detail='Token expired or invalid.') return Response({'email': token_email}, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): """ Attempts to register a new user based on the registration token. """ if not request.GET.get('token', False): raise ValidationError(detail='Registration token is required.') token_email = Registrations.validate_registration_token( request.GET.get('token', False)) if token_email is None: raise ValidationError(detail='Token expired or invalid.') serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = User.objects.create_user(email=token_email, **serializer.validated_data) user_group = AbakusGroup.objects.get(name=constants.USER_GROUP) user_group.add_user(user) payload = get_jwt_token(user) return Response(payload, status=status.HTTP_201_CREATED)