From dc78a7888f099c03e5dd22a115223e1c9deff862 Mon Sep 17 00:00:00 2001 From: Umair Khan Date: Fri, 16 Jun 2017 09:50:48 +0500 Subject: [PATCH] login_or_register_remote_user: Send login emails for mobile. Fixes #5389 --- zerver/tests/test_auth_backends.py | 7 ++++++- zerver/views/auth.py | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 9ba5e6cd55..fb83042c7c 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -759,6 +759,7 @@ class GoogleOAuthTest(ZulipTestCase): headers['HTTP_HOST'] = subdomain + ".testserver" if mobile_flow_otp is not None: params['mobile_flow_otp'] = mobile_flow_otp + headers['HTTP_USER_AGENT'] = "ZulipAndroid" if len(params) > 0: url += "?%s" % (urllib.parse.urlencode(params)) @@ -828,7 +829,9 @@ class GoogleSubdomainLoginTest(GoogleOAuthTest): emails=[dict(type="account", value=self.example_email("hamlet"))]) account_response = ResponseMock(200, account_data) - with self.settings(REALMS_HAVE_SUBDOMAINS=True): + self.assertEqual(len(mail.outbox), 0) + with self.settings(REALMS_HAVE_SUBDOMAINS=True, + SEND_LOGIN_EMAILS=True): # Verify that the right thing happens with an invalid-format OTP result = self.google_oauth2_test(token_response, account_response, 'zulip', mobile_flow_otp="1234") @@ -850,6 +853,8 @@ class GoogleSubdomainLoginTest(GoogleOAuthTest): encrypted_api_key = query_params["otp_encrypted_api_key"][0] self.assertEqual(self.example_user('hamlet').api_key, otp_decrypt_api_key(encrypted_api_key, mobile_flow_otp)) + self.assertEqual(len(mail.outbox), 1) + self.assertIn('Zulip on Android', mail.outbox[0].body) def test_log_into_subdomain(self): # type: () -> None diff --git a/zerver/views/auth.py b/zerver/views/auth.py index 720c1d9aff..00ffb3c005 100644 --- a/zerver/views/auth.py +++ b/zerver/views/auth.py @@ -126,6 +126,12 @@ def login_or_register_remote_user(request, remote_username, user_profile, full_n # We can't use HttpResponseRedirect, since it only allows HTTP(S) URLs response = HttpResponse(status=302) response['Location'] = 'zulip://login?' + urllib.parse.urlencode(params) + # Maybe sending 'user_logged_in' signal is the better approach: + # user_logged_in.send(sender=user_profile.__class__, request=request, user=user_profile) + # Not doing this only because over here we don't add the user information + # in the session. If the signal receiver assumes that we do then that + # would cause problems. + email_on_new_login(sender=user_profile.__class__, request=request, user=user_profile) return response login(request, user_profile)