Skip to content

Commit

Permalink
Add tests for UpdateTenantView. Refs #6
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jun 2, 2021
1 parent c80d6fb commit 4980494
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions tcms_tenants/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.conf import settings
from django.contrib.auth.models import Permission
from django.http import HttpResponseRedirect
from django.utils.translation import gettext_lazy as _

from tcms_tenants.models import Tenant
from tcms_tenants.forms import VALIDATION_RE
Expand Down Expand Up @@ -48,6 +49,7 @@ def setUpClass(cls):

def test_create_tenant_shows_defaults_for_trial_and_paid_until(self):
response = self.client.get(reverse('tcms_tenants:create-tenant'))
self.assertContains(response, _('New tenant'))
# assert hidden fields are shown with defaults b/c the tests below
# can't simulate opening the page and clicking the submit button
self.assertContains(
Expand Down Expand Up @@ -167,3 +169,111 @@ def test_creating_tenant_sends_email(self, send_mail):

self.assertTrue(send_mail.called)
self.assertEqual(send_mail.call_count, 1)


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

change_tenant = Permission.objects.get(
content_type__app_label='tcms_tenants',
codename='change_tenant')
cls.tester.user_permissions.add(change_tenant)

def tearDown(self):
self.tenant.publicly_readable = False
self.tenant.save()

self.tester.is_superuser = False
self.tester.save()

def update_and_assert_tenant(self, client):
response = client.post(
reverse('tcms_tenants:edit-tenant', args=[self.tenant.pk]),
{
'name': self.tenant.name,
'schema_name': self.tenant.schema_name,
'publicly_readable': True,
'paid_until': '',
},
follow=True
)

# Then
self.assertRedirects(response, '/')

self.tenant.refresh_from_db()
self.assertTrue(self.tenant.publicly_readable)

def test_super_user_can_edit_any_tenant(self):
# Given
self.tester.is_superuser = True
self.tester.save()
self.assertNotEqual(self.tester, self.tenant.owner)
self.assertFalse(self.tenant.publicly_readable)

# When
response = self.client.get(reverse('tcms_tenants:edit-tenant', args=[self.tenant.pk]))

# Then
self.assertContains(response, _('Edit tenant'))
self.update_and_assert_tenant(self.client)

def test_owner_can_edit_own_tenant(self):
# Given
self.tenant.owner.set_password('password')
self.tenant.owner.save()

client = self.client.__class__(self.tenant)
client.login(username=self.tenant.owner.username, # nosec:B106:hardcoded_password_funcarg
password='password')
self.assertFalse(self.tenant.publicly_readable)

# When
response = client.get(reverse('tcms_tenants:edit-tenant', args=[self.tenant.pk]))

# Then
self.assertContains(response, _('Edit tenant'))
self.update_and_assert_tenant(client)

def test_non_owner_cant_edit_tenant(self):
# Given
self.assertNotEqual(self.tester, self.tenant.owner)
self.assertFalse(self.tester.is_superuser)
self.assertFalse(self.tenant.publicly_readable)

# When: get
response = self.client.get(
reverse('tcms_tenants:edit-tenant', args=[self.tenant.pk]),
follow=True
)

# Then
self.assertContains(
response,
_('Only super-user and tenant owner are allowed to edit tenant properties')
)
self.assertRedirects(response, '/')

# When: post
response = self.client.post(
reverse('tcms_tenants:edit-tenant', args=[self.tenant.pk]),
{
'name': self.tenant.name,
'schema_name': self.tenant.schema_name,
'publicly_readable': True,
'paid_until': '',
},
follow=True
)

# Then
self.assertContains(
response,
_('Only super-user and tenant owner are allowed to edit tenant properties')
)
self.assertRedirects(response, '/')

self.tenant.refresh_from_db()
self.assertFalse(self.tenant.publicly_readable)

0 comments on commit 4980494

Please sign in to comment.