Example #1
0
 def post(self, request, format=None):
     serializer = UserCreationSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     validated_data = serializer.validated_data
     user = User.objects.create_user(**validated_data)
     serializer = UserSerializer(instance=user)
     return Response(serializer.data, status=status.HTTP_201_CREATED)
Example #2
0
 def create(self, request):
     try:
         data = JSONParser().parse(request)
         user_serializer = UserCreationSerializer(data=data)
         if 'uid' not in data:
             serializer = NormalUserCreationSerializer(data=data)
             tipo = "normal"
         else:
             serializer = ExternUserCreationSerializer(data=data)
             tipo = "externo"
         if serializer.is_valid() and user_serializer.is_valid():
             existe_username = User.objects.filter(
                 username=user_serializer.validated_data.get('username'))
             existe_email = User.objects.filter(
                 email=user_serializer.validated_data.get('email'))
             if existe_email.exists() or existe_username.exists():
                 return Response(status=status.HTTP_400_BAD_REQUEST)
             u = User()
             u.username = user_serializer.validated_data.get('username')
             u.firstname = user_serializer.validated_data.get('firstname')
             u.lastname = user_serializer.validated_data.get('lastname')
             u.email = user_serializer.validated_data.get('email')
             u.save()
             if tipo == "externo":
                 eu = ExternUser()
                 eu.uid = serializer.validated_data.get('uid')
                 eu.provider = serializer.validated_data.get('provider')
                 eu.user = u
                 eu.save()
                 dto = ExternalUserDTO(id=u.id,
                                       username=u.username,
                                       firstname=u.firstname,
                                       lastname=u.lastname,
                                       email=u.email,
                                       uid=eu.uid,
                                       provider=eu.provider)
                 serialized = ExternalUserDTOSerializer(dto)
             else:
                 nu = NormalUser()
                 nu.password = serializer.validated_data.get('password')
                 u.gender = serializer.validated_data.get('gender')
                 u.birthdate = serializer.validated_data.get('birthdate')
                 u.save()
                 nu.user = u
                 nu.save()
                 dto = NormalUserDTO(id=u.id,
                                     username=u.username,
                                     firstname=u.firstname,
                                     lastname=u.lastname,
                                     email=u.email,
                                     password=nu.password,
                                     gender=u.gender,
                                     birthdate=u.birthdate)
                 serialized = NormalUserDTOSerializer(dto)
             return Response(serialized.data,
                             status=status.HTTP_201_CREATED)
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     except User.DoesNotExist:
         return Response(status=status.HTTP_400_BAD_REQUEST)
Example #3
0
    def test_already_registered_user(self):
        self.data = self.set_data()
        user1 = User.objects.create_user(username=self.data['username'], \
            email=self.data['email'], password=self.data['password'])

        with self.assertRaises(serializers.ValidationError):
            serial = UserCreationSerializer(data=self.data)
            self.assertFalse(serial.is_valid(raise_exception=True))
Example #4
0
 def test_create_user_with_correct_data(self):
     self.data = self.set_data()
     serial = UserCreationSerializer(data=self.data)
     self.assertTrue(serial.is_valid())
     serial.save()
     self.assertEqual(
         User.objects.all().filter(username=self.data['username']).count(),
         1)
Example #5
0
def user_register(request):
    """API view for registering new users.
    """

    serializer = UserCreationSerializer(data=request.data,
                                        context={'request': request})
    if serializer.is_valid(raise_exception=True):
        serializer.save()
        return Response(status=status.HTTP_201_CREATED)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)
Example #6
0
 def test_user_with_no_name_is_spoko(self):
     self.data = self.set_data(name='')
     serial = UserCreationSerializer(data=self.data)
     self.assertTrue(serial.is_valid())
Example #7
0
    def test_password_is_required(self):
        self.data = self.set_data(passw='')

        with self.assertRaises(serializers.ValidationError):
            serial = UserCreationSerializer(data=self.data)
            self.assertFalse(serial.is_valid(raise_exception=True))
Example #8
0
    def test_user_with_incorrect_email(self):
        self.data = self.set_data(email='email')

        with self.assertRaises(serializers.ValidationError):
            serial = UserCreationSerializer(data=self.data)
            self.assertFalse(serial.is_valid(raise_exception=True))