hobo/tests_authentic/conftest.py

115 lines
3.7 KiB
Python

import os
import json
from django_webtest import WebTestMixin, DjangoTestApp
import pytest
from django.db import connection, transaction
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, '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',
'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()
tenants.append(t)
return t
try:
yield factory
finally:
# cleanup all created tenants
connection.set_schema_to_public()
with tenant_context(FakeTenant('public')):
for tenant in tenants:
tenant.delete(force_drop=True)
@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')