streams: Add default stream description tests and functions.

This includes making the default stream description setting into a
dict.  That is an API change; we'll discuss it in the changelog but it
seems small enough to be OK.

With some small tweaks by tabbott to remove unnecessary backwards
compatibility code for the settings.

Fixes #2427.
This commit is contained in:
Joy Chen
2016-12-07 18:43:15 -06:00
committed by Tim Abbott
parent dd95a5e03f
commit bfb6ac5fdb
6 changed files with 62 additions and 22 deletions

View File

@@ -2313,11 +2313,16 @@ def do_change_default_language(user_profile, setting_value, log=True):
log_event(event)
send_event(event, [user_profile.id])
def set_default_streams(realm, stream_names):
# type: (Realm, Iterable[text_type]) -> None
def set_default_streams(realm, stream_dict):
# type: (Realm, Dict[text_type, Dict[text_type, Any]]) -> None
DefaultStream.objects.filter(realm=realm).delete()
for stream_name in stream_names:
stream, _ = create_stream_if_needed(realm, stream_name)
stream_names = []
for name, options in stream_dict.items():
stream_names.append(name)
stream, _ = create_stream_if_needed(realm,
name,
invite_only = options["invite_only"],
stream_description = options["description"])
DefaultStream.objects.create(stream=stream, realm=realm)
# Always include the realm's default notifications streams, if it exists
@@ -2328,7 +2333,6 @@ def set_default_streams(realm, stream_names):
'domain': realm.domain,
'streams': stream_names})
def notify_default_streams(realm):
# type: (Realm) -> None
event = dict(

View File

@@ -2,7 +2,7 @@ from __future__ import absolute_import
from __future__ import print_function
from optparse import make_option
from typing import Any
from typing import Any, Text
from django.conf import settings
from django.core.management.base import BaseCommand, CommandParser
@@ -113,8 +113,11 @@ Usage: ./manage.py create_realm --string_id=acme --name='Acme'"""
deployment.realms.add(realm)
deployment.save()
# In the else case, we are not using the Deployments feature.
set_default_streams(realm, ["social", "engineering"])
stream_dict = {
"social": {"description": "For socializing", "invite_only": False},
"engineering": {"description": "For engineering", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]]
set_default_streams(realm, stream_dict)
print("\033[1;36mDefault streams set to social,engineering,zulip!\033[0m")
else:

View File

@@ -1,7 +1,7 @@
from __future__ import absolute_import
from __future__ import print_function
from typing import Any
from typing import Any, Text
from django.core.management.base import BaseCommand, CommandParser
@@ -46,6 +46,9 @@ For example:
set of streams (which can be empty, with `--streams=`).", file=sys.stderr)
exit(1)
stream_names = [stream.strip() for stream in options["streams"].split(",")]
stream_dict = {
stream.strip(): {"description": stream.strip(), "invite_only": False}
for stream in options["streams"].split(",")
} # type: Dict[Text, Dict[Text, Any]]
realm = get_realm(options["domain"])
set_default_streams(realm, stream_names)
set_default_streams(realm, stream_dict)

View File

@@ -139,7 +139,11 @@ class AddNewUserHistoryTest(ZulipTestCase):
# type: () -> None
"""Sends a message during user creation"""
# Create a user who hasn't had historical messages added
set_default_streams(get_realm_by_string_id("zulip"), ["Denmark", "Verona"])
stream_dict = {
"Denmark": {"description": "A Scandinavian country", "invite_only": False},
"Verona": {"description": "A city in Italy", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]]
set_default_streams(get_realm_by_string_id("zulip"), stream_dict)
with patch("zerver.lib.actions.add_new_user_history"):
self.register("test", "test")
user_profile = get_user_profile_by_email("test@zulip.com")
@@ -236,11 +240,12 @@ class LoginTest(ZulipTestCase):
def test_register(self):
# type: () -> None
realm = get_realm_by_string_id("zulip")
stream_names = ["stream_%s" % i for i in range(40)]
for stream_name in stream_names:
stream_dict = {"stream_"+str(i): {"description": "stream_%s_description" % i, "invite_only": False}
for i in range(40)} # type: Dict[Text, Dict[Text, Any]]
for stream_name in stream_dict.keys():
self.make_stream(stream_name, realm=realm)
set_default_streams(realm, stream_names)
set_default_streams(realm, stream_dict)
with queries_captured() as queries:
self.register("test", "test")
# Ensure the number of queries we make is not O(streams)

View File

@@ -617,25 +617,46 @@ class DefaultStreamTest(ZulipTestCase):
stream_names = [s.name for s in streams]
return set(stream_names)
def get_default_stream_descriptions(self, realm):
# type: (Realm) -> Set[Text]
streams = get_default_streams_for_realm(realm)
stream_descriptions = [s.description for s in streams]
return set(stream_descriptions)
def test_set_default_streams(self):
# type: () -> None
(realm, _) = do_create_realm("testrealm", "Test Realm")
stream_names = ['apple', 'banana', 'Carrot Cake']
expected_names = stream_names + ['announce']
set_default_streams(realm, stream_names)
stream_dict = {
"apple": {"description": "A red fruit", "invite_only": False},
"banana": {"description": "A yellow fruit", "invite_only": False},
"Carrot Cake": {"description": "A delicious treat", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]]
expected_names = list(stream_dict.keys())
expected_names.append("announce")
expected_descriptions = [i["description"] for i in stream_dict.values()] + [""]
set_default_streams(realm, stream_dict)
stream_names_set = self.get_default_stream_names(realm)
stream_descriptions_set = self.get_default_stream_descriptions(realm)
self.assertEqual(stream_names_set, set(expected_names))
self.assertEqual(stream_descriptions_set, set(expected_descriptions))
def test_set_default_streams_no_notifications_stream(self):
# type: () -> None
(realm, _) = do_create_realm("testrealm", "Test Realm")
realm.notifications_stream = None
realm.save(update_fields=["notifications_stream"])
stream_names = ['apple', 'banana', 'Carrot Cake']
expected_names = stream_names
set_default_streams(realm, stream_names)
stream_dict = {
"apple": {"description": "A red fruit", "invite_only": False},
"banana": {"description": "A yellow fruit", "invite_only": False},
"Carrot Cake": {"description": "A delicious treat", "invite_only": False}
} # type: Dict[Text, Dict[Text, Any]]
expected_names = list(stream_dict.keys())
expected_descriptions = [i["description"] for i in stream_dict.values()]
set_default_streams(realm, stream_dict)
stream_names_set = self.get_default_stream_names(realm)
stream_descriptions_set = self.get_default_stream_descriptions(realm)
self.assertEqual(stream_names_set, set(expected_names))
self.assertEqual(stream_descriptions_set, set(expected_descriptions))
def test_add_and_remove_default_stream(self):
# type: () -> None

View File

@@ -173,7 +173,11 @@ DEFAULT_SETTINGS = {'TWITTER_CONSUMER_KEY': '',
'DBX_APNS_KEY_FILE': None,
'PERSONAL_ZMIRROR_SERVER': None,
'EXTRA_INSTALLED_APPS': [],
'DEFAULT_NEW_REALM_STREAMS': ["social", "general", "zulip"],
'DEFAULT_NEW_REALM_STREAMS': {
"social": {"description": "For socializing", "invite_only": False},
"general": {"description": "For general stuff", "invite_only": False},
"zulip": {"description": "For zulip stuff", "invite_only": False}
},
'REALM_CREATION_LINK_VALIDITY_DAYS': 7,
'TERMS_OF_SERVICE': None,
'TOS_VERSION': None,