From dea4187affd2d1c6a64dd816b107e2f86e208573 Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Fri, 13 Aug 2021 16:07:15 -0230 Subject: [PATCH] devlogin: Add support for creating development demo realms. This is a part of #19523. --- templates/zerver/development/dev_login.html | 3 ++ zerver/forms.py | 1 + zerver/lib/actions.py | 5 +++ zerver/tests/test_signup.py | 22 ++++++++++++ zerver/views/development/registration.py | 37 +++++++++++++++++++++ zerver/views/registration.py | 5 ++- zproject/dev_urls.py | 6 ++++ 7 files changed, 78 insertions(+), 1 deletion(-) diff --git a/templates/zerver/development/dev_login.html b/templates/zerver/development/dev_login.html index 04cfbc7cd9..a4f1ad3020 100644 --- a/templates/zerver/development/dev_login.html +++ b/templates/zerver/development/dev_login.html @@ -89,6 +89,9 @@ page can be easily identified in it's respective JavaScript file -->
+
+ +
Zulip developer tools diff --git a/zerver/forms.py b/zerver/forms.py index 7ef8ef3ca8..8ed89771ef 100644 --- a/zerver/forms.py +++ b/zerver/forms.py @@ -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 diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index a06617903a..d9803e27f3 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -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( diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 48d827e7ec..65a6655975 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -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: diff --git a/zerver/views/development/registration.py b/zerver/views/development/registration.py index 502c87595b..8524a6bcfa 100644 --- a/zerver/views/development/registration.py +++ b/zerver/views/development/registration.py @@ -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) diff --git a/zerver/views/registration.py b/zerver/views/registration.py index 7b36decc86..599e62c4cb 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -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 diff --git a/zproject/dev_urls.py b/zproject/dev_urls.py index 076ddd1f41..cb8cea9f9d 100644 --- a/zproject/dev_urls.py +++ b/zproject/dev_urls.py @@ -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")),