2021-08-13 20:37:15 +02:00
|
|
|
import random
|
|
|
|
import string
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2019-03-13 18:26:01 +01:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
from confirmation.models import Confirmation, create_confirmation_link
|
2022-01-23 20:37:40 +01:00
|
|
|
from zerver.context_processors import get_realm_from_request
|
2019-01-11 20:56:22 +01:00
|
|
|
from zerver.lib.response import json_success
|
2019-03-13 18:26:01 +01:00
|
|
|
from zerver.lib.subdomains import get_subdomain
|
2021-06-24 20:05:06 +02:00
|
|
|
from zerver.models import Realm, UserProfile
|
2019-03-13 18:26:01 +01:00
|
|
|
from zerver.views.auth import create_preregistration_user
|
|
|
|
from zerver.views.registration import accounts_register
|
|
|
|
|
2019-01-11 20:56:22 +01:00
|
|
|
|
2021-03-31 21:40:49 +02:00
|
|
|
# This is used only by the Puppeteer test in 01-realm-creation.js.
|
2019-01-11 20:56:22 +01:00
|
|
|
def confirmation_key(request: HttpRequest) -> HttpResponse:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data=request.session.get("confirmation_key"))
|
2019-03-13 18:26:01 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
def modify_postdata(request: HttpRequest, **kwargs: Any) -> None:
|
|
|
|
request.POST._mutable = True
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
request.POST[key] = value
|
|
|
|
request.POST._mutable = False
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-13 20:37:15 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
@csrf_exempt
|
|
|
|
def register_development_user(request: HttpRequest) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
if get_subdomain(request) == "":
|
|
|
|
request.META["HTTP_HOST"] = settings.REALM_HOSTS["zulip"]
|
2019-03-13 18:26:01 +01:00
|
|
|
count = UserProfile.objects.count()
|
2021-02-12 08:20:45 +01:00
|
|
|
name = f"user-{count}"
|
|
|
|
email = f"{name}@zulip.com"
|
2022-01-23 20:37:40 +01:00
|
|
|
realm = get_realm_from_request(request)
|
2021-02-12 08:19:30 +01:00
|
|
|
prereg = create_preregistration_user(
|
2022-01-23 20:37:40 +01:00
|
|
|
email, realm, realm_creation=False, password_required=False
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
activation_url = create_confirmation_link(prereg, Confirmation.USER_REGISTRATION)
|
2021-02-12 08:20:45 +01:00
|
|
|
key = activation_url.split("/")[-1]
|
2020-08-11 01:47:44 +02:00
|
|
|
# Need to add test data to POST request as it doesn't originally contain the required parameters
|
2021-02-12 08:20:45 +01:00
|
|
|
modify_postdata(request, key=key, full_name=name, password="test", terms="true")
|
2019-03-13 18:26:01 +01:00
|
|
|
|
|
|
|
return accounts_register(request)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
@csrf_exempt
|
|
|
|
def register_development_realm(request: HttpRequest) -> HttpResponse:
|
|
|
|
count = UserProfile.objects.count()
|
2021-02-12 08:20:45 +01:00
|
|
|
name = f"user-{count}"
|
|
|
|
email = f"{name}@zulip.com"
|
|
|
|
realm_name = f"realm-{count}"
|
2021-06-24 20:05:06 +02:00
|
|
|
realm_type = Realm.ORG_TYPES["business"]["id"]
|
2022-01-23 20:37:40 +01:00
|
|
|
prereg = create_preregistration_user(email, None, realm_creation=True, password_required=False)
|
2021-02-12 08:19:30 +01:00
|
|
|
activation_url = create_confirmation_link(prereg, Confirmation.REALM_CREATION)
|
2021-02-12 08:20:45 +01:00
|
|
|
key = activation_url.split("/")[-1]
|
2020-08-11 01:47:44 +02:00
|
|
|
# Need to add test data to POST request as it doesn't originally contain the required parameters
|
2021-02-12 08:19:30 +01:00
|
|
|
modify_postdata(
|
|
|
|
request,
|
|
|
|
key=key,
|
|
|
|
realm_name=realm_name,
|
2021-06-24 20:05:06 +02:00
|
|
|
realm_type=realm_type,
|
2021-02-12 08:19:30 +01:00
|
|
|
full_name=name,
|
2021-02-12 08:20:45 +01:00
|
|
|
password="test",
|
2021-02-12 08:19:30 +01:00
|
|
|
realm_subdomain=realm_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
terms="true",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-03-13 18:26:01 +01:00
|
|
|
|
|
|
|
return accounts_register(request)
|
2021-08-13 20:37:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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"]
|
2022-01-23 20:37:40 +01:00
|
|
|
prereg = create_preregistration_user(email, None, realm_creation=True, password_required=False)
|
2021-08-13 20:37:15 +02:00
|
|
|
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)
|