From 326f9a850698703da9b76fb5bde965246466ec6a Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Sun, 30 Apr 2017 16:42:17 -0700 Subject: [PATCH] email: Fix zephyr mirror registration email not working with subdomains. If a user is trying to register for a mit zephyr mirroring realm, we send them a specific registration email with a link to a few more instructions. There is only one server that we know about that has such a realm, and that server uses subdomains. This commit changes the logic to work in the subdomains case, rather than in the non-subdomains case (though see next para). Note that the current check is deceptive, and is not actually correct in the non-subdomains case. The prereg user has a realm only in the atypical case of someone registering via the special URL for completely-open realms. To do this correctly in the non-subdomains case, we would need to copy a bunch of the logic from the beginning of accounts_register to figure out which realm the user is signing up for, so that we can check if that realm is a zephyr mirroring realm. Given how complicated the registration code is already, I think it is probably not worth it at the moment. This commit also removes the partial (deceptive) check, since I think it does more harm than good. --- zerver/tests/test_signup.py | 30 ++++++++++++++++++++++++++++-- zerver/views/registration.py | 10 ++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index a7cb29a847..8258b1bbf7 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -18,8 +18,8 @@ from zilencer.models import Deployment from zerver.forms import HomepageForm, WRONG_SUBDOMAIN_ERROR from zerver.lib.actions import do_change_password from zerver.views.invite import get_invitee_emails_set -from zerver.views.registration import (confirmation_key, - redirect_and_log_into_subdomain) +from zerver.views.registration import confirmation_key, \ + redirect_and_log_into_subdomain, send_registration_completion_email from zerver.models import ( get_realm, get_prereg_user_by_email, get_user_profile_by_email, get_unique_open_realm, completely_open, @@ -1463,6 +1463,32 @@ class UserSignUpTest(ZulipTestCase): mock_ldap.reset() mock_initialize.stop() + def test_registration_email_for_mirror_dummy_user(self): + # type: () -> None + email = 'sipbtest@mit.edu' + + user_profile = get_user_profile_by_email(email) + user_profile.is_mirror_dummy = True + user_profile.is_active = False + user_profile.save() + + with self.settings(REALMS_HAVE_SUBDOMAINS=True): + with patch('zerver.forms.get_subdomain', return_value='zephyr'): + with patch('zerver.views.registration.get_subdomain', return_value='zephyr'): + result = self.client_post('/accounts/home/', {'email': email}) + self.assertEqual(result.status_code, 302) + + from django.core.mail import outbox + for message in reversed(outbox): + if email in message.to: + # The main difference between the zephyr registation email + # and the normal one is this string + index = message.body.find('https://zephyr.zulipchat.com/zephyr') + if index >= 0: + return + else: + raise AssertionError("Couldn't find the right confirmation email.") + @patch('DNS.dnslookup', return_value=[['sipbtest:*:20922:101:Fred Sipb,,,:/mit/sipbtest:/bin/athena/tcsh']]) def test_registration_of_mirror_dummy_user(self, ignored): # type: (Any) -> None diff --git a/zerver/views/registration.py b/zerver/views/registration.py index da2a58ab00..59e8876c03 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -287,8 +287,14 @@ def send_registration_completion_email(email, request, realm_creation=False): can complete their registration. """ template_prefix = 'confirmation/preregistrationuser_confirmation_email' - if prereg_user.realm and prereg_user.realm.is_zephyr_mirror_realm: # nocoverage, see next commit - template_prefix = 'confirmation/mituser_confirmation_email' # nocoverage, see next commit + # Note: to make the following work in the non-subdomains case, you'll + # need to copy the logic from the beginning of accounts_register to + # figure out which realm the user is trying to sign up for, and then + # check if it is a zephyr mirror realm. + if settings.REALMS_HAVE_SUBDOMAINS: + realm = get_realm(get_subdomain(request)) + if realm and realm.is_zephyr_mirror_realm: + template_prefix = 'confirmation/mituser_confirmation_email' prereg_user = create_preregistration_user(email, request, realm_creation) context = {'support_email': settings.ZULIP_ADMINISTRATOR,