Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 create sessions when they don't exist #4

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hitcount/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def hit_count(request, hitcount):
"""
UpdateHitCountResponse = namedtuple(
'UpdateHitCountResponse', 'hit_counted hit_message')
# as of Django 1.8.4 empty sessions are not being saved
# https://code.djangoproject.com/ticket/25489
if not request.session.session_key:
request.session.create()

user = request.user

Expand Down
14 changes: 14 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from django.contrib.sessions.backends.db import SessionStore
from django.utils import timezone

from hitcount.conf import settings
Expand All @@ -28,6 +29,19 @@ def test_when_use_ip_is_disabled(self):
hit = Hit.objects.last()
self.assertIsNone(hit.ip)

def test_session_key_is_not_present(self):
session = SessionStore(session_key=None)

self.request_post.session = session

response = HitCountViewMixin.hit_count(self.request_post, self.hit_count)

self.assertIs(response.hit_counted, True)
self.assertEqual(response.hit_message, 'Hit counted: session key')
# test database
hit = Hit.objects.last()
self.assertIsNotNone(hit.session)

def test_anonymous_user_hit(self):
response = HitCountViewMixin.hit_count(self.request_post, self.hit_count)

Expand Down