<s> import functools <EOL> import mock <EOL> import warnings <EOL> try : <EOL> import urlparse <EOL> except ImportError : <EOL> from urllib import parse as urlparse <EOL> from django . core . urlresolvers import NoReverseMatch , set_urlconf <EOL> from django . template import Context , Template <EOL> from django . test import TestCase <EOL> from django . test . client import RequestFactory <EOL> from django . test . utils import override_settings <EOL> from subdomains . middleware import ( SubdomainMiddleware , <EOL> SubdomainURLRoutingMiddleware ) <EOL> from subdomains . utils import reverse , urljoin <EOL> def prefix_values ( dictionary , prefix ) : <EOL> return dict ( ( key , '<STR_LIT>' % ( prefix , value ) ) <EOL> for key , value in dictionary . items ( ) ) <EOL> class SubdomainTestMixin ( object ) : <EOL> DOMAIN = '<STR_LIT>' <EOL> URL_MODULE_PATH = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( SubdomainTestMixin , self ) . setUp ( ) <EOL> from django . contrib . sites . models import Site <EOL> self . site = Site . objects . get_current ( ) <EOL> self . site . domain = self . DOMAIN <EOL> self . site . save ( ) <EOL> @ override_settings ( <EOL> DEFAULT_URL_SCHEME = '<STR_LIT:http>' , <EOL> ROOT_URLCONF = '<STR_LIT>' % URL_MODULE_PATH , <EOL> SUBDOMAIN_URLCONFS = prefix_values ( { <EOL> None : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , prefix = URL_MODULE_PATH ) , <EOL> MIDDLEWARE_CLASSES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) ) <EOL> def run ( self , * args , ** kwargs ) : <EOL> super ( SubdomainTestMixin , self ) . run ( * args , ** kwargs ) <EOL> def get_path_to_urlconf ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:.>' . join ( ( self . URL_MODULE_PATH , name ) ) <EOL> def get_host_for_subdomain ( self , subdomain = None ) : <EOL> """<STR_LIT>""" <EOL> if subdomain is not None : <EOL> host = '<STR_LIT>' % ( subdomain , self . site . domain ) <EOL> else : <EOL> host = '<STR_LIT:%s>' % self . site . domain <EOL> return host <EOL> class SubdomainMiddlewareTestCase ( SubdomainTestMixin , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SubdomainMiddlewareTestCase , self ) . setUp ( ) <EOL> self . middleware = SubdomainMiddleware ( ) <EOL> def test_subdomain_attribute ( self ) : <EOL> def subdomain ( subdomain ) : <EOL> """<STR_LIT>""" <EOL> host = self . get_host_for_subdomain ( subdomain ) <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' , HTTP_HOST = host ) <EOL> self . middleware . process_request ( request ) <EOL> return request . subdomain <EOL> self . assertEqual ( subdomain ( None ) , None ) <EOL> self . assertEqual ( subdomain ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( subdomain ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( subdomain ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( subdomain ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_www_domain ( self ) : <EOL> def host ( host ) : <EOL> """<STR_LIT>""" <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' , HTTP_HOST = host ) <EOL> self . middleware . process_request ( request ) <EOL> return request . subdomain <EOL> self . site . domain = '<STR_LIT>' % self . DOMAIN <EOL> self . site . save ( ) <EOL> with override_settings ( REMOVE_WWW_FROM_DOMAIN = False ) : <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , None ) <EOL> with warnings . catch_warnings ( record = True ) as warnlist : <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , None ) <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , None ) <EOL> del warnlist <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , <EOL> '<STR_LIT>' ) <EOL> with override_settings ( REMOVE_WWW_FROM_DOMAIN = True ) : <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , '<STR_LIT>' ) <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , '<STR_LIT>' ) <EOL> self . assertEqual ( host ( '<STR_LIT>' % self . DOMAIN ) , <EOL> '<STR_LIT>' ) <EOL> def test_case_insensitive_subdomain ( self ) : <EOL> host = '<STR_LIT>' % self . DOMAIN <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' , HTTP_HOST = host ) <EOL> self . middleware . process_request ( request ) <EOL> self . assertEqual ( request . subdomain , '<STR_LIT>' ) <EOL> host = '<STR_LIT>' % self . DOMAIN . upper ( ) <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' , HTTP_HOST = host ) <EOL> self . middleware . process_request ( request ) <EOL> self . assertEqual ( request . subdomain , '<STR_LIT>' ) <EOL> class SubdomainURLRoutingTestCase ( SubdomainTestMixin , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SubdomainURLRoutingTestCase , self ) . setUp ( ) <EOL> self . middleware = SubdomainURLRoutingMiddleware ( ) <EOL> def test_url_routing ( self ) : <EOL> def urlconf ( subdomain ) : <EOL> """<STR_LIT>""" <EOL> host = self . get_host_for_subdomain ( subdomain ) <EOL> request = RequestFactory ( ) . get ( '<STR_LIT:/>' , HTTP_HOST = host ) <EOL> self . middleware . process_request ( request ) <EOL> return getattr ( request , '<STR_LIT>' , None ) <EOL> self . assertEqual ( urlconf ( None ) , self . get_path_to_urlconf ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( urlconf ( '<STR_LIT>' ) , self . get_path_to_urlconf ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( urlconf ( '<STR_LIT>' ) , self . get_path_to_urlconf ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( urlconf ( '<STR_LIT>' ) , None ) <EOL> def test_appends_slash ( self ) : <EOL> for subdomain in ( None , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> host = self . get_host_for_subdomain ( subdomain ) <EOL> path = '<STR_LIT>' <EOL> response = self . client . get ( path , HTTP_HOST = host ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> normalize = functools . partial ( <EOL> urlparse . urljoin , <EOL> '<STR_LIT>' % ( host , ) , <EOL> ) <EOL> self . assertEqual ( <EOL> normalize ( response [ '<STR_LIT>' ] ) , <EOL> normalize ( path + '<STR_LIT:/>' ) , <EOL> ) <EOL> class SubdomainURLReverseTestCase ( SubdomainTestMixin , TestCase ) : <EOL> def test_url_join ( self ) : <EOL> self . assertEqual ( urljoin ( self . DOMAIN ) , '<STR_LIT>' % self . DOMAIN ) <EOL> self . assertEqual ( urljoin ( self . DOMAIN , scheme = '<STR_LIT>' ) , <EOL> '<STR_LIT>' % self . DOMAIN ) <EOL> with override_settings ( DEFAULT_URL_SCHEME = '<STR_LIT>' ) : <EOL> self . assertEqual ( urljoin ( self . DOMAIN ) , '<STR_LIT>' % self . DOMAIN ) <EOL> self . assertEqual ( urljoin ( self . DOMAIN , path = '<STR_LIT>' ) , <EOL> '<STR_LIT>' % self . DOMAIN ) <EOL> def test_implicit_reverse ( self ) : <EOL> self . assertEqual ( reverse ( '<STR_LIT>' ) , '<STR_LIT>' % self . DOMAIN ) <EOL> def test_explicit_reverse ( self ) : <EOL> self . assertEqual ( reverse ( '<STR_LIT>' , subdomain = '<STR_LIT>' ) , <EOL> '<STR_LIT>' % self . DOMAIN ) <EOL> self . assertEqual ( reverse ( '<STR_LIT>' , subdomain = '<STR_LIT>' ) , <EOL> '<STR_LIT>' % self . DOMAIN ) <EOL> def test_wildcard_reverse ( self ) : <EOL> subdomain = '<STR_LIT>' <EOL> self . assertEqual ( reverse ( '<STR_LIT>' , subdomain ) , <EOL> '<STR_LIT>' % ( subdomain , self . DOMAIN ) ) <EOL> self . assertEqual ( reverse ( '<STR_LIT>' , subdomain ) , <EOL> '<STR_LIT>' % ( subdomain , self . DOMAIN ) ) <EOL> def test_reverse_subdomain_mismatch ( self ) : <EOL> self . assertRaises ( NoReverseMatch , lambda : reverse ( '<STR_LIT>' ) ) <EOL> def test_reverse_invalid_urlconf_argument ( self ) : <EOL> self . assertRaises ( TypeError , <EOL> lambda : reverse ( '<STR_LIT>' , <EOL> urlconf = self . get_path_to_urlconf ( '<STR_LIT>' ) ) ) <EOL> def test_using_not_default_urlconf ( self ) : <EOL> set_urlconf ( self . get_path_to_urlconf ( '<STR_LIT>' ) ) <EOL> subdomain = '<STR_LIT>' <EOL> self . assertEqual ( reverse ( '<STR_LIT>' , subdomain = subdomain ) , <EOL> '<STR_LIT>' % ( subdomain , self . DOMAIN ) ) <EOL> class SubdomainTemplateTagTestCase ( SubdomainTestMixin , TestCase ) : <EOL> def make_template ( self , template ) : <EOL> return Template ( '<STR_LIT>' + template ) <EOL> def test_without_subdomain ( self ) : <EOL> defaults = { '<STR_LIT>' : '<STR_LIT>' } <EOL> template = self . make_template ( '<STR_LIT>' ) <EOL> context = Context ( defaults ) <EOL> rendered = template . render ( context ) . strip ( ) <EOL> self . assertEqual ( rendered , '<STR_LIT>' % self . DOMAIN ) <EOL> def test_with_subdomain ( self ) : <EOL> defaults = { '<STR_LIT>' : '<STR_LIT>' } <EOL> template = self . make_template ( '<STR_LIT>' ) <EOL> for subdomain in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> context = Context ( dict ( defaults , subdomain = subdomain ) ) <EOL> rendered = template . render ( context ) . strip ( ) <EOL> self . assertEqual ( rendered , <EOL> '<STR_LIT>' % ( subdomain , self . DOMAIN ) ) <EOL> def test_no_reverse ( self ) : <EOL> template = self . make_template ( '<STR_LIT>' ) <EOL> context = Context ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRaises ( NoReverseMatch , lambda : template . render ( context ) ) <EOL> def test_implied_subdomain_from_request ( self ) : <EOL> template = self . make_template ( '<STR_LIT>' ) <EOL> defaults = { '<STR_LIT>' : '<STR_LIT>' } <EOL> request = mock . Mock ( ) <EOL> request . subdomain = None <EOL> context = Context ( dict ( defaults , request = request ) ) <EOL> rendered = template . render ( context ) . strip ( ) <EOL> self . assertEqual ( rendered , '<STR_LIT>' % self . DOMAIN ) <EOL> for subdomain in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> request = mock . Mock ( ) <EOL> request . subdomain = subdomain <EOL> context = Context ( dict ( defaults , request = request ) ) <EOL> rendered = template . render ( context ) . strip ( ) <EOL> self . assertEqual ( rendered , <EOL> '<STR_LIT>' % ( subdomain , self . DOMAIN ) ) </s>
