auth: Added time conversion in new login emails.

This commit adds code for date and time conversion in
"new login" emails according to user's timezone.

Fixes #9336.
This commit is contained in:
Roman Godov 2018-05-27 01:17:08 +03:00 committed by Tim Abbott
parent f7f039e772
commit 0b9d4fbb06
2 changed files with 22 additions and 6 deletions

View File

@ -13,6 +13,7 @@ from django.utils.translation import ugettext_lazy as _
from zerver.lib.queue import queue_json_publish
from zerver.lib.send_email import FromAddress
from zerver.models import UserProfile
from zerver.lib.timezone import get_timezone
def get_device_browser(user_agent: str) -> Optional[str]:
user_agent = user_agent.lower()
@ -72,8 +73,11 @@ def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: A
context = common_context(user)
context['user_email'] = user.email
context['login_time'] = timezone_now().strftime('%A, %B %d, %Y at %I:%M%p ') + \
timezone_get_current_timezone_name()
user_tz = user.timezone
if user_tz == '':
user_tz = timezone_get_current_timezone_name()
local_time = timezone_now().astimezone(get_timezone(user_tz))
context['login_time'] = local_time.strftime('%A, %B %d, %Y at %I:%M%p ') + user_tz
context['device_ip'] = request.META.get('REMOTE_ADDR') or _("Unknown IP address")
context['device_os'] = get_device_os(user_agent)
context['device_browser'] = get_device_browser(user_agent)

View File

@ -1,3 +1,4 @@
import datetime
from django.conf import settings
from django.core import mail
from django.contrib.auth.signals import user_logged_in
@ -6,6 +7,8 @@ from zerver.signals import get_device_browser, get_device_os
from zerver.lib.actions import notify_new_user
from zerver.models import Recipient, Stream, Realm
from zerver.lib.initial_password import initial_password
from unittest import mock
from zerver.lib.timezone import get_timezone
class SendLoginEmailTest(ZulipTestCase):
"""
@ -22,16 +25,25 @@ class SendLoginEmailTest(ZulipTestCase):
with self.settings(SEND_LOGIN_EMAILS=True):
self.assertTrue(settings.SEND_LOGIN_EMAILS)
# we don't use the self.login method since we spoof the user-agent
email = self.example_email('hamlet')
password = initial_password(email)
user = self.example_user('hamlet')
user.timezone = 'US/Pacific'
user.save()
password = initial_password(user.email)
firefox_windows = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
self.client_post("/accounts/login/", info={"username": email, "password": password},
HTTP_USER_AGENT=firefox_windows)
utc = get_timezone('utc')
user_tz = get_timezone(user.timezone)
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=utc)
reference_time = mock_time.astimezone(user_tz).strftime('%A, %B %d, %Y at %I:%M%p ') + user.timezone
with mock.patch('zerver.signals.timezone_now', return_value=mock_time):
self.client_post("/accounts/login/", info={"username": user.email, "password": password},
HTTP_USER_AGENT=firefox_windows)
# email is sent and correct subject
self.assertEqual(len(mail.outbox), 1)
subject = 'New login from Firefox on Windows'
self.assertEqual(mail.outbox[0].subject, subject)
# local time is correct and in email's body
self.assertIn(reference_time, mail.outbox[0].body)
def test_dont_send_login_emails_if_send_login_emails_is_false(self) -> None:
self.assertFalse(settings.SEND_LOGIN_EMAILS)