auth: Improve robustness of not sending duplicate login emails.

Previously, we had a very not-robust check on the URL, which might
have caused cases like Google auth registration to not do the right
thing.
This commit is contained in:
Tim Abbott 2017-08-22 16:14:45 -07:00
parent 50ad679342
commit 7c37fc8909
3 changed files with 9 additions and 10 deletions

View File

@ -64,13 +64,9 @@ def email_on_new_login(sender, user, request, **kwargs):
return
if request:
# Login emails are for returning users, not new registrations.
# Determine if login request was from new registration.
path = request.META.get('PATH_INFO', None)
if path:
if path == "/accounts/register/":
return
# If the user's account was just created, avoid sending an email.
if getattr(user, "just_registered", False):
return
login_time = timezone_now().strftime('%A, %B %d, %Y at %I:%M%p ') + \
timezone_get_current_timezone_name()

View File

@ -47,9 +47,9 @@ class SendLoginEmailTest(ZulipTestCase):
with self.settings(SEND_LOGIN_EMAILS=True):
self.register("test@zulip.com", "test")
for email in mail.outbox:
subject = 'New login from an unknown browser on an unknown operating system'
self.assertNotEqual(email.subject, subject)
# Verify that there's just 1 email for new user registration.
self.assertEqual(mail.outbox[0].subject, "Activate your Zulip account")
self.assertEqual(len(mail.outbox), 1)
def test_without_path_info_dont_send_login_emails_for_new_user_registration_logins(self):
# type: () -> None

View File

@ -244,6 +244,9 @@ def accounts_register(request):
logging.error("Subdomain mismatch in registration %s: %s" % (
realm.subdomain, user_profile.email,))
return redirect('/')
# Mark the user as having been just created, so no login email is sent
auth_result.just_registered = True
login(request, auth_result)
return HttpResponseRedirect(realm.uri + reverse('zerver.views.home.home'))