# Path: chamber/multidomains/domain.py
# class Domain:
# 
#     def __init__(self, name, urlconf=None, user_model=None, url=None, protocol=None, hostname=None, port=None):
#         self.name = name
#         self.protocol = protocol
#         self.hostname = hostname
#         self.urlconf = urlconf
#         self.port = port
# 
#         if url:
#             parsed_url = urlparse(url)
#             self.protocol, self.hostname, self.port = parsed_url.scheme, parsed_url.hostname, parsed_url.port
# 
#         if self.protocol is None:
#             raise ImproperlyConfigured('protocol must be set')
#         if self.hostname is None:
#             raise ImproperlyConfigured('hostname must be set')
# 
#         if self.port is None:
#             if self.protocol == 'http':
#                 self.port = 80
#             elif self.protocol == 'https':
#                 self.port = 443
#             else:
#                 raise ImproperlyConfigured('port must be set')
#         self.user_model = user_model
# 
#     @property
#     def url(self):
#         return ('{}://{}'.format(self.protocol, self.hostname)
#                 if (self.protocol == 'http' and self.port == 80) or (self.protocol == 'https' and self.port == 443)
#                 else '{}://{}:{}'.format(self.protocol, self.hostname, self.port))
# 
#     @property
#     def user_class(self):
#         return apps.get_model(*self.user_model.split('.', 1))
# 
# def get_current_domain():
#     from django.conf import settings
# 
#     return get_domain(settings.SITE_ID)
# 
# def get_domain(site_id):
#     from django.conf import settings
# 
#     if site_id in settings.DOMAINS:
#         return settings.DOMAINS.get(site_id)
#     else:
#         raise ImproperlyConfigured('Domain with ID "{}" does not exists'.format(site_id))
# 
# def get_domain_choices():
#     from django.conf import settings
# 
#     return [(key, domain.name) for key, domain in settings.DOMAINS.items()]
# 
# def get_user_class():
#     return get_current_domain().user_class
# 
# Path: chamber/multidomains/urlresolvers.py
# def reverse(viewname, site_id=None, add_domain=False, urlconf=None, args=None, kwargs=None, current_app=None,
#             qs_kwargs=None):
#     from .domain import get_domain
# 
#     urlconf = (get_domain(site_id).urlconf
#                if site_id is not None and urlconf is None and settings.SITE_ID != site_id
#                else urlconf)
#     site_id = settings.SITE_ID if site_id is None else site_id
#     domain = get_domain(site_id).url if add_domain else ''
#     qs = '?{}'.format(urlencode(qs_kwargs)) if qs_kwargs else ''
#     return ''.join((domain, django_reverse(viewname, urlconf, args, kwargs, current_app), qs))


