# Path: shared_schema_tenants/helpers/tenant_relationships.py
# def create_relationship(tenant, user, groups=[], permissions=[]):
#     with transaction.atomic():
#         relationship = TenantRelationship.objects.create(user=user, tenant=tenant)
#         for group in groups:
#             relationship.groups.add(group)
#         for perm in permissions:
#             relationship.permissions.add(perm)
# 
#         return relationship
# 
# Path: shared_schema_tenants/helpers/tenants.py
# def set_current_tenant(tenant_slug):
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.set_tenant(tenant_slug)
# 
# def create_default_tenant_groups():
#     with transaction.atomic():
#         group, created = Group.objects.get_or_create(name='tenant_owner')
# 
#         if created:
#             for perm in get_setting('DEFAULT_TENANT_OWNER_PERMISSIONS'):
#                 try:
#                     group.permissions.add(Permission.objects.get(
#                         content_type__app_label=perm.split('.')[0],
#                         codename=perm.split('.')[1]))
#                 except Permission.DoesNotExist:
#                     pass
# 
#         return [group]


from django.test import TestCase
from django.contrib.auth.models import User
from rest_framework.test import APITestCase
from model_mommy import mommy
from shared_schema_tenants.helpers.tenant_relationships import create_relationship
from shared_schema_tenants.helpers.tenants import (
    set_current_tenant, create_default_tenant_groups)



class SharedSchemaTenantsTestCase(TestCase):

    def setUp(self):
        self.tenant = mommy.make('shared_schema_tenants.Tenant')
        set_current_tenant(self.tenant.slug)
        self.user = User.objects.create_user(
            first_name='test', last_name='test',
            username='test', email='test@sharedschematenants.com',
            password='test')
        self.relationship = create_relationship(self.tenant, self.user,
                                                groups=create_default_tenant_groups())
        self.tenant_site = mommy.make('shared_schema_tenants.TenantSite',
                                      tenant=self.tenant)


class SharedSchemaTenantsAPITestCase(APITestCase):

    def setUp(self):
        self.tenant = mommy.make('shared_schema_tenants.Tenant')
        set_current_tenant(self.tenant.slug)
        self.user = User.objects.create_user(
            first_name='test', last_name='test',
            username='test', email='test@sharedschematenants.com',
            password='test')
        self.relationship = create_relationship(self.tenant, self.user,

# Path: shared_schema_tenants/helpers/tenant_relationships.py
# def create_relationship(tenant, user, groups=[], permissions=[]):
#     with transaction.atomic():
#         relationship = TenantRelationship.objects.create(user=user, tenant=tenant)
#         for group in groups:
#             relationship.groups.add(group)
#         for perm in permissions:
#             relationship.permissions.add(perm)
# 
#         return relationship
# 
# Path: shared_schema_tenants/helpers/tenants.py
# def set_current_tenant(tenant_slug):
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.set_tenant(tenant_slug)
# 
# def create_default_tenant_groups():
#     with transaction.atomic():
#         group, created = Group.objects.get_or_create(name='tenant_owner')
# 
#         if created:
#             for perm in get_setting('DEFAULT_TENANT_OWNER_PERMISSIONS'):
#                 try:
#                     group.permissions.add(Permission.objects.get(
#                         content_type__app_label=perm.split('.')[0],
#                         codename=perm.split('.')[1]))
#                 except Permission.DoesNotExist:
#                     pass
# 
#         return [group]


# Path: tests/utils.py
from django.test import TestCase
from django.contrib.auth.models import User
from rest_framework.test import APITestCase
from model_mommy import mommy
from shared_schema_tenants.helpers.tenant_relationships import create_relationship
from shared_schema_tenants.helpers.tenants import (
    set_current_tenant, create_default_tenant_groups)




class SharedSchemaTenantsTestCase(TestCase):

    def setUp(self):
        self.tenant = mommy.make('shared_schema_tenants.Tenant')
        set_current_tenant(self.tenant.slug)
        self.user = User.objects.create_user(
            first_name='test', last_name='test',
            username='test', email='test@sharedschematenants.com',
            password='test')
        self.relationship = create_relationship(self.tenant, self.user,
                                                groups=create_default_tenant_groups())
        self.tenant_site = mommy.make('shared_schema_tenants.TenantSite',
                                      tenant=self.tenant)


class SharedSchemaTenantsAPITestCase(APITestCase):

    def setUp(self):
        self.tenant = mommy.make('shared_schema_tenants.Tenant')
        set_current_tenant(self.tenant.slug)
        self.user = User.objects.create_user(
            first_name='test', last_name='test',
            username='test', email='test@sharedschematenants.com',
            password='test')
        self.relationship = create_relationship(self.tenant, self.user,

                                                groups=create_default_tenant_groups())