Skip to content

Commit

Permalink
Fixed #2 -- Provide custom test client
Browse files Browse the repository at this point in the history
Moved test Client implementation to utils inside user_sessions so
it is available to users installing the egg only.
  • Loading branch information
Horacio Duran authored and Bouke committed Jan 7, 2014
1 parent 6789d7d commit 99dba5b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
48 changes: 5 additions & 43 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
Empty file added user_sessions/utils/__init__.py
Empty file.
53 changes: 53 additions & 0 deletions user_sessions/utils/tests.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 99dba5b

Please sign in to comment.