Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

demo-orgs: Use new wordlist to create demo organization subdomains.

Lauryn Menard8602a4bcc530
diff --git a/zerver/lib/demo_organizations.py b/zerver/lib/demo_organizations.py
index 2b7eb633d29..a797adef6db 100644
--- a/zerver/lib/demo_organizations.py
+++ b/zerver/lib/demo_organizations.py
@@ -1,9 +1,22 @@
+import os
+
+import orjson
+from django.conf import settings
from django.utils.translation import gettext as _
from zerver.lib.exceptions import JsonableError
from zerver.models.realms import Realm
+def get_demo_organization_wordlist() -> dict[str, list[str]]:
+ path = os.path.join(settings.DEPLOY_ROOT, "zerver/lib", "demo_organization_words.json")
+ with open(path, "rb") as reader:
+ return orjson.loads(reader.read())
+
+
+DEMO_ORGANIZATION_WORD_LIST = get_demo_organization_wordlist()
+
+
def demo_organization_owner_email_exists(realm: Realm) -> bool:
human_owner_emails = set(realm.get_human_owner_users().values_list("delivery_email", flat=True))
return human_owner_emails != {""}
diff --git a/zerver/tests/test_realm_creation.py b/zerver/tests/test_realm_creation.py
index 1ce5a582486..cee66f9d32f 100644
--- a/zerver/tests/test_realm_creation.py
+++ b/zerver/tests/test_realm_creation.py
@@ -49,7 +49,6 @@ class DemoCreationTest(ZulipTestCase):
)
)
- self.assertIn("demo-", realm.string_id)
self.assertIn("demo test", realm.name)
expected_deletion_date = realm.date_created + timedelta(days=30)
self.assertEqual(realm.demo_organization_scheduled_deletion_date, expected_deletion_date)
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index 7ad61cb031d..3662a5875c2 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -3549,7 +3549,6 @@ class UserSignUpTest(ZulipTestCase):
)
self.assertEqual(realm.demo_organization_scheduled_deletion_date, expected_deletion_date)
self.assertIn("Demo organization", realm.name)
- self.assertIn("demo-", realm.string_id)
# Make sure the correct Welcome Bot direct message is sent.
welcome_msg = Message.objects.filter(
diff --git a/zerver/views/registration.py b/zerver/views/registration.py
index c2e477524b8..3570b071bc0 100644
--- a/zerver/views/registration.py
+++ b/zerver/views/registration.py
@@ -1,6 +1,5 @@
import logging
import random
-import string
from collections.abc import Iterable
from contextlib import suppress
from typing import Annotated, Any, cast
@@ -68,6 +67,7 @@ from zerver.forms import (
RegistrationForm,
check_subdomain_available,
)
+from zerver.lib.demo_organizations import DEMO_ORGANIZATION_WORD_LIST
from zerver.lib.email_validation import email_allowed_for_realm, validate_email_not_already_in_realm
from zerver.lib.exceptions import JsonableError, RateLimitedError
from zerver.lib.i18n import (
@@ -1420,11 +1420,13 @@ def create_realm(request: HttpRequest, confirmation_key: str | None = None) -> H
def generate_demo_realm_subdomain() -> str:
demo_subdomain = ""
while True:
- # Generate a moderate-entropy random ID for the organization;
- # there are 10**4 * 26**4 ~= 4.5 billion possibilities.
- letters = "".join(random.SystemRandom().choice(string.ascii_lowercase) for i in range(4))
- digits = "".join(random.SystemRandom().choice(string.digits) for i in range(4))
- demo_subdomain = f"demo-{letters}{digits}"
+ # Generate a moderate-entropy string for the organization;
+ # there are ~460 adverbs, ~1020 adjectives, ~2810 nouns in
+ # the word list so ~= 1.32 billion possibilities.
+ adverb = random.SystemRandom().choice(DEMO_ORGANIZATION_WORD_LIST["adverbs"])
+ adjective = random.SystemRandom().choice(DEMO_ORGANIZATION_WORD_LIST["adjectives"])
+ noun = random.SystemRandom().choice(DEMO_ORGANIZATION_WORD_LIST["nouns"])
+ demo_subdomain = f"{adverb}-{adjective}-{noun}"
try:
check_subdomain_available(demo_subdomain)
break