Skip to content

Commit

Permalink
Merge a04fcc9 into 105ec5c
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjackson committed Sep 26, 2014
2 parents 105ec5c + a04fcc9 commit 886d24c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

Django
South
django-ipware

# Testing

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
download_url='https://pypi.python.org/pypi/django-user-sessions',
license='MIT',
packages=find_packages(exclude=('example', 'tests',)),
install_requires=['Django>=1.4'],
install_requires=['Django>=1.4', 'django-ipware'],
include_package_data=True,
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
2 changes: 2 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import os
BASE_DIR = os.path.dirname(__file__)

Expand All @@ -8,6 +9,7 @@
'django.contrib.auth',
'django.contrib.contenttypes',
'user_sessions',
'ipware',
'tests',
]

Expand Down
13 changes: 8 additions & 5 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@


class MiddlewareTest(TestCase):
client_class = lambda self: Client(REMOTE_ADDR='85.1.1.1')

def test_unmodified_session(self):
self.client.get('/', HTTP_USER_AGENT='Python/2.7')
self.assertNotIn(settings.SESSION_COOKIE_NAME, self.client.cookies)
Expand All @@ -47,7 +49,7 @@ def test_modify_session(self):
pk=self.client.cookies[settings.SESSION_COOKIE_NAME].value
)
self.assertEqual(session.user_agent, 'Python/2.7')
self.assertEqual(session.ip, '127.0.0.1')
self.assertEqual(session.ip, '85.1.1.1')

def test_login(self):
if django.VERSION < (1, 7):
Expand Down Expand Up @@ -75,7 +77,7 @@ def test_long_ua(self):


class ViewsTest(TestCase):
client_class = Client
client_class = lambda self: Client(REMOTE_ADDR='85.1.1.1')

def setUp(self):
User.objects.create_user('bouke', '', 'secret')
Expand All @@ -94,7 +96,7 @@ def test_delete(self):


class AdminTest(TestCase):
client_class = Client
client_class = lambda self: Client(REMOTE_ADDR='85.1.1.1')

def setUp(self):
User.objects.create_superuser('bouke', '', 'secret')
Expand All @@ -111,14 +113,15 @@ def setUp(self):
def test_list(self):
response = self.client.get(self.admin_url)
self.assertContains(response, 'Select session to change')
self.assertContains(response, '127.0.0.1')
print response
self.assertContains(response, '85.1.1.1')
self.assertContains(response, '20.13.1.1')
self.assertContains(response, '1.1.1.1')

def test_mine(self):
my_sessions = '%s?%s' % (self.admin_url, urlencode({'owner': 'my'}))
response = self.client.get(my_sessions)
self.assertContains(response, '127.0.0.1')
self.assertContains(response, '85.1.1.1')
self.assertNotContains(response, '1.1.1.1')

def test_expired(self):
Expand Down
5 changes: 4 additions & 1 deletion user_sessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.cache import patch_vary_headers
from django.utils.http import cookie_date
from django.utils.importlib import import_module
from ipware.ip import get_real_ip


class SessionMiddleware(object):
Expand All @@ -13,8 +14,10 @@ class SessionMiddleware(object):
def process_request(self, request):
engine = import_module(settings.SESSION_ENGINE)
session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None)

ip = get_real_ip(request) or ''
request.session = engine.SessionStore(
ip=request.META.get('REMOTE_ADDR', ''),
ip=ip,
user_agent=request.META.get('HTTP_USER_AGENT', ''),
session_key=session_key
)
Expand Down
4 changes: 2 additions & 2 deletions user_sessions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def get_decoded(self):
user_agent = models.CharField(max_length=200)
last_activity = models.DateTimeField(auto_now=True)
if django.VERSION[:2] >= (1, 6):
ip = models.GenericIPAddressField()
ip = models.GenericIPAddressField(null=True)
else:
ip = models.IPAddressField()
ip = models.IPAddressField(null=True)


# At bottom to avoid circular import
Expand Down
5 changes: 3 additions & 2 deletions user_sessions/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def login(self, **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')
request.session = SessionStore('Python/2.7', '85.1.1.1')
login(request, user)

# Save the session values.
Expand Down Expand Up @@ -59,5 +60,5 @@ def _session(self):
if 'user_sessions' in settings.INSTALLED_APPS:
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
if cookie:
return SessionStore('Python/2.7', '127.0.0.1', cookie.value)
return SessionStore('Python/2.7', '85.1.1.1', cookie.value)
session = property(_session)

0 comments on commit 886d24c

Please sign in to comment.