diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py index be032b1b07..fb6659018b 100644 --- a/zerver/tests/test_management_commands.py +++ b/zerver/tests/test_management_commands.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- -import os import glob +import os +import re from datetime import timedelta from mock import MagicMock, patch, call from typing import List, Dict, Any, Optional @@ -188,15 +189,30 @@ class TestGenerateRealmCreationLink(ZulipTestCase): self.assertIsNone(get_realm('test')) result = self.client_post(generated_link, {'email': email}) self.assertEqual(result.status_code, 302) - self.assertTrue(result["Location"].endswith( - "/accounts/send_confirm/%s" % (email,))) + self.assertTrue(re.search('/accounts/do_confirm/\w+$', result["Location"])) result = self.client_get(result["Location"]) - self.assert_in_response("Check your email so we can get started.", result) + self.assert_in_response('action="/accounts/register/"', result) # Generated link used for creating realm result = self.client_get(generated_link) self.assert_in_success_response(["The organization creation link has expired or is not valid."], result) + def test_generate_link_confirm_email(self) -> None: + email = "user1@test.com" + generated_link = generate_realm_creation_url(by_admin=False) + + with self.settings(OPEN_REALM_CREATION=False): + result = self.client_post(generated_link, {'email': email}) + self.assertEqual(result.status_code, 302) + self.assertTrue(re.search('/accounts/send_confirm/{}$'.format(email), + result["Location"])) + result = self.client_get(result["Location"]) + self.assert_in_response("Check your email so we can get started", result) + + # Original link is now dead + result = self.client_get(generated_link) + self.assert_in_success_response(["The organization creation link has expired or is not valid."], result) + def test_realm_creation_with_random_link(self) -> None: with self.settings(OPEN_REALM_CREATION=False): # Realm creation attempt with an invalid link should fail diff --git a/zerver/views/registration.py b/zerver/views/registration.py index c6b9a55347..23ad647ba0 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -345,6 +345,14 @@ def create_realm(request: HttpRequest, creation_key: Optional[Text]=None) -> Htt if form.is_valid(): email = form.cleaned_data['email'] activation_url = prepare_activation_url(email, request, realm_creation=True) + if key_record is not None and key_record.presume_email_valid: + # The user has a token created from the server command line; + # skip confirming the email is theirs, taking their word for it. + # This is essential on first install if the admin hasn't stopped + # to configure outbound email up front, or it isn't working yet. + key_record.delete() + return HttpResponseRedirect(activation_url) + try: send_confirm_registration_email(email, activation_url) except smtplib.SMTPException as e: