def setUp(self): self.owner = create_user(username='******', password='******') self.domain = 'pip.dev.readthedocs.io' self.pip = get( Project, name='pip', slug='pip', users=[self.owner], privacy_level='public', urlconf='subpath/$subproject/$version/$language/$filename' # Flipped ) self.subproject = get( Project, name='subproject', slug='subproject', users=[self.owner], privacy_level='public', main_language_project=None, ) self.relationship = get( ProjectRelationship, parent=self.pip, child=self.subproject, ) self.old_urlconf = get_urlconf() sys.modules['fake_urlconf'] = self.pip.proxito_urlconf set_urlconf('fake_urlconf')
def middleware(request): if request.get_host() == settings.BRASILIO_API_HOST: set_urlconf(settings.API_ROOT_URLCONF) setattr(request, "urlconf", settings.API_ROOT_URLCONF) else: set_urlconf(None) return get_response(request)
def setUp(self): self.owner = create_user(username='******', password='******') self.domain = 'pip.dev.readthedocs.io' self.pip = get( Project, slug='pip', users=[self.owner], privacy_level='public', urlconf='subpath/to/$version/$language/$filename' # Flipped ) self.old_urlconf = get_urlconf() sys.modules['fake_urlconf'] = self.pip.proxito_urlconf set_urlconf('fake_urlconf')
def process_response(self, request, response): # noqa """ Set the Strict-Transport-Security (HSTS) header for docs sites. * For the public domain, set the HSTS header if settings.PUBLIC_DOMAIN_USES_HTTPS * For custom domains, check the HSTS values on the Domain object. The domain object should be saved already in request.domain. """ # Reset URLconf for this thread # to the original one. set_urlconf(None) host = request.get_host().lower().split(':')[0] public_domain = settings.PUBLIC_DOMAIN.lower().split(':')[0] hsts_header_values = [] self.add_proxito_headers(request, response) if not request.is_secure(): # Only set the HSTS header if the request is over HTTPS return response if settings.PUBLIC_DOMAIN_USES_HTTPS and public_domain in host: hsts_header_values = [ 'max-age=31536000', 'includeSubDomains', 'preload', ] elif hasattr(request, 'domain'): domain = request.domain if domain.hsts_max_age: hsts_header_values.append(f'max-age={domain.hsts_max_age}') # These other options don't make sense without max_age > 0 if domain.hsts_include_subdomains: hsts_header_values.append('includeSubDomains') if domain.hsts_preload: hsts_header_values.append('preload') if hsts_header_values: # See https://tools.ietf.org/html/rfc6797 response['Strict-Transport-Security'] = '; '.join( hsts_header_values) return response
def test_restore_urlconf_after_request(self): """ The urlconf attribute for the current thread should remain intact after each request, When is set to None it means 'use default from settings'. """ set_urlconf(None) urlconf = get_urlconf() self.assertIsNone(urlconf) self.client.get(self.url, HTTP_HOST='pip.readthedocs.org') urlconf = get_urlconf() self.assertIsNone(urlconf) self.client.get(self.url) urlconf = get_urlconf() self.assertIsNone(urlconf) self.client.get(self.url, HTTP_HOST='pip.readthedocs.org') urlconf = get_urlconf() self.assertIsNone(urlconf)
def setUp(self): self.owner = create_user(username='******', password='******') self.domain = 'pip.dev.readthedocs.io' self.pip = get( Project, slug='pip', users=[self.owner], privacy_level=PUBLIC, urlconf='subpath/to/$version/$language/$filename' # Flipped ) self.testing_version = get( Version, slug='testing', project=self.pip, built=True, active=True, ) self.pip.versions.update(privacy_level=PUBLIC) sys.modules['fake_urlconf'] = self.pip.proxito_urlconf set_urlconf('fake_urlconf')
def process_response(self, request, response): # Reset URLconf for this thread # to the original one. set_urlconf(None) return response
def tearDown(self): set_urlconf(self.old_urlconf)
def tearDown(self): set_urlconf(None)