예제 #1
0
    def test_tenant_cache_functions_success(self, redis_connection):
        schema_name = self.tenant.schema_name
        key = f"rbac::tenant::schema={schema_name}"
        dump_content = pickle.dumps(self.tenant)

        # Save tenant to cache
        tenant_cache = TenantCache()
        tenant_cache.save_tenant(self.tenant)
        self.assertTrue(call().__enter__().set(key, dump_content) in
                        redis_connection.pipeline.mock_calls)

        redis_connection.get.return_value = dump_content
        # Get tenant from cache
        tenant = tenant_cache.get_tenant(schema_name)
        redis_connection.get.assert_called_once_with(key)
        self.assertEqual(tenant, self.tenant)

        # Delete tenant from cache
        tenant_cache.delete_tenant(schema_name)
        redis_connection.delete.assert_called_once_with(key)
예제 #2
0
import logging

import pytz
from django.conf import settings
from django.db.migrations.recorder import MigrationRecorder
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from management.cache import TenantCache
from management.models import Group, Role
from management.tasks import run_migrations_in_worker, run_seeds_in_worker
from tenant_schemas.utils import tenant_context

from api.models import Tenant

logger = logging.getLogger(__name__)
TENANTS = TenantCache()


def destructive_ok():
    """Determine if it's ok to run destructive operations."""
    now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
    return now < settings.INTERNAL_DESTRUCTIVE_API_OK_UNTIL


def tenant_is_unmodified():
    """Determine whether or not the tenant has been modified."""
    if Role.objects.filter(system=True).count() != Role.objects.count():
        return False
    if Group.objects.count() != 1:
        return False
    if Group.objects.filter(system=True).count() != 1: