2017-03-08 12:15:16 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-07-01 03:56:40 +02:00
|
|
|
import datetime
|
2017-03-08 12:15:16 +01:00
|
|
|
import ujson
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from mock import patch
|
2017-04-09 00:35:41 +02:00
|
|
|
from typing import Any, Dict, List, Text, Union
|
2017-03-08 12:15:16 +01:00
|
|
|
|
2017-03-18 20:19:44 +01:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
do_change_is_admin,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property,
|
2017-03-18 20:19:44 +01:00
|
|
|
do_deactivate_realm,
|
|
|
|
)
|
2017-03-08 12:15:16 +01:00
|
|
|
|
2017-07-01 03:56:40 +02:00
|
|
|
from zerver.lib.send_email import send_future_email
|
2017-03-08 12:15:16 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.test_helpers import tornado_redirected_to_list
|
2017-07-02 21:10:41 +02:00
|
|
|
from zerver.models import get_realm, Realm, UserProfile, ScheduledEmail
|
2017-03-08 12:15:16 +01:00
|
|
|
|
|
|
|
class RealmTest(ZulipTestCase):
|
2017-05-24 02:42:31 +02:00
|
|
|
def assert_user_profile_cache_gets_new_name(self, user_profile, new_realm_name):
|
|
|
|
# type: (UserProfile, Text) -> None
|
2017-03-08 12:15:16 +01:00
|
|
|
self.assertEqual(user_profile.realm.name, new_realm_name)
|
|
|
|
|
|
|
|
def test_do_set_realm_name_caching(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""The main complicated thing about setting realm names is fighting the
|
|
|
|
cache, and we start by populating the cache for Hamlet, and we end
|
|
|
|
by checking the cache to ensure that the new value is there."""
|
2017-05-07 17:21:26 +02:00
|
|
|
self.example_user('hamlet')
|
2017-03-08 12:15:16 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
new_name = u'Zed You Elle Eye Pea'
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'name', new_name)
|
2017-03-08 12:15:16 +01:00
|
|
|
self.assertEqual(get_realm(realm.string_id).name, new_name)
|
2017-05-24 02:42:31 +02:00
|
|
|
self.assert_user_profile_cache_gets_new_name(self.example_user('hamlet'), new_name)
|
2017-03-08 12:15:16 +01:00
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
def test_update_realm_name_events(self):
|
2017-03-08 12:15:16 +01:00
|
|
|
# type: () -> None
|
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
new_name = u'Puliz'
|
2017-03-21 18:08:40 +01:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2017-03-08 12:15:16 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(realm, 'name', new_name)
|
2017-03-08 12:15:16 +01:00
|
|
|
event = events[0]['event']
|
|
|
|
self.assertEqual(event, dict(
|
2017-03-21 18:08:40 +01:00
|
|
|
type='realm',
|
|
|
|
op='update',
|
|
|
|
property='name',
|
|
|
|
value=new_name,
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_update_realm_description_events(self):
|
|
|
|
# type: () -> None
|
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
new_description = u'zulip dev group'
|
2017-03-21 18:08:40 +01:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
do_set_realm_property(realm, 'description', new_description)
|
|
|
|
event = events[0]['event']
|
|
|
|
self.assertEqual(event, dict(
|
|
|
|
type='realm',
|
|
|
|
op='update',
|
|
|
|
property='description',
|
|
|
|
value=new_description,
|
2017-03-08 12:15:16 +01:00
|
|
|
))
|
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
def test_update_realm_description(self):
|
2017-03-18 20:19:44 +01:00
|
|
|
# type: () -> None
|
2017-05-25 01:44:04 +02:00
|
|
|
email = self.example_email("iago")
|
2017-03-20 06:49:13 +01:00
|
|
|
self.login(email)
|
2017-03-18 20:19:44 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
new_description = u'zulip dev group'
|
2017-03-20 06:49:13 +01:00
|
|
|
data = dict(description=ujson.dumps(new_description))
|
2017-03-21 18:08:40 +01:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2017-03-18 20:19:44 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
2017-03-20 06:49:13 +01:00
|
|
|
result = self.client_patch('/json/realm', data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.description, new_description)
|
|
|
|
|
2017-03-18 20:19:44 +01:00
|
|
|
event = events[0]['event']
|
|
|
|
self.assertEqual(event, dict(
|
|
|
|
type='realm',
|
|
|
|
op='update',
|
|
|
|
property='description',
|
|
|
|
value=new_description,
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_realm_description_length(self):
|
|
|
|
# type: () -> None
|
2017-05-11 22:52:14 +02:00
|
|
|
new_description = u'A' * 1001
|
2017-03-18 20:19:44 +01:00
|
|
|
data = dict(description=ujson.dumps(new_description))
|
|
|
|
|
|
|
|
# create an admin user
|
2017-05-25 01:44:04 +02:00
|
|
|
email = self.example_email("iago")
|
2017-03-18 20:19:44 +01:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
result = self.client_patch('/json/realm', data)
|
2017-05-11 22:52:14 +02:00
|
|
|
self.assert_json_error(result, 'Realm description is too long.')
|
2017-03-18 20:19:44 +01:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.description, new_description)
|
|
|
|
|
2017-03-08 12:15:16 +01:00
|
|
|
def test_admin_restrictions_for_changing_realm_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_name = 'Mice will play while the cat is away'
|
|
|
|
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('othello')
|
|
|
|
email = user_profile.email
|
2017-03-08 12:15:16 +01:00
|
|
|
self.login(email)
|
|
|
|
do_change_is_admin(user_profile, False)
|
|
|
|
|
|
|
|
req = dict(name=ujson.dumps(new_name))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
|
|
|
|
2017-03-13 18:33:49 +01:00
|
|
|
def test_unauthorized_name_change(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'full_name': 'Sir Hamlet'}
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
2017-03-13 18:33:49 +01:00
|
|
|
self.login(email)
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(user_profile.realm, 'name_changes_disabled', True)
|
2017-03-13 18:33:49 +01:00
|
|
|
url = '/json/settings/change'
|
|
|
|
result = self.client_post(url, data)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
# Since the setting fails silently, no message is returned
|
|
|
|
self.assert_in_response("", result)
|
|
|
|
|
2017-05-24 01:26:30 +02:00
|
|
|
def test_do_deactivate_realm_clears_user_realm_cache(self):
|
2017-03-08 12:15:16 +01:00
|
|
|
# type: () -> None
|
|
|
|
"""The main complicated thing about deactivating realm names is
|
|
|
|
updating the cache, and we start by populating the cache for
|
|
|
|
Hamlet, and we end by checking the cache to ensure that his
|
|
|
|
realm appears to be deactivated. You can make this test fail
|
|
|
|
by disabling cache.flush_realm()."""
|
2017-05-07 17:21:26 +02:00
|
|
|
self.example_user('hamlet')
|
2017-03-08 12:15:16 +01:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
do_deactivate_realm(realm)
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-03-08 12:15:16 +01:00
|
|
|
self.assertTrue(user.realm.deactivated)
|
|
|
|
|
2017-07-01 03:56:40 +02:00
|
|
|
def test_do_deactivate_realm_clears_scheduled_jobs(self):
|
|
|
|
# type: () -> None
|
|
|
|
user = self.example_user('hamlet')
|
2017-07-02 21:10:41 +02:00
|
|
|
send_future_email('zerver/emails/followup_day1', to_user_id=user.id, delay=datetime.timedelta(hours=1))
|
|
|
|
self.assertEqual(ScheduledEmail.objects.count(), 1)
|
2017-07-01 03:56:40 +02:00
|
|
|
do_deactivate_realm(user.realm)
|
2017-07-02 21:10:41 +02:00
|
|
|
self.assertEqual(ScheduledEmail.objects.count(), 0)
|
2017-07-01 03:56:40 +02:00
|
|
|
|
2017-05-24 01:26:30 +02:00
|
|
|
def test_do_deactivate_realm_on_deactived_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""Ensure early exit is working in realm deactivation"""
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertFalse(realm.deactivated)
|
|
|
|
|
|
|
|
do_deactivate_realm(realm)
|
|
|
|
self.assertTrue(realm.deactivated)
|
|
|
|
|
|
|
|
do_deactivate_realm(realm)
|
|
|
|
self.assertTrue(realm.deactivated)
|
|
|
|
|
2017-06-09 20:50:38 +02:00
|
|
|
def test_change_notifications_stream(self):
|
|
|
|
# type: () -> None
|
|
|
|
# We need an admin user.
|
|
|
|
email = 'iago@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
disabled_notif_stream_id = -1
|
|
|
|
req = dict(notifications_stream_id = ujson.dumps(disabled_notif_stream_id))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.notifications_stream, None)
|
|
|
|
|
|
|
|
new_notif_stream_id = 4
|
|
|
|
req = dict(notifications_stream_id = ujson.dumps(new_notif_stream_id))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.notifications_stream.id, new_notif_stream_id)
|
|
|
|
|
|
|
|
invalid_notif_stream_id = 1234
|
|
|
|
req = dict(notifications_stream_id = ujson.dumps(invalid_notif_stream_id))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, 'Invalid stream id')
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.notifications_stream.id, invalid_notif_stream_id)
|
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
def test_change_realm_default_language(self):
|
2017-03-08 12:15:16 +01:00
|
|
|
# type: () -> None
|
|
|
|
new_lang = "de"
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.default_language, new_lang)
|
|
|
|
# we need an admin user.
|
2017-05-25 01:44:04 +02:00
|
|
|
email = self.example_email("iago")
|
2017-03-08 12:15:16 +01:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
req = dict(default_language=ujson.dumps(new_lang))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.default_language, new_lang)
|
|
|
|
|
|
|
|
# Test to make sure that when invalid languages are passed
|
|
|
|
# as the default realm language, correct validation error is
|
|
|
|
# raised and the invalid language is not saved in db
|
|
|
|
invalid_lang = "invalid_lang"
|
|
|
|
req = dict(default_language=ujson.dumps(invalid_lang))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, "Invalid language '%s'" % (invalid_lang,))
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.default_language, invalid_lang)
|
2017-04-09 00:35:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RealmAPITest(ZulipTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# type: () -> None
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
|
|
|
email = user_profile.email
|
2017-04-09 00:35:41 +02:00
|
|
|
self.login(email)
|
|
|
|
do_change_is_admin(user_profile, True)
|
|
|
|
|
|
|
|
def set_up_db(self, attr, value):
|
|
|
|
# type: (str, Any) -> None
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
setattr(realm, attr, value)
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
def update_with_api(self, name, value):
|
|
|
|
# type: (str, Union[Text, int, bool]) -> Realm
|
|
|
|
result = self.client_patch('/json/realm', {name: ujson.dumps(value)})
|
|
|
|
self.assert_json_success(result)
|
2017-07-11 21:39:43 +02:00
|
|
|
return get_realm('zulip') # refresh data
|
2017-04-09 00:35:41 +02:00
|
|
|
|
|
|
|
def do_test_realm_update_api(self, name):
|
|
|
|
# type: (str) -> None
|
|
|
|
"""Test updating realm properties.
|
|
|
|
|
|
|
|
If new realm properties have been added to the Realm model but the
|
|
|
|
test_values dict below has not been updated, this will raise an
|
|
|
|
assertion error.
|
|
|
|
"""
|
|
|
|
|
2017-07-11 21:39:43 +02:00
|
|
|
bool_tests = [False, True] # type: List[bool]
|
2017-04-09 00:35:41 +02:00
|
|
|
test_values = dict(
|
|
|
|
default_language=[u'de', u'en'],
|
|
|
|
description=[u'Realm description', u'New description'],
|
|
|
|
message_retention_days=[10, 20],
|
|
|
|
name=[u'Zulip', u'New Name'],
|
|
|
|
waiting_period_threshold=[10, 20],
|
2017-07-11 21:39:43 +02:00
|
|
|
) # type: Dict[str, Any]
|
2017-04-09 00:35:41 +02:00
|
|
|
vals = test_values.get(name)
|
2017-07-04 23:18:29 +02:00
|
|
|
if Realm.property_types[name] is bool:
|
|
|
|
vals = bool_tests
|
2017-04-09 00:35:41 +02:00
|
|
|
if vals is None:
|
|
|
|
raise AssertionError('No test created for %s' % (name))
|
|
|
|
|
|
|
|
self.set_up_db(name, vals[0])
|
|
|
|
realm = self.update_with_api(name, vals[1])
|
|
|
|
self.assertEqual(getattr(realm, name), vals[1])
|
|
|
|
realm = self.update_with_api(name, vals[0])
|
|
|
|
self.assertEqual(getattr(realm, name), vals[0])
|
|
|
|
|
|
|
|
def test_update_realm_properties(self):
|
|
|
|
# type: () -> None
|
|
|
|
for prop in Realm.property_types:
|
|
|
|
self.do_test_realm_update_api(prop)
|
|
|
|
|
|
|
|
def test_update_realm_allow_message_editing(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""Tests updating the realm property 'allow_message_editing'."""
|
|
|
|
self.set_up_db('allow_message_editing', False)
|
|
|
|
self.set_up_db('message_content_edit_limit_seconds', 0)
|
|
|
|
realm = self.update_with_api('allow_message_editing', True)
|
|
|
|
realm = self.update_with_api('message_content_edit_limit_seconds', 100)
|
|
|
|
self.assertEqual(realm.allow_message_editing, True)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 100)
|
|
|
|
realm = self.update_with_api('allow_message_editing', False)
|
|
|
|
self.assertEqual(realm.allow_message_editing, False)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 100)
|
|
|
|
realm = self.update_with_api('message_content_edit_limit_seconds', 200)
|
|
|
|
self.assertEqual(realm.allow_message_editing, False)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 200)
|