hobo/tests_authentic/conftest.py

69 lines
2.1 KiB
Python

import os
import tempfile
import shutil
import json
import pytest
@pytest.fixture
def tenant_base(request, settings):
base = tempfile.mkdtemp('authentic-tenant-base')
settings.TENANT_BASE = base
def fin():
shutil.rmtree(base)
request.addfinalizer(fin)
return base
@pytest.fixture(scope='function')
def tenant(transactional_db, request, settings, tenant_base):
from hobo.multitenant.models import Tenant
base = tenant_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}, fd)
with open(os.path.join(tenant_dir, 'hobo.json'), 'w') as fd:
json.dump({
'variables': {
'hobo_test_variable': True,
'other_variable': 'foo',
},
'services': [
{'slug': 'test',
'service-id': 'authentic',
'title': 'Test',
'this': True,
'secret_key': '12345',
'base_url': 'http://%s' % name,
'variables': {
'other_variable': 'bar',
}
},
{'slug': 'other',
'title': 'Other',
'service-id': 'welco',
'secret_key': 'abcdef',
'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('authentic.example.net')]
def fin():
from django.db import connection
connection.set_schema_to_public()
for t in tenants:
t.delete(True)
request.addfinalizer(fin)
return tenants[0]