create_realm: Let server admin skip confirming email.

This will let us defer configuring outbound email to the end of the
install procedure, so we can greatly simplify it by consolidating
several scripted steps.

The new flow could be simplified further by giving the user the full
form in the first place, rather than first a form for just their
email address and then a form with the other details.  We'll leave
that improvement for a separate change.
This commit is contained in:
Greg Price 2018-01-26 12:12:58 -08:00 committed by Tim Abbott
parent 0dceeebd05
commit 48791b731e
2 changed files with 28 additions and 4 deletions

View File

@ -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

View File

@ -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: