hobo/tests_multitenant/conftest.py

82 lines
3.2 KiB
Python

import os
import tempfile
import shutil
import json
import pytest
@pytest.fixture(scope='function')
def tenants(transactional_db, request, settings):
from hobo.multitenant.models import Tenant
base = tempfile.mkdtemp('combo-tenant-base')
settings.TENANT_BASE = base
@pytest.mark.django_db
def make_tenant(name):
tenant_dir = os.path.join(base, name)
os.mkdir(tenant_dir)
with open(os.path.join(tenant_dir, 'unsecure'), 'w') as fd:
fd.write('1')
with open(os.path.join(tenant_dir, 'settings.json'), 'w') as fd:
json.dump({
'HOBO_TEST_VARIABLE': name,
'UPDATE_ME.update': {'y': 2},
'EXTEND_ME.extend': [2],
}, fd)
with open(os.path.join(tenant_dir, 'hobo.json'), 'w') as fd:
json.dump({
'variables': {
'hobo_test_variable': True,
'other_variable': 'foo',
'theme': 'publik',
'SETTING_GLOBAL1': True,
'SETTING_GLOBAL2.extend': [2, 3, 4],
'SETTING_GLOBAL3.update': {"x": 1, "y": 2},
'SETTING_OVERRIDE1': False,
'SETTING_OVERRIDE2.extend': [6, 7, 8],
'SETTING_OVERRIDE3.update': {"a": 1, "b": 2},
},
'services': [
{'slug': 'test',
'title': 'Test',
'service-id': 'welco',
'this': True,
'secret_key': '12345',
'base_url': 'http://%s' % name,
'saml-sp-metadata-url': 'http://%s/saml/metadata' % name,
'variables': {
'other_variable': 'bar',
'SETTING_OVERRIDE1': True,
'SETTING_OVERRIDE2.extend': [name, 7, 8],
'SETTING_OVERRIDE3.update': {"a": name, "b": 2},
'SETTING_LOCAL1': False,
'SETTING_LOCAL2.extend': [name, 7, 8],
'SETTING_LOCAL3.update': {"a": name, "b": 2},
}
},
{'slug': 'slug-with-hyphen',
'title': 'Hyphen',
'base_url': 'http://slug-with-hyphen.example.net'
},
{'slug': 'other',
'title': 'Other',
'secret_key': 'abcde',
'service-id': 'authentic',
'base_url': 'http://other.example.net'},
]}, fd)
t = Tenant(domain_url=name,
schema_name=name.replace('-', '_').replace('.', '_'))
t.create_schema()
return t
tenants = [make_tenant('tenant1.example.net'), make_tenant('tenant2.example.net')]
def fin():
from django.db import connection
from hobo.multitenant.middleware import TenantMiddleware
connection.set_schema_to_public()
for t in TenantMiddleware.get_tenants():
t.delete(True)
shutil.rmtree(base)
request.addfinalizer(fin)
return tenants