Skip to content

Instantly share code, notes, and snippets.

@maxwellamaral
Last active September 2, 2022 21:08
Show Gist options
  • Save maxwellamaral/498bb3b6d879009c2816127241471eb2 to your computer and use it in GitHub Desktop.
Save maxwellamaral/498bb3b6d879009c2816127241471eb2 to your computer and use it in GitHub Desktop.
Testando o admin do Django utilizando django_tenants
"""
Adaptado de Erik Kalkoken em https://stackoverflow.com/questions/60322847/how-to-test-admin-change-views
"""
from django.contrib.auth.models import User, Group
from django.urls import reverse
from django_tenants.test.cases import TenantTestCase
from django_tenants.test.client import TenantClient
def get_admin_change_view_url(obj: object) -> str:
return reverse(
'admin:{}_{}_change'.format(
obj._meta.app_label,
type(obj).__name__.lower()
),
args=(obj.pk,)
)
class BaseSetup(TenantTestCase):
def setUp(self):
super().setUp()
self.c = TenantClient(self.tenant)
def test_user_profile_view(self):
# criando superuser de testes
User.objects.create_superuser(
username='superuser', password='secret', email='[email protected]'
)
self.c.login(username='superuser', password='secret')
# testando a página principal do Admin
response = self.c.get(reverse('admin:index'))
self.assertEqual(response.status_code, 200)
# criar dados de teste e executar
my_group = Group.objects.create(name='Test Group')
response = self.c.get(get_admin_change_view_url(my_group))
self.assertEqual(response.status_code, 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment