126 lines
4.2 KiB
Python
126 lines
4.2 KiB
Python
import json
|
|
import os
|
|
|
|
import django.db.utils
|
|
import pytest
|
|
from django.core.management import call_command
|
|
from django.db import connection, transaction
|
|
from django_webtest import DjangoTestApp, WebTestMixin
|
|
from tenant_schemas.postgresql_backend.base import FakeTenant
|
|
from tenant_schemas.utils import tenant_context
|
|
|
|
from hobo.multitenant.models import Tenant
|
|
|
|
|
|
@pytest.fixture
|
|
def tenant_base(tmpdir, settings):
|
|
base = str(tmpdir.mkdir('authentic-tenant-base'))
|
|
settings.TENANT_BASE = base
|
|
return base
|
|
|
|
|
|
@pytest.fixture
|
|
def tenant_factory(transactional_db, tenant_base, settings):
|
|
tenants = []
|
|
|
|
settings.ALLOWED_HOSTS = ['*']
|
|
|
|
def factory(name):
|
|
tenant_dir = os.path.join(tenant_base, name)
|
|
os.mkdir(tenant_dir)
|
|
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': 'https://%s' % name,
|
|
'variables': {
|
|
'other_variable': 'bar',
|
|
},
|
|
},
|
|
{
|
|
'slug': 'other',
|
|
'title': 'Other',
|
|
'service-id': 'welco',
|
|
'secret_key': 'abcdef',
|
|
'url': 'http://other.example.net',
|
|
'base_url': 'http://other.example.net',
|
|
'provisionning-url': 'http://other.example.net/__provision__/',
|
|
'saml-sp-metadata-url': 'http://other.example.net/metadata/',
|
|
},
|
|
{
|
|
'slug': 'more',
|
|
'title': 'More',
|
|
'service-id': 'wcs',
|
|
'secret_key': 'abcdef',
|
|
'url': 'http://more.example.net',
|
|
'base_url': 'http://more.example.net',
|
|
'provisionning-url': 'http://more.example.net/__provision__/',
|
|
'saml-sp-metadata-url': 'http://more.example.net/metadata/',
|
|
},
|
|
],
|
|
},
|
|
fd,
|
|
)
|
|
schema_name = name.replace('-', '_').replace('.', '_')
|
|
t = Tenant(domain_url=name, schema_name=schema_name)
|
|
with transaction.atomic():
|
|
t.create_schema(check_if_exists=True)
|
|
connection.set_schema_to_public()
|
|
tenants.append(t)
|
|
return t
|
|
|
|
try:
|
|
yield factory
|
|
finally:
|
|
# cleanup all created tenants
|
|
connection.set_schema_to_public()
|
|
for tenant in tenants:
|
|
with tenant_context(tenant):
|
|
call_command(
|
|
"flush",
|
|
verbosity=0,
|
|
interactive=False,
|
|
database='default',
|
|
reset_sequences=False,
|
|
allow_cascade=True,
|
|
inhibit_post_migrate=False,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def tenant(tenant_factory):
|
|
return tenant_factory('authentic.example.net')
|
|
|
|
|
|
@pytest.fixture
|
|
def app_factory(request):
|
|
wtm = WebTestMixin()
|
|
wtm._patch_settings()
|
|
|
|
def factory(hostname='testserver'):
|
|
if hasattr(hostname, 'domain_url'):
|
|
hostname = hostname.domain_url
|
|
return DjangoTestApp(extra_environ={'HTTP_HOST': hostname})
|
|
|
|
try:
|
|
yield factory
|
|
finally:
|
|
wtm._unpatch_settings()
|
|
|
|
|
|
@pytest.fixture
|
|
def notify_agents(mocker):
|
|
yield mocker.patch('hobo.agent.authentic2.provisionning.notify_agents')
|