def post(self, request): serializer = RegisterSerializer(data=request.data) if serializer.is_valid(): serializer.save() instance = serializer.instance return Response(data={"user_id": instance.id, "status": instance.is_active}, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_that_create_throws_exception_when_duplicate_username_is_given( self): self.second_user["username"] = self.user["username"] RegisterSerializer().create(self.user) with self.assertRaises(IntegrityError): RegisterSerializer().create(self.second_user)
def test_given_inactive_user_login_should_fail(self): user = RegisterSerializer().create(self.user) user.is_active = False user.save() with self.assertRaisesRegex(ValidationError, "Incorrect credentials"): LoginSerializer().validate(self.user)
def post(self, request, format=None, *args, **kwargs): serializer = RegisterSerializer(data=request.data) if serializer.is_valid(): profile = serializer.save() else: return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST) self.send_mail(request, profile) # should run asynchronously response = serializer.data response.update({ 'msg': 'Please confirm your email address to complete registration' }) return Response(response, status=status.HTTP_201_CREATED)
def test_that_create_adds_user_with_given_username(self): returned_user_object = RegisterSerializer().create(self.user) created_user_object = User.objects.first() self.assertEqual(returned_user_object.username, self.user["username"]) self.assertEqual(returned_user_object.username, created_user_object.username)
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.save() return Response({ "user": RegisterSerializer(user, context=self.get_serializer_context()).data, "token": AuthToken.objects.create(user)[1] })
def post(self, request, *args, **kwargs): try: data = JSONParser().parse(request) serializer = RegisterSerializer(data=data) if serializer.is_valid(): user = serializer.save() token, _ = Token.objects.get_or_create(user=user) else: return Response( { 'status': 'error', 'message': serializer.errors }, status=400) return Response({ 'status': 'success', 'token': token.key }, status=201) except Exception as e: return Response({'status': 'error', 'message': str(e)}, status=400)
def userListApi(request): if request.method == 'GET': pagination_class = api_settings.DEFAULT_PAGINATION_CLASS paginator = pagination_class() queryset = CustomUser.objects.filter(is_staff=False).order_by('nim') page = paginator.paginate_queryset(queryset, request) serializer = UserSerializer(page, many=True) return paginator.get_paginated_response(serializer.data) if request.method == 'POST': data = request.data serializer = RegisterSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(request): # Comprovem que l'usuari no sigui manager if not request.data["is_manager"]: return Response("A manager can not be client", status=status.HTTP_400_BAD_REQUEST) # Creem l'usuari user = RegisterSerializer(data=request.data) user.is_valid(raise_exception=True) user.save() # Creem el manager amb l'id i l'username de l'usuari manager = { "_id": user.data["id"], "username": user.data["username"], "nif": request.data.get("nif", None), "telf": request.data.get("telf", None) } serializer = ManagersSerializer(data=manager) serializer.is_valid(raise_exception=True) serializer.save() return Response(data=serializer.data, status=status.HTTP_201_CREATED)
def post(request): # Comprovem que l'usuari no sigui manager if request.data["is_manager"]: return Response("A client can not be manager", status=status.HTTP_400_BAD_REQUEST) # Creem l'usuari user = RegisterSerializer(data=request.data) user.is_valid(raise_exception=True) user.save() # Creem el client amb l'id i l'username de l'usuari client = { "_id": user.data["id"], "username": user.data["username"], "age": request.data.get('age', None), "country": request.data.get('country', None) } serializer = ClientSerializer(data=client) serializer.is_valid(raise_exception=True) serializer.save() return Response(data=serializer.data, status=status.HTTP_201_CREATED)
def test_that_create_can_add_multiple_users(self): RegisterSerializer().create(self.user) RegisterSerializer().create(self.second_user) self.assertEqual(User.objects.all().count(), 2)
def post(self, request): serialize = RegisterSerializer(data=request.data) serialize.is_valid(raise_exception=True) user = serialize.save() instance, token = AuthToken.objects.create(user) return Response({"user": UserSerializer(user).data, "token": token})
def get_or_add_users(self, users): for email in users: if not User.objects.filter(email=email).exists(): register_serializer = RegisterSerializer(data={'email': email}) register_serializer.is_valid(raise_exception=True) register_serializer.save()
def test_that_created_user_is_instance_of_user_class(self): returned_user_object = RegisterSerializer().create(self.user) created_user_object = User.objects.first() self.assertTrue(isinstance(returned_user_object, User)) self.assertTrue(isinstance(created_user_object, User))
def test_that_create_adds_new_user(self): RegisterSerializer().create(self.user) self.assertEqual(User.objects.all().count(), 1)
def test_given_registered_user_when_credentials_are_correct_validate_should_login_user( self): RegisterSerializer().create(self.user) logged_user = LoginSerializer().validate(self.user) self.assertEqual(logged_user, User.objects.first())
def test_that_create_user_has_same_password_as_returned(self): returned_user_object = RegisterSerializer().create(self.user) created_user_object = User.objects.first() self.assertEqual(returned_user_object.password, created_user_object.password)