Skip to content

Commit

Permalink
user.tests timezone middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Jun 6, 2016
1 parent c7545d4 commit 2500401
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
21 changes: 14 additions & 7 deletions spirit/user/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

from .models import UserProfile


__all__ = [
'TimezoneMiddleware',
'LastIPMiddleware',
'LastSeenMiddleware',
'ActiveUserMiddleware']

logger = logging.getLogger('django')


Expand All @@ -20,7 +27,7 @@ def process_request(self, request):
if request.user.is_authenticated():
try:
timezone.activate(request.user.st.timezone)
except pytz.InvalidTimeError:
except pytz.exceptions.UnknownTimeZoneError:
timezone.deactivate()
logger.error(
'%s is not a valid timezone.' %
Expand All @@ -40,9 +47,9 @@ def process_request(self, request):
if request.user.st.last_ip == last_ip:
return

UserProfile.objects\
.filter(user__pk=request.user.pk)\
.update(last_ip=last_ip)
(UserProfile.objects
.filter(user__pk=request.user.pk)
.update(last_ip=last_ip))


class LastSeenMiddleware(object):
Expand All @@ -57,9 +64,9 @@ def process_request(self, request):
if delta.seconds < threshold:
return

UserProfile.objects\
.filter(user__pk=request.user.pk)\
.update(last_seen=timezone.now())
(UserProfile.objects
.filter(user__pk=request.user.pk)
.update(last_seen=timezone.now()))


class ActiveUserMiddleware(object):
Expand Down
51 changes: 51 additions & 0 deletions spirit/user/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .utils.tokens import UserActivationTokenGenerator, UserEmailChangeTokenGenerator
from .utils.email import send_activation_email, send_email_change_email, sender
from .utils import email
from . import middleware

User = get_user_model()

Expand Down Expand Up @@ -842,3 +843,53 @@ def monkey_render_to_string(*args, **kw):

self.assertEquals(len(mail.outbox), 1)
self.assertEquals(mail.outbox[0].from_email, "foo@bar.com")


class UserMiddlewareTest(TestCase):

def setUp(self):
timezone.deactivate()
utils.cache_clear()
self.user = utils.create_user()

@override_settings(TIME_ZONE='UTC')
def test_timezone(self):
"""
Should activate the user timezone
"""
timezone.deactivate()
utils.login(self)
req = RequestFactory().get('/')
req.user = self.user
time_zone = 'America/Argentina/Buenos_Aires'
self.user.st.timezone = time_zone

self.assertEqual(timezone.get_current_timezone().zone, 'UTC')
middleware.TimezoneMiddleware().process_request(req)
self.assertEqual(timezone.get_current_timezone().zone, time_zone)

@override_settings(TIME_ZONE='UTC')
def test_timezone_bad_tz(self):
timezone.deactivate()
utils.login(self)
req = RequestFactory().get('/')
req.user = self.user
self.user.st.timezone = 'badtimezone'

self.assertEqual(timezone.get_current_timezone().zone, 'UTC')
middleware.TimezoneMiddleware().process_request(req)
self.assertEqual(timezone.get_current_timezone().zone, 'UTC')

@override_settings(TIME_ZONE='UTC')
def test_timezone_anonymous_user(self):
class AnonymUserMock(object):
def is_authenticated(self):
return False

timezone.deactivate()
req = RequestFactory().get('/')
req.user = AnonymUserMock()

self.assertEqual(timezone.get_current_timezone().zone, 'UTC')
middleware.TimezoneMiddleware().process_request(req)
self.assertEqual(timezone.get_current_timezone().zone, 'UTC')

0 comments on commit 2500401

Please sign in to comment.