Skip to content

Commit

Permalink
fixed real, testing and cookieless testing switches
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Crewe committed Nov 9, 2012
1 parent 5264bd8 commit d24ef0d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
8 changes: 6 additions & 2 deletions README.txt
Expand Up @@ -114,6 +114,10 @@ Then run via:

bin/django-admin.py or manage.py test cookieless --settings="cookieless.tests.settings"

Note: Because the django test browser has some session implementation specific mocking,
Note: cookieless/decorator.py

Because the django test browser has some session implementation specific mocking,
it fails to work if used directly against cookieless, so to stop it breaking other tests
cookieless checks to see if META['SERVER_NAME'] == 'testserver' and reverts to django.contrib.sessions if so.
cookieless checks to see if the django admin command has been called with the 'test' argument and sets settings.TESTING = True, and doesnt decorate with no_cookies if so.

To override this automatic disabling setting, just add TESTING = False, to your test settings.
27 changes: 22 additions & 5 deletions cookieless/decorators.py
@@ -1,6 +1,18 @@
# The django test client has mock session / cookies which assume cookies are in use
# so turn off cookieless for tests
# TODO: Find better work around for test browser switch hardcoded
# to django.contrib.session being in MIDDLEWARE
from functools import wraps
from django.utils.decorators import available_attrs
# Just tags the view for special handling by the middleware
# but only do so if we aren't in testing mode ...
from django.conf import settings
import sys
if not hasattr(settings, 'TESTING'):
try:
settings.TESTING = sys.argv[1:2] == ['test']
except:
settings.TESTING = False

def no_cookies(view_func):
"""
Expand All @@ -12,13 +24,18 @@ def wrapped_view(*args, **kwargs):
are nicer if they don't have side-effects, so we return a new
function.
'''
#Assumes request is args[0]
if args:
setattr(args[0], 'no_cookies', True)
request = args[0]
elif kwargs.has_key('request'):
setattr(kwargs['request'], 'no_cookies', True)
request = kwargs['request']

if not settings.TESTING:
setattr(request, 'no_cookies', True)

return view_func(*args, **kwargs)

wrapped_view.csrf_exempt = True
wrapped_view.no_cookies = True
if not settings.TESTING:
# mark on intial decoration - rather than on running the view
view_func.no_cookies = True
view_func.csrf_exempt = True
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
10 changes: 3 additions & 7 deletions cookieless/middleware.py
Expand Up @@ -46,9 +46,11 @@ def process_request(self, request):
NB: Use resolve to check view for no_cookies
- because its before view is processed
"""
name = settings.SESSION_COOKIE_NAME
session_key = ''
match = resolve(request.path)

