class RoutesTestCase(TransactionTestCase): @classmethod def setUpClass(cls): settings.TENANT_APPS = ( 'tenant_schemas', 'django.contrib.contenttypes', 'django.contrib.auth', ) def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' settings.PUBLIC_SCHEMA_URL_TOKEN = '/public' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save() def tearDown(self): Tenant.objects.all().delete() def test_tenant_routing(self): """ request path should not be altered """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.tenant) def test_public_schema_routing(self): """ request path should get prepended with PUBLIC_SCHEMA_URL_TOKEN """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.public_tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, settings.PUBLIC_SCHEMA_URL_TOKEN + request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
def setUp(self): super(RoutesTestCase, self).setUp() self.factory = RequestFactory() self.tm = TenantMiddleware() self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save()
class RoutesTestCase(BaseTestCase): @classmethod def setUpClass(cls): super(RoutesTestCase, cls).setUpClass() settings.SHARED_APPS = ('tenant_schemas', ) settings.TENANT_APPS = ( 'dts_test_app', 'django.contrib.contenttypes', 'django.contrib.auth', ) settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS cls.sync_shared() cls.public_tenant = Tenant(domain_url='test.com', schema_name=get_public_schema_name()) cls.public_tenant.save(verbosity=BaseTestCase.get_verbosity()) def setUp(self): super(RoutesTestCase, self).setUp() self.factory = RequestFactory() self.tm = TenantMiddleware() self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save(verbosity=BaseTestCase.get_verbosity()) def test_tenant_routing(self): """ Request path should not be altered. """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(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_tenant.domain_url) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
class RoutesTestCase(TransactionTestCase): @classmethod def setUpClass(cls): settings.TENANT_APPS = ('tenant_schemas', 'django.contrib.contenttypes', 'django.contrib.auth', ) def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' settings.PUBLIC_SCHEMA_URL_TOKEN = '/public' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save() def tearDown(self): Tenant.objects.all().delete() def test_tenant_routing(self): """ request path should not be altered """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.tenant) def test_public_schema_routing(self): """ request path should get prepended with PUBLIC_SCHEMA_URL_TOKEN """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.public_tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, settings.PUBLIC_SCHEMA_URL_TOKEN + request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
def setUp(self): super(RoutesTestCase, self).setUp() self.factory = RequestFactory() self.tm = TenantMiddleware() self.dtm = DefaultTenantMiddleware() self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save(verbosity=BaseTestCase.get_verbosity()) self.non_existent_domain = 'no-tenant.test.com' self.non_existent_tenant = Tenant(domain_url=self.non_existent_domain, schema_name='no-tenant') self.url = '/any/path/'
class RoutesTestCase(BaseTestCase): @classmethod def setUpClass(cls): super(RoutesTestCase, cls).setUpClass() settings.SHARED_APPS = ('tenant_schemas', ) settings.TENANT_APPS = ('dts_test_app', 'django.contrib.contenttypes', 'django.contrib.auth', ) settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS cls.sync_shared() cls.public_tenant = Tenant(domain_url='test.com', schema_name=get_public_schema_name()) cls.public_tenant.save(verbosity=BaseTestCase.get_verbosity()) def setUp(self): super(RoutesTestCase, self).setUp() self.factory = RequestFactory() self.tm = TenantMiddleware() self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save(verbosity=BaseTestCase.get_verbosity()) def test_tenant_routing(self): """ Request path should not be altered. """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(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_tenant.domain_url) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
class RoutesTestCase(TestCase): def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' settings.TENANT_URL_TOKEN = '/tenant' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save() def tearDown(self): Tenant.objects.all().delete() def test_tenant_routing(self): """ request path should get prepended with TENANT_URL_TOKEN """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, settings.TENANT_URL_TOKEN + request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.tenant) def test_public_schema_routing(self): """ request path should not be altered, tenant should be the public one """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.public_tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save()
class TenantClient(Client): tm = TenantMiddleware() def __init__(self, tenant, enforce_csrf_checks=False, **defaults): super(TenantClient, self).__init__(enforce_csrf_checks, **defaults) self.tenant = tenant def get(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantClient, self).get(path, data, **extra)
class TenantRequestFactory(RequestFactory): tm = TenantMiddleware() def __init__(self, tenant, **defaults): super(TenantRequestFactory, self).__init__(**defaults) self.tenant = tenant def get(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url request = super(TenantRequestFactory, self).get(path, data, **extra) return self.tm.process_request(request)
class RoutesTestCase(BaseTestCase): def setUp(self): super(RoutesTestCase, self).setUp() self.factory = RequestFactory() self.tm = TenantMiddleware() self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save() def test_tenant_routing(self): """ request path should not be altered """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(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_tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' settings.PUBLIC_SCHEMA_URL_TOKEN = '/public' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save()
class TenantClient(Client): tm = TenantMiddleware() def __init__(self, tenant, enforce_csrf_checks=False, **defaults): super(TenantClient, self).__init__(enforce_csrf_checks, **defaults) self.tenant = tenant def get(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantClient, self).get(path, data, **extra) def post(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantClient, self).post(path, data, **extra) def patch(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url if 'content_type' not in extra: extra["content_type"] = "application/json" return super(TenantClient, self).patch(path, data, **extra) def put(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url if 'content_type' not in extra: extra["content_type"] = "application/json" return super(TenantClient, self).put(path, data, **extra) def delete(self, path, data='', content_type='application/octet-stream', **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantClient, self).delete(path, data, **extra)
class TenantRequestFactory(RequestFactory): tm = TenantMiddleware() def __init__(self, tenant, **defaults): super(TenantRequestFactory, self).__init__(**defaults) self.tenant = tenant def get(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantRequestFactory, self).get(path, data, **extra) def post(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantRequestFactory, self).post(path, data, **extra) def patch(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url if 'content_type' not in extra: extra["content_type"] = "application/json" return super(TenantRequestFactory, self).patch(path, data, **extra) def put(self, path, data={}, **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url if 'content_type' not in extra: extra["content_type"] = "application/json" return super(TenantRequestFactory, self).put(path, data, **extra) def delete(self, path, data='', content_type='application/octet-stream', **extra): if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(TenantRequestFactory, self).delete(path, data, **extra)
class RoutesTestCase(BaseTestCase): @classmethod def setUpClass(cls): super(RoutesTestCase, cls).setUpClass() settings.SHARED_APPS = ('tenant_schemas', ) settings.TENANT_APPS = ('dts_test_app', 'django.contrib.contenttypes', 'django.contrib.auth', ) settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS cls.sync_shared() cls.public_tenant = Tenant(domain_url='test.com', schema_name=get_public_schema_name()) cls.public_tenant.save(verbosity=BaseTestCase.get_verbosity()) def setUp(self): super(RoutesTestCase, self).setUp() self.factory = RequestFactory() self.tm = TenantMiddleware() self.dtm = DefaultTenantMiddleware() self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save(verbosity=BaseTestCase.get_verbosity()) self.non_existent_domain = 'no-tenant.test.com' self.non_existent_tenant = Tenant(domain_url=self.non_existent_domain, schema_name='no-tenant') self.url = '/any/path/' def test_tenant_routing(self): request = self.factory.get( self.url, HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, self.url) self.assertEquals(request.tenant, self.tenant) def test_public_schema_routing(self): request = self.factory.get( self.url, HTTP_HOST=self.public_tenant.domain_url) self.tm.process_request(request) self.assertEquals(request.path_info, self.url) self.assertEquals(request.tenant, self.public_tenant) def test_non_existent_tenant_routing(self): """Raise 404 for unrecognised hostnames.""" request = self.factory.get( self.url, HTTP_HOST=self.non_existent_tenant.domain_url) self.assertRaises(Http404, self.tm.process_request, request) def test_non_existent_tenant_to_default_schema_routing(self): """Route unrecognised hostnames to the 'public' tenant.""" request = self.factory.get( self.url, HTTP_HOST=self.non_existent_tenant.domain_url) self.dtm.process_request(request) self.assertEquals(request.path_info, self.url) self.assertEquals(request.tenant, self.public_tenant) def test_non_existent_tenant_custom_middleware(self): """Route unrecognised hostnames to the 'test' tenant.""" dtm = TestDefaultTenantMiddleware() request = self.factory.get( self.url, HTTP_HOST=self.non_existent_tenant.domain_url) dtm.process_request(request) self.assertEquals(request.path_info, self.url) self.assertEquals(request.tenant, self.tenant) def test_non_existent_tenant_and_default_custom_middleware(self): """Route unrecognised hostnames to the 'missing' tenant.""" dtm = MissingDefaultTenantMiddleware() request = self.factory.get( self.url, HTTP_HOST=self.non_existent_tenant.domain_url) self.assertRaises(DisallowedHost, dtm.process_request, request)
class RoutesTestCase(TransactionTestCase): @classmethod def setUpClass(cls): settings.TENANT_APPS = ('tenant_schemas', 'django.contrib.contenttypes', 'django.contrib.auth', ) def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save() def tearDown(self): """ Delete all tenant schemas. Tenant schema are not deleted automatically by django. """ connection.set_schema_to_public() do_not_delete = [get_public_schema_name(), 'information_schema'] cursor = connection.cursor() # Use information_schema.schemata instead of pg_catalog.pg_namespace in # utils.schema_exists, so that we only "see" schemas that we own cursor.execute('SELECT schema_name FROM information_schema.schemata') for row in cursor.fetchall(): if not row[0].startswith('pg_') and row[0] not in do_not_delete: print("Deleting schema %s" % row[0]) cursor.execute('DROP SCHEMA %s CASCADE' % row[0]) Tenant.objects.all().delete() def test_tenant_routing(self): """ request path should not be altered """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(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_tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
class RoutesTestCase(TransactionTestCase): @classmethod def setUpClass(cls): settings.TENANT_APPS = ( 'tenant_schemas', 'django.contrib.contenttypes', 'django.contrib.auth', ) def setUp(self): self.factory = RequestFactory() self.tm = TenantMiddleware() # settings needs some patching settings.TENANT_MODEL = 'tenant_schemas.Tenant' # add the public tenant self.public_tenant_domain = 'test.com' self.public_tenant = Tenant(domain_url=self.public_tenant_domain, schema_name='public') self.public_tenant.save() # add a test tenant self.tenant_domain = 'tenant.test.com' self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test') self.tenant.save() def tearDown(self): """ Delete all tenant schemas. Tenant schema are not deleted automatically by django. """ connection.set_schema_to_public() do_not_delete = [get_public_schema_name(), 'information_schema'] cursor = connection.cursor() # Use information_schema.schemata instead of pg_catalog.pg_namespace in # utils.schema_exists, so that we only "see" schemas that we own cursor.execute('SELECT schema_name FROM information_schema.schemata') for row in cursor.fetchall(): if not row[0].startswith('pg_') and row[0] not in do_not_delete: print("Deleting schema %s" % row[0]) cursor.execute('DROP SCHEMA %s CASCADE' % row[0]) Tenant.objects.all().delete() def test_tenant_routing(self): """ request path should not be altered """ request_url = '/any/request/' request = self.factory.get('/any/request/', HTTP_HOST=self.tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(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_tenant_domain) self.tm.process_request(request) self.assertEquals(request.path_info, request_url) # request.tenant should also have been set self.assertEquals(request.tenant, self.public_tenant)
class ApiClient(RestAPIClient): tm = TenantMiddleware() renderer_classes_list = api_settings.TEST_REQUEST_RENDERER_CLASSES default_format = api_settings.TEST_REQUEST_DEFAULT_FORMAT def __init__(self, tenant, enforce_csrf_checks=False, **defaults): super(ApiClient, self).__init__(enforce_csrf_checks, **defaults) self.tenant = tenant self.renderer_classes = {} for cls in self.renderer_classes_list: self.renderer_classes[cls.format] = cls def get(self, path, data=None, **extra): if 'token' in extra: extra['HTTP_AUTHORIZATION'] = extra['token'] del extra['token'] if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(ApiClient, self).get(path, data=data, **extra) def post(self, path, data=None, format='json', content_type=None, **extra): if 'token' in extra: extra['HTTP_AUTHORIZATION'] = extra['token'] del extra['token'] if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(ApiClient, self).post(path, data=data, format=format, content_type=content_type, **extra) def put(self, path, data=None, format='json', content_type=None, **extra): if 'token' in extra: extra['HTTP_AUTHORIZATION'] = extra['token'] del extra['token'] if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(ApiClient, self).put(path, data=data, format=format, content_type=content_type, **extra) def patch(self, path, data=None, format='json', content_type=None, **extra): if 'token' in extra: extra['HTTP_AUTHORIZATION'] = extra['token'] del extra['token'] if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(ApiClient, self).patch(path, data=data, format=format, content_type=content_type, **extra) def delete(self, path, data=None, format='json', content_type=None, **extra): if 'token' in extra: extra['HTTP_AUTHORIZATION'] = extra['token'] del extra['token'] if 'HTTP_HOST' not in extra: extra['HTTP_HOST'] = self.tenant.domain_url return super(ApiClient, self).delete(path, data=data, format=format, content_type=content_type, **extra)