from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from chamber.multidomains.domain import Domain, get_current_domain, get_domain, get_domain_choices, get_user_class
from chamber.multidomains.urlresolvers import reverse
from germanium.tools import assert_equal, assert_raises  # pylint: disable=E0401
from test_chamber.models import BackendUser, FrontendUser  # pylint: disable=E0401
    def test_get_domain_url(self):
        assert_equal(get_domain(settings.BACKEND_SITE_ID).url, 'http://localhost:8000')
        assert_equal(get_domain(settings.FRONTEND_SITE_ID).url, 'https://localhost')

    def test_new_domain_port(self):
        assert_equal(Domain('testA', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost').port, 80)
        assert_equal(Domain('testB', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost').port, 443)
        assert_equal(Domain('testC', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost', port=443).port, 443)
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost', port=80).port, 80)
        assert_raises(ImproperlyConfigured, Domain, 'testF', 'dj.backend_urls', 'test_chamber.BackendUser',
                      protocol='hbbs', hostname='localhost')
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            url='https://localhost:80').port, 80)
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            url='https://localhost').port, 443)

        assert_equal(Domain('testA', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost').url, 'http://localhost')
        assert_equal(Domain('testB', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost').url, 'https://localhost')
        assert_equal(Domain('testC', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost', port=443).url, 'http://localhost:443')
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost', port=80).url, 'https://localhost:80')

    def test_reverse(self):

# Path: chamber/multidomains/domain.py
# class Domain:
# 
#     def __init__(self, name, urlconf=None, user_model=None, url=None, protocol=None, hostname=None, port=None):
#         self.name = name
#         self.protocol = protocol
#         self.hostname = hostname
#         self.urlconf = urlconf
#         self.port = port
# 
#         if url:
#             parsed_url = urlparse(url)
#             self.protocol, self.hostname, self.port = parsed_url.scheme, parsed_url.hostname, parsed_url.port
# 
#         if self.protocol is None:
#             raise ImproperlyConfigured('protocol must be set')
#         if self.hostname is None:
#             raise ImproperlyConfigured('hostname must be set')
# 
#         if self.port is None:
#             if self.protocol == 'http':
#                 self.port = 80
#             elif self.protocol == 'https':
#                 self.port = 443
#             else:
#                 raise ImproperlyConfigured('port must be set')
#         self.user_model = user_model
# 
#     @property
#     def url(self):
#         return ('{}://{}'.format(self.protocol, self.hostname)
#                 if (self.protocol == 'http' and self.port == 80) or (self.protocol == 'https' and self.port == 443)
#                 else '{}://{}:{}'.format(self.protocol, self.hostname, self.port))
# 
#     @property
#     def user_class(self):
#         return apps.get_model(*self.user_model.split('.', 1))
# 
# def get_current_domain():
#     from django.conf import settings
# 
#     return get_domain(settings.SITE_ID)
# 
# def get_domain(site_id):
#     from django.conf import settings
# 
#     if site_id in settings.DOMAINS:
#         return settings.DOMAINS.get(site_id)
#     else:
#         raise ImproperlyConfigured('Domain with ID "{}" does not exists'.format(site_id))
# 
# def get_domain_choices():
#     from django.conf import settings
# 
#     return [(key, domain.name) for key, domain in settings.DOMAINS.items()]
# 
# def get_user_class():
#     return get_current_domain().user_class
# 
# Path: chamber/multidomains/urlresolvers.py
# def reverse(viewname, site_id=None, add_domain=False, urlconf=None, args=None, kwargs=None, current_app=None,
#             qs_kwargs=None):
#     from .domain import get_domain
# 
#     urlconf = (get_domain(site_id).urlconf
#                if site_id is not None and urlconf is None and settings.SITE_ID != site_id
#                else urlconf)
#     site_id = settings.SITE_ID if site_id is None else site_id
#     domain = get_domain(site_id).url if add_domain else ''
#     qs = '?{}'.format(urlencode(qs_kwargs)) if qs_kwargs else ''
#     return ''.join((domain, django_reverse(viewname, urlconf, args, kwargs, current_app), qs))


# Path: example/dj/apps/test_chamber/tests/multidomains.py
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from chamber.multidomains.domain import Domain, get_current_domain, get_domain, get_domain_choices, get_user_class
from chamber.multidomains.urlresolvers import reverse
from germanium.tools import assert_equal, assert_raises  # pylint: disable=E0401
from test_chamber.models import BackendUser, FrontendUser  # pylint: disable=E0401

    def test_get_domain_url(self):
        assert_equal(get_domain(settings.BACKEND_SITE_ID).url, 'http://localhost:8000')
        assert_equal(get_domain(settings.FRONTEND_SITE_ID).url, 'https://localhost')

    def test_new_domain_port(self):
        assert_equal(Domain('testA', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost').port, 80)
        assert_equal(Domain('testB', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost').port, 443)
        assert_equal(Domain('testC', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost', port=443).port, 443)
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost', port=80).port, 80)
        assert_raises(ImproperlyConfigured, Domain, 'testF', 'dj.backend_urls', 'test_chamber.BackendUser',
                      protocol='hbbs', hostname='localhost')
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            url='https://localhost:80').port, 80)
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            url='https://localhost').port, 443)

        assert_equal(Domain('testA', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost').url, 'http://localhost')
        assert_equal(Domain('testB', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost').url, 'https://localhost')
        assert_equal(Domain('testC', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='http', hostname='localhost', port=443).url, 'http://localhost:443')
        assert_equal(Domain('testD', 'dj.backend_urls', 'test_chamber.BackendUser',
                            protocol='https', hostname='localhost', port=80).url, 'https://localhost:80')

    def test_reverse(self):

        assert_equal(reverse('current-datetime'), '/current_time_backend/')