if match and getattr(match.func, 'no_cookies', False):
name = settings.SESSION_COOKIE_NAME
session_key = self._sesh.decrypt(request,
request.POST.get(name, None))
if not session_key and self.settings.get('USE_GET', False):
Expand Down Expand Up @@ -80,12 +82,6 @@ def process_response(self, request, response):
NB: request.COOKIES are the sent ones and response.cookies the set ones!
"""
if getattr(request, 'no_cookies', False):
# The django test client has mock session / cookies which assume cookies are in use
# so to turn off cookieless for tests
# TODO: Find work around for test browser switch hardcoded to session being from django.contrib.session
if request.META.get('SERVER_NAME', '') == 'testserver':
return self.standard_session.process_response(request, response)
# raise Exception(request.session.values())
if request.COOKIES:
if self.settings.get('NO_COOKIE_PERSIST', False):
# Don't persist a session with cookieless for any session
Expand Down
33 changes: 16 additions & 17 deletions cookieless/tests/functional_tests.py
Expand Up @@ -22,7 +22,7 @@ def setUp(self):
self.settings = getattr(settings, 'COOKIELESS', DEFAULT_SETTINGS)
self.settings['HOSTS'] = []
self.browser = Client()
self.browser.request(SERVER_NAME='enable_testserver')
self.browser.request()
self.engine = import_module(settings.SESSION_ENGINE)
self.crypt_sesh = CryptSession()
self.factory = RequestFactory()
Expand All @@ -36,7 +36,7 @@ def get_session(self, response):
return {}, ''
parts = parts[1].split('"')
session_id = parts[0]
request = self.factory.get('/', SERVER_NAME='enable_testserver')
request = self.factory.get('/')
session_key = self.crypt_sesh.decrypt(request, session_id)
try:
session = self.engine.SessionStore(session_key)
Expand All @@ -47,7 +47,7 @@ def get_session(self, response):
def test_session_in_tags_html(self):
""" Confirm session is generated in html via tags """
self.settings['REWRITE'] = False
response = self.browser.get('/', SERVER_NAME='enable_testserver')
response = self.browser.get('/')
url = '?%s=' % settings.SESSION_COOKIE_NAME
# Check form session id is set
self.assertTrue(self.hidden in response.content)
Expand All @@ -56,8 +56,7 @@ def test_session_in_tags_html(self):
def test_session_in_rewritten_html(self):
""" Confirm session is rewritten into html """
self.settings['REWRITE'] = True
response = self.browser.get('/plain-view.html',
SERVER_NAME='enable_testserver')
response = self.browser.get('/plain-view.html')
url = '?%s=' % settings.SESSION_COOKIE_NAME
# Check form session id is set
self.assertTrue(self.hidden in response.content)
Expand All @@ -67,26 +66,27 @@ def test_session_no_url_rewrite_option(self):
""" Confirm session is rewritten into html """
self.settings['REWRITE'] = True
self.settings['USE_GET'] = False
response = self.browser.get('/plain-view.html',
SERVER_NAME='enable_testserver')
response = self.browser.get('/plain-view.html')
url = '?%s=' % settings.SESSION_COOKIE_NAME
# Check form session id is set but urls aren't
self.assertTrue(self.hidden in response.content)
self.assertTrue(url not in response.content)

def test_disabled_for_standard_test_browser(self):
""" Confirm standard test browser doesnt use cookieless """
def test_disabled_for_testing_flag(self):
""" Confirm that normally test browser will not use cookieless """
self.settings['REWRITE'] = True
settings.TESTING = True
response = self.browser.get('/plain-view.html')
self.assertTrue(self.hidden not in response.content)
settings.TESTING = False

def test_session_retained(self):
""" Get the first page then retrieve the session
and confirm it is retained and populated in the second page
"""
self.settings['REWRITE'] = False
self.settings['URL_SPECIFIC'] = False
response = self.browser.get('/', SERVER_NAME='enable_testserver')
response = self.browser.get('/')
session, session_id = self.get_session(response)
self.assertTrue('classview' in session.keys())
self.assertEqual(len(session.keys()), 2)
Expand All @@ -104,20 +104,19 @@ def test_session_not_retained_other_url(self):
"""
self.settings['REWRITE'] = False
self.settings['URL_SPECIFIC'] = True
response = self.browser.get('/', SERVER_NAME='enable_testserver')
response = self.browser.get('/')
session, session_id = self.get_session(response)
self.assertTrue('classview' in session.keys())
self.assertEqual(len(session.keys()), 2)
# Post form to second page where session is restarted
postdict = { self.skey : session_id, }
self.browser.post("/function-view.html", postdict,
SERVER_NAME='enable_testserver')
self.browser.post("/function-view.html", postdict)
session = self.engine.SessionStore(session.session_key)
self.assertTrue('funcview' not in session.keys())
self.assertEqual(len(session.keys()), 2)
# Post form back to first page where session is retained
postdict = { self.skey : session_id, }
self.browser.post("/", postdict, SERVER_NAME='enable_testserver')
self.browser.post("/", postdict)
session = self.engine.SessionStore(session.session_key)
self.assertEqual(len(session.keys()), 3)

Expand All @@ -128,17 +127,17 @@ def test_session_not_retained_other_host(self):
"""
self.settings['REWRITE'] = False
self.settings['URL_SPECIFIC'] = True
response = self.browser.get('/', SERVER_NAME='enable_testserver')
response = self.browser.get('/')
session, session_id = self.get_session(response)
self.assertTrue('classview' in session.keys())
self.assertEqual(len(session.keys()), 2)
# Post form back to first page where other session is started
postdict = { self.skey : session_id, }
self.browser.post("/", postdict)
self.browser.post("/", postdict, SERVER_NAME='another_testserver')
session = self.engine.SessionStore(session.session_key)
self.assertEqual(len(session.keys()), 2)
# Post form back to first page where session is retained
postdict = { self.skey : session_id, }
self.browser.post("/", postdict, SERVER_NAME='enable_testserver')
self.browser.post("/", postdict)
session = self.engine.SessionStore(session.session_key)
self.assertEqual(len(session.keys()), 3)
3 changes: 3 additions & 0 deletions cookieless/tests/settings.py
Expand Up @@ -5,6 +5,9 @@

COOKIELESS = {}

# Turn cookieless back on
TESTING = False

# Rewriting the response automatically rather than use manual <% session_token %> <% session_url %>
COOKIELESS['REWRITE'] = False

Expand Down
1 change: 1 addition & 0 deletions docs/HISTORY.txt
Expand Up @@ -7,6 +7,7 @@ Changelog
- Fix issue of not having no_cookies to test in process_request by getting it from urlresolvers
Now we only check for cookie session where we should, and cookies cannot mess with cookieless sessions
- Make the deletion of any cookies that are passed on to the URL, an optional feature
- Use settings.TESTING based on argv to disable, instead of check for servername

0.3 - 7th November 2012
-----------------------
Expand Down

0 comments on commit d24ef0d

Please sign in to comment.