2017-11-16 00:55:49 +01:00
|
|
|
import itertools
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
from typing import Any, Callable, Dict, Iterable, List, \
|
|
|
|
Mapping, Optional, Sequence, Set, Text, Tuple
|
|
|
|
|
|
|
|
import ujson
|
|
|
|
from django.conf import settings
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2017-07-28 12:14:57 +02:00
|
|
|
from django.db.models import F, Max
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-02-13 13:56:53 +01:00
|
|
|
from django.utils.timezone import timedelta as timezone_timedelta
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS, check_add_realm_emoji, \
|
2018-03-12 01:51:07 +01:00
|
|
|
do_change_is_admin, do_send_messages, do_update_user_custom_profile_data, \
|
|
|
|
try_add_realm_custom_profile_field
|
2017-07-18 22:49:10 +02:00
|
|
|
from zerver.lib.bulk_create import bulk_create_streams, bulk_create_users
|
2018-03-26 14:42:39 +02:00
|
|
|
from zerver.lib.cache import cache_set
|
2017-05-23 23:57:42 +02:00
|
|
|
from zerver.lib.generate_test_data import create_test_data
|
2017-07-14 14:56:53 +02:00
|
|
|
from zerver.lib.upload import upload_backend
|
2018-03-26 14:42:39 +02:00
|
|
|
from zerver.lib.url_preview.preview import CACHE_NAME as PREVIEW_CACHE_NAME
|
2017-11-13 07:49:01 +01:00
|
|
|
from zerver.lib.user_groups import create_user_group
|
2018-03-12 01:51:07 +01:00
|
|
|
from zerver.models import CustomProfileField, DefaultStream, Message, Realm, RealmAuditLog, \
|
2017-11-16 00:55:49 +01:00
|
|
|
RealmDomain, RealmEmoji, Recipient, Service, Stream, Subscription, \
|
|
|
|
UserMessage, UserPresence, UserProfile, clear_database, \
|
|
|
|
email_to_username, get_client, get_huddle, get_realm, get_stream, \
|
|
|
|
get_system_bot, get_user, get_user_profile_by_id
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-11-08 21:49:04 +01:00
|
|
|
settings.TORNADO_SERVER = None
|
2017-02-27 05:46:17 +01:00
|
|
|
# Disable using memcached caches to avoid 'unsupported pickle
|
|
|
|
# protocol' errors if `populate_db` is run with a different Python
|
|
|
|
# from `run-dev.py`.
|
|
|
|
settings.CACHES['default'] = {
|
|
|
|
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
|
|
|
|
}
|
2012-10-12 21:27:19 +02:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def create_users(realm: Realm, name_list: Iterable[Tuple[Text, Text]],
|
|
|
|
bot_type: Optional[int]=None,
|
|
|
|
bot_owner: Optional[UserProfile]=None) -> None:
|
2017-07-09 02:11:44 +02:00
|
|
|
user_set = set() # type: Set[Tuple[Text, Text, Text, bool]]
|
2012-10-23 23:29:56 +02:00
|
|
|
for full_name, email in name_list:
|
2013-07-18 18:48:56 +02:00
|
|
|
short_name = email_to_username(email)
|
2012-10-23 23:29:56 +02:00
|
|
|
user_set.add((email, full_name, short_name, True))
|
2016-08-10 03:05:26 +02:00
|
|
|
tos_version = settings.TOS_VERSION if bot_type is None else None
|
2017-09-26 11:34:31 +02:00
|
|
|
bulk_create_users(realm, user_set, bot_type=bot_type, bot_owner=bot_owner, tos_version=tos_version)
|
2012-10-23 23:29:56 +02:00
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
class Command(BaseCommand):
|
2012-08-28 18:45:10 +02:00
|
|
|
help = "Populate a test database"
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('-n', '--num-messages',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='num_messages',
|
|
|
|
type=int,
|
2017-05-23 23:57:42 +02:00
|
|
|
default=500,
|
2016-12-03 00:04:17 +01:00
|
|
|
help='The number of messages to create.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--extra-users',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='extra_users',
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2017-02-11 02:32:54 +01:00
|
|
|
parser.add_argument('--extra-bots',
|
|
|
|
dest='extra_bots',
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help='The number of extra bots to create')
|
|
|
|
|
2018-01-02 18:03:14 +01:00
|
|
|
parser.add_argument('--extra-streams',
|
|
|
|
dest='extra_streams',
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help='The number of extra streams to create')
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('--huddles',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='num_huddles',
|
|
|
|
type=int,
|
|
|
|
default=3,
|
|
|
|
help='The number of huddles to create.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--personals',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='num_personals',
|
|
|
|
type=int,
|
|
|
|
default=6,
|
|
|
|
help='The number of personal pairs to create.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--threads',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='threads',
|
|
|
|
type=int,
|
2018-02-13 13:56:53 +01:00
|
|
|
default=1,
|
2016-12-03 00:04:17 +01:00
|
|
|
help='The number of threads to use.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--percent-huddles',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='percent_huddles',
|
|
|
|
type=float,
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be huddles.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--percent-personals',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='percent_personals',
|
|
|
|
type=float,
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be personals.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--stickyness',
|
2016-12-03 00:04:17 +01:00
|
|
|
dest='stickyness',
|
|
|
|
type=float,
|
|
|
|
default=20,
|
|
|
|
help='The percent of messages to repeat recent folks.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--nodelete',
|
2016-12-03 00:04:17 +01:00
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
dest='delete',
|
|
|
|
help='Whether to delete all the existing messages.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument('--test-suite',
|
2016-12-03 00:04:17 +01:00
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
help='Whether to delete all the existing messages.')
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def handle(self, **options: Any) -> None:
|
2012-09-05 18:38:35 +02:00
|
|
|
if options["percent_huddles"] + options["percent_personals"] > 100:
|
|
|
|
self.stderr.write("Error! More than 100% of messages allocated.\n")
|
|
|
|
return
|
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-10-29 19:43:00 +01:00
|
|
|
# Start by clearing all the data in our database
|
|
|
|
clear_database()
|
2012-09-05 21:49:56 +02:00
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
# Create our two default realms
|
2016-09-28 00:03:13 +02:00
|
|
|
# Could in theory be done via zerver.lib.actions.do_create_realm, but
|
|
|
|
# welcome-bot (needed for do_create_realm) hasn't been created yet
|
2016-10-28 21:43:47 +02:00
|
|
|
zulip_realm = Realm.objects.create(
|
|
|
|
string_id="zulip", name="Zulip Dev", restricted_to_domain=True,
|
2017-11-04 14:16:50 +01:00
|
|
|
description="The Zulip development environment default organization."
|
|
|
|
" It's great for testing!",
|
2017-03-14 00:51:51 +01:00
|
|
|
invite_required=False, org_type=Realm.CORPORATE)
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=zulip_realm, domain="zulip.com")
|
2015-08-20 03:54:07 +02:00
|
|
|
if options["test_suite"]:
|
2016-10-28 21:43:47 +02:00
|
|
|
mit_realm = Realm.objects.create(
|
2017-03-04 09:19:37 +01:00
|
|
|
string_id="zephyr", name="MIT", restricted_to_domain=True,
|
2017-03-14 00:51:51 +01:00
|
|
|
invite_required=False, org_type=Realm.CORPORATE)
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.create(realm=mit_realm, domain="mit.edu")
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2017-11-28 02:42:11 +01:00
|
|
|
lear_realm = Realm.objects.create(
|
|
|
|
string_id="lear", name="Lear & Co.", restricted_to_domain=False,
|
|
|
|
invite_required=False, org_type=Realm.CORPORATE)
|
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create test Users (UserProfiles are automatically created,
|
|
|
|
# as are subscriptions to the ability to receive personals).
|
2016-11-18 21:46:47 +01:00
|
|
|
names = [
|
|
|
|
("Zoe", "ZOE@zulip.com"),
|
|
|
|
("Othello, the Moor of Venice", "othello@zulip.com"),
|
|
|
|
("Iago", "iago@zulip.com"),
|
|
|
|
("Prospero from The Tempest", "prospero@zulip.com"),
|
|
|
|
("Cordelia Lear", "cordelia@zulip.com"),
|
|
|
|
("King Hamlet", "hamlet@zulip.com"),
|
|
|
|
("aaron", "AARON@zulip.com"),
|
2018-04-20 22:12:02 +02:00
|
|
|
("Polonius", "polonius@zulip.com"),
|
2016-11-18 21:46:47 +01:00
|
|
|
]
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["extra_users"]):
|
2013-07-24 20:56:42 +02:00
|
|
|
names.append(('Extra User %d' % (i,), 'extrauser%d@zulip.com' % (i,)))
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, names)
|
2017-08-15 21:54:50 +02:00
|
|
|
|
|
|
|
iago = get_user("iago@zulip.com", zulip_realm)
|
2015-08-20 03:53:48 +02:00
|
|
|
do_change_is_admin(iago, True)
|
2017-03-28 00:37:08 +02:00
|
|
|
iago.is_staff = True
|
|
|
|
iago.save(update_fields=['is_staff'])
|
2017-10-12 08:16:05 +02:00
|
|
|
|
2018-04-20 22:12:02 +02:00
|
|
|
guest_user = get_user("polonius@zulip.com", zulip_realm)
|
|
|
|
guest_user.is_guest = True
|
|
|
|
guest_user.save(update_fields=['is_guest'])
|
|
|
|
|
2017-10-12 08:16:05 +02:00
|
|
|
# These bots are directly referenced from code and thus
|
|
|
|
# are needed for the test suite.
|
|
|
|
all_realm_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.INTERNAL_BOTS]
|
|
|
|
zulip_realm_bots = [
|
|
|
|
("Zulip New User Bot", "new-user-bot@zulip.com"),
|
|
|
|
("Zulip Error Bot", "error-bot@zulip.com"),
|
|
|
|
("Zulip Default Bot", "default-bot@zulip.com"),
|
|
|
|
("Welcome Bot", "welcome-bot@zulip.com"),
|
|
|
|
]
|
|
|
|
|
|
|
|
for i in range(options["extra_bots"]):
|
|
|
|
zulip_realm_bots.append(('Extra Bot %d' % (i,), 'extrabot%d@zulip.com' % (i,)))
|
|
|
|
zulip_realm_bots.extend(all_realm_bots)
|
|
|
|
create_users(zulip_realm, zulip_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
|
2018-03-13 23:36:11 +01:00
|
|
|
zoe = get_user("zoe@zulip.com", zulip_realm)
|
2017-10-12 08:16:05 +02:00
|
|
|
zulip_webhook_bots = [
|
|
|
|
("Zulip Webhook Bot", "webhook-bot@zulip.com"),
|
|
|
|
]
|
2018-03-13 23:36:11 +01:00
|
|
|
# If a stream is not supplied in the webhook URL, the webhook
|
|
|
|
# will (in some cases) send the notification as a PM to the
|
|
|
|
# owner of the webhook bot, so bot_owner can't be None
|
2017-10-12 08:16:05 +02:00
|
|
|
create_users(zulip_realm, zulip_webhook_bots,
|
2018-03-13 23:36:11 +01:00
|
|
|
bot_type=UserProfile.INCOMING_WEBHOOK_BOT, bot_owner=zoe)
|
|
|
|
aaron = get_user("AARON@zulip.com", zulip_realm)
|
2017-10-12 08:16:05 +02:00
|
|
|
zulip_outgoing_bots = [
|
|
|
|
("Outgoing Webhook", "outgoing-webhook@zulip.com")
|
|
|
|
]
|
|
|
|
create_users(zulip_realm, zulip_outgoing_bots,
|
|
|
|
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_owner=aaron)
|
|
|
|
# TODO: Clean up this initial bot creation code
|
|
|
|
Service.objects.create(
|
|
|
|
name="test",
|
|
|
|
user_profile=get_user("outgoing-webhook@zulip.com", zulip_realm),
|
|
|
|
base_url="http://127.0.0.1:5002/bots/followup",
|
|
|
|
token="abcd1234",
|
|
|
|
interface=1)
|
|
|
|
|
2012-10-10 23:17:43 +02:00
|
|
|
# Create public streams.
|
2012-10-23 23:29:56 +02:00
|
|
|
stream_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"]
|
2016-12-08 00:02:21 +01:00
|
|
|
stream_dict = {
|
|
|
|
"Verona": {"description": "A city in Italy", "invite_only": False},
|
|
|
|
"Denmark": {"description": "A Scandinavian country", "invite_only": False},
|
|
|
|
"Scotland": {"description": "Located in the United Kingdom", "invite_only": False},
|
|
|
|
"Venice": {"description": "A northeastern Italian city", "invite_only": False},
|
|
|
|
"Rome": {"description": "Yet another Italian city", "invite_only": False}
|
2017-07-09 02:11:44 +02:00
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
2016-12-08 00:02:21 +01:00
|
|
|
|
2016-12-15 17:07:45 +01:00
|
|
|
bulk_create_streams(zulip_realm, stream_dict)
|
2016-08-03 23:37:12 +02:00
|
|
|
recipient_streams = [Stream.objects.get(name=name, realm=zulip_realm).id
|
2017-07-09 02:11:44 +02:00
|
|
|
for name in stream_list] # type: List[int]
|
2016-04-13 04:49:13 +02:00
|
|
|
# Create subscriptions to streams. The following
|
|
|
|
# algorithm will give each of the users a different but
|
|
|
|
# deterministic subset of the streams (given a fixed list
|
|
|
|
# of users).
|
2017-07-09 02:11:44 +02:00
|
|
|
subscriptions_to_add = [] # type: List[Subscription]
|
2017-07-12 17:28:27 +02:00
|
|
|
event_time = timezone_now()
|
|
|
|
all_subscription_logs = [] # type: (List[RealmAuditLog])
|
2017-10-12 08:16:05 +02:00
|
|
|
profiles = UserProfile.objects.select_related().filter(
|
2018-04-20 22:12:02 +02:00
|
|
|
is_bot=False, is_guest=False).order_by("email") # type: Sequence[UserProfile]
|
2012-09-10 20:38:29 +02:00
|
|
|
for i, profile in enumerate(profiles):
|
2012-10-10 23:17:43 +02:00
|
|
|
# Subscribe to some streams.
|
2012-10-23 23:29:56 +02:00
|
|
|
for type_id in recipient_streams[:int(len(recipient_streams) *
|
|
|
|
float(i)/len(profiles)) + 1]:
|
|
|
|
r = Recipient.objects.get(type=Recipient.STREAM, type_id=type_id)
|
2016-08-03 23:37:12 +02:00
|
|
|
s = Subscription(
|
|
|
|
recipient=r,
|
|
|
|
user_profile=profile,
|
|
|
|
color=STREAM_ASSIGNMENT_COLORS[i % len(STREAM_ASSIGNMENT_COLORS)])
|
|
|
|
|
2012-10-23 23:29:56 +02:00
|
|
|
subscriptions_to_add.append(s)
|
2017-07-12 17:28:27 +02:00
|
|
|
|
|
|
|
log = RealmAuditLog(realm=profile.realm,
|
|
|
|
modified_user=profile,
|
|
|
|
modified_stream_id=type_id,
|
|
|
|
event_last_message_id=0,
|
|
|
|
event_type='subscription_created',
|
|
|
|
event_time=event_time)
|
|
|
|
all_subscription_logs.append(log)
|
|
|
|
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2017-07-12 17:28:27 +02:00
|
|
|
RealmAuditLog.objects.bulk_create(all_subscription_logs)
|
2018-03-12 01:51:07 +01:00
|
|
|
|
2018-03-19 20:17:52 +01:00
|
|
|
# Create custom profile field data
|
|
|
|
phone_number = try_add_realm_custom_profile_field(zulip_realm, "Phone number",
|
2018-03-31 07:30:24 +02:00
|
|
|
CustomProfileField.SHORT_TEXT,
|
|
|
|
hint='')
|
2018-03-19 20:17:52 +01:00
|
|
|
biography = try_add_realm_custom_profile_field(zulip_realm, "Biography",
|
2018-03-31 07:30:24 +02:00
|
|
|
CustomProfileField.LONG_TEXT,
|
|
|
|
hint='What are you known for?')
|
2018-04-02 15:04:22 +02:00
|
|
|
favorite_food = try_add_realm_custom_profile_field(zulip_realm, "Favorite food",
|
2018-03-31 07:30:24 +02:00
|
|
|
CustomProfileField.SHORT_TEXT,
|
|
|
|
hint="Or drink, if you'd prefer")
|
2018-03-19 20:17:52 +01:00
|
|
|
|
|
|
|
# Fill in values for Iago and Hamlet
|
|
|
|
hamlet = get_user("hamlet@zulip.com", zulip_realm)
|
|
|
|
do_update_user_custom_profile_data(iago, [
|
|
|
|
{"id": phone_number.id, "value": "+1-234-567-8901"},
|
|
|
|
{"id": biography.id, "value": "Betrayer of Othello."},
|
2018-04-02 15:04:22 +02:00
|
|
|
{"id": favorite_food.id, "value": "Apples"},
|
2018-03-19 20:17:52 +01:00
|
|
|
])
|
|
|
|
do_update_user_custom_profile_data(hamlet, [
|
|
|
|
{"id": phone_number.id, "value": "+0-11-23-456-7890"},
|
|
|
|
{"id": biography.id, "value": "Prince of Denmark, and other things!"},
|
2018-04-02 15:04:22 +02:00
|
|
|
{"id": favorite_food.id, "value": "Dark chocolate"},
|
2018-03-19 20:17:52 +01:00
|
|
|
])
|
2012-09-10 20:38:29 +02:00
|
|
|
else:
|
2017-01-04 05:30:48 +01:00
|
|
|
zulip_realm = get_realm("zulip")
|
2012-10-10 23:17:43 +02:00
|
|
|
recipient_streams = [klass.type_id for klass in
|
2012-10-10 22:57:21 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)]
|
2012-09-10 20:38:29 +02:00
|
|
|
|
|
|
|
# Extract a list of all users
|
2017-10-12 08:16:05 +02:00
|
|
|
user_profiles = list(UserProfile.objects.filter(is_bot=False)) # type: List[UserProfile]
|
2016-12-09 20:18:52 +01:00
|
|
|
|
2017-07-14 14:56:53 +02:00
|
|
|
# Create a test realm emoji.
|
2017-10-02 20:38:51 +02:00
|
|
|
IMAGE_FILE_PATH = os.path.join(settings.STATIC_ROOT, 'images', 'test-images', 'checkbox.png')
|
2017-07-14 14:56:53 +02:00
|
|
|
with open(IMAGE_FILE_PATH, 'rb') as fp:
|
2018-03-11 18:55:20 +01:00
|
|
|
check_add_realm_emoji(zulip_realm, 'green_tick', iago, fp)
|
2017-07-14 14:56:53 +02:00
|
|
|
|
2016-12-09 20:18:52 +01:00
|
|
|
if not options["test_suite"]:
|
|
|
|
# Populate users with some bar data
|
|
|
|
for user in user_profiles:
|
2017-07-09 02:11:44 +02:00
|
|
|
status = UserPresence.ACTIVE # type: int
|
2017-04-15 04:03:56 +02:00
|
|
|
date = timezone_now()
|
2016-12-09 20:18:52 +01:00
|
|
|
client = get_client("website")
|
2017-02-11 08:50:09 +01:00
|
|
|
if user.full_name[0] <= 'H':
|
|
|
|
client = get_client("ZulipAndroid")
|
2017-11-04 14:16:50 +01:00
|
|
|
UserPresence.objects.get_or_create(user_profile=user,
|
|
|
|
client=client,
|
|
|
|
timestamp=date,
|
|
|
|
status=status)
|
2016-12-09 20:18:52 +01:00
|
|
|
|
|
|
|
user_profiles_ids = [user_profile.id for user_profile in user_profiles]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-05 18:38:35 +02:00
|
|
|
# Create several initial huddles
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["num_huddles"]):
|
2016-12-09 20:18:52 +01:00
|
|
|
get_huddle(random.sample(user_profiles_ids, random.randint(3, 4)))
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
# Create several initial pairs for personals
|
2016-12-09 20:18:52 +01:00
|
|
|
personals_pairs = [random.sample(user_profiles_ids, 2)
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(options["num_personals"])]
|
2012-09-14 17:52:38 +02:00
|
|
|
|
2017-08-05 20:10:11 +02:00
|
|
|
# Generate a new set of test data.
|
|
|
|
create_test_data()
|
|
|
|
|
2018-03-26 14:42:39 +02:00
|
|
|
# prepopulate the URL preview/embed data for the links present
|
|
|
|
# in the config.generate_data.json data set. This makes it
|
|
|
|
# possible for populate_db to run happily without Internet
|
|
|
|
# access.
|
2018-04-19 20:17:24 +02:00
|
|
|
with open("zerver/tests/fixtures/docs_url_preview_data.json", "r") as f:
|
2018-03-26 14:42:39 +02:00
|
|
|
urls_with_preview_data = ujson.load(f)
|
|
|
|
for url in urls_with_preview_data:
|
|
|
|
cache_set(url, urls_with_preview_data[url], PREVIEW_CACHE_NAME)
|
|
|
|
|
2012-09-14 22:52:09 +02:00
|
|
|
threads = options["threads"]
|
2017-08-05 20:10:11 +02:00
|
|
|
jobs = [] # type: List[Tuple[int, List[List[int]], Dict[str, Any], Callable[[str], int], int]]
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(threads):
|
2016-01-24 03:56:05 +01:00
|
|
|
count = options["num_messages"] // threads
|
2012-10-03 21:07:40 +02:00
|
|
|
if i < options["num_messages"] % threads:
|
2012-09-14 18:31:11 +02:00
|
|
|
count += 1
|
2017-08-05 20:10:11 +02:00
|
|
|
jobs.append((count, personals_pairs, options, self.stdout.write, random.randint(0, 10**10)))
|
2013-11-01 20:51:09 +01:00
|
|
|
|
|
|
|
for job in jobs:
|
|
|
|
send_messages(job)
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-11-28 23:09:11 +01:00
|
|
|
# Create the "website" and "API" clients; if we don't, the
|
2013-07-29 23:03:31 +02:00
|
|
|
# default values in zerver/decorators.py will not work
|
2012-11-28 23:09:11 +01:00
|
|
|
# with the Django test suite.
|
|
|
|
get_client("website")
|
|
|
|
get_client("API")
|
|
|
|
|
2015-08-19 05:43:13 +02:00
|
|
|
if options["test_suite"]:
|
|
|
|
# Create test users; the MIT ones are needed to test
|
|
|
|
# the Zephyr mirroring codepaths.
|
|
|
|
testsuite_mit_users = [
|
|
|
|
("Fred Sipb (MIT)", "sipbtest@mit.edu"),
|
|
|
|
("Athena Consulting Exchange User (MIT)", "starnine@mit.edu"),
|
|
|
|
("Esp Classroom (MIT)", "espuser@mit.edu"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(mit_realm, testsuite_mit_users)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2017-11-28 02:42:11 +01:00
|
|
|
testsuite_lear_users = [
|
|
|
|
("King Lear", "king@lear.org"),
|
2017-11-28 03:29:56 +01:00
|
|
|
("Cordelia Lear", "cordelia@zulip.com"),
|
2017-11-28 02:42:11 +01:00
|
|
|
]
|
|
|
|
create_users(lear_realm, testsuite_lear_users)
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
if not options["test_suite"]:
|
2016-02-08 03:59:38 +01:00
|
|
|
# Initialize the email gateway bot as an API Super User
|
2017-08-15 21:54:50 +02:00
|
|
|
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT)
|
2016-02-08 03:59:38 +01:00
|
|
|
email_gateway_bot.is_api_super_user = True
|
|
|
|
email_gateway_bot.save()
|
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# To keep the messages.json fixtures file for the test
|
|
|
|
# suite fast, don't add these users and subscriptions
|
|
|
|
# when running populate_db for the test suite
|
|
|
|
|
2016-12-08 00:02:21 +01:00
|
|
|
zulip_stream_dict = {
|
|
|
|
"devel": {"description": "For developing", "invite_only": False},
|
|
|
|
"all": {"description": "For everything", "invite_only": False},
|
|
|
|
"announce": {"description": "For announcements", "invite_only": False},
|
|
|
|
"design": {"description": "For design", "invite_only": False},
|
|
|
|
"support": {"description": "For support", "invite_only": False},
|
|
|
|
"social": {"description": "For socializing", "invite_only": False},
|
|
|
|
"test": {"description": "For testing", "invite_only": False},
|
|
|
|
"errors": {"description": "For errors", "invite_only": False},
|
|
|
|
"sales": {"description": "For sales discussion", "invite_only": False}
|
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
2018-01-02 18:03:14 +01:00
|
|
|
|
2018-01-02 22:54:53 +01:00
|
|
|
# Calculate the maximum number of digits in any extra stream's
|
|
|
|
# number, since a stream with name "Extra Stream 3" could show
|
|
|
|
# up after "Extra Stream 29". (Used later to pad numbers with
|
|
|
|
# 0s).
|
|
|
|
maximum_digits = len(str(options['extra_streams'] - 1))
|
|
|
|
|
2018-01-02 18:03:14 +01:00
|
|
|
for i in range(options['extra_streams']):
|
2018-01-02 22:54:53 +01:00
|
|
|
# Pad the number with 0s based on `maximum_digits`.
|
2018-01-02 18:03:14 +01:00
|
|
|
number_str = str(i).zfill(maximum_digits)
|
|
|
|
|
|
|
|
extra_stream_name = 'Extra Stream ' + number_str
|
|
|
|
|
|
|
|
zulip_stream_dict[extra_stream_name] = {
|
|
|
|
"description": "Auto-generated extra stream.",
|
|
|
|
"invite_only": False,
|
|
|
|
}
|
|
|
|
|
2016-12-15 17:07:45 +01:00
|
|
|
bulk_create_streams(zulip_realm, zulip_stream_dict)
|
2016-12-16 07:40:15 +01:00
|
|
|
# Now that we've created the notifications stream, configure it properly.
|
|
|
|
zulip_realm.notifications_stream = get_stream("announce", zulip_realm)
|
|
|
|
zulip_realm.save(update_fields=['notifications_stream'])
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2013-07-08 23:24:26 +02:00
|
|
|
# Add a few default streams
|
2016-12-08 00:02:21 +01:00
|
|
|
for default_stream_name in ["design", "devel", "social", "support"]:
|
|
|
|
DefaultStream.objects.create(realm=zulip_realm,
|
|
|
|
stream=get_stream(default_stream_name, zulip_realm))
|
2013-07-08 23:24:26 +02:00
|
|
|
|
2013-01-08 21:59:52 +01:00
|
|
|
# Now subscribe everyone to these streams
|
|
|
|
subscriptions_to_add = []
|
2017-07-12 17:28:27 +02:00
|
|
|
event_time = timezone_now()
|
|
|
|
all_subscription_logs = []
|
2013-08-06 17:40:44 +02:00
|
|
|
profiles = UserProfile.objects.select_related().filter(realm=zulip_realm)
|
2016-12-08 00:02:21 +01:00
|
|
|
for i, stream_name in enumerate(zulip_stream_dict):
|
2016-08-03 23:37:12 +02:00
|
|
|
stream = Stream.objects.get(name=stream_name, realm=zulip_realm)
|
2013-01-08 21:59:52 +01:00
|
|
|
recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
|
|
|
|
for profile in profiles:
|
|
|
|
# Subscribe to some streams.
|
2016-08-03 23:37:12 +02:00
|
|
|
s = Subscription(
|
|
|
|
recipient=recipient,
|
|
|
|
user_profile=profile,
|
|
|
|
color=STREAM_ASSIGNMENT_COLORS[i % len(STREAM_ASSIGNMENT_COLORS)])
|
2013-01-08 21:59:52 +01:00
|
|
|
subscriptions_to_add.append(s)
|
2017-07-12 17:28:27 +02:00
|
|
|
|
|
|
|
log = RealmAuditLog(realm=profile.realm,
|
|
|
|
modified_user=profile,
|
|
|
|
modified_stream=stream,
|
|
|
|
event_last_message_id=0,
|
|
|
|
event_type='subscription_created',
|
|
|
|
event_time=event_time)
|
|
|
|
all_subscription_logs.append(log)
|
2013-03-27 15:58:23 +01:00
|
|
|
Subscription.objects.bulk_create(subscriptions_to_add)
|
2017-07-12 17:28:27 +02:00
|
|
|
RealmAuditLog.objects.bulk_create(all_subscription_logs)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
|
|
|
# These bots are not needed by the test suite
|
2013-08-06 22:19:25 +02:00
|
|
|
internal_zulip_users_nosubs = [
|
2013-07-24 20:19:19 +02:00
|
|
|
("Zulip Commit Bot", "commit-bot@zulip.com"),
|
2013-07-24 20:05:33 +02:00
|
|
|
("Zulip Trac Bot", "trac-bot@zulip.com"),
|
2013-07-24 20:03:00 +02:00
|
|
|
("Zulip Nagios Bot", "nagios-bot@zulip.com"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, internal_zulip_users_nosubs, bot_type=UserProfile.DEFAULT_BOT)
|
2013-01-08 21:59:52 +01:00
|
|
|
|
2016-11-02 23:09:49 +01:00
|
|
|
zulip_cross_realm_bots = [
|
|
|
|
("Zulip Feedback Bot", "feedback@zulip.com"),
|
2017-01-24 06:34:26 +01:00
|
|
|
]
|
2016-12-15 17:25:59 +01:00
|
|
|
create_users(zulip_realm, zulip_cross_realm_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2016-11-02 23:09:49 +01:00
|
|
|
|
2013-03-07 17:12:35 +01:00
|
|
|
# Mark all messages as read
|
2013-11-01 20:51:09 +01:00
|
|
|
UserMessage.objects.all().update(flags=UserMessage.flags.read)
|
2013-03-07 17:12:35 +01:00
|
|
|
|
2017-07-28 12:14:57 +02:00
|
|
|
if not options["test_suite"]:
|
|
|
|
# Update pointer of each user to point to the last message in their
|
|
|
|
# UserMessage rows with sender_id=user_profile_id.
|
|
|
|
users = list(UserMessage.objects.filter(
|
|
|
|
message__sender_id=F('user_profile_id')).values(
|
|
|
|
'user_profile_id').annotate(pointer=Max('message_id')))
|
|
|
|
for user in users:
|
|
|
|
UserProfile.objects.filter(id=user['user_profile_id']).update(
|
|
|
|
pointer=user['pointer'])
|
|
|
|
|
2017-11-13 07:49:01 +01:00
|
|
|
create_user_groups()
|
2012-09-10 20:38:29 +02:00
|
|
|
self.stdout.write("Successfully populated test database.\n")
|
2012-10-02 22:58:13 +02:00
|
|
|
|
2017-07-09 02:11:44 +02:00
|
|
|
recipient_hash = {} # type: Dict[int, Recipient]
|
2017-10-27 12:57:54 +02:00
|
|
|
def get_recipient_by_id(rid: int) -> Recipient:
|
2012-09-14 22:43:54 +02:00
|
|
|
if rid in recipient_hash:
|
|
|
|
return recipient_hash[rid]
|
|
|
|
return Recipient.objects.get(id=rid)
|
|
|
|
|
2012-10-03 21:07:40 +02:00
|
|
|
# Create some test messages, including:
|
2012-10-10 23:17:43 +02:00
|
|
|
# - multiple streams
|
2012-10-11 00:01:39 +02:00
|
|
|
# - multiple subjects per stream
|
2012-09-14 18:31:11 +02:00
|
|
|
# - multiple huddles
|
|
|
|
# - multiple personals converastions
|
2012-10-11 00:01:39 +02:00
|
|
|
# - multiple messages per subject
|
2012-09-14 18:31:11 +02:00
|
|
|
# - both single and multi-line content
|
2017-10-27 12:57:54 +02:00
|
|
|
def send_messages(data: Tuple[int, Sequence[Sequence[int]], Mapping[str, Any],
|
|
|
|
Callable[[str], Any], int]) -> int:
|
2017-08-05 20:10:11 +02:00
|
|
|
(tot_messages, personals_pairs, options, output, random_seed) = data
|
|
|
|
random.seed(random_seed)
|
2017-05-23 23:57:42 +02:00
|
|
|
|
|
|
|
with open("var/test_messages.json", "r") as infile:
|
|
|
|
dialog = ujson.load(infile)
|
2017-08-05 20:10:11 +02:00
|
|
|
random.shuffle(dialog)
|
2017-05-23 23:57:42 +02:00
|
|
|
texts = itertools.cycle(dialog)
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-10 23:17:43 +02:00
|
|
|
recipient_streams = [klass.id for klass in
|
2017-07-09 02:11:44 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.STREAM)] # type: List[int]
|
|
|
|
recipient_huddles = [h.id for h in Recipient.objects.filter(type=Recipient.HUDDLE)] # type: List[int]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2017-07-09 02:11:44 +02:00
|
|
|
huddle_members = {} # type: Dict[int, List[int]]
|
2012-09-14 18:31:11 +02:00
|
|
|
for h in recipient_huddles:
|
2012-10-22 20:15:25 +02:00
|
|
|
huddle_members[h] = [s.user_profile.id for s in
|
2012-09-14 22:22:23 +02:00
|
|
|
Subscription.objects.filter(recipient_id=h)]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-10-03 21:05:48 +02:00
|
|
|
num_messages = 0
|
2012-09-14 18:31:11 +02:00
|
|
|
random_max = 1000000
|
2017-07-09 02:11:44 +02:00
|
|
|
recipients = {} # type: Dict[int, Tuple[int, int, Dict[str, Any]]]
|
2012-10-03 21:05:48 +02:00
|
|
|
while num_messages < tot_messages:
|
2017-07-09 02:11:44 +02:00
|
|
|
saved_data = {} # type: Dict[str, Any]
|
2012-10-03 21:05:48 +02:00
|
|
|
message = Message()
|
2012-10-19 21:30:42 +02:00
|
|
|
message.sending_client = get_client('populate_db')
|
2017-05-23 23:57:42 +02:00
|
|
|
|
|
|
|
message.content = next(texts)
|
2012-09-14 18:31:11 +02:00
|
|
|
|
|
|
|
randkey = random.randint(1, random_max)
|
2012-10-03 21:05:48 +02:00
|
|
|
if (num_messages > 0 and
|
2016-12-03 18:19:09 +01:00
|
|
|
random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
|
2012-09-14 18:31:11 +02:00
|
|
|
# Use an old recipient
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type, recipient_id, saved_data = recipients[num_messages - 1]
|
|
|
|
if message_type == Recipient.PERSONAL:
|
2016-01-27 22:14:21 +01:00
|
|
|
personals_pair = saved_data['personals_pair']
|
2012-09-14 18:31:11 +02:00
|
|
|
random.shuffle(personals_pair)
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2016-01-27 22:14:21 +01:00
|
|
|
message.subject = saved_data['subject']
|
2012-10-03 21:05:48 +02:00
|
|
|
message.recipient = get_recipient_by_id(recipient_id)
|
|
|
|
elif message_type == Recipient.HUDDLE:
|
|
|
|
message.recipient = get_recipient_by_id(recipient_id)
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * options["percent_huddles"] / 100.):
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type = Recipient.HUDDLE
|
|
|
|
message.recipient = get_recipient_by_id(random.choice(recipient_huddles))
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
|
2012-10-03 21:05:48 +02:00
|
|
|
message_type = Recipient.PERSONAL
|
2012-09-14 18:31:11 +02:00
|
|
|
personals_pair = random.choice(personals_pairs)
|
|
|
|
random.shuffle(personals_pair)
|
|
|
|
elif (randkey <= random_max * 1.0):
|
2012-10-10 22:57:21 +02:00
|
|
|
message_type = Recipient.STREAM
|
2012-10-10 23:17:43 +02:00
|
|
|
message.recipient = get_recipient_by_id(random.choice(recipient_streams))
|
2012-10-03 21:05:48 +02:00
|
|
|
|
|
|
|
if message_type == Recipient.HUDDLE:
|
|
|
|
sender_id = random.choice(huddle_members[message.recipient.id])
|
|
|
|
message.sender = get_user_profile_by_id(sender_id)
|
|
|
|
elif message_type == Recipient.PERSONAL:
|
|
|
|
message.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
2016-11-30 14:17:35 +01:00
|
|
|
type_id=personals_pair[0])
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = get_user_profile_by_id(personals_pair[1])
|
2016-01-27 22:14:21 +01:00
|
|
|
saved_data['personals_pair'] = personals_pair
|
2012-10-10 22:57:21 +02:00
|
|
|
elif message_type == Recipient.STREAM:
|
2012-10-10 22:53:24 +02:00
|
|
|
stream = Stream.objects.get(id=message.recipient.type_id)
|
2012-10-10 23:17:43 +02:00
|
|
|
# Pick a random subscriber to the stream
|
2012-10-03 21:05:48 +02:00
|
|
|
message.sender = random.choice(Subscription.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
recipient=message.recipient)).user_profile
|
2016-12-08 05:06:51 +01:00
|
|
|
message.subject = stream.name + Text(random.randint(1, 3))
|
2016-01-27 22:14:21 +01:00
|
|
|
saved_data['subject'] = message.subject
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2018-02-13 13:56:53 +01:00
|
|
|
# Spoofing time not supported with threading
|
|
|
|
if options['threads'] != 1:
|
|
|
|
message.pub_date = timezone_now()
|
|
|
|
else:
|
|
|
|
# Distrubutes 80% of messages starting from 5 days ago, over a period
|
|
|
|
# of 3 days. Then, distributes remaining messages over past 24 hours.
|
|
|
|
spoofed_date = timezone_now() - timezone_timedelta(days = 5)
|
|
|
|
if (num_messages < tot_messages * 0.8):
|
|
|
|
# Maximum of 3 days ahead, convert to minutes
|
|
|
|
time_ahead = 3 * 24 * 60
|
|
|
|
time_ahead //= int(tot_messages * 0.8)
|
|
|
|
else:
|
|
|
|
time_ahead = 24 * 60
|
|
|
|
time_ahead //= int(tot_messages * 0.2)
|
|
|
|
|
|
|
|
spoofed_minute = random.randint(time_ahead * num_messages, time_ahead * (num_messages + 1))
|
|
|
|
spoofed_date += timezone_timedelta(minutes = spoofed_minute)
|
|
|
|
message.pub_date = spoofed_date
|
|
|
|
|
2017-11-21 18:31:05 +01:00
|
|
|
# We disable USING_RABBITMQ here, so that deferred work is
|
|
|
|
# executed in do_send_message_messages, rather than being
|
|
|
|
# queued. This is important, because otherwise, if run-dev.py
|
|
|
|
# wasn't running when populate_db was run, a developer can end
|
|
|
|
# up with queued events that reference objects from a previous
|
|
|
|
# life of the database, which naturally throws exceptions.
|
|
|
|
settings.USING_RABBITMQ = False
|
2017-01-22 07:07:29 +01:00
|
|
|
do_send_messages([{'message': message}])
|
2017-11-21 18:31:05 +01:00
|
|
|
settings.USING_RABBITMQ = True
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2016-01-26 02:19:31 +01:00
|
|
|
recipients[num_messages] = (message_type, message.recipient.id, saved_data)
|
2012-10-03 21:05:48 +02:00
|
|
|
num_messages += 1
|
|
|
|
return tot_messages
|
2017-01-27 00:06:55 +01:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def create_user_presences(user_profiles: Iterable[UserProfile]) -> None:
|
2017-01-27 00:06:55 +01:00
|
|
|
for user in user_profiles:
|
2017-07-09 02:11:44 +02:00
|
|
|
status = 1 # type: int
|
2017-04-15 04:03:56 +02:00
|
|
|
date = timezone_now()
|
2017-01-27 00:06:55 +01:00
|
|
|
client = get_client("website")
|
|
|
|
UserPresence.objects.get_or_create(
|
|
|
|
user_profile=user,
|
|
|
|
client=client,
|
|
|
|
timestamp=date,
|
|
|
|
status=status)
|
2017-11-13 07:49:01 +01:00
|
|
|
|
2017-11-27 12:08:59 +01:00
|
|
|
def create_user_groups() -> None:
|
2017-11-13 07:49:01 +01:00
|
|
|
zulip = get_realm('zulip')
|
|
|
|
members = [get_user('cordelia@zulip.com', zulip),
|
|
|
|
get_user('hamlet@zulip.com', zulip)]
|
|
|
|
create_user_group("hamletcharacters", members, zulip,
|
|
|
|
description="Characters of Hamlet")
|