56 lines
2.3 KiB
Python
56 lines
2.3 KiB
Python
import pytest
|
|
import mock
|
|
import os
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
class RecordTenant(object):
|
|
def __init__(self):
|
|
self.tenants = []
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
from django.db import connection
|
|
self.tenants.append(connection.tenant)
|
|
|
|
@mock.patch('django.contrib.sessions.management.commands.clearsessions.Command.handle')
|
|
def test_all_tenants(handle, tenants):
|
|
from django.core.management import execute_from_command_line
|
|
handle.side_effect = RecordTenant()
|
|
execute_from_command_line(['manage.py', 'tenant_command', 'clearsessions', '--all-tenants'])
|
|
assert handle.call_count == 2
|
|
assert len(handle.side_effect.tenants) == 2
|
|
assert set(tenant.domain_url for tenant in handle.side_effect.tenants) == \
|
|
set(['tenant1.example.net', 'tenant2.example.net'])
|
|
|
|
@mock.patch('django.contrib.sessions.management.commands.clearsessions.Command.handle')
|
|
def test_one_tenant(handle, tenants, tenant_in_call=None):
|
|
from django.core.management import execute_from_command_line
|
|
handle.side_effect = RecordTenant()
|
|
execute_from_command_line(['manage.py', 'tenant_command', 'clearsessions', '-d', 'tenant2.example.net'])
|
|
assert handle.call_count == 1
|
|
assert len(handle.side_effect.tenants) == 1
|
|
assert handle.side_effect.tenants[0].domain_url == 'tenant2.example.net'
|
|
|
|
def test_delete_tenant(tenants):
|
|
from django.core.management import execute_from_command_line
|
|
from hobo.multitenant.middleware import TenantMiddleware
|
|
base = os.path.dirname(tenants[0].get_directory())
|
|
if any('removed' in d for d in os.listdir(base)):
|
|
assert False
|
|
def get_schemas():
|
|
from django.db import connection
|
|
cursor = connection.cursor()
|
|
cursor.execute('select schema_name from information_schema.schemata')
|
|
return [x[0] for x in cursor.fetchall()]
|
|
if any('removed' in x for x in get_schemas()):
|
|
assert False
|
|
all_tenants = list(TenantMiddleware.get_tenants())
|
|
assert len(all_tenants) == 2
|
|
execute_from_command_line(['manage.py', 'delete_tenant', 'tenant2.example.net'])
|
|
if not any('removed' in d for d in os.listdir(base)):
|
|
assert False
|
|
if not any('removed' in x for x in get_schemas()):
|
|
assert False
|
|
all_tenants = list(TenantMiddleware.get_tenants())
|
|
assert len(all_tenants) == 1
|