def test_subfolder_routing_without_prefix(self): """ Should raise ImproperlyConfigured if no sensible TENANT_SUBFOLDER_PREFIX is found in settings. """ settings.TENANT_SUBFOLDER_PREFIX = None with self.assertRaises(ImproperlyConfigured): TenantSubfolderMiddleware() settings.TENANT_SUBFOLDER_PREFIX = ' ' with self.assertRaises(ImproperlyConfigured): TenantSubfolderMiddleware()
def setUp(self): super().setUp() self.factory = RequestFactory() self.tsf = TenantSubfolderMiddleware() self.sync_shared() self.public_tenant = get_tenant_model()(schema_name=get_public_schema_name()) self.public_tenant.save() self.public_domain = get_tenant_domain_model()(domain='test.com', tenant=self.public_tenant) self.public_domain.save() self.tenant_domain = 'tenant.test.com' self.tenant = get_tenant_model()(schema_name='test') self.tenant.save() self.domain = get_tenant_domain_model()(tenant=self.tenant, domain=self.tenant_domain) self.domain.save()
class SubfolderRoutesTestCase(BaseTestCase): @classmethod def setUpClass(cls): super().setUpClass() settings.SHARED_APPS = ('django_tenants', 'customers') settings.TENANT_APPS = ('dts_test_app', 'django.contrib.contenttypes', 'django.contrib.auth', ) settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS cls.available_apps = settings.INSTALLED_APPS settings.TENANT_SUBFOLDER_PREFIX = 'clients/' def setUp(self): super().setUp() self.factory = RequestFactory() self.tsf = TenantSubfolderMiddleware() self.sync_shared() self.public_tenant = get_tenant_model()(schema_name=get_public_schema_name()) self.public_tenant.save() self.public_domain = get_tenant_domain_model()(domain='test.com', tenant=self.public_tenant) self.public_domain.save() self.tenant_domain = 'tenant.test.com' self.tenant = get_tenant_model()(schema_name='test') self.tenant.save() self.domain = get_tenant_domain_model()(tenant=self.tenant, domain=self.tenant_domain) self.domain.save() def tearDown(self): from django.db import connection connection.set_schema_to_public() self.domain.delete() self.tenant.delete(force_drop=True) self.public_domain.delete() self.public_tenant.delete() super().tearDown() def test_tenant_routing(self): """ Request path should not be altered. """ request_url = '/clients/tenant.test.com/any/request/' request = self.factory.get('/clients/tenant.test.com/any/request/', HTTP_HOST=self.public_domain.domain) self.tsf.process_request(request) self.assertEqual(request.path_info, request_url) # request.tenant should also have been set self.assertEqual(request.tenant, self.tenant) def test_public_schema_routing(self): """ Request path should not be altered. """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.public_domain.domain) self.tsf.process_request(request) self.assertEqual(request.path_info, request_url) # request.tenant should also have been set self.assertEqual(request.tenant, self.public_tenant) def test_missing_tenant(self): """ Request path should not be altered. """ request = self.factory.get('/clients/not-found/any/request/', HTTP_HOST=self.public_domain.domain) with self.assertRaises(self.tsf.TENANT_NOT_FOUND_EXCEPTION): self.tsf.process_request(request)