Skip to content

Instantly share code, notes, and snippets.

@atroche
Created July 26, 2011 12:34
Show Gist options
  • Save atroche/1106654 to your computer and use it in GitHub Desktop.
Save atroche/1106654 to your computer and use it in GitHub Desktop.
from flask import current_app, request
from streetlife.helpers.tests import TestCase
from streetlife.helpers.views import BaseView
from streetlife.helpers.https import secure_required, redirect_https_to_http
__all__ = ("HttpsTests", )
class TestSecureView(BaseView):
@secure_required
def get(self):
return "Test return string"
class TestUnsecureView(BaseView):
def get(self):
return "Test return string"
class HttpsTests(TestCase):
"""Tests helper functions for dealing with HTTPS"""
def test_redirect_https_on_unsecured_views(self):
current_app.after_request_funcs = {None: [redirect_https_to_http]}
test_context = current_app.test_request_context(
base_url="https://localhost/")
with test_context:
current_app.preprocess_request()
response = current_app.make_response(TestUnsecureView().get())
pr = current_app.process_response(response)
self.assertEqual(pr.status_code, 302)
self.assertTrue(dict(pr.headers)['Location'].startswith("http:"))
def test_dont_redirect_http_on_unsecured_views(self):
current_app.after_request_funcs = {None: [redirect_https_to_http]}
test_context = current_app.test_request_context(
base_url="http://localhost/")
with test_context:
current_app.preprocess_request()
response = current_app.make_response(TestUnsecureView().get())
pr = current_app.process_response(response)
self.assertEqual(pr.status_code, 200)
def test_secure_required_redirects_http_url(self):
test_context = current_app.test_request_context(
base_url="http://localhost/")
with test_context:
current_app.preprocess_request()
response = current_app.make_response(TestSecureView().get())
pr = current_app.process_response(response)
self.assertEqual(pr.status_code, 302)
self.assertTrue(dict(pr.headers)['Location'].startswith("https:"))
def test_secure_required_doesnt_redirect_https_url(self):
test_context = current_app.test_request_context(
base_url="https://localhost/")
with test_context:
current_app.preprocess_request()
response = current_app.make_response(TestSecureView().get())
pr = current_app.process_response(response)
self.assertEqual(pr.status_code, 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment