dev tooling: Add "create new user" and "create new realm" buttons.

Significantly tweaked by tabbott to clean up and expand the tests.

Fixes: #6018.
This commit is contained in:
Mayank Madan 2019-03-13 22:56:01 +05:30 committed by Tim Abbott
parent 6a42280e31
commit d5e61e20e5
6 changed files with 115 additions and 0 deletions

View File

@ -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;

View File

@ -63,6 +63,14 @@ page can be easily identified in it's respective JavaScript file -->
</select>
</form>
<div id="devtools-wrapper">
<div id="devtools-registration">
<form name="register_dev_user" action="{{ url('zerver.views.development.registration.register_development_user') }}" method="POST">
<input type="submit" class="btn btn-admin" value="Create New User" />
</form>
<form name="register_dev_realm" action="{{ url('zerver.views.development.registration.register_development_realm') }}" method="POST">
<input type="submit" class="btn btn-admin" value="Create New Realm" />
</form>
</div>
<a href="/devtools">Zulip developer tools</a>
</div>
</div>

View File

@ -78,6 +78,16 @@
<td>None needed</td>
<td>Test incoming webhook integrations</td>
</tr>
<tr>
<td><a href="/devtools/register_user">/devtools/register_user</a></td>
<td>None needed</td>
<td>Creates a new user</td>
</tr>
<tr>
<td><a href="/devtools/register_realm">/devtools/register_realm</a></td>
<td>None needed</td>
<td>Creates a new realm</td>
</tr>
</tbody>
</table>
<p>Development-specific management commands live in <code>zilencer/management/commands</code>. Highlights include:

View File

@ -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:

View File

@ -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)

View File

@ -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')),