From d5e61e20e57bbca156356c158a255db8d4e70534 Mon Sep 17 00:00:00 2001 From: Mayank Madan Date: Wed, 13 Mar 2019 22:56:01 +0530 Subject: [PATCH] dev tooling: Add "create new user" and "create new realm" buttons. Significantly tweaked by tabbott to clean up and expand the tests. Fixes: #6018. --- static/styles/portico.scss | 7 ++++ templates/zerver/dev_login.html | 8 ++++ templates/zerver/dev_tools.html | 10 +++++ zerver/tests/test_signup.py | 31 ++++++++++++++ zerver/views/development/registration.py | 52 ++++++++++++++++++++++++ zproject/dev_urls.py | 7 ++++ 6 files changed, 115 insertions(+) diff --git a/static/styles/portico.scss b/static/styles/portico.scss index 14892a5975..0c35afe549 100644 --- a/static/styles/portico.scss +++ b/static/styles/portico.scss @@ -2234,6 +2234,13 @@ input.new-organization-button { text-align: right; } +#devtools-registration { + float: left; + form { + display: inline; + } +} + #devtools-page { max-width: 800px; margin: 0 auto; diff --git a/templates/zerver/dev_login.html b/templates/zerver/dev_login.html index 606d7fdd5f..c8f7dfea15 100644 --- a/templates/zerver/dev_login.html +++ b/templates/zerver/dev_login.html @@ -63,6 +63,14 @@ page can be easily identified in it's respective JavaScript file -->
+
+
+ +
+
+ +
+
Zulip developer tools
diff --git a/templates/zerver/dev_tools.html b/templates/zerver/dev_tools.html index 7c36afc5ef..f45d58faa6 100644 --- a/templates/zerver/dev_tools.html +++ b/templates/zerver/dev_tools.html @@ -78,6 +78,16 @@ None needed Test incoming webhook integrations + + /devtools/register_user + None needed + Creates a new user + + + /devtools/register_realm + None needed + Creates a new realm +

Development-specific management commands live in zilencer/management/commands. Highlights include: diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index b09609087d..e5bb7077e9 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -3118,6 +3118,37 @@ class UserSignUpTest(InviteUserBase): with self.assertRaisesRegex(AssertionError, "Mirror dummy user is already active!"): self.client_post('/register/', {'email': email}, subdomain="zephyr") + @override_settings(TERMS_OF_SERVICE=False) + def test_dev_user_registration(self) -> None: + """Verify that /devtools/register_user creates a new user, logs them + in, and redirects to the logged-in app.""" + count = UserProfile.objects.count() + email = "user-%d@zulip.com" % (count,) + + result = self.client_post('/devtools/register_user/') + user_profile = UserProfile.objects.all().order_by("id").last() + + self.assertEqual(result.status_code, 302) + self.assertEqual(user_profile.email, email) + self.assertEqual(result['Location'], "http://zulip.testserver/") + self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) + + @override_settings(TERMS_OF_SERVICE=False) + def test_dev_user_registration_create_realm(self) -> None: + count = UserProfile.objects.count() + string_id = "realm-%d" % (count,) + + result = self.client_post('/devtools/register_realm/') + self.assertEqual(result.status_code, 302) + self.assertTrue(result["Location"].startswith( + 'http://{}.testserver/accounts/login/subdomain'.format(string_id))) + result = self.client_get(result["Location"], subdomain=string_id) + self.assertEqual(result.status_code, 302) + self.assertEqual(result["Location"], 'http://{}.testserver'.format(string_id)) + + user_profile = UserProfile.objects.all().order_by("id").last() + self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) + class DeactivateUserTest(ZulipTestCase): def test_deactivate_user(self) -> None: diff --git a/zerver/views/development/registration.py b/zerver/views/development/registration.py index 432c71c5c5..5516216f9c 100644 --- a/zerver/views/development/registration.py +++ b/zerver/views/development/registration.py @@ -1,6 +1,58 @@ +from django.conf import settings from django.http import HttpResponse, HttpRequest +from django.views.decorators.csrf import csrf_exempt + +from confirmation.models import Confirmation, create_confirmation_link + +from typing import Any + +from zerver.models import UserProfile from zerver.lib.response import json_success +from zerver.lib.subdomains import get_subdomain +from zerver.views.auth import create_preregistration_user +from zerver.views.registration import accounts_register + # This is used only by the casper test in 00-realm-creation.js. def confirmation_key(request: HttpRequest) -> HttpResponse: return json_success(request.session.get('confirmation_key')) + +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 + +@csrf_exempt +def register_development_user(request: HttpRequest) -> HttpResponse: + if get_subdomain(request) == '': + request.META['HTTP_HOST'] = settings.REALM_HOSTS['zulip'] + count = UserProfile.objects.count() + name = 'user-%d' % (count,) + email = '%s@zulip.com' % (name,) + prereg = create_preregistration_user(email, request, realm_creation=False, + password_required=False) + activation_url = create_confirmation_link(prereg, request.get_host(), + Confirmation.USER_REGISTRATION) + key = activation_url.split('/')[-1] + # Need to add test data to POST request as it doesnt originally contain the required parameters + modify_postdata(request, key=key, full_name=name, password='test') + + return accounts_register(request) + +@csrf_exempt +def register_development_realm(request: HttpRequest) -> HttpResponse: + count = UserProfile.objects.count() + name = 'user-%d' % (count,) + email = '%s@zulip.com' % (name,) + realm_name = 'realm-%d' % (count,) + prereg = create_preregistration_user(email, request, realm_creation=True, + password_required=False) + activation_url = create_confirmation_link(prereg, request.get_host(), + Confirmation.REALM_CREATION) + key = activation_url.split('/')[-1] + # Need to add test data to POST request as it doesnt originally contain the required parameters + modify_postdata(request, key=key, realm_name=realm_name, full_name=name, password='test', + realm_subdomain=realm_name) + + return accounts_register(request) diff --git a/zproject/dev_urls.py b/zproject/dev_urls.py index d0a1f890ba..3825c1abbd 100644 --- a/zproject/dev_urls.py +++ b/zproject/dev_urls.py @@ -45,6 +45,13 @@ urls = [ # Listing of useful URLs and various tools for development url(r'^devtools/$', TemplateView.as_view(template_name='zerver/dev_tools.html')), + # Register New User and Realm + url(r'^devtools/register_user/$', + zerver.views.development.registration.register_development_user, + name='zerver.views.development.registration.register_development_user'), + url(r'^devtools/register_realm/$', + zerver.views.development.registration.register_development_realm, + name='zerver.views.development.registration.register_development_realm'), # Have easy access for error pages url(r'^errors/404/$', TemplateView.as_view(template_name='404.html')),