def get_template_request_context(request=None): """ Returns the template processing context to use for the current request, or returns None if there is not a current request. """ if request is None: request = get_current_request() if request is None: return None request_cache_dict = RequestCache('edxmako').data cache_key = "request_context" if cache_key in request_cache_dict: return request_cache_dict[cache_key] context = RequestContext(request) context['is_secure'] = request.is_secure() context['site'] = safe_get_host(request) request_cache_dict[cache_key] = context return context
def assertChangeEmailSent(self, email_user): """ Assert that the correct email was sent to confirm an email change """ context = { 'old_email': self.user.email, 'new_email': self.pending_change_request.new_email, } self.assertEmailUser( email_user, 'emails/email_change_subject.txt', context, 'emails/confirm_email_change.txt', context ) # Thorough tests for safe_get_host are elsewhere; here we just want a quick URL sanity check request = RequestFactory().post('unused_url') request.user = self.user request.META['HTTP_HOST'] = "aGenericValidHostName" self.append_allowed_hosts("aGenericValidHostName") with patch('edxmako.request_context.get_current_request', return_value=request): body = render_to_string('emails/confirm_email_change.txt', context) url = safe_get_host(request) self.assertIn(url, body)
def assertReactivateEmailSent(self, email_user): """ Assert that the correct reactivation email has been sent """ context = generate_activation_email_context(self.user, self.registration) self.assertEmailUser( email_user, 'emails/activation_email_subject.txt', context, 'emails/activation_email.txt', context ) # Thorough tests for safe_get_host are elsewhere; here we just want a quick URL sanity check request = RequestFactory().post('unused_url') request.user = self.user request.META['HTTP_HOST'] = "aGenericValidHostName" self.append_allowed_hosts("aGenericValidHostName") with patch('edxmako.request_context.get_current_request', return_value=request): body = render_to_string('emails/activation_email.txt', context) host = safe_get_host(request) self.assertIn(host, body)
def assertReactivateEmailSent(self, email_user): """ Assert that the correct reactivation email has been sent """ context = generate_activation_email_context(self.user, self.registration) self.assertEmailUser(email_user, 'emails/activation_email_subject.txt', context, 'emails/activation_email.txt', context) # Thorough tests for safe_get_host are elsewhere; here we just want a quick URL sanity check request = RequestFactory().post('unused_url') request.user = self.user request.META['HTTP_HOST'] = "aGenericValidHostName" self.append_allowed_hosts("aGenericValidHostName") with patch('edxmako.request_context.get_current_request', return_value=request): body = render_to_string('emails/activation_email.txt', context) host = safe_get_host(request) self.assertIn(host, body)
def assertChangeEmailSent(self, email_user): """ Assert that the correct email was sent to confirm an email change """ context = { 'old_email': self.user.email, 'new_email': self.pending_change_request.new_email, } self.assertEmailUser(email_user, 'emails/email_change_subject.txt', context, 'emails/confirm_email_change.txt', context) # Thorough tests for safe_get_host are elsewhere; here we just want a quick URL sanity check request = RequestFactory().post('unused_url') request.user = self.user request.META['HTTP_HOST'] = "aGenericValidHostName" self.append_allowed_hosts("aGenericValidHostName") with patch('edxmako.request_context.get_current_request', return_value=request): body = render_to_string('emails/confirm_email_change.txt', context) url = safe_get_host(request) self.assertIn(url, body)
def test_safe_get_host(self): """ Tests that the safe_get_host function returns the desired host """ settings.SITE_NAME = 'siteName.com' factory = RequestFactory() request = factory.request() request.META['HTTP_HOST'] = 'www.userProvidedHost.com' # If ALLOWED_HOSTS is not set properly, safe_get_host should return SITE_NAME settings.ALLOWED_HOSTS = None self.assertEqual(safe_get_host(request), "siteName.com") settings.ALLOWED_HOSTS = ["*"] self.assertEqual(safe_get_host(request), "siteName.com") settings.ALLOWED_HOSTS = ["foo.com", "*"] self.assertEqual(safe_get_host(request), "siteName.com") # If ALLOWED_HOSTS is set properly, and the host is valid, we just return the user-provided host settings.ALLOWED_HOSTS = [request.META['HTTP_HOST']] self.assertEqual(safe_get_host(request), request.META['HTTP_HOST']) # If ALLOWED_HOSTS is set properly but the host is invalid, we should get a SuspiciousOperation settings.ALLOWED_HOSTS = ["the_valid_website.com"] with self.assertRaises(SuspiciousOperation): safe_get_host(request)
def test_safe_get_host(self): """ Tests that the safe_get_host function returns the desired host """ settings.SITE_NAME = 'siteName.com' factory = RequestFactory() request = factory.request() request.META['HTTP_HOST'] = 'www.userProvidedHost.com' # If ALLOWED_HOSTS is not set properly, safe_get_host should return SITE_NAME settings.ALLOWED_HOSTS = None self.assertEqual(safe_get_host(request), "siteName.com") settings.ALLOWED_HOSTS = ["*"] self.assertEqual(safe_get_host(request), "siteName.com") settings.ALLOWED_HOSTS = ["foo.com", "*"] self.assertEqual(safe_get_host(request), "siteName.com") # If ALLOWED_HOSTS is set properly, and the host is valid, we just return the user-provided host settings.ALLOWED_HOSTS = [request.META['HTTP_HOST']] self.assertEqual(safe_get_host(request), request.META['HTTP_HOST']) # If ALLOWED_HOSTS is set properly but the host is invalid, we should get a SuspiciousOperation settings.ALLOWED_HOSTS = ["the_valid_website.com"] with self.assertRaises(SuspiciousOperation): safe_get_host(request)