Skip to content

Commit

Permalink
Merge 80cf49d into 7a05749
Browse files Browse the repository at this point in the history
  • Loading branch information
nagyv committed Apr 24, 2014
2 parents 7a05749 + 80cf49d commit 2a7e21c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Contents:

installation
usage
testing
reference
release-notes

Expand Down
29 changes: 29 additions & 0 deletions docs/testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Testing with Django User Sessions
==================================

Django user sessions needs a special test client if
you use calls like `self.client.login` in your code.

For this reason, your tests should either inherit the TestCase
provided by django-user-sessions, or your test client should use the test Client from django-user-sessions.

Example for inheriting the TestCase::

from user_sessions.testclient import TestCase

class MyTestCase(TestCase):

def test_mytest(self):
assert True

Example for using the test client directly::

from django.test import TestCase
from user_sessions.testclient import Client

class MyTestCase(TestCase):

client_class = Client

def test_mytest(self):
assert True
66 changes: 66 additions & 0 deletions user_sessions/testclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from django.test import TestCase
from django.test.client import Client

from django.conf import settings
from django.contrib.auth import authenticate, login
from django.utils.importlib import import_module
from django.http import HttpRequest, SimpleCookie

class Client(Client):

def login(self, **credentials):
"""
Sets the Factory to appear as if it has successfully logged into a site.
Returns True if login is possible; False if the provided credentials
are incorrect, or the user is inactive, or if the sessions framework is
not available.
"""
user = authenticate(**credentials)
if user and user.is_active \
and 'user_sessions' in settings.INSTALLED_APPS:
engine = import_module(settings.SESSION_ENGINE)

# Create a fake request to store login details.
request = HttpRequest()
if self.session:
request.session = self.session
else:
request.session = engine.SessionStore('ua', '127.0.0.1')
login(request, user)

# Save the session values.
request.session.save()

# Set the cookie to represent the session.
session_cookie = settings.SESSION_COOKIE_NAME
self.cookies[session_cookie] = request.session.session_key
cookie_data = {
'max-age': None,
'path': '/',
'domain': settings.SESSION_COOKIE_DOMAIN,
'secure': settings.SESSION_COOKIE_SECURE or None,
'expires': None,
}
self.cookies[session_cookie].update(cookie_data)

return True
else:
return False

def logout(self):
"""
Removes the authenticated user's cookies and session object.
Causes the authenticated user to be logged out.
"""
session = import_module(settings.SESSION_ENGINE).SessionStore('ua', '127.0.0.1')
session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
if session_cookie:
session.delete(session_key=session_cookie.value)
self.cookies = SimpleCookie()

class TestCase(TestCase):
"""Our Client should use the user_session"""
client_class = Client

0 comments on commit 2a7e21c

Please sign in to comment.