devlogin: Add support for creating development demo realms.

This is a part of #19523.
This commit is contained in:
Eeshan Garg 2021-08-13 16:07:15 -02:30 committed by Tim Abbott
parent 082dd99ed6
commit dea4187aff
7 changed files with 78 additions and 1 deletions

View File

@ -89,6 +89,9 @@ page can be easily identified in it's respective JavaScript file -->
<form name="register_dev_realm" action="{{ url('register_dev_realm') }}" method="POST">
<input type="submit" class="btn btn-admin" value="Create New Realm" />
</form>
<form name="register_demo_dev_realm" action="{{ url('register_demo_dev_realm') }}" method="POST">
<input type="submit" class="btn btn-admin" value="Create Demo Realm" />
</form>
</div>
<a href="/devtools">Zulip developer tools</a>
</div>

View File

@ -115,6 +115,7 @@ class RegistrationForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput, max_length=MAX_PASSWORD_LENGTH)
realm_subdomain = forms.CharField(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH, required=False)
realm_type = forms.IntegerField(required=False)
is_demo_organization = forms.BooleanField(required=False)
def __init__(self, *args: Any, **kwargs: Any) -> None:
# Since the superclass doesn't except random extra kwargs, we

View File

@ -4930,6 +4930,7 @@ def do_create_realm(
plan_type: Optional[int] = None,
org_type: Optional[int] = None,
date_created: Optional[datetime.datetime] = None,
is_demo_organization: Optional[bool] = False,
) -> Realm:
if Realm.objects.filter(string_id=string_id).exists():
raise AssertionError(f"Realm {string_id} already exists!")
@ -4959,6 +4960,10 @@ def do_create_realm(
with transaction.atomic():
realm = Realm(string_id=string_id, name=name, **kwargs)
if is_demo_organization:
realm.demo_organization_scheduled_deletion_date = (
realm.date_created + datetime.timedelta(days=30)
)
realm.save()
RealmAuditLog.objects.create(

View File

@ -5000,6 +5000,28 @@ class UserSignUpTest(InviteUserBase):
assert user_profile is not None
self.assert_logged_in_user_id(user_profile.id)
@override_settings(TERMS_OF_SERVICE=False)
def test_dev_user_registration_create_demo_realm(self) -> None:
result = self.client_post("/devtools/register_demo_realm/")
self.assertEqual(result.status_code, 302)
realm = Realm.objects.latest("date_created")
self.assertTrue(
result["Location"].startswith(
f"http://{realm.string_id}.testserver/accounts/login/subdomain"
)
)
result = self.client_get(result["Location"], subdomain=realm.string_id)
self.assertEqual(result.status_code, 302)
self.assertEqual(result["Location"], f"http://{realm.string_id}.testserver")
user_profile = UserProfile.objects.all().order_by("id").last()
assert user_profile is not None
self.assert_logged_in_user_id(user_profile.id)
expected_deletion_date = realm.date_created + datetime.timedelta(days=30)
self.assertEqual(realm.demo_organization_scheduled_deletion_date, expected_deletion_date)
class DeactivateUserTest(ZulipTestCase):
def test_deactivate_user(self) -> None:

View File

@ -1,3 +1,5 @@
import random
import string
from typing import Any
from django.conf import settings
@ -24,6 +26,13 @@ def modify_postdata(request: HttpRequest, **kwargs: Any) -> None:
request.POST._mutable = False
def generate_demo_realm_name() -> str:
letters = "".join(random.SystemRandom().choice(string.ascii_lowercase) for _ in range(4))
digits = "".join(random.SystemRandom().choice(string.digits) for _ in range(4))
demo_realm_name = f"demo-{letters}{digits}"
return demo_realm_name
@csrf_exempt
def register_development_user(request: HttpRequest) -> HttpResponse:
if get_subdomain(request) == "":
@ -67,3 +76,31 @@ def register_development_realm(request: HttpRequest) -> HttpResponse:
)
return accounts_register(request)
@csrf_exempt
def register_demo_development_realm(request: HttpRequest) -> HttpResponse:
count = UserProfile.objects.count()
name = f"user-{count}"
email = f"{name}@zulip.com"
realm_name = generate_demo_realm_name()
realm_type = Realm.ORG_TYPES["business"]["id"]
prereg = create_preregistration_user(
email, request, realm_creation=True, password_required=False
)
activation_url = create_confirmation_link(prereg, Confirmation.REALM_CREATION)
key = activation_url.split("/")[-1]
# Need to add test data to POST request as it doesn't originally contain the required parameters
modify_postdata(
request,
key=key,
realm_name=realm_name,
realm_type=realm_type,
full_name=name,
password="test",
realm_subdomain=realm_name,
terms="true",
is_demo_organization="true",
)
return accounts_register(request)

View File

@ -324,7 +324,10 @@ def accounts_register(
string_id = form.cleaned_data["realm_subdomain"]
realm_name = form.cleaned_data["realm_name"]
realm_type = form.cleaned_data["realm_type"]
realm = do_create_realm(string_id, realm_name, org_type=realm_type)
is_demo_org = form.cleaned_data["is_demo_organization"]
realm = do_create_realm(
string_id, realm_name, org_type=realm_type, is_demo_organization=is_demo_org
)
setup_realm_internal_bots(realm)
assert realm is not None

View File

@ -26,6 +26,7 @@ from zerver.views.development.integrations import (
)
from zerver.views.development.registration import (
confirmation_key,
register_demo_development_realm,
register_development_realm,
register_development_user,
)
@ -70,6 +71,11 @@ urls = [
# Register new user and realm
path("devtools/register_user/", register_development_user, name="register_dev_user"),
path("devtools/register_realm/", register_development_realm, name="register_dev_realm"),
path(
"devtools/register_demo_realm/",
register_demo_development_realm,
name="register_demo_dev_realm",
),
# Have easy access for error pages
path("errors/404/", TemplateView.as_view(template_name="404.html")),
path("errors/5xx/", TemplateView.as_view(template_name="500.html")),