Skip to content

Commit

Permalink
Merge 49f8268 into 64f6e7d
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jun 18, 2021
2 parents 64f6e7d + 49f8268 commit 5473980
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
36 changes: 36 additions & 0 deletions tcms_tenants/tests/test_utils.py
@@ -0,0 +1,36 @@
# Copyright (c) 2021 Alexander Todorov <atodorov@MrSenko.com>

# Licensed under the GPL 3.0: https://www.gnu.org/licenses/gpl-3.0.txt
# pylint: disable=too-many-ancestors
from django.test import override_settings
from django_tenants import utils as django_tenant_utils

from tcms_tenants import utils
from tcms_tenants.tests import LoggedInTestCase


class TenantDomainTestCase(LoggedInTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()

with utils.schema_context('public'):
cls.tenant2 = django_tenant_utils.get_tenant_model()(
schema_name='other2',
owner=cls.tester)
cls.tenant2.save()

cls.domain2 = django_tenant_utils.get_tenant_domain_model()(
tenant=cls.tenant2,
domain='other2.example.com')
cls.domain2.save()

def test_should_return_existing_domain_from_db(self):
with override_settings(KIWI_TENANTS_DOMAIN='qa.kiwitcms.org'):
result = utils.tenant_domain(self.tenant2.schema_name)
self.assertEqual(result, "other2.example.com")

def test_should_fallback_to_prefix_and_settings_when_domain_doesnt_exist(self):
with override_settings(KIWI_TENANTS_DOMAIN='qa.kiwitcms.org'):
result = utils.tenant_domain('public')
self.assertEqual(result, "public.qa.kiwitcms.org")
8 changes: 4 additions & 4 deletions tcms_tenants/tests/test_views.py
Expand Up @@ -19,17 +19,17 @@

class RedirectToTestCase(LoggedInTestCase):
def test_redirect_to_tenant_path(self):
expected_url = 'https://%s.%s/plans/search/' % (self.get_test_schema_name(),
settings.KIWI_TENANTS_DOMAIN)
# note: see LoggedInTestCase.get_test_tenant_domain()
expected_url = 'https://tenant.fast-test.com/plans/search/'
response = self.client.get(reverse('tcms_tenants:redirect-to',
args=[self.tenant.schema_name, 'plans/search/']))

self.assertIsInstance(response, HttpResponseRedirect)
self.assertEqual(response['Location'], expected_url)

def test_redirect_to_tenant_root_url(self):
expected_url = 'https://%s.%s/' % (self.get_test_schema_name(),
settings.KIWI_TENANTS_DOMAIN)
# note: see LoggedInTestCase.get_test_tenant_domain()
expected_url = 'https://tenant.fast-test.com/'
response = self.client.get(reverse('tcms_tenants:redirect-to',
args=[self.tenant.schema_name, '']))

Expand Down
7 changes: 7 additions & 0 deletions tcms_tenants/utils.py
Expand Up @@ -62,6 +62,13 @@ def schema_name_not_used(name):

# warning: doesn't play well when the domain has a port number
def tenant_domain(schema_name):
# take into account the fact that some customers deploy their 'public' schema
# without a prefix, e.g. tcms.example.com == KIWI_TENANTS_DOMAIN or could use a
# different domain for their tenant(s)!
domain = Domain.objects.filter(tenant__schema_name=schema_name, is_primary=True).first()
if domain:
return domain.domain

return "%s.%s" % (schema_name, settings.KIWI_TENANTS_DOMAIN)


Expand Down

0 comments on commit 5473980

Please sign in to comment.