Exemplo n.º 1
0
    def test_get_users_for_customer(self):
        """Can current user remove provider."""
        # 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)
        users = manager.get_users_for_customer()

        self.assertEqual(len(users), 2)

        new_user_found = False
        owner_found = False
        for found_user in users:
            if found_user.username == new_user_dict['username']:
                new_user_found = True
            elif found_user.username == customer.owner.username:
                owner_found = True

        self.assertTrue(new_user_found)
        self.assertTrue(owner_found)
Exemplo n.º 2
0
    def test_get_tenant(self):
        """Get the tenant object for a customer."""
        # Create customer.
        customer_name = 'test_customer_tenant'
        customer_json = {'name': customer_name,
                         'owner': self.gen_user_data()}

        customer = None
        serializer = CustomerSerializer(data=customer_json)
        if serializer.is_valid(raise_exception=True):
            customer = serializer.save()
        customer_uuid = customer.uuid

        # Create tenant
        customer_obj = Customer.objects.filter(name=customer_name).get()
        tenant = Tenant(schema_name=customer_obj.schema_name)
        tenant.save()

        # Get manager
        manager = CustomerManager(customer_uuid)
        customer_obj = manager.get_model()
        customer_schema_name = customer_obj.schema_name

        # Verify tenant is returned from manager
        tenant_obj = Tenant.objects.filter(schema_name=customer_schema_name).get()

        self.assertEqual(manager.get_tenant(), tenant_obj)
Exemplo n.º 3
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())
Exemplo n.º 4
0
    def test_get_name(self):
        """Can the customer name be returned."""
        # 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

        manager = CustomerManager(customer_uuid)
        self.assertEqual(manager.get_name(), self.customer_data[0]['name'])
Exemplo n.º 5
0
    def test_removing_tenant(self):
        """Remove the tenant object for a customer."""
        # Create customer.
        customer_name = 'test_customer_tenant'
        customer_json = {'name': customer_name,
                         'owner': self.gen_user_data()}

        customer = None
        serializer = CustomerSerializer(data=customer_json)
        if serializer.is_valid(raise_exception=True):
            customer = serializer.save()
        customer_uuid = customer.uuid

        # Create tenant
        customer_obj = Customer.objects.filter(name=customer_name).get()
        tenant = Tenant(schema_name=customer_obj.schema_name)
        tenant.save()

        # Get manager
        manager = CustomerManager(customer_uuid)
        customer_obj = manager.get_model()
        customer_schema_name = customer_obj.schema_name

        # Verify tenant is returned from manager
        tenant_obj = Tenant.objects.filter(schema_name=customer_schema_name).get()

        self.assertEqual(manager.get_tenant(), tenant_obj)

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

        # Attempt to remove tenant as super user
        superuser = User.objects.filter(is_superuser=True).first()
        manager.remove_tenant(superuser)
Exemplo n.º 6
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
Exemplo n.º 7
0
    def test_get_tenant_not_found(self):
        """Try to get a missing tenant object for a customer."""
        # Create customer.
        customer_name = 'test_customer_tenant'
        customer_json = {'name': customer_name, 'owner': self.gen_user_data()}

        customer = None
        serializer = CustomerSerializer(data=customer_json)
        if serializer.is_valid(raise_exception=True):
            customer = serializer.save()
        customer_uuid = customer.uuid

        # Get manager
        manager = CustomerManager(customer_uuid)

        self.assertEqual(manager.get_tenant(), None)
Exemplo n.º 8
0
def get_tenant(user):
    """Get the tenant for the given user.

    Args:
        user    (DjangoUser): user to get the associated tenant
    Returns:
        (Tenant): Object used to get tenant specific data tables
    Raises:
        (ValidationError): If no tenant could be found for the user
    """
    tenant = None
    group = user.groups.first()
    if group:
        try:
            customer = Customer.objects.get(pk=group.id)
            manager = CustomerManager(customer.uuid)
            tenant = manager.get_tenant()
        except Customer.DoesNotExist:
            pass
    if tenant is None:
        error = {'details': _('Invalid user definition')}
        raise ValidationError(error)
    return tenant
Exemplo n.º 9
0
 def test_invalid_uuid(self):
     """Exception test for invalid uuid."""
     with self.assertRaises(CustomerManagerValidationError):
         uuid = 'invalidid'
         CustomerManager(uuid)