summaryrefslogtreecommitdiffstats
path: root/tests_multitenant/test_create_tenant.py
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2018-04-12 09:10:21 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2019-03-18 14:01:11 (GMT)
commit2263f596794c22dfd3c9006cd36284af9cc9a810 (patch)
treed086bf73915744bd7e5c2fa1a76cd1f49581fce5 /tests_multitenant/test_create_tenant.py
parent27e006a180330c6ddec06abfac4b45fc4246becd (diff)
downloadhobo-2263f596794c22dfd3c9006cd36284af9cc9a810.zip
hobo-2263f596794c22dfd3c9006cd36284af9cc9a810.tar.gz
hobo-2263f596794c22dfd3c9006cd36284af9cc9a810.tar.bz2
multitenant: wrap schema creation in atomic() (#23119)
Diffstat (limited to 'tests_multitenant/test_create_tenant.py')
-rw-r--r--tests_multitenant/test_create_tenant.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests_multitenant/test_create_tenant.py b/tests_multitenant/test_create_tenant.py
new file mode 100644
index 0000000..b7aafcf
--- /dev/null
+++ b/tests_multitenant/test_create_tenant.py
@@ -0,0 +1,65 @@
+# 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()]