def setUp(self): journal_kwargs = dict(code="test", domain="journal.org") press_kwargs = dict(domain="press.org", ) self.middleware = TimezoneMiddleware() self.request_factory = RequestFactory() self.journal = make_test_journal(**journal_kwargs) self.press = Press(**press_kwargs) self.press.save() self.regular_user = helpers.create_user("*****@*****.**") self.regular_user.is_active = True self.regular_user.save()
class TimezoneMiddlewareTestCase(TestCase): def setUp(self): self.middleware = TimezoneMiddleware() self.request = Mock() self.request.COOKIES = {} def test_process_request_without_tz_cookie(self): self.middleware.process_request(self.request) active_tz = timezone.get_current_timezone_name() self.assertEqual(active_tz, settings.TIME_ZONE) def test_process_request_with_tz_cookie(self): user_tz = 'Asia/Vladivostok' self.request.COOKIES = {'USER_TZ': user_tz} self.middleware.process_request(self.request) active_tz = timezone.get_current_timezone_name() self.assertEqual(active_tz, user_tz) def test_uses_settings_tz_for_invalid_cookie(self): # ensure starts with one that is not the one in settings.py initial_tz = 'Asia/Vladivostok' self.assertNotEqual(settings.TIME_ZONE, initial_tz) timezone.activate(initial_tz) # set a invalid TZ via cookie user_tz = 'Wonder/Land' self.request.COOKIES = {'USER_TZ': user_tz} self.middleware.process_request(self.request) active_tz = timezone.get_current_timezone_name() self.assertEqual(settings.TIME_ZONE, active_tz)
class TestTimezoneMiddleware(TestCase): def setUp(self): journal_kwargs = dict(code="test", domain="journal.org") press_kwargs = dict(domain="press.org", ) self.middleware = TimezoneMiddleware() self.request_factory = RequestFactory() self.journal = make_test_journal(**journal_kwargs) self.press = Press(**press_kwargs) self.press.save() self.regular_user = helpers.create_user("*****@*****.**") self.regular_user.is_active = True self.regular_user.save() @override_settings(URL_CONFIG="path") def test_default_case(self): user = AnonymousUser() request = self.request_factory.get("/test/", SERVER_NAME="press.org") request.session = {} request.user = user response = self.middleware.process_request(request) self.assertEqual(request.timezone, None) def test_user_preference_case(self): request = self.request_factory.get("/test/", SERVER_NAME="press.org") request.session = {} user = Account.objects.get(email='*****@*****.**') user.preferred_timezone = "Europe/London" user.save() request.user = user response = self.middleware.process_request(request) self.assertEqual(request.timezone.zone, user.preferred_timezone) def test_browser_timezone_case(self): user = AnonymousUser() tzname = "Atlantic/Canary" request = self.request_factory.get("/test/", SERVER_NAME="press.org") request.session = {} request.session["janeway_timezone"] = tzname request.user = user response = self.middleware.process_request(request) self.assertEqual(request.timezone.zone, tzname) def test_user_preference_over_browser(self): user_timezone = "Europe/Madrid" browser_timezone = "Atlantic/Canary" user = Account.objects.get(email='*****@*****.**') user.preferred_timezone = user_timezone user.save() request = self.request_factory.get("/test/", SERVER_NAME="press.org") request.session = {} request.session["janeway_timezone"] = browser_timezone request.user = user response = self.middleware.process_request(request) self.assertEqual(request.timezone.zone, user_timezone)
def setUp(self): self.middleware = TimezoneMiddleware() self.request = Mock() self.request.COOKIES = {}