From 84bfcaf4b80e0633d11e41e533254bbec90459a3 Mon Sep 17 00:00:00 2001
From: Viktor Nagy <viktor.nagy@gmail.com>
Date: Thu, 24 Apr 2014 15:58:03 +0200
Subject: [PATCH 1/2] Added custom test client

---
 user_sessions/testclient.py | 66 +++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
 create mode 100644 user_sessions/testclient.py

diff --git a/user_sessions/testclient.py b/user_sessions/testclient.py
new file mode 100644
index 0000000..37dbc99
--- /dev/null
+++ b/user_sessions/testclient.py
@@ -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
+

From 80cf49d7072b2bdfa07621c7286b50b7539a9644 Mon Sep 17 00:00:00 2001
From: Viktor Nagy <viktor.nagy@gmail.com>
Date: Thu, 24 Apr 2014 16:02:01 +0200
Subject: [PATCH 2/2] docs added

---
 docs/index.rst   |  1 +
 docs/testing.rst | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 docs/testing.rst

diff --git a/docs/index.rst b/docs/index.rst
index db03db8..8b53e01 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -13,6 +13,7 @@ Contents:
 
    installation
    usage
+   testing
    reference
    release-notes
 
diff --git a/docs/testing.rst b/docs/testing.rst
new file mode 100644
index 0000000..3d62587
--- /dev/null
+++ b/docs/testing.rst
@@ -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