Skip to content

Commit

Permalink
Only check session timeout if the session is not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Sep 4, 2017
1 parent 301589f commit 48f4c70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/django_session_timeout/middleware.py
Expand Up @@ -14,7 +14,7 @@

class SessionTimeoutMiddleware(MiddlewareMixin):
def process_request(self, request):
if not hasattr(request, 'session'):
if not hasattr(request, 'session') or request.session.is_empty():
return

init_time = request.session.setdefault(SESSION_TIMEOUT_KEY, time.time())
Expand Down
10 changes: 10 additions & 0 deletions tests/test_middleware.py
Expand Up @@ -11,6 +11,8 @@ def r(rf):
req = rf.get('/')
middleware = SessionMiddleware()
middleware.process_request(req)
req.session['example_key'] = '1'

req.session.save()
yield req

Expand All @@ -21,6 +23,14 @@ def test_session_new(r):
assert r.session[SESSION_TIMEOUT_KEY]


def test_session_new_empty_session(r):
r.session.flush()

middleware = SessionTimeoutMiddleware()
assert middleware.process_request(r) is None
assert SESSION_TIMEOUT_KEY not in r.session


def test_session_expire(r, settings):
settings.SESSION_EXPIRE_SECONDS = 3600
middleware = SessionTimeoutMiddleware()
Expand Down

0 comments on commit 48f4c70

Please sign in to comment.