예제 #1
0
    def destroy(self, request, *args, **kwargs):
        """Delete a customer.

            @api {delete} /api/v1/customers/:uuid/ Delete a customer
            @apiName DeleteCustomers
            @apiGroup Customer
            @apiVersion 1.0.0
            @apiDescription Delete a customer.

            @apiHeader {String} token Service Admin authorizaton token.
            @apiHeaderExample {json} Header-Example:
                {
                    "Authorizaton": "Token 45138a913da44ab89532bab0352ef84b"
                }

            @apiParam {String} uuid Customer unique ID.

            @apiSuccessExample {json} Success-Response:
                HTTP/1.1 204 NO CONTENT
            """
        user_savepoint = transaction.savepoint()
        customer_manager = None
        try:
            customer_manager = CustomerManager(kwargs['uuid'])
        except CustomerManagerDoesNotExist:
            LOG.error('Unable to find provider for uuid {}.'.format(
                kwargs['uuid']))
            raise NotFound

        providers = ProviderManager.get_providers_queryset_for_customer(
            customer_manager.get_model())

        try:
            customer_manager.remove_users(request.user)
            customer_manager.remove_tenant(request.user)
        except DatabaseError:
            transaction.savepoint_rollback(user_savepoint)
            LOG.error('Failed to remove assets for customer {}.'.format(
                customer_manager.get_name()))
            raise UserDeleteException

        try:
            for provider in providers:
                provider_manager = ProviderManager(provider.uuid)
                provider_manager.remove(request.user,
                                        customer_remove_context=True)
        except (DatabaseError, ProviderManagerError):
            transaction.savepoint_rollback(user_savepoint)
            LOG.error('{} failed to remove provider {}.'.format(
                request.user.username, provider_manager.get_name()))
            raise ProviderDeleteException

        http_response = super().destroy(request=request,
                                        args=args,
                                        kwargs=kwargs)
        if http_response.status_code is not 204:
            transaction.savepoint_rollback(user_savepoint)
        return http_response
예제 #2
0
    def test_remove_users(self):
        """Remove users associated with a customer."""
        # Create Customer
        customer = None
        serializer = CustomerSerializer(data=self.customer_data[0])
        if serializer.is_valid(raise_exception=True):
            customer = serializer.save()
        customer_uuid = customer.uuid

        # Add another user
        group = Group.objects.get(name=customer.name)

        new_user_dict = self.gen_user_data()
        user_serializer = UserSerializer(data=new_user_dict)
        new_user = None
        if user_serializer.is_valid(raise_exception=True):
            new_user = user_serializer.save()
        group.user_set.add(new_user)

        manager = CustomerManager(customer_uuid)

        # Attempt to remove as customer owner
        with self.assertRaises(CustomerManagerPermissionError):
            manager.remove_users(customer.owner)

        # Attempt to remove as regular user
        with self.assertRaises(CustomerManagerPermissionError):
            manager.remove_users(new_user)

        # Attempt to remove as super user
        superuser = User.objects.filter(is_superuser=True).first()
        manager.remove_users(superuser)

        self.assertFalse(manager.get_users_for_customer())