Example #1
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)
Example #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)
Example #3
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)
Example #4
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