def test_raise_error_when_missing_required_field(self): # pylint: disable=no-self-use """ test raise error when missing required field """ incomplete_data = { 'id': 1, } serializer = UserProfileSerializer(data=incomplete_data) # Este ContextManager nos permite verificar que # se ejecute correctamente una Excepcion with pytest.raises(ValidationError): serializer.is_valid(raise_exception=True)
def post(self, request, format=None): serializer = UserProfileSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(None, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): user_profile = self.get_user_profile(self) serializer = UserProfileSerializer(user_profile, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response( {"Status 201": "User Profile updated successfully"}, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = UserProfileSerializer(data=request.data) if serializer.is_valid(): user = UserProfile.objects.create_user( email=request.data.get("email"), password=request.data.get("password")) serializer_user = UserProfileSerializer(user) return Response(serializer_user.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def profile(self, request): profile = { "email": request.user.email, "roles": get_user_roles(request.user), "is_superuser": request.user.is_superuser, } profile_serializer = UserProfileSerializer(data=profile) if not profile_serializer.is_valid(): return Response( {"detail": "Invalid user profile"}, status=status.HTTP_403_FORBIDDEN ) return Response(data=profile_serializer.data)
def post(self, request): # data = request.data data = request.data['params'] data['password'] = make_password(data['password']) serializer = UserProfileSerializer(data=data) if serializer.is_valid(): serializer.save() userId = serializer.data['id'] if 'extraParams' in request.data: extraData = request.data['extraParams'] if 'userRoleParams' in extraData: userRoleParams = extraData['userRoleParams'] if not userRoleParams is None: u = UserProfile.objects.get(id=userId) g = Group.objects.get(id=int(userRoleParams)) u.groups.add(g) u.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_auth(request): serialized = UserProfileSerializer(data=request.data) jwt = JSONWebTokenSerializer(data=request.data) data = jwt.is_valid() if serialized.is_valid(): serialized.save() result = { "success": True, "sessionId": jwt.validate(request.data)["token"] } code = status.HTTP_201_CREATED else: code = status.HTTP_400_BAD_REQUEST result = { "success": False, "message": "Wrong request data", "details": str(serialized._errors), "code": code } return Response(result, status=code)
def put(self, request, pk): user = self.get_object(pk) data = request.data['params'] serializer = UserProfileSerializer(user, data=data) if serializer.is_valid(): serializer.save() if user.is_superuser: pass else: if 'extraParams' in request.data: extraData = request.data['extraParams'] if 'userRoleParams' in extraData: userRoleParams = extraData['userRoleParams'] if not userRoleParams is None: g = Group.objects.get(id=userRoleParams) u = UserProfile.objects.get(id=pk) u.groups.clear() u.groups.add(g) u.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)