From 10f3f13a37b76677fe080ba1d44cd8924d146951 Mon Sep 17 00:00:00 2001 From: Horacio Duran Date: Tue, 7 Jan 2014 09:30:33 -0300 Subject: [PATCH] * Moved test Client implementation to utils inside user_sessions so it is available to users installing the egg only * Changed comments format to suit the project's requirements --- tests/tests.py | 48 ++++------------------------- user_sessions/utils/__init__.py | 0 user_sessions/utils/tests.py | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 user_sessions/utils/__init__.py create mode 100644 user_sessions/utils/tests.py diff --git a/tests/tests.py b/tests/tests.py index 210f2c7..1a9a36f 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -6,19 +6,20 @@ from urllib import urlencode from django.conf import settings -from django.contrib.auth import SESSION_KEY, authenticate, login +from django.contrib.auth import SESSION_KEY from django.contrib.auth.models import User from django.contrib.sessions.backends.base import CreateError from django.core.urlresolvers import reverse from django.db import IntegrityError -from django.http import HttpRequest -from django.test import TestCase, Client as BaseClient + +from django.test import TestCase from django.test.utils import override_settings from django.utils.timezone import now from user_sessions.backends.db import SessionStore from user_sessions.models import Session from user_sessions.templatetags.user_sessions import location, device +from user_sessions.utils.tests import Client if sys.version_info[:2] < (2, 7): from django.utils.unittest.case import skipUnless @@ -34,45 +35,6 @@ geoip_msg = str(e) -class Client(BaseClient): - 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: - # Create a fake request to store login details. - request = HttpRequest() - if self.session: - request.session = self.session - else: - request.session = SessionStore('Python/2.7', '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 - - class MiddlewareTest(TestCase): def test_unmodified_session(self): self.client.get('/', HTTP_USER_AGENT='Python/2.7') @@ -134,7 +96,7 @@ def setUp(self): assert self.client.login(username='bouke', password='secret') expired = SessionStore('Python/2.5', '20.13.1.1') - expired.set_expiry(-365*86400) + expired.set_expiry(-365 * 86400) expired.save() unexpired = SessionStore('Python/2.7', '1.1.1.1') unexpired.save() diff --git a/user_sessions/utils/__init__.py b/user_sessions/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/user_sessions/utils/tests.py b/user_sessions/utils/tests.py new file mode 100644 index 0000000..f2dfd55 --- /dev/null +++ b/user_sessions/utils/tests.py @@ -0,0 +1,53 @@ +from django.conf import settings +from django.contrib.auth import authenticate, login +from django.http import HttpRequest +from django.test import Client as BaseClient + +from user_sessions.backends.db import SessionStore + + +class Client(BaseClient): + """ + Custom implementation of django.test.Client. + + It is required to perform tests that require to login in sites using + django-user-sessions since its implementation of SessionStore has to + required parameters which is not in concordance with what is expected + from the original 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: + # Create a fake request to store login details. + request = HttpRequest() + if self.session: + request.session = self.session + else: + request.session = SessionStore('Python/2.7', '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