1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from django.contrib.auth.models import User
12 from django.test import Client
13 from django.test import TestCase
14 from django.urls import reverse
15 from django.utils import timezone
17 from account.models import UserProfile
20 class AccountMiddlewareTestCase(TestCase):
22 self.client = Client()
23 self.user1 = User.objects.create(username='user1')
24 self.user1.set_password('user1')
25 self.user1profile = UserProfile.objects.create(user=self.user1)
28 def test_timezone_middleware(self):
30 Verify timezone is being set by Middleware.
32 The timezone should be UTC for anonymous users,
33 for authenticated users it should be set to user.userprofile.timezone
36 self.assertEqual(timezone.get_current_timezone_name(), 'UTC')
38 url = reverse('account:settings')
41 self.assertEqual(timezone.get_current_timezone_name(), 'UTC')
43 # authenticated user with UTC timezone (userprofile default)
44 self.client.login(username='user1', password='user1')
46 self.assertEqual(timezone.get_current_timezone_name(), 'UTC')
48 # authenticated user with custom timezone (userprofile default)
49 self.user1profile.timezone = 'Etc/Greenwich'
50 self.user1profile.save()
52 self.assertEqual(timezone.get_current_timezone_name(), 'GMT')
54 # if there is no profile for a user, it should be created
55 user2 = User.objects.create(username='user2')
56 user2.set_password('user2')
58 self.client.login(username='user2', password='user2')
60 self.assertTrue(user2.userprofile)