2017-03-08 12:15:16 +01:00
|
|
|
|
2017-07-01 03:56:40 +02:00
|
|
|
import datetime
|
2017-03-08 12:15:16 +01:00
|
|
|
import ujson
|
2018-09-14 13:14:40 +02:00
|
|
|
import re
|
2018-10-15 09:59:29 +02:00
|
|
|
import mock
|
2018-11-12 14:15:49 +01:00
|
|
|
from email.utils import parseaddr
|
2017-03-08 12:15:16 +01:00
|
|
|
|
2018-10-23 19:25:59 +02:00
|
|
|
from django.conf import settings
|
2017-03-08 12:15:16 +01:00
|
|
|
from django.http import HttpResponse
|
2018-10-17 10:50:59 +02:00
|
|
|
from django.conf import settings
|
2017-03-08 12:15:16 +01:00
|
|
|
from mock import patch
|
2018-05-11 01:39:38 +02:00
|
|
|
from typing import Any, Dict, List, Union, Mapping
|
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,
|
2018-11-15 23:29:04 +01:00
|
|
|
do_change_realm_subdomain,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property,
|
2017-03-18 20:19:44 +01:00
|
|
|
do_deactivate_realm,
|
2017-08-24 00:36:29 +02:00
|
|
|
do_deactivate_stream,
|
2018-08-09 21:38:22 +02:00
|
|
|
do_create_realm,
|
2018-09-14 13:14:40 +02:00
|
|
|
do_scrub_realm,
|
|
|
|
create_stream_if_needed,
|
2018-10-23 19:25:59 +02:00
|
|
|
do_change_plan_type,
|
2018-11-12 14:15:49 +01:00
|
|
|
do_send_realm_reactivation_email
|
2017-03-18 20:19:44 +01:00
|
|
|
)
|
2017-03-08 12:15:16 +01:00
|
|
|
|
2018-11-12 14:15:49 +01:00
|
|
|
from confirmation.models import create_confirmation_link, Confirmation
|
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-10-07 00:29:18 +02:00
|
|
|
from zerver.lib.test_runner import slow
|
2018-09-14 13:14:40 +02:00
|
|
|
from zerver.models import get_realm, Realm, UserProfile, ScheduledEmail, get_stream, \
|
2018-11-15 23:29:04 +01:00
|
|
|
CustomProfileField, Message, UserMessage, Attachment, get_user_profile_by_email
|
2017-03-08 12:15:16 +01:00
|
|
|
|
|
|
|
class RealmTest(ZulipTestCase):
|
2017-11-17 07:00:53 +01:00
|
|
|
def assert_user_profile_cache_gets_new_name(self, user_profile: UserProfile,
|
2018-05-11 01:39:38 +02:00
|
|
|
new_realm_name: str) -> None:
|
2017-03-08 12:15:16 +01:00
|
|
|
self.assertEqual(user_profile.realm.name, new_realm_name)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_set_realm_name_caching(self) -> None:
|
2017-03-08 12:15:16 +01:00
|
|
|
"""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-11-05 10:51:25 +01:00
|
|
|
def test_update_realm_name_events(self) -> None:
|
2017-03-08 12:15:16 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
new_name = u'Puliz'
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[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,
|
|
|
|
))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_realm_description_events(self) -> None:
|
2017-03-21 18:08:40 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
new_description = u'zulip dev group'
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[str, Any]]
|
2017-03-21 18:08:40 +01:00
|
|
|
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-11-05 10:51:25 +01:00
|
|
|
def test_update_realm_description(self) -> 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-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[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,
|
|
|
|
))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_realm_description_length(self) -> 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)
|
2018-03-17 00:47:55 +01:00
|
|
|
self.assert_json_error(result, 'Organization description is too long.')
|
2017-03-18 20:19:44 +01:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.description, new_description)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_realm_name_length(self) -> None:
|
2017-08-23 07:03:19 +02:00
|
|
|
new_name = u'A' * (Realm.MAX_REALM_NAME_LENGTH + 1)
|
|
|
|
data = dict(name=ujson.dumps(new_name))
|
|
|
|
|
|
|
|
# create an admin user
|
|
|
|
email = self.example_email("iago")
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
result = self.client_patch('/json/realm', data)
|
2018-03-17 00:47:55 +01:00
|
|
|
self.assert_json_error(result, 'Organization name is too long.')
|
2017-08-23 07:03:19 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.name, new_name)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_admin_restrictions_for_changing_realm_name(self) -> None:
|
2017-03-08 12:15:16 +01:00
|
|
|
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)
|
2018-03-08 01:47:17 +01:00
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
2017-03-08 12:15:16 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unauthorized_name_change(self) -> None:
|
2017-03-13 18:33:49 +01:00
|
|
|
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-07-31 20:44:52 +02:00
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
2017-03-13 18:33:49 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
# Since the setting fails silently, no message is returned
|
|
|
|
self.assert_in_response("", result)
|
2018-02-02 16:54:26 +01:00
|
|
|
# Realm admins can change their name even setting is disabled.
|
|
|
|
data = {'full_name': 'New Iago'}
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assert_in_success_response(['"full_name":"New Iago"'], result)
|
2017-03-13 18:33:49 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_deactivate_realm_clears_user_realm_cache(self) -> None:
|
2017-03-08 12:15:16 +01:00
|
|
|
"""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)
|
|
|
|
|
2018-11-15 23:29:04 +01:00
|
|
|
def test_do_change_realm_subdomain_clears_user_realm_cache(self) -> None:
|
|
|
|
"""The main complicated thing about changing realm subdomains 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()."""
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
do_change_realm_subdomain(realm, "newzulip")
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertEqual(user.realm.string_id, "newzulip")
|
|
|
|
# This doesn't use a cache right now, but may later.
|
|
|
|
self.assertIsNone(get_realm("zulip"))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_deactivate_realm_clears_scheduled_jobs(self) -> None:
|
2017-07-01 03:56:40 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-12-05 03:19:48 +01:00
|
|
|
send_future_email('zerver/emails/followup_day1', user.realm,
|
2018-12-03 23:26:51 +01:00
|
|
|
to_user_ids=[user.id], delay=datetime.timedelta(hours=1))
|
2017-07-02 21:10:41 +02:00
|
|
|
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-11-05 10:51:25 +01:00
|
|
|
def test_do_deactivate_realm_on_deactived_realm(self) -> None:
|
2017-05-24 01:26:30 +02:00
|
|
|
"""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)
|
|
|
|
|
2018-11-12 14:15:49 +01:00
|
|
|
def test_realm_reactivation_link(self) -> None:
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
do_deactivate_realm(realm)
|
|
|
|
self.assertTrue(realm.deactivated)
|
|
|
|
confirmation_url = create_confirmation_link(realm, realm.host, Confirmation.REALM_REACTIVATION)
|
|
|
|
response = self.client_get(confirmation_url)
|
|
|
|
self.assert_in_success_response(['Your organization has been successfully reactivated'], response)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertFalse(realm.deactivated)
|
|
|
|
|
|
|
|
def test_do_send_realm_reactivation_email(self) -> None:
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
do_send_realm_reactivation_email(realm)
|
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 1)
|
|
|
|
from_email = outbox[0].from_email
|
|
|
|
tokenized_no_reply_email = parseaddr(from_email)[1]
|
|
|
|
self.assertIn("Zulip Account Security", from_email)
|
|
|
|
self.assertTrue(re.search(self.TOKENIZED_NOREPLY_REGEX, tokenized_no_reply_email))
|
|
|
|
self.assertIn('Reactivate your Zulip organization', outbox[0].subject)
|
|
|
|
self.assertIn('To reactivate organization, please click here:', outbox[0].body)
|
|
|
|
admins = realm.get_admin_users()
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(admins[0].email)
|
|
|
|
response = self.client_get(confirmation_url)
|
|
|
|
self.assert_in_success_response(['Your organization has been successfully reactivated'], response)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertFalse(realm.deactivated)
|
|
|
|
|
|
|
|
def test_realm_reactivation_with_random_link(self) -> None:
|
|
|
|
random_link = "/reactivate/5e89081eb13984e0f3b130bf7a4121d153f1614b"
|
|
|
|
response = self.client_get(random_link)
|
|
|
|
self.assert_in_success_response(['The organization reactivation link has expired or is not valid.'], response)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_notifications_stream(self) -> None:
|
2017-06-09 20:50:38 +02:00
|
|
|
# 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-11-05 10:51:25 +01:00
|
|
|
def test_get_default_notifications_stream(self) -> None:
|
2017-08-24 00:36:29 +02:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
verona = get_stream("verona", realm)
|
2017-09-17 19:53:38 +02:00
|
|
|
realm.notifications_stream_id = verona.id
|
2018-11-15 23:21:22 +01:00
|
|
|
realm.save(update_fields=["notifications_stream"])
|
2017-08-24 00:36:29 +02:00
|
|
|
|
|
|
|
notifications_stream = realm.get_notifications_stream()
|
2017-09-17 19:53:38 +02:00
|
|
|
self.assertEqual(notifications_stream.id, verona.id)
|
2017-08-24 00:36:29 +02:00
|
|
|
do_deactivate_stream(notifications_stream)
|
|
|
|
self.assertIsNone(realm.get_notifications_stream())
|
|
|
|
|
2017-10-20 16:55:04 +02:00
|
|
|
def test_change_signup_notifications_stream(self) -> None:
|
|
|
|
# We need an admin user.
|
|
|
|
email = 'iago@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
disabled_signup_notifications_stream_id = -1
|
|
|
|
req = dict(signup_notifications_stream_id = ujson.dumps(disabled_signup_notifications_stream_id))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.signup_notifications_stream, None)
|
|
|
|
|
|
|
|
new_signup_notifications_stream_id = 4
|
|
|
|
req = dict(signup_notifications_stream_id = ujson.dumps(new_signup_notifications_stream_id))
|
2017-12-20 17:58:49 +01:00
|
|
|
|
2017-10-20 16:55:04 +02:00
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.signup_notifications_stream.id, new_signup_notifications_stream_id)
|
|
|
|
|
|
|
|
invalid_signup_notifications_stream_id = 1234
|
|
|
|
req = dict(signup_notifications_stream_id = ujson.dumps(invalid_signup_notifications_stream_id))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, 'Invalid stream id')
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertNotEqual(realm.signup_notifications_stream.id, invalid_signup_notifications_stream_id)
|
|
|
|
|
2017-10-04 02:01:22 +02:00
|
|
|
def test_get_default_signup_notifications_stream(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
verona = get_stream("verona", realm)
|
|
|
|
realm.signup_notifications_stream = verona
|
2018-11-15 23:21:22 +01:00
|
|
|
realm.save(update_fields=["signup_notifications_stream"])
|
2017-10-04 02:01:22 +02:00
|
|
|
|
|
|
|
signup_notifications_stream = realm.get_signup_notifications_stream()
|
|
|
|
self.assertEqual(signup_notifications_stream, verona)
|
|
|
|
do_deactivate_stream(signup_notifications_stream)
|
|
|
|
self.assertIsNone(realm.get_signup_notifications_stream())
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_realm_default_language(self) -> None:
|
2017-03-08 12:15:16 +01:00
|
|
|
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
|
|
|
|
2018-01-30 14:58:50 +01:00
|
|
|
def test_deactivate_realm_by_admin(self) -> None:
|
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertFalse(realm.deactivated)
|
|
|
|
|
|
|
|
result = self.client_post('/json/realm/deactivate')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertTrue(realm.deactivated)
|
|
|
|
|
|
|
|
def test_deactivate_realm_by_non_admin(self) -> None:
|
|
|
|
email = self.example_email('hamlet')
|
|
|
|
self.login(email)
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertFalse(realm.deactivated)
|
|
|
|
|
|
|
|
result = self.client_post('/json/realm/deactivate')
|
2018-03-08 01:47:17 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|
2018-01-30 14:58:50 +01:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertFalse(realm.deactivated)
|
|
|
|
|
2018-01-29 16:10:54 +01:00
|
|
|
def test_change_bot_creation_policy(self) -> None:
|
|
|
|
# We need an admin user.
|
|
|
|
email = 'iago@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
req = dict(bot_creation_policy = ujson.dumps(Realm.BOT_CREATION_LIMIT_GENERIC_BOTS))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
invalid_add_bot_permission = 4
|
|
|
|
req = dict(bot_creation_policy = ujson.dumps(invalid_add_bot_permission))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, 'Invalid bot creation policy')
|
|
|
|
|
2018-12-07 00:48:06 +01:00
|
|
|
def test_change_email_address_visibility(self) -> None:
|
|
|
|
# We need an admin user.
|
|
|
|
email = 'iago@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
invalid_value = 4
|
|
|
|
req = dict(email_address_visibility = ujson.dumps(invalid_value))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, 'Invalid email address visibility policy')
|
|
|
|
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
self.assertEqual(realm.email_address_visibility, Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE)
|
|
|
|
req = dict(email_address_visibility = ujson.dumps(Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
self.assertEqual(realm.email_address_visibility, Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS)
|
|
|
|
|
2018-04-23 14:51:30 +02:00
|
|
|
def test_change_video_chat_provider(self) -> None:
|
|
|
|
self.assertEqual(get_realm('zulip').video_chat_provider, "Jitsi")
|
|
|
|
email = self.example_email("iago")
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
req = {"video_chat_provider": ujson.dumps("Google Hangouts")}
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, "Invalid domain: Domain can't be empty.")
|
|
|
|
|
|
|
|
req = {
|
|
|
|
"video_chat_provider": ujson.dumps("Google Hangouts"),
|
|
|
|
"google_hangouts_domain": ujson.dumps("invaliddomain"),
|
|
|
|
}
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_error(result, "Invalid domain: Domain must have at least one dot (.)")
|
|
|
|
|
|
|
|
req = {
|
|
|
|
"video_chat_provider": ujson.dumps("Google Hangouts"),
|
|
|
|
"google_hangouts_domain": ujson.dumps("zulip.com"),
|
|
|
|
}
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(get_realm('zulip').video_chat_provider, "Google Hangouts")
|
|
|
|
|
|
|
|
req = {"video_chat_provider": ujson.dumps("Jitsi")}
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(get_realm('zulip').video_chat_provider, "Jitsi")
|
2017-04-09 00:35:41 +02:00
|
|
|
|
2018-08-09 21:38:22 +02:00
|
|
|
def test_initial_plan_type(self) -> None:
|
|
|
|
with self.settings(BILLING_ENABLED=True):
|
2018-10-17 10:50:59 +02:00
|
|
|
self.assertEqual(do_create_realm('hosted', 'hosted').plan_type, Realm.LIMITED)
|
|
|
|
self.assertEqual(get_realm("hosted").max_invites, settings.INVITES_DEFAULT_REALM_DAILY_MAX)
|
|
|
|
self.assertEqual(get_realm("hosted").message_visibility_limit, Realm.MESSAGE_VISIBILITY_LIMITED)
|
|
|
|
|
2018-08-09 21:38:22 +02:00
|
|
|
with self.settings(BILLING_ENABLED=False):
|
2018-10-17 10:50:59 +02:00
|
|
|
self.assertEqual(do_create_realm('onpremise', 'onpremise').plan_type, Realm.SELF_HOSTED)
|
|
|
|
self.assertEqual(get_realm('onpremise').max_invites, settings.INVITES_DEFAULT_REALM_DAILY_MAX)
|
|
|
|
self.assertEqual(get_realm('onpremise').message_visibility_limit, None)
|
2018-08-09 21:38:22 +02:00
|
|
|
|
2018-10-23 19:25:59 +02:00
|
|
|
def test_change_plan_type(self) -> None:
|
2018-10-17 10:50:59 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.plan_type, Realm.SELF_HOSTED)
|
|
|
|
self.assertEqual(realm.max_invites, settings.INVITES_DEFAULT_REALM_DAILY_MAX)
|
|
|
|
self.assertEqual(realm.message_visibility_limit, None)
|
|
|
|
|
2018-12-13 07:54:43 +01:00
|
|
|
do_change_plan_type(realm, Realm.STANDARD)
|
2018-10-17 10:50:59 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.max_invites, Realm.INVITES_STANDARD_REALM_DAILY_MAX)
|
|
|
|
self.assertEqual(realm.message_visibility_limit, None)
|
|
|
|
|
2018-12-13 07:54:43 +01:00
|
|
|
do_change_plan_type(realm, Realm.LIMITED)
|
2018-10-17 10:50:59 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.max_invites, settings.INVITES_DEFAULT_REALM_DAILY_MAX)
|
|
|
|
self.assertEqual(realm.message_visibility_limit, Realm.MESSAGE_VISIBILITY_LIMITED)
|
|
|
|
|
2018-12-13 07:54:43 +01:00
|
|
|
do_change_plan_type(realm, Realm.STANDARD_FREE)
|
2018-10-17 10:50:59 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
self.assertEqual(realm.max_invites, Realm.INVITES_STANDARD_REALM_DAILY_MAX)
|
|
|
|
self.assertEqual(realm.message_visibility_limit, None)
|
2018-10-23 19:25:59 +02:00
|
|
|
|
2017-04-09 00:35:41 +02:00
|
|
|
class RealmAPITest(ZulipTestCase):
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> 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)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def set_up_db(self, attr: str, value: Any) -> None:
|
2017-04-09 00:35:41 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
setattr(realm, attr, value)
|
2018-11-15 23:21:22 +01:00
|
|
|
realm.save(update_fields=[attr])
|
2017-04-09 00:35:41 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def update_with_api(self, name: str, value: int) -> Realm:
|
2017-04-09 00:35:41 +02:00
|
|
|
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
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def do_test_realm_update_api(self, name: str) -> None:
|
2017-04-09 00:35:41 +02:00
|
|
|
"""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],
|
2018-01-29 16:10:54 +01:00
|
|
|
bot_creation_policy=[1, 2],
|
2018-12-07 00:48:06 +01:00
|
|
|
email_address_visibility=[Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
|
|
|
|
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS],
|
2018-04-23 14:51:30 +02:00
|
|
|
video_chat_provider=[u'Jitsi', u'Hangouts'],
|
|
|
|
google_hangouts_domain=[u'zulip.com', u'zulip.org'],
|
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])
|
|
|
|
|
2017-10-07 00:29:18 +02:00
|
|
|
@slow("Tests a dozen properties in a loop")
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_realm_properties(self) -> None:
|
2017-04-09 00:35:41 +02:00
|
|
|
for prop in Realm.property_types:
|
|
|
|
self.do_test_realm_update_api(prop)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_realm_allow_message_editing(self) -> None:
|
2017-04-09 00:35:41 +02:00
|
|
|
"""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)
|
2017-12-03 00:50:48 +01:00
|
|
|
self.set_up_db('allow_community_topic_editing', False)
|
2017-04-09 00:35:41 +02:00
|
|
|
realm = self.update_with_api('allow_message_editing', True)
|
|
|
|
realm = self.update_with_api('message_content_edit_limit_seconds', 100)
|
2017-12-03 00:50:48 +01:00
|
|
|
realm = self.update_with_api('allow_community_topic_editing', True)
|
2017-04-09 00:35:41 +02:00
|
|
|
self.assertEqual(realm.allow_message_editing, True)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 100)
|
2017-12-03 00:50:48 +01:00
|
|
|
self.assertEqual(realm.allow_community_topic_editing, True)
|
2017-04-09 00:35:41 +02:00
|
|
|
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)
|
2017-12-03 00:50:48 +01:00
|
|
|
self.assertEqual(realm.allow_community_topic_editing, True)
|
2017-04-09 00:35:41 +02:00
|
|
|
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)
|
2017-12-03 00:50:48 +01:00
|
|
|
self.assertEqual(realm.allow_community_topic_editing, True)
|
|
|
|
realm = self.update_with_api('allow_community_topic_editing', False)
|
|
|
|
self.assertEqual(realm.allow_message_editing, False)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 200)
|
|
|
|
self.assertEqual(realm.allow_community_topic_editing, False)
|
2017-11-26 09:12:10 +01:00
|
|
|
|
|
|
|
def test_update_realm_allow_message_deleting(self) -> None:
|
|
|
|
"""Tests updating the realm property 'allow_message_deleting'."""
|
|
|
|
self.set_up_db('allow_message_deleting', True)
|
|
|
|
self.set_up_db('message_content_delete_limit_seconds', 0)
|
|
|
|
realm = self.update_with_api('allow_message_deleting', False)
|
|
|
|
self.assertEqual(realm.allow_message_deleting, False)
|
|
|
|
self.assertEqual(realm.message_content_delete_limit_seconds, 0)
|
|
|
|
realm = self.update_with_api('allow_message_deleting', True)
|
|
|
|
realm = self.update_with_api('message_content_delete_limit_seconds', 100)
|
|
|
|
self.assertEqual(realm.allow_message_deleting, True)
|
|
|
|
self.assertEqual(realm.message_content_delete_limit_seconds, 100)
|
|
|
|
realm = self.update_with_api('message_content_delete_limit_seconds', 600)
|
|
|
|
self.assertEqual(realm.allow_message_deleting, True)
|
|
|
|
self.assertEqual(realm.message_content_delete_limit_seconds, 600)
|
2018-09-14 13:14:40 +02:00
|
|
|
|
|
|
|
class ScrubRealmTest(ZulipTestCase):
|
|
|
|
def test_scrub_realm(self) -> None:
|
|
|
|
zulip = get_realm("zulip")
|
|
|
|
lear = get_realm("lear")
|
|
|
|
|
|
|
|
iago = self.example_user("iago")
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
|
|
|
|
cordelia = self.lear_user("cordelia")
|
|
|
|
king = self.lear_user("king")
|
|
|
|
|
|
|
|
create_stream_if_needed(lear, "Shakespeare")
|
|
|
|
|
|
|
|
self.subscribe(cordelia, "Shakespeare")
|
|
|
|
self.subscribe(king, "Shakespeare")
|
|
|
|
|
|
|
|
Message.objects.all().delete()
|
|
|
|
UserMessage.objects.all().delete()
|
|
|
|
|
|
|
|
for i in range(5):
|
|
|
|
self.send_stream_message(iago.email, "Scotland")
|
|
|
|
self.send_stream_message(othello.email, "Scotland")
|
|
|
|
self.send_stream_message(cordelia.email, "Shakespeare", sender_realm="lear")
|
|
|
|
self.send_stream_message(king.email, "Shakespeare", sender_realm="lear")
|
|
|
|
|
|
|
|
Attachment.objects.filter(realm=zulip).delete()
|
|
|
|
Attachment.objects.create(realm=zulip, owner=iago, path_id="a/b/temp1.txt")
|
|
|
|
Attachment.objects.create(realm=zulip, owner=othello, path_id="a/b/temp2.txt")
|
|
|
|
|
|
|
|
Attachment.objects.filter(realm=lear).delete()
|
|
|
|
Attachment.objects.create(realm=lear, owner=cordelia, path_id="c/d/temp1.txt")
|
|
|
|
Attachment.objects.create(realm=lear, owner=king, path_id="c/d/temp2.txt")
|
|
|
|
|
|
|
|
CustomProfileField.objects.create(realm=lear)
|
|
|
|
|
|
|
|
self.assertEqual(Message.objects.filter(sender__in=[iago, othello]).count(), 10)
|
|
|
|
self.assertEqual(Message.objects.filter(sender__in=[cordelia, king]).count(), 10)
|
|
|
|
self.assertEqual(UserMessage.objects.filter(user_profile__in=[iago, othello]).count(), 20)
|
|
|
|
self.assertEqual(UserMessage.objects.filter(user_profile__in=[cordelia, king]).count(), 20)
|
|
|
|
|
|
|
|
self.assertNotEqual(CustomProfileField.objects.filter(realm=zulip).count(), 0)
|
|
|
|
|
2018-10-15 09:59:29 +02:00
|
|
|
with mock.patch('logging.warning'):
|
|
|
|
do_scrub_realm(zulip)
|
2018-09-14 13:14:40 +02:00
|
|
|
|
|
|
|
self.assertEqual(Message.objects.filter(sender__in=[iago, othello]).count(), 0)
|
|
|
|
self.assertEqual(Message.objects.filter(sender__in=[cordelia, king]).count(), 10)
|
|
|
|
self.assertEqual(UserMessage.objects.filter(user_profile__in=[iago, othello]).count(), 0)
|
|
|
|
self.assertEqual(UserMessage.objects.filter(user_profile__in=[cordelia, king]).count(), 20)
|
|
|
|
|
|
|
|
self.assertEqual(Attachment.objects.filter(realm=zulip).count(), 0)
|
|
|
|
self.assertEqual(Attachment.objects.filter(realm=lear).count(), 2)
|
|
|
|
|
|
|
|
self.assertEqual(CustomProfileField.objects.filter(realm=zulip).count(), 0)
|
|
|
|
self.assertNotEqual(CustomProfileField.objects.filter(realm=lear).count(), 0)
|
|
|
|
|
|
|
|
zulip_users = UserProfile.objects.filter(realm=zulip)
|
|
|
|
for user in zulip_users:
|
|
|
|
self.assertTrue(re.search("Scrubbed [a-z0-9]{15}", user.full_name))
|
|
|
|
self.assertTrue(re.search("scrubbed-[a-z0-9]{15}@" + zulip.host, user.email))
|
2018-10-15 09:10:37 +02:00
|
|
|
self.assertTrue(re.search("scrubbed-[a-z0-9]{15}@" + zulip.host, user.delivery_email))
|
2018-09-14 13:14:40 +02:00
|
|
|
|
|
|
|
lear_users = UserProfile.objects.filter(realm=lear)
|
|
|
|
for user in lear_users:
|
|
|
|
self.assertIsNone(re.search("Scrubbed [a-z0-9]{15}", user.full_name))
|
|
|
|
self.assertIsNone(re.search("scrubbed-[a-z0-9]{15}@" + zulip.host, user.email))
|
2018-10-15 09:10:37 +02:00
|
|
|
self.assertIsNone(re.search("scrubbed-[a-z0-9]{15}@" + zulip.host, user.delivery_email))
|