summaryrefslogtreecommitdiffstats
path: root/tests_multitenant/conftest.py
blob: c64523f21619f76e658e8c29b77145e5c62150e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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'
                    },
                    {'slug': 'another',
                     'title': 'Agent portal',
                     'service-id': 'combo',
                     'template_name': '...portal-agent...',
                     'base_url': 'http://portal-agent.example.net'
                    },
                    {'slug': 'another2',
                     'title': 'User portal',
                     'service-id': 'combo',
                     'template_name': '...portal-user...',
                     'base_url': 'http://portal-user.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