|
1 |
# hobo - portal to configure and deploy applications
|
|
2 |
# Copyright (C) 2019 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU Affero General Public License
|
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
|
17 |
from django.db import connection
|
|
18 |
from django.core.management import call_command
|
|
19 |
from django.core.management.base import CommandError
|
|
20 |
from django.contrib.auth.models import User
|
|
21 |
|
|
22 |
from tenant_schemas.utils import tenant_context
|
|
23 |
|
|
24 |
from hobo.multitenant.middleware import TenantMiddleware
|
|
25 |
|
|
26 |
import mock
|
|
27 |
import pytest
|
|
28 |
|
|
29 |
|
|
30 |
@pytest.fixture(autouse=True)
|
|
31 |
def configuration(settings, tmpdir):
|
|
32 |
settings.TENANT_BASE = str(tmpdir.mkdir('tenants'))
|
|
33 |
|
|
34 |
|
|
35 |
def schema_exists(schema_name):
|
|
36 |
with connection.cursor() as cursor:
|
|
37 |
cursor.execute('select schema_name from information_schema.schemata')
|
|
38 |
return 'www_example_com' in [row[0] for row in cursor.fetchall()]
|
|
39 |
|
|
40 |
|
|
41 |
def test_create_tenant(db):
|
|
42 |
assert not schema_exists('www_example_com')
|
|
43 |
call_command('create_tenant', 'www.example.com')
|
|
44 |
assert schema_exists('www_example_com')
|
|
45 |
tenants = list(TenantMiddleware.get_tenants())
|
|
46 |
assert len(tenants) == 1
|
|
47 |
tenant = tenants[0]
|
|
48 |
assert tenant.domain_url == 'www.example.com'
|
|
49 |
assert tenant.schema_name == 'www_example_com'
|
|
50 |
with tenant_context(tenant):
|
|
51 |
User.objects.create(username='admin')
|
|
52 |
|
|
53 |
|
|
54 |
def test_create_tenant_failure(db, caplog):
|
|
55 |
with mock.patch('hobo.multitenant.management.commands.migrate_schemas.MigrateSchemasCommand.handle') as handle:
|
|
56 |
handle.side_effect = CommandError('unable to migrate')
|
|
57 |
assert not schema_exists('www_example_com')
|
|
58 |
with pytest.raises(CommandError) as exc_info:
|
|
59 |
call_command('create_tenant', 'www.example.com')
|
|
60 |
assert str(exc_info.value) == 'tenant creation failed (unable to migrate)'
|
|
61 |
assert not schema_exists('www_example_com')
|
|
62 |
|
|
63 |
with connection.cursor() as cursor:
|
|
64 |
cursor.execute('select schema_name from information_schema.schemata')
|
|
65 |
assert 'www_example_com' not in [row[0] for row in cursor.fetchall()]
|
0 |
|
-
|