summaryrefslogtreecommitdiffstats
path: root/tests_multitenant/test_create_tenant.py
blob: ac5937d0452785cc1050d2c3ef4f67b2ebc7f586 (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
# hobo - portal to configure and deploy applications
# Copyright (C) 2019  Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.db import connection
from django.core.management import call_command
from django.core.management.base import CommandError
from django.contrib.auth.models import User

from tenant_schemas.utils import tenant_context

from hobo.multitenant.middleware import TenantMiddleware

import mock
import pytest


@pytest.fixture(autouse=True)
def configuration(settings, tmpdir):
    settings.TENANT_BASE = str(tmpdir.mkdir('tenants'))


def schema_exists(schema_name):
    with connection.cursor() as cursor:
        cursor.execute('select schema_name from information_schema.schemata')
        return 'www_example_com' in [row[0] for row in cursor.fetchall()]


def test_create_tenant(db):
    assert not schema_exists('www_example_com')
    call_command('create_tenant', 'www.example.com')
    assert schema_exists('www_example_com')
    tenants = list(TenantMiddleware.get_tenants())
    assert len(tenants) == 1
    tenant = tenants[0]
    assert tenant.domain_url == 'www.example.com'
    assert tenant.schema_name == 'www_example_com'
    with tenant_context(tenant):
        User.objects.create(username='admin')


def test_create_tenant_failure(db, caplog):
    with mock.patch('hobo.multitenant.management.commands.migrate_schemas.MigrateSchemasCommand.handle') as handle:
        handle.side_effect = CommandError('unable to migrate')
        assert not schema_exists('www_example_com')
        with pytest.raises(CommandError) as exc_info:
            call_command('create_tenant', 'www.example.com')
        assert str(exc_info.value) == 'tenant creation failed (unable to migrate)'
        assert not schema_exists('www_example_com')

    with connection.cursor() as cursor:
        cursor.execute('select schema_name from information_schema.schemata')
        assert 'www_example_com' not in [row[0] for row in cursor.fetchall()]


def test_migrate_schemas_skip_applied(db, capsys):
    assert not schema_exists('www_example_com')
    call_command('create_tenant', 'www.example.com')
    captured = capsys.readouterr()
    assert 'Running migrate for schema www_example_com' in captured.out
    call_command('migrate_schemas', verbosity=1)
    captured = capsys.readouterr()
    assert 'Skipping migrations of schema www_example_com' in captured.out
    call_command('migrate_schemas', 'common', '0001_initial', verbosity=1)
    captured = capsys.readouterr()
    assert 'Running migrate for schema www_example_com' in captured.out
    assert 'Unapplying common.0002' in captured.out
    call_command('migrate_schemas', verbosity=1)
    captured = capsys.readouterr()
    assert 'Running migrate for schema www_example_com' in captured.out
    assert 'Applying common.0002' in captured.out
    call_command('migrate_schemas', verbosity=1)
    captured = capsys.readouterr()
    assert 'Skipping migrations of schema www_example_com' in captured.out