2013-03-20 22:08:48 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-03-20 22:08:48 +01:00
|
|
|
|
2017-03-06 08:45:59 +01:00
|
|
|
from typing import (Any, Callable, Dict, Iterable, List, Mapping,
|
|
|
|
Optional, Tuple, TypeVar, Text, Union)
|
2016-06-22 10:36:22 +02:00
|
|
|
from mock import patch, MagicMock
|
2016-06-15 23:47:22 +02:00
|
|
|
|
2016-09-14 02:08:08 +02:00
|
|
|
from django.http import HttpResponse
|
2016-08-24 07:53:05 +02:00
|
|
|
from django.test import TestCase, override_settings
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2014-01-27 22:53:36 +01:00
|
|
|
from zerver.lib.test_helpers import (
|
2016-07-15 10:14:51 +02:00
|
|
|
queries_captured, simulated_empty_cache,
|
2016-11-10 19:30:09 +01:00
|
|
|
simulated_queue_client, tornado_redirected_to_list,
|
2016-12-19 08:48:03 +01:00
|
|
|
most_recent_message, make_client, avatar_disk_path,
|
|
|
|
get_test_image_file
|
2014-01-27 22:53:36 +01:00
|
|
|
)
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2016-07-16 03:56:45 +02:00
|
|
|
from zerver.lib.test_runner import slow
|
2016-10-10 12:51:59 +02:00
|
|
|
from zerver.forms import WRONG_SUBDOMAIN_ERROR
|
2014-01-27 22:53:36 +01:00
|
|
|
|
2014-01-31 23:13:58 +01:00
|
|
|
from zerver.models import UserProfile, Recipient, \
|
2016-09-19 23:13:13 +02:00
|
|
|
Realm, RealmAlias, UserActivity, \
|
2016-12-26 19:19:02 +01:00
|
|
|
get_user_profile_by_email, get_realm, get_client, get_stream, \
|
2017-03-06 08:45:59 +01:00
|
|
|
Message, get_unique_open_realm, completely_open, get_context_for_message
|
2014-02-07 22:47:30 +01:00
|
|
|
|
2017-02-16 22:35:57 +01:00
|
|
|
from zerver.lib.avatar import avatar_url
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.initial_password import initial_password
|
2016-06-22 10:36:22 +02:00
|
|
|
from zerver.lib.email_mirror import create_missed_message_address
|
2014-01-31 16:44:45 +01:00
|
|
|
from zerver.lib.actions import \
|
2014-01-29 22:07:09 +01:00
|
|
|
get_emails_from_user_ids, do_deactivate_user, do_reactivate_user, \
|
2014-02-14 19:29:42 +01:00
|
|
|
do_change_is_admin, extract_recipients, \
|
2015-10-13 22:47:21 +02:00
|
|
|
do_set_realm_name, do_deactivate_realm, \
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only
|
2017-03-06 08:45:59 +01:00
|
|
|
from zerver.lib.notifications import handle_missedmessage_emails, \
|
|
|
|
send_missedmessage_email
|
2017-03-08 11:43:35 +01:00
|
|
|
from zerver.lib.sessions import get_session_dict_user
|
2013-12-26 15:01:46 +01:00
|
|
|
from zerver.middleware import is_slow_query
|
2017-01-06 18:56:36 +01:00
|
|
|
from zerver.lib.utils import split_by
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2013-10-17 17:48:19 +02:00
|
|
|
from zerver.worker import queue_processors
|
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
from django.conf import settings
|
2014-07-15 21:03:51 +02:00
|
|
|
from django.core import mail
|
2017-03-08 11:57:55 +01:00
|
|
|
from six.moves import range
|
2013-06-19 20:08:48 +02:00
|
|
|
import os
|
2014-07-15 21:03:51 +02:00
|
|
|
import re
|
2013-01-10 19:41:49 +01:00
|
|
|
import sys
|
2013-06-20 18:47:19 +02:00
|
|
|
import time
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2016-07-13 19:05:41 +02:00
|
|
|
import random
|
2016-12-19 08:48:03 +01:00
|
|
|
import filecmp
|
2017-01-06 18:56:36 +01:00
|
|
|
import subprocess
|
2013-03-14 22:12:25 +01:00
|
|
|
|
2016-06-15 23:47:22 +02:00
|
|
|
K = TypeVar('K')
|
|
|
|
V = TypeVar('V')
|
2014-01-14 20:38:45 +01:00
|
|
|
def find_dict(lst, k, v):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: (Iterable[Dict[K, V]], K, V) -> Dict[K, V]
|
2014-01-14 20:38:45 +01:00
|
|
|
for dct in lst:
|
|
|
|
if dct[k] == v:
|
|
|
|
return dct
|
2017-03-05 08:17:36 +01:00
|
|
|
raise AssertionError('Cannot find element in list where key %s == %s' % (k, v))
|
2014-01-14 20:38:45 +01:00
|
|
|
|
2013-12-26 15:01:46 +01:00
|
|
|
class SlowQueryTest(TestCase):
|
|
|
|
def test_is_slow_query(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-12-26 15:16:49 +01:00
|
|
|
self.assertFalse(is_slow_query(1.1, '/some/random/url'))
|
2013-12-26 15:01:46 +01:00
|
|
|
self.assertTrue(is_slow_query(2, '/some/random/url'))
|
2013-12-26 15:23:18 +01:00
|
|
|
self.assertTrue(is_slow_query(5.1, '/activity'))
|
2013-12-26 15:01:46 +01:00
|
|
|
self.assertFalse(is_slow_query(2, '/activity'))
|
|
|
|
self.assertFalse(is_slow_query(2, '/json/report_error'))
|
|
|
|
self.assertFalse(is_slow_query(2, '/api/v1/deployments/report_error'))
|
|
|
|
self.assertFalse(is_slow_query(2, '/realm_activity/whatever'))
|
|
|
|
self.assertFalse(is_slow_query(2, '/user_activity/whatever'))
|
2013-12-26 15:20:59 +01:00
|
|
|
self.assertFalse(is_slow_query(9, '/accounts/webathena_kerberos_login/'))
|
|
|
|
self.assertTrue(is_slow_query(11, '/accounts/webathena_kerberos_login/'))
|
2013-12-26 15:01:46 +01:00
|
|
|
|
2016-09-20 03:00:27 +02:00
|
|
|
class ModelTest(TestCase):
|
|
|
|
def test_miscellaneous_things(self):
|
|
|
|
# type: () -> None
|
|
|
|
'''
|
|
|
|
This is a kitchen sink test that is designed simply to get
|
|
|
|
test coverage up to 100% for models.py.
|
|
|
|
'''
|
|
|
|
client = make_client('some_client')
|
|
|
|
self.assertEqual(str(client), u'<Client: some_client>')
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class RealmTest(ZulipTestCase):
|
2014-01-28 15:11:10 +01:00
|
|
|
def assert_user_profile_cache_gets_new_name(self, email, new_realm_name):
|
2016-12-04 18:38:56 +01:00
|
|
|
# type: (Text, Text) -> None
|
2014-01-28 15:11:10 +01:00
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(user_profile.realm.name, new_realm_name)
|
|
|
|
|
2014-01-28 22:23:31 +01:00
|
|
|
def test_do_set_realm_name_caching(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# 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."""
|
2014-01-28 15:11:10 +01:00
|
|
|
get_user_profile_by_email('hamlet@zulip.com')
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2014-01-28 15:11:10 +01:00
|
|
|
new_name = 'Zed You Elle Eye Pea'
|
|
|
|
do_set_realm_name(realm, new_name)
|
2017-01-04 05:30:48 +01:00
|
|
|
self.assertEqual(get_realm(realm.string_id).name, new_name)
|
2014-01-28 15:11:10 +01:00
|
|
|
self.assert_user_profile_cache_gets_new_name('hamlet@zulip.com', new_name)
|
|
|
|
|
2014-01-28 22:23:31 +01:00
|
|
|
def test_do_set_realm_name_events(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2014-01-28 22:23:31 +01:00
|
|
|
new_name = 'Puliz'
|
2016-06-15 23:47:22 +02:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2014-01-28 22:23:31 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
do_set_realm_name(realm, new_name)
|
|
|
|
event = events[0]['event']
|
|
|
|
self.assertEqual(event, dict(
|
|
|
|
type = 'realm',
|
|
|
|
op = 'update',
|
|
|
|
property = 'name',
|
|
|
|
value = new_name,
|
|
|
|
))
|
|
|
|
|
2016-09-17 16:06:33 +02:00
|
|
|
def test_update_realm_api(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-01-28 23:22:50 +01:00
|
|
|
new_name = 'Zulip: Worldwide Exporter of APIs'
|
|
|
|
|
|
|
|
email = 'cordelia@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
do_change_is_admin(user_profile, True)
|
|
|
|
|
2016-09-17 16:06:33 +02:00
|
|
|
def set_up_db(attr, value):
|
|
|
|
# type: (str, Any) -> None
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-09-17 16:06:33 +02:00
|
|
|
setattr(realm, attr, value)
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
def update_with_api(**kwarg):
|
|
|
|
# type: (**Any) -> Realm
|
|
|
|
params = {k: ujson.dumps(v) for k, v in kwarg.items()}
|
|
|
|
result = self.client_patch('/json/realm', params)
|
|
|
|
self.assert_json_success(result)
|
2017-01-04 05:30:48 +01:00
|
|
|
return get_realm('zulip') # refresh data
|
2016-09-17 16:06:33 +02:00
|
|
|
|
|
|
|
# name
|
|
|
|
realm = update_with_api(name=new_name)
|
2014-01-28 23:22:50 +01:00
|
|
|
self.assertEqual(realm.name, new_name)
|
|
|
|
|
2016-09-17 16:06:33 +02:00
|
|
|
# restricted
|
|
|
|
set_up_db('restricted_to_domain', False)
|
|
|
|
realm = update_with_api(restricted_to_domain=True)
|
|
|
|
self.assertEqual(realm.restricted_to_domain, True)
|
|
|
|
realm = update_with_api(restricted_to_domain=False)
|
|
|
|
self.assertEqual(realm.restricted_to_domain, False)
|
|
|
|
|
|
|
|
# invite_required
|
|
|
|
set_up_db('invite_required', False)
|
|
|
|
realm = update_with_api(invite_required=True)
|
|
|
|
self.assertEqual(realm.invite_required, True)
|
|
|
|
realm = update_with_api(invite_required=False)
|
|
|
|
self.assertEqual(realm.invite_required, False)
|
|
|
|
|
|
|
|
# invite_by_admins_only
|
|
|
|
set_up_db('invite_by_admins_only', False)
|
|
|
|
realm = update_with_api(invite_by_admins_only=True)
|
|
|
|
self.assertEqual(realm.invite_by_admins_only, True)
|
|
|
|
realm = update_with_api(invite_by_admins_only=False)
|
|
|
|
self.assertEqual(realm.invite_by_admins_only, False)
|
|
|
|
|
|
|
|
# create_stream_by_admins_only
|
|
|
|
set_up_db('create_stream_by_admins_only', False)
|
|
|
|
realm = update_with_api(create_stream_by_admins_only=True)
|
|
|
|
self.assertEqual(realm.create_stream_by_admins_only, True)
|
|
|
|
realm = update_with_api(create_stream_by_admins_only=False)
|
|
|
|
self.assertEqual(realm.create_stream_by_admins_only, False)
|
|
|
|
|
2017-03-04 06:39:45 +01:00
|
|
|
# email address change disabled
|
|
|
|
set_up_db('email_changes_disabled', False)
|
|
|
|
realm = update_with_api(email_changes_disabled=True)
|
|
|
|
self.assertEqual(realm.email_changes_disabled, True)
|
|
|
|
realm = update_with_api(email_changes_disabled=False)
|
|
|
|
self.assertEqual(realm.email_changes_disabled, False)
|
|
|
|
|
2016-12-20 15:41:30 +01:00
|
|
|
# add_emoji_by_admins_only
|
|
|
|
set_up_db('add_emoji_by_admins_only', False)
|
|
|
|
realm = update_with_api(add_emoji_by_admins_only=True)
|
|
|
|
self.assertEqual(realm.add_emoji_by_admins_only, True)
|
|
|
|
realm = update_with_api(add_emoji_by_admins_only=False)
|
|
|
|
self.assertEqual(realm.add_emoji_by_admins_only, False)
|
|
|
|
|
2016-09-17 16:06:33 +02:00
|
|
|
# allow_message_editing
|
|
|
|
set_up_db('allow_message_editing', False)
|
|
|
|
set_up_db('message_content_edit_limit_seconds', 0)
|
|
|
|
realm = update_with_api(allow_message_editing=True,
|
|
|
|
message_content_edit_limit_seconds=100)
|
|
|
|
self.assertEqual(realm.allow_message_editing, True)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 100)
|
|
|
|
realm = update_with_api(allow_message_editing=False)
|
|
|
|
self.assertEqual(realm.allow_message_editing, False)
|
|
|
|
self.assertEqual(realm.message_content_edit_limit_seconds, 100)
|
|
|
|
realm = 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-02-25 20:53:32 +01:00
|
|
|
|
|
|
|
# waiting_period_threshold
|
|
|
|
set_up_db('waiting_period_threshold', 10)
|
|
|
|
realm = update_with_api(waiting_period_threshold=20)
|
|
|
|
self.assertEqual(realm.waiting_period_threshold, 20)
|
|
|
|
realm = update_with_api(waiting_period_threshold=10)
|
|
|
|
self.assertEqual(realm.waiting_period_threshold, 10)
|
2016-09-17 16:06:33 +02:00
|
|
|
|
2014-01-28 23:29:09 +01:00
|
|
|
def test_admin_restrictions_for_changing_realm_name(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-01-28 23:29:09 +01:00
|
|
|
new_name = 'Mice will play while the cat is away'
|
|
|
|
|
|
|
|
email = 'othello@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
user_profile = get_user_profile_by_email(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')
|
|
|
|
|
2014-01-28 17:29:00 +01:00
|
|
|
def test_do_deactivate_realm(self):
|
2016-06-15 23:47:22 +02: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()."""
|
2014-01-28 17:29:00 +01:00
|
|
|
get_user_profile_by_email('hamlet@zulip.com')
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2014-01-28 17:29:00 +01:00
|
|
|
do_deactivate_realm(realm)
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertTrue(user.realm.deactivated)
|
|
|
|
|
2016-08-04 17:32:41 +02:00
|
|
|
def test_do_set_realm_default_language(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_lang = "de"
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-08-04 17:32:41 +02:00
|
|
|
self.assertNotEqual(realm.default_language, new_lang)
|
|
|
|
# we need an admin user.
|
|
|
|
email = 'iago@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
req = dict(default_language=ujson.dumps(new_lang))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-08-04 17:32:41 +02:00
|
|
|
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,))
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-08-04 17:32:41 +02:00
|
|
|
self.assertNotEqual(realm.default_language, invalid_lang)
|
|
|
|
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class PermissionTest(ZulipTestCase):
|
2013-11-02 15:36:17 +01:00
|
|
|
def test_get_admin_users(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-11-02 15:36:17 +01:00
|
|
|
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
2014-01-22 20:12:29 +01:00
|
|
|
do_change_is_admin(user_profile, False)
|
2013-11-02 15:36:17 +01:00
|
|
|
admin_users = user_profile.realm.get_admin_users()
|
|
|
|
self.assertFalse(user_profile in admin_users)
|
2014-01-22 20:12:29 +01:00
|
|
|
do_change_is_admin(user_profile, True)
|
2013-11-02 15:36:17 +01:00
|
|
|
admin_users = user_profile.realm.get_admin_users()
|
|
|
|
self.assertTrue(user_profile in admin_users)
|
2013-09-30 22:52:04 +02:00
|
|
|
|
2016-07-13 05:05:55 +02:00
|
|
|
def test_updating_non_existent_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login('hamlet@zulip.com')
|
|
|
|
admin = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
do_change_is_admin(admin, True)
|
|
|
|
|
|
|
|
result = self.client_patch('/json/users/nonexistentuser@zulip.com', {})
|
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
|
2014-01-14 16:19:26 +01:00
|
|
|
def test_admin_api(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-01-14 16:19:26 +01:00
|
|
|
self.login('hamlet@zulip.com')
|
|
|
|
admin = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
user = get_user_profile_by_email('othello@zulip.com')
|
|
|
|
realm = admin.realm
|
2014-01-22 20:12:29 +01:00
|
|
|
do_change_is_admin(admin, True)
|
2014-01-14 20:38:45 +01:00
|
|
|
|
|
|
|
# Make sure we see is_admin flag in /json/users
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get('/json/users')
|
2014-01-14 20:38:45 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
members = ujson.loads(result.content)['members']
|
|
|
|
hamlet = find_dict(members, 'email', 'hamlet@zulip.com')
|
|
|
|
self.assertTrue(hamlet['is_admin'])
|
|
|
|
othello = find_dict(members, 'email', 'othello@zulip.com')
|
|
|
|
self.assertFalse(othello['is_admin'])
|
2014-01-14 16:19:26 +01:00
|
|
|
|
|
|
|
# Giveth
|
|
|
|
req = dict(is_admin=ujson.dumps(True))
|
2014-01-21 19:27:22 +01:00
|
|
|
|
2016-06-15 23:47:22 +02:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2014-01-21 19:27:22 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.client_patch('/json/users/othello@zulip.com', req)
|
2014-01-14 16:19:26 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
admin_users = realm.get_admin_users()
|
|
|
|
self.assertTrue(user in admin_users)
|
2014-01-21 19:27:22 +01:00
|
|
|
person = events[0]['event']['person']
|
|
|
|
self.assertEqual(person['email'], 'othello@zulip.com')
|
|
|
|
self.assertEqual(person['is_admin'], True)
|
2014-01-14 16:19:26 +01:00
|
|
|
|
|
|
|
# Taketh away
|
|
|
|
req = dict(is_admin=ujson.dumps(False))
|
2014-01-21 19:27:22 +01:00
|
|
|
events = []
|
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.client_patch('/json/users/othello@zulip.com', req)
|
2014-01-14 16:19:26 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
admin_users = realm.get_admin_users()
|
|
|
|
self.assertFalse(user in admin_users)
|
2014-01-21 19:27:22 +01:00
|
|
|
person = events[0]['event']['person']
|
|
|
|
self.assertEqual(person['email'], 'othello@zulip.com')
|
|
|
|
self.assertEqual(person['is_admin'], False)
|
2014-01-14 16:19:26 +01:00
|
|
|
|
2016-12-05 06:40:00 +01:00
|
|
|
# Cannot take away from last admin
|
|
|
|
self.login('iago@zulip.com')
|
|
|
|
req = dict(is_admin=ujson.dumps(False))
|
|
|
|
events = []
|
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.client_patch('/json/users/hamlet@zulip.com', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
admin_users = realm.get_admin_users()
|
|
|
|
self.assertFalse(admin in admin_users)
|
|
|
|
person = events[0]['event']['person']
|
|
|
|
self.assertEqual(person['email'], 'hamlet@zulip.com')
|
|
|
|
self.assertEqual(person['is_admin'], False)
|
|
|
|
with tornado_redirected_to_list([]):
|
|
|
|
result = self.client_patch('/json/users/iago@zulip.com', req)
|
|
|
|
self.assert_json_error(result, 'Cannot remove the only organization administrator')
|
|
|
|
|
2014-01-14 16:19:26 +01:00
|
|
|
# Make sure only admins can patch other user's info.
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
result = self.client_patch('/json/users/hamlet@zulip.com', req)
|
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
2016-09-27 14:25:52 +02:00
|
|
|
def test_admin_user_can_change_full_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_name = 'new name'
|
|
|
|
self.login('iago@zulip.com')
|
|
|
|
req = dict(full_name=ujson.dumps(new_name))
|
|
|
|
result = self.client_patch('/json/users/hamlet@zulip.com', req)
|
|
|
|
self.assertTrue(result.status_code == 200)
|
|
|
|
hamlet = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertEqual(hamlet.full_name, new_name)
|
|
|
|
|
|
|
|
def test_non_admin_cannot_change_full_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login('hamlet@zulip.com')
|
|
|
|
req = dict(full_name=ujson.dumps('new name'))
|
|
|
|
result = self.client_patch('/json/users/othello@zulip.com', req)
|
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
|
|
|
def test_admin_cannot_set_long_full_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_name = 'a' * (UserProfile.MAX_NAME_LENGTH + 1)
|
|
|
|
self.login('iago@zulip.com')
|
|
|
|
req = dict(full_name=ujson.dumps(new_name))
|
|
|
|
result = self.client_patch('/json/users/hamlet@zulip.com', req)
|
|
|
|
self.assert_json_error(result, 'Name too long!')
|
|
|
|
|
2017-01-16 04:18:42 +01:00
|
|
|
def test_admin_cannot_set_full_name_with_invalid_characters(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_name = 'Opheli*'
|
|
|
|
self.login('iago@zulip.com')
|
|
|
|
req = dict(full_name=ujson.dumps(new_name))
|
|
|
|
result = self.client_patch('/json/users/hamlet@zulip.com', req)
|
|
|
|
self.assert_json_error(result, 'Invalid characters in name!')
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class AdminCreateUserTest(ZulipTestCase):
|
2016-07-12 20:46:55 +02:00
|
|
|
def test_create_user_backend(self):
|
|
|
|
# type: () -> None
|
|
|
|
|
|
|
|
# This test should give us complete coverage on
|
|
|
|
# create_user_backend. It mostly exercises error
|
|
|
|
# conditions, and it also does a basic test of the success
|
|
|
|
# path.
|
|
|
|
|
|
|
|
admin_email = 'hamlet@zulip.com'
|
|
|
|
self.login(admin_email)
|
|
|
|
admin = get_user_profile_by_email(admin_email)
|
|
|
|
do_change_is_admin(admin, True)
|
|
|
|
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", dict())
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result, "Missing 'email' argument")
|
|
|
|
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", dict(
|
2016-07-12 20:46:55 +02:00
|
|
|
email='romeo@not-zulip.com',
|
2017-01-24 06:34:26 +01:00
|
|
|
))
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result, "Missing 'password' argument")
|
|
|
|
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", dict(
|
2016-07-12 20:46:55 +02:00
|
|
|
email='romeo@not-zulip.com',
|
|
|
|
password='xxxx',
|
2017-01-24 06:34:26 +01:00
|
|
|
))
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result, "Missing 'full_name' argument")
|
|
|
|
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", dict(
|
2016-07-12 20:46:55 +02:00
|
|
|
email='romeo@not-zulip.com',
|
|
|
|
password='xxxx',
|
|
|
|
full_name='Romeo Montague',
|
2017-01-24 06:34:26 +01:00
|
|
|
))
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result, "Missing 'short_name' argument")
|
|
|
|
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", dict(
|
2016-07-12 20:46:55 +02:00
|
|
|
email='broken',
|
|
|
|
password='xxxx',
|
|
|
|
full_name='Romeo Montague',
|
|
|
|
short_name='Romeo',
|
2017-01-24 06:34:26 +01:00
|
|
|
))
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result, "Bad name or username")
|
|
|
|
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", dict(
|
2016-07-12 20:46:55 +02:00
|
|
|
email='romeo@not-zulip.com',
|
|
|
|
password='xxxx',
|
|
|
|
full_name='Romeo Montague',
|
|
|
|
short_name='Romeo',
|
2017-01-24 06:34:26 +01:00
|
|
|
))
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result,
|
2016-12-03 00:04:17 +01:00
|
|
|
"Email 'romeo@not-zulip.com' does not belong to domain 'zulip.com'")
|
2016-07-12 20:46:55 +02:00
|
|
|
|
2017-01-04 05:30:48 +01:00
|
|
|
RealmAlias.objects.create(realm=get_realm('zulip'), domain='zulip.net')
|
2016-09-19 23:13:13 +02:00
|
|
|
|
2016-07-12 20:46:55 +02:00
|
|
|
# HAPPY PATH STARTS HERE
|
|
|
|
valid_params = dict(
|
2016-09-19 23:13:13 +02:00
|
|
|
email='romeo@zulip.net',
|
2016-07-12 20:46:55 +02:00
|
|
|
password='xxxx',
|
|
|
|
full_name='Romeo Montague',
|
|
|
|
short_name='Romeo',
|
|
|
|
)
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", valid_params)
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2016-09-19 23:13:13 +02:00
|
|
|
new_user = get_user_profile_by_email('romeo@zulip.net')
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assertEqual(new_user.full_name, 'Romeo Montague')
|
|
|
|
self.assertEqual(new_user.short_name, 'Romeo')
|
|
|
|
|
|
|
|
# One more error condition to test--we can't create
|
|
|
|
# the same user twice.
|
2016-12-31 08:07:22 +01:00
|
|
|
result = self.client_post("/json/users", valid_params)
|
2016-07-12 20:46:55 +02:00
|
|
|
self.assert_json_error(result,
|
2016-12-03 00:04:17 +01:00
|
|
|
"Email 'romeo@zulip.net' already in use")
|
2016-07-12 20:46:55 +02:00
|
|
|
|
2013-10-17 17:48:19 +02:00
|
|
|
class WorkerTest(TestCase):
|
2015-10-14 22:43:04 +02:00
|
|
|
class FakeClient(object):
|
2013-10-17 17:48:19 +02:00
|
|
|
def __init__(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
self.consumers = {} # type: Dict[str, Callable]
|
|
|
|
self.queue = [] # type: List[Tuple[str, Dict[str, Any]]]
|
2013-10-17 17:48:19 +02:00
|
|
|
|
|
|
|
def register_json_consumer(self, queue_name, callback):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: (str, Callable) -> None
|
2013-10-17 17:48:19 +02:00
|
|
|
self.consumers[queue_name] = callback
|
|
|
|
|
|
|
|
def start_consuming(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-10-17 17:48:19 +02:00
|
|
|
for queue_name, data in self.queue:
|
|
|
|
callback = self.consumers[queue_name]
|
2013-10-30 22:03:50 +01:00
|
|
|
callback(data)
|
2013-10-17 17:48:19 +02:00
|
|
|
|
|
|
|
def test_UserActivityWorker(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-10-17 17:48:19 +02:00
|
|
|
fake_client = self.FakeClient()
|
|
|
|
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
UserActivity.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
user_profile = user.id,
|
|
|
|
client = get_client('ios')
|
2013-10-17 17:48:19 +02:00
|
|
|
).delete()
|
|
|
|
|
|
|
|
data = dict(
|
2017-01-24 07:06:13 +01:00
|
|
|
user_profile_id = user.id,
|
|
|
|
client = 'ios',
|
|
|
|
time = time.time(),
|
|
|
|
query = 'send_message'
|
2013-10-17 17:48:19 +02:00
|
|
|
)
|
|
|
|
fake_client.queue.append(('user_activity', data))
|
|
|
|
|
|
|
|
with simulated_queue_client(lambda: fake_client):
|
|
|
|
worker = queue_processors.UserActivityWorker()
|
2015-11-24 07:01:35 +01:00
|
|
|
worker.setup()
|
2013-10-17 17:48:19 +02:00
|
|
|
worker.start()
|
|
|
|
activity_records = UserActivity.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
user_profile = user.id,
|
|
|
|
client = get_client('ios')
|
2013-10-17 17:48:19 +02:00
|
|
|
)
|
|
|
|
self.assertTrue(len(activity_records), 1)
|
|
|
|
self.assertTrue(activity_records[0].count, 1)
|
|
|
|
|
2013-10-29 20:03:42 +01:00
|
|
|
def test_error_handling(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-10-29 20:03:42 +01:00
|
|
|
processed = []
|
|
|
|
|
2015-10-22 19:29:24 +02:00
|
|
|
@queue_processors.assign_queue('unreliable_worker')
|
|
|
|
class UnreliableWorker(queue_processors.QueueProcessingWorker):
|
2013-10-30 22:03:50 +01:00
|
|
|
def consume(self, data):
|
2016-07-30 06:16:35 +02:00
|
|
|
# type: (Mapping[str, Any]) -> None
|
|
|
|
if data["type"] == 'unexpected behaviour':
|
2015-10-22 19:29:24 +02:00
|
|
|
raise Exception('Worker task not performing as expected!')
|
2016-07-30 06:16:35 +02:00
|
|
|
processed.append(data["type"])
|
2013-10-29 20:03:42 +01:00
|
|
|
|
|
|
|
def _log_problem(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
|
2013-10-29 20:03:42 +01:00
|
|
|
# keep the tests quiet
|
|
|
|
pass
|
|
|
|
|
|
|
|
fake_client = self.FakeClient()
|
2015-10-22 19:29:24 +02:00
|
|
|
for msg in ['good', 'fine', 'unexpected behaviour', 'back to normal']:
|
2016-07-30 06:16:35 +02:00
|
|
|
fake_client.queue.append(('unreliable_worker', {'type': msg}))
|
2013-10-29 20:03:42 +01:00
|
|
|
|
2015-10-22 19:29:24 +02:00
|
|
|
fn = os.path.join(settings.QUEUE_ERROR_DIR, 'unreliable_worker.errors')
|
2013-10-29 20:03:42 +01:00
|
|
|
try:
|
|
|
|
os.remove(fn)
|
2017-03-05 08:17:36 +01:00
|
|
|
except OSError: # nocoverage # error handling for the directory not existing
|
2013-10-29 20:03:42 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
with simulated_queue_client(lambda: fake_client):
|
2015-10-22 19:29:24 +02:00
|
|
|
worker = UnreliableWorker()
|
2015-11-24 07:01:35 +01:00
|
|
|
worker.setup()
|
2013-10-29 20:03:42 +01:00
|
|
|
worker.start()
|
|
|
|
|
|
|
|
self.assertEqual(processed, ['good', 'fine', 'back to normal'])
|
|
|
|
line = open(fn).readline().strip()
|
|
|
|
event = ujson.loads(line.split('\t')[1])
|
2016-07-30 06:16:35 +02:00
|
|
|
self.assertEqual(event["type"], 'unexpected behaviour')
|
2013-10-29 20:03:42 +01:00
|
|
|
|
2016-01-26 02:06:26 +01:00
|
|
|
def test_worker_noname(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-01-26 02:06:26 +01:00
|
|
|
class TestWorker(queue_processors.QueueProcessingWorker):
|
|
|
|
def __init__(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-01-26 02:06:26 +01:00
|
|
|
super(TestWorker, self).__init__()
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2016-01-26 02:06:26 +01:00
|
|
|
def consume(self, data):
|
2016-07-30 06:16:35 +02:00
|
|
|
# type: (Mapping[str, Any]) -> None
|
2017-03-05 08:17:36 +01:00
|
|
|
pass # nocoverage # this is intentionally not called
|
2016-01-26 02:06:26 +01:00
|
|
|
with self.assertRaises(queue_processors.WorkerDeclarationException):
|
|
|
|
TestWorker()
|
|
|
|
|
|
|
|
def test_worker_noconsume(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-01-26 02:06:26 +01:00
|
|
|
@queue_processors.assign_queue('test_worker')
|
|
|
|
class TestWorker(queue_processors.QueueProcessingWorker):
|
|
|
|
def __init__(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-01-26 02:06:26 +01:00
|
|
|
super(TestWorker, self).__init__()
|
|
|
|
|
|
|
|
with self.assertRaises(queue_processors.WorkerDeclarationException):
|
|
|
|
worker = TestWorker()
|
|
|
|
worker.consume({})
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class DocPageTest(ZulipTestCase):
|
2016-08-18 18:09:47 +02:00
|
|
|
def _test(self, url, expected_content):
|
|
|
|
# type: (str, str) -> None
|
|
|
|
result = self.client_get(url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assertIn(expected_content, str(result.content))
|
|
|
|
|
|
|
|
def test_doc_endpoints(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._test('/api/', 'We hear you like APIs')
|
2017-02-28 23:44:58 +01:00
|
|
|
self._test('/api/endpoints/', 'pre-built API bindings for')
|
2016-10-16 20:19:27 +02:00
|
|
|
self._test('/about/', 'Cambridge, Massachusetts')
|
2016-10-16 20:26:01 +02:00
|
|
|
# Test the i18n version of one of these pages.
|
|
|
|
self._test('/en/about/', 'Cambridge, Massachusetts')
|
2016-08-18 18:09:47 +02:00
|
|
|
self._test('/apps/', 'Appsolutely')
|
|
|
|
self._test('/features/', 'Talk about multiple topics at once')
|
|
|
|
self._test('/hello/', 'workplace chat that actually improves your productivity')
|
|
|
|
self._test('/integrations/', 'require creating a Zulip bot')
|
|
|
|
self._test('/login/', '(Normal users)')
|
|
|
|
self._test('/register/', 'get started')
|
|
|
|
|
|
|
|
result = self.client_get('/new-user/')
|
|
|
|
self.assertEqual(result.status_code, 301)
|
|
|
|
self.assertIn('hello', result['Location'])
|
|
|
|
|
|
|
|
result = self.client_get('/robots.txt')
|
|
|
|
self.assertEqual(result.status_code, 301)
|
|
|
|
self.assertIn('static/robots.txt', result['Location'])
|
|
|
|
|
|
|
|
result = self.client_get('/static/robots.txt')
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assertIn(
|
|
|
|
'Disallow: /',
|
|
|
|
''.join(str(x) for x in list(result.streaming_content))
|
|
|
|
)
|
|
|
|
|
2013-10-20 21:10:03 +02:00
|
|
|
class UserProfileTest(TestCase):
|
|
|
|
def test_get_emails_from_user_ids(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-10-20 21:10:03 +02:00
|
|
|
hamlet = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
othello = get_user_profile_by_email('othello@zulip.com')
|
|
|
|
dct = get_emails_from_user_ids([hamlet.id, othello.id])
|
|
|
|
self.assertEqual(dct[hamlet.id], 'hamlet@zulip.com')
|
|
|
|
self.assertEqual(dct[othello.id], 'othello@zulip.com')
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class UserChangesTest(ZulipTestCase):
|
2013-08-08 16:49:32 +02:00
|
|
|
def test_update_api_key(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-08-08 16:49:32 +02:00
|
|
|
email = "hamlet@zulip.com"
|
|
|
|
self.login(email)
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
old_api_key = user.api_key
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/json/users/me/api_key/regenerate')
|
2013-08-08 16:49:32 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
new_api_key = ujson.loads(result.content)['api_key']
|
|
|
|
self.assertNotEqual(old_api_key, new_api_key)
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(new_api_key, user.api_key)
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class ActivateTest(ZulipTestCase):
|
2013-11-15 18:57:44 +01:00
|
|
|
def test_basics(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-11-15 18:57:44 +01:00
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
2013-11-16 17:11:15 +01:00
|
|
|
do_deactivate_user(user)
|
2013-11-15 18:57:44 +01:00
|
|
|
self.assertFalse(user.is_active)
|
2013-11-16 17:11:15 +01:00
|
|
|
do_reactivate_user(user)
|
2013-11-15 18:57:44 +01:00
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
2013-11-15 19:39:03 +01:00
|
|
|
def test_api(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-11-15 19:39:03 +01:00
|
|
|
admin = get_user_profile_by_email('othello@zulip.com')
|
2014-01-22 20:12:29 +01:00
|
|
|
do_change_is_admin(admin, True)
|
2013-11-15 19:39:03 +01:00
|
|
|
self.login('othello@zulip.com')
|
|
|
|
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_delete('/json/users/hamlet@zulip.com')
|
2013-11-15 19:39:03 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertFalse(user.is_active)
|
|
|
|
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/json/users/hamlet@zulip.com/reactivate')
|
2013-11-15 19:39:03 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
2017-02-19 00:18:19 +01:00
|
|
|
def test_api_me_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""This test helps ensure that our URL patterns for /users/me URLs
|
|
|
|
handle email addresses starting with "me" correctly."""
|
|
|
|
self.register("me@zulip.com", "testpassword")
|
|
|
|
self.login('iago@zulip.com')
|
|
|
|
|
|
|
|
result = self.client_delete('/json/users/me@zulip.com')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
user = get_user_profile_by_email('me@zulip.com')
|
|
|
|
self.assertFalse(user.is_active)
|
|
|
|
|
|
|
|
result = self.client_post('/json/users/me@zulip.com/reactivate')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
user = get_user_profile_by_email('me@zulip.com')
|
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
2016-07-13 05:24:11 +02:00
|
|
|
def test_api_with_nonexistent_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
admin = get_user_profile_by_email('othello@zulip.com')
|
|
|
|
do_change_is_admin(admin, True)
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
|
|
|
|
# Can not deactivate a user with the bot api
|
2014-02-11 17:14:33 +01:00
|
|
|
result = self.client_delete('/json/bots/hamlet@zulip.com')
|
|
|
|
self.assert_json_error(result, 'No such bot')
|
|
|
|
|
2016-07-13 05:24:11 +02:00
|
|
|
# Can not deactivate a nonexistent user.
|
|
|
|
result = self.client_delete('/json/users/nonexistent@zulip.com')
|
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
|
2016-12-05 06:40:00 +01:00
|
|
|
result = self.client_delete('/json/users/iago@zulip.com')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_delete('/json/users/othello@zulip.com')
|
|
|
|
self.assert_json_error(result, 'Cannot deactivate the only organization administrator')
|
|
|
|
|
2016-07-13 05:24:11 +02:00
|
|
|
# Can not reactivate a nonexistent user.
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/json/users/nonexistent@zulip.com/reactivate')
|
2016-07-13 05:24:11 +02:00
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
|
2016-07-13 05:42:29 +02:00
|
|
|
def test_api_with_insufficient_permissions(self):
|
|
|
|
# type: () -> None
|
|
|
|
non_admin = get_user_profile_by_email('othello@zulip.com')
|
|
|
|
do_change_is_admin(non_admin, False)
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
|
|
|
|
# Can not deactivate a user with the users api
|
|
|
|
result = self.client_delete('/json/users/hamlet@zulip.com')
|
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
|
|
|
# Can not reactivate a user
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/json/users/hamlet@zulip.com/reactivate')
|
2016-07-13 05:42:29 +02:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class BotTest(ZulipTestCase):
|
2013-07-03 21:34:32 +02:00
|
|
|
def assert_num_bots_equal(self, count):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: (int) -> None
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/json/bots")
|
2013-07-03 21:34:32 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
self.assertEqual(count, len(json['bots']))
|
|
|
|
|
2014-02-11 18:43:30 +01:00
|
|
|
def create_bot(self, **extras):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: (**Any) -> Dict[str, Any]
|
2013-07-03 21:34:32 +02:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2014-02-11 18:43:30 +01:00
|
|
|
bot_info.update(extras)
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2013-07-03 21:34:32 +02:00
|
|
|
self.assert_json_success(result)
|
2014-02-11 18:43:30 +01:00
|
|
|
return ujson.loads(result.content)
|
2013-07-03 21:34:32 +02:00
|
|
|
|
2013-07-03 21:54:10 +02:00
|
|
|
def deactivate_bot(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 17:14:33 +01:00
|
|
|
result = self.client_delete("/json/bots/hambot-bot@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2016-07-13 18:31:07 +02:00
|
|
|
def test_add_bot_with_bad_username(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
bot_info = dict(
|
|
|
|
full_name='',
|
|
|
|
short_name='',
|
2017-01-24 06:34:26 +01:00
|
|
|
)
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2016-07-13 18:31:07 +02:00
|
|
|
self.assert_json_error(result, 'Bad name or username')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2013-07-03 21:34:32 +02:00
|
|
|
def test_add_bot(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-03 21:34:32 +02:00
|
|
|
self.assert_num_bots_equal(0)
|
2016-06-15 23:47:22 +02:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2014-02-26 00:12:14 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.create_bot()
|
2013-07-03 21:34:32 +02:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2016-10-26 03:35:32 +02:00
|
|
|
bot = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
|
2014-02-26 00:12:14 +01:00
|
|
|
event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
|
|
|
|
self.assertEqual(
|
|
|
|
dict(
|
|
|
|
type='realm_bot',
|
|
|
|
op='add',
|
|
|
|
bot=dict(email='hambot-bot@zulip.com',
|
2016-12-03 00:04:17 +01:00
|
|
|
user_id=bot.id,
|
|
|
|
full_name='The Bot of Hamlet',
|
2017-02-06 20:45:26 +01:00
|
|
|
is_active=True,
|
2016-12-03 00:04:17 +01:00
|
|
|
api_key=result['api_key'],
|
|
|
|
avatar_url=result['avatar_url'],
|
|
|
|
default_sending_stream=None,
|
|
|
|
default_events_register_stream=None,
|
|
|
|
default_all_public_streams=False,
|
|
|
|
owner='hamlet@zulip.com')
|
2014-02-26 00:12:14 +01:00
|
|
|
),
|
|
|
|
event['event']
|
|
|
|
)
|
|
|
|
|
2016-07-28 00:38:45 +02:00
|
|
|
users_result = self.client_get('/json/users')
|
2016-07-13 04:28:54 +02:00
|
|
|
members = ujson.loads(users_result.content)['members']
|
|
|
|
bots = [m for m in members if m['email'] == 'hambot-bot@zulip.com']
|
|
|
|
self.assertEqual(len(bots), 1)
|
|
|
|
bot = bots[0]
|
|
|
|
self.assertEqual(bot['bot_owner'], 'hamlet@zulip.com')
|
2017-02-15 00:45:38 +01:00
|
|
|
self.assertEqual(bot['user_id'], get_user_profile_by_email('hambot-bot@zulip.com').id)
|
2016-07-13 04:28:54 +02:00
|
|
|
|
2016-07-13 19:49:42 +02:00
|
|
|
def test_add_bot_with_username_in_use(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
bot_info = dict(
|
|
|
|
full_name='Duplicate',
|
|
|
|
short_name='hambot',
|
2017-01-24 06:34:26 +01:00
|
|
|
)
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2016-07-13 19:49:42 +02:00
|
|
|
self.assert_json_error(result, 'Username already in use')
|
|
|
|
|
2016-07-13 18:21:24 +02:00
|
|
|
def test_add_bot_with_user_avatar(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
2016-12-19 08:48:03 +01:00
|
|
|
with get_test_image_file('img.png') as fp:
|
2016-07-13 18:21:24 +02:00
|
|
|
self.create_bot(file=fp)
|
2016-12-19 08:48:03 +01:00
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
# Make sure that avatar image that we've uploaded is same with avatar image in the server
|
|
|
|
self.assertTrue(filecmp.cmp(fp.name,
|
|
|
|
os.path.splitext(avatar_disk_path(profile))[0] +
|
|
|
|
".original"))
|
2016-07-13 18:21:24 +02:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER)
|
2016-12-19 08:48:03 +01:00
|
|
|
self.assertTrue(os.path.exists(avatar_disk_path(profile)))
|
2016-07-13 18:21:24 +02:00
|
|
|
|
2016-07-13 18:21:46 +02:00
|
|
|
def test_add_bot_with_too_many_files(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
2016-12-19 08:48:03 +01:00
|
|
|
with get_test_image_file('img.png') as fp1, \
|
|
|
|
get_test_image_file('img.gif') as fp2:
|
2016-07-13 18:21:46 +02:00
|
|
|
bot_info = dict(
|
|
|
|
full_name='whatever',
|
|
|
|
short_name='whatever',
|
|
|
|
file1=fp1,
|
|
|
|
file2=fp2,
|
2017-01-24 06:34:26 +01:00
|
|
|
)
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2016-07-13 18:21:46 +02:00
|
|
|
self.assert_json_error(result, 'You may only upload one file at a time')
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
2014-02-11 18:43:30 +01:00
|
|
|
def test_add_bot_with_default_sending_stream(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_sending_stream='Denmark')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_sending_stream'], 'Denmark')
|
|
|
|
|
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(profile.default_sending_stream.name, 'Denmark')
|
|
|
|
|
2014-02-13 19:39:54 +01:00
|
|
|
def test_add_bot_with_default_sending_stream_not_subscribed(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_sending_stream='Rome')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_sending_stream'], 'Rome')
|
|
|
|
|
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(profile.default_sending_stream.name, 'Rome')
|
|
|
|
|
2016-06-26 14:19:18 +02:00
|
|
|
def test_bot_add_subscription(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Calling POST /json/users/me/subscriptions should successfully add
|
|
|
|
streams, and a stream to the
|
|
|
|
list of subscriptions and confirm the right number of events
|
|
|
|
are generated.
|
|
|
|
When 'principals' has a bot, no notification message event or invitation email
|
|
|
|
is sent when add_subscriptions_backend is called in the above api call.
|
|
|
|
"""
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
|
|
|
# Normal user i.e. not a bot.
|
|
|
|
request_data = {
|
|
|
|
'principals': '["iago@zulip.com"]'
|
|
|
|
}
|
|
|
|
events = [] # type: List[Dict[str, Any]]
|
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.common_subscribe_to_streams("hamlet@zulip.com", ['Rome'], request_data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
msg_event = [e for e in events if e['event']['type'] == 'message']
|
2016-09-25 21:30:10 +02:00
|
|
|
self.assert_length(msg_event, 1) # Notification message event is sent.
|
2016-06-26 14:19:18 +02:00
|
|
|
|
|
|
|
# Create a bot.
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
# A bot
|
|
|
|
bot_request_data = {
|
|
|
|
'principals': '["hambot-bot@zulip.com"]'
|
|
|
|
}
|
|
|
|
events_bot = [] # type: List[Dict[str, Any]]
|
|
|
|
with tornado_redirected_to_list(events_bot):
|
|
|
|
result = self.common_subscribe_to_streams("hamlet@zulip.com", ['Rome'], bot_request_data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# No notification message event or invitation email is sent because of bot.
|
|
|
|
msg_event = [e for e in events_bot if e['event']['type'] == 'message']
|
2016-09-25 21:30:10 +02:00
|
|
|
self.assert_length(msg_event, 0)
|
2016-06-26 14:19:18 +02:00
|
|
|
self.assertEqual(len(events_bot), len(events) - 1)
|
|
|
|
|
|
|
|
# Test runner automatically redirects all sent email to a dummy 'outbox'.
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2014-02-11 18:43:30 +01:00
|
|
|
def test_add_bot_with_default_sending_stream_private_allowed(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2016-10-20 00:54:19 +02:00
|
|
|
self.subscribe_to_stream(user_profile.email, stream.name)
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-11 18:43:30 +01:00
|
|
|
|
|
|
|
self.assert_num_bots_equal(0)
|
2016-06-15 23:47:22 +02:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2014-02-26 00:12:14 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.create_bot(default_sending_stream='Denmark')
|
2014-02-11 18:43:30 +01:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_sending_stream'], 'Denmark')
|
|
|
|
|
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(profile.default_sending_stream.name, 'Denmark')
|
|
|
|
|
2014-02-26 00:12:14 +01:00
|
|
|
event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
|
|
|
|
self.assertEqual(
|
|
|
|
dict(
|
|
|
|
type='realm_bot',
|
|
|
|
op='add',
|
|
|
|
bot=dict(email='hambot-bot@zulip.com',
|
2016-12-03 00:04:17 +01:00
|
|
|
user_id=profile.id,
|
|
|
|
full_name='The Bot of Hamlet',
|
2017-02-06 20:45:26 +01:00
|
|
|
is_active=True,
|
2016-12-03 00:04:17 +01:00
|
|
|
api_key=result['api_key'],
|
|
|
|
avatar_url=result['avatar_url'],
|
|
|
|
default_sending_stream='Denmark',
|
|
|
|
default_events_register_stream=None,
|
|
|
|
default_all_public_streams=False,
|
|
|
|
owner='hamlet@zulip.com')
|
2014-02-26 00:12:14 +01:00
|
|
|
),
|
|
|
|
event['event']
|
|
|
|
)
|
|
|
|
self.assertEqual(event['users'], (user_profile.id,))
|
|
|
|
|
2014-02-11 18:43:30 +01:00
|
|
|
def test_add_bot_with_default_sending_stream_private_denied(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2016-10-20 16:49:29 +02:00
|
|
|
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-11 18:43:30 +01:00
|
|
|
|
|
|
|
bot_info = {
|
2017-01-24 07:06:13 +01:00
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'default_sending_stream': 'Denmark',
|
2017-01-24 06:02:39 +01:00
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2017-01-30 03:17:42 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
2014-02-11 18:43:30 +01:00
|
|
|
|
|
|
|
def test_add_bot_with_default_events_register_stream(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_events_register_stream='Denmark')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_events_register_stream'], 'Denmark')
|
|
|
|
|
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(profile.default_events_register_stream.name, 'Denmark')
|
|
|
|
|
|
|
|
def test_add_bot_with_default_events_register_stream_private_allowed(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = self.subscribe_to_stream(user_profile.email, 'Denmark')
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-11 18:43:30 +01:00
|
|
|
|
|
|
|
self.assert_num_bots_equal(0)
|
2016-06-15 23:47:22 +02:00
|
|
|
events = [] # type: List[Dict[str, Any]]
|
2014-02-26 00:12:14 +01:00
|
|
|
with tornado_redirected_to_list(events):
|
|
|
|
result = self.create_bot(default_events_register_stream='Denmark')
|
2014-02-11 18:43:30 +01:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertEqual(result['default_events_register_stream'], 'Denmark')
|
|
|
|
|
2016-10-26 03:35:32 +02:00
|
|
|
bot_profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(bot_profile.default_events_register_stream.name, 'Denmark')
|
2014-02-11 18:43:30 +01:00
|
|
|
|
2014-02-26 00:12:14 +01:00
|
|
|
event = [e for e in events if e['event']['type'] == 'realm_bot'][0]
|
|
|
|
self.assertEqual(
|
|
|
|
dict(
|
|
|
|
type='realm_bot',
|
|
|
|
op='add',
|
|
|
|
bot=dict(email='hambot-bot@zulip.com',
|
2016-12-03 00:04:17 +01:00
|
|
|
full_name='The Bot of Hamlet',
|
|
|
|
user_id=bot_profile.id,
|
2017-02-06 20:45:26 +01:00
|
|
|
is_active=True,
|
2016-12-03 00:04:17 +01:00
|
|
|
api_key=result['api_key'],
|
|
|
|
avatar_url=result['avatar_url'],
|
|
|
|
default_sending_stream=None,
|
|
|
|
default_events_register_stream='Denmark',
|
|
|
|
default_all_public_streams=False,
|
|
|
|
owner='hamlet@zulip.com')
|
2014-02-26 00:12:14 +01:00
|
|
|
),
|
|
|
|
event['event']
|
|
|
|
)
|
|
|
|
self.assertEqual(event['users'], (user_profile.id,))
|
|
|
|
|
2014-02-11 18:43:30 +01:00
|
|
|
def test_add_bot_with_default_events_register_stream_private_denied(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2016-10-20 16:49:29 +02:00
|
|
|
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-11 18:43:30 +01:00
|
|
|
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
bot_info = {
|
2017-01-24 07:06:13 +01:00
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
'default_events_register_stream': 'Denmark',
|
2017-01-24 06:02:39 +01:00
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2017-01-30 03:17:42 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
2014-02-11 18:43:30 +01:00
|
|
|
|
|
|
|
def test_add_bot_with_default_all_public_streams(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-11 18:43:30 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
result = self.create_bot(default_all_public_streams=ujson.dumps(True))
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.assertTrue(result['default_all_public_streams'])
|
|
|
|
|
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(profile.default_all_public_streams, True)
|
|
|
|
|
2013-07-03 21:54:10 +02:00
|
|
|
def test_deactivate_bot(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
self.deactivate_bot()
|
|
|
|
# You can deactivate the same bot twice.
|
|
|
|
self.deactivate_bot()
|
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
|
|
|
|
def test_deactivate_bogus_bot(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
"""Deleting a bogus bot will succeed silently."""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
2014-02-11 17:14:33 +01:00
|
|
|
result = self.client_delete("/json/bots/bogus-bot@zulip.com")
|
|
|
|
self.assert_json_error(result, 'No such bot')
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
def test_bot_deactivation_attacks(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
"""You cannot deactivate somebody else's bot."""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
# Have Othello try to deactivate both Hamlet and
|
|
|
|
# Hamlet's bot.
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("othello@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
|
2014-02-11 17:14:33 +01:00
|
|
|
# Can not deactivate a user as a bot
|
|
|
|
result = self.client_delete("/json/bots/hamlet@zulip.com")
|
|
|
|
self.assert_json_error(result, 'No such bot')
|
2013-07-03 21:54:10 +02:00
|
|
|
|
2014-02-11 17:14:33 +01:00
|
|
|
result = self.client_delete("/json/bots/hambot-bot@zulip.com")
|
2013-07-08 23:34:43 +02:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
2013-07-03 21:54:10 +02:00
|
|
|
|
|
|
|
# But we don't actually deactivate the other person's bot.
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2014-02-11 17:14:33 +01:00
|
|
|
# Can not deactivate a bot as a user
|
|
|
|
result = self.client_delete("/json/users/hambot-bot@zulip.com")
|
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2013-07-19 17:45:06 +02:00
|
|
|
def test_bot_permissions(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-19 17:45:06 +02:00
|
|
|
self.assert_num_bots_equal(0)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
# Have Othello try to mess with Hamlet's bots.
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("othello@zulip.com")
|
2013-07-19 17:45:06 +02:00
|
|
|
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots/hambot-bot@zulip.com/api_key/regenerate")
|
2013-07-19 17:45:06 +02:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
2013-07-23 15:57:32 +02:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2013-08-01 19:35:39 +02:00
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
2013-07-23 15:57:32 +02:00
|
|
|
self.assert_json_error(result, 'Insufficient permission')
|
|
|
|
|
2013-07-16 22:25:34 +02:00
|
|
|
def get_bot(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> Dict[str, Any]
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/json/bots")
|
2013-07-16 22:25:34 +02:00
|
|
|
bots = ujson.loads(result.content)['bots']
|
|
|
|
return bots[0]
|
|
|
|
|
2013-07-19 17:45:06 +02:00
|
|
|
def test_update_api_key(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-19 17:45:06 +02:00
|
|
|
self.create_bot()
|
|
|
|
bot = self.get_bot()
|
|
|
|
old_api_key = bot['api_key']
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/json/bots/hambot-bot@zulip.com/api_key/regenerate')
|
2013-07-19 17:45:06 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
new_api_key = ujson.loads(result.content)['api_key']
|
|
|
|
self.assertNotEqual(old_api_key, new_api_key)
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(new_api_key, bot['api_key'])
|
|
|
|
|
2016-07-13 16:51:00 +02:00
|
|
|
def test_update_api_key_for_invalid_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/json/bots/nonexistentuser@zulip.com/api_key/regenerate')
|
2016-07-13 16:51:00 +02:00
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
|
2013-07-16 22:25:34 +02:00
|
|
|
def test_patch_bot_full_name(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-16 22:25:34 +02:00
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2013-07-16 22:25:34 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2013-08-01 19:35:39 +02:00
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
2013-07-16 22:25:34 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
full_name = ujson.loads(result.content)['full_name']
|
|
|
|
self.assertEqual('Fred', full_name)
|
2013-10-21 18:54:57 +02:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Fred', bot['full_name'])
|
|
|
|
|
2017-02-24 06:36:54 +01:00
|
|
|
def test_patch_bot_owner(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/bots", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'bot_owner': 'othello@zulip.com',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Test bot's owner has been changed successfully.
|
|
|
|
bot_owner = ujson.loads(result.content)['bot_owner']
|
|
|
|
self.assertEqual(bot_owner, 'othello@zulip.com')
|
|
|
|
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('The Bot of Hamlet', bot['full_name'])
|
|
|
|
|
2017-02-17 06:21:01 +01:00
|
|
|
@override_settings(LOCAL_UPLOADS_DIR='var/bot_avatar')
|
2016-07-14 00:26:49 +02:00
|
|
|
def test_patch_bot_avatar(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2016-07-14 00:26:49 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
|
|
|
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_GRAVATAR)
|
|
|
|
|
|
|
|
# Try error case first (too many files):
|
2016-12-19 08:48:03 +01:00
|
|
|
with get_test_image_file('img.png') as fp1, \
|
|
|
|
get_test_image_file('img.gif') as fp2:
|
2016-07-14 00:26:49 +02:00
|
|
|
result = self.client_patch_multipart(
|
|
|
|
'/json/bots/hambot-bot@zulip.com',
|
|
|
|
dict(file1=fp1, file2=fp2))
|
|
|
|
self.assert_json_error(result, 'You may only upload one file at a time')
|
|
|
|
|
2017-01-28 19:05:20 +01:00
|
|
|
profile = get_user_profile_by_email("hambot-bot@zulip.com")
|
|
|
|
self.assertEqual(profile.avatar_version, 1)
|
|
|
|
|
2016-07-14 00:26:49 +02:00
|
|
|
# HAPPY PATH
|
2016-12-19 08:48:03 +01:00
|
|
|
with get_test_image_file('img.png') as fp:
|
2016-07-14 00:26:49 +02:00
|
|
|
result = self.client_patch_multipart(
|
|
|
|
'/json/bots/hambot-bot@zulip.com',
|
|
|
|
dict(file=fp))
|
2016-12-19 08:48:03 +01:00
|
|
|
profile = get_user_profile_by_email('hambot-bot@zulip.com')
|
2017-01-28 19:05:20 +01:00
|
|
|
self.assertEqual(profile.avatar_version, 2)
|
2016-12-19 08:48:03 +01:00
|
|
|
# Make sure that avatar image that we've uploaded is same with avatar image in the server
|
|
|
|
self.assertTrue(filecmp.cmp(fp.name,
|
|
|
|
os.path.splitext(avatar_disk_path(profile))[0] +
|
|
|
|
".original"))
|
2016-07-14 00:26:49 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER)
|
2016-12-19 08:48:03 +01:00
|
|
|
self.assertTrue(os.path.exists(avatar_disk_path(profile)))
|
2016-07-14 00:26:49 +02:00
|
|
|
|
2014-02-13 19:39:54 +01:00
|
|
|
def test_patch_bot_to_stream(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
|
|
|
self.assertEqual('Denmark', default_sending_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_sending_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_to_stream_not_subscribed(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Rome',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
|
|
|
self.assertEqual('Rome', default_sending_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Rome', bot['default_sending_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_to_stream_none(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': '',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-01-30 03:17:42 +01:00
|
|
|
default_sending_stream = get_user_profile_by_email(
|
|
|
|
"hambot-bot@zulip.com").default_sending_stream
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assertEqual(None, default_sending_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(None, bot['default_sending_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_to_stream_private_allowed(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = self.subscribe_to_stream(user_profile.email, "Denmark")
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_sending_stream = ujson.loads(result.content)['default_sending_stream']
|
|
|
|
self.assertEqual('Denmark', default_sending_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_sending_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_to_stream_private_denied(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2016-10-20 16:49:29 +02:00
|
|
|
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
2017-01-30 03:17:42 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
def test_patch_bot_to_stream_not_found(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_sending_stream': 'missing',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
2017-01-30 03:17:42 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'missing'")
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
def test_patch_bot_events_register_stream(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
|
|
|
|
self.assertEqual('Denmark', default_events_register_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_events_register_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_events_register_stream_allowed(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = self.subscribe_to_stream(user_profile.email, "Denmark")
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_events_register_stream = ujson.loads(result.content)['default_events_register_stream']
|
|
|
|
self.assertEqual('Denmark', default_events_register_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Denmark', bot['default_events_register_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_events_register_stream_denied(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2017-01-30 03:44:01 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2016-10-20 16:49:29 +02:00
|
|
|
self.unsubscribe_from_stream("hamlet@zulip.com", "Denmark")
|
2017-01-30 03:52:55 +01:00
|
|
|
do_change_stream_invite_only(stream, True)
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'Denmark',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
2017-01-30 03:17:42 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'Denmark'")
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
def test_patch_bot_events_register_stream_none(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': '',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-01-30 03:17:42 +01:00
|
|
|
default_events_register_stream = get_user_profile_by_email(
|
|
|
|
"hambot-bot@zulip.com").default_events_register_stream
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assertEqual(None, default_events_register_stream)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(None, bot['default_events_register_stream'])
|
|
|
|
|
|
|
|
def test_patch_bot_events_register_stream_not_found(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_events_register_stream': 'missing',
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
2017-01-30 03:17:42 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream name 'missing'")
|
2014-02-13 19:39:54 +01:00
|
|
|
|
|
|
|
def test_patch_bot_default_all_public_streams_true(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_all_public_streams': ujson.dumps(True),
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_events_register_stream = ujson.loads(result.content)['default_all_public_streams']
|
|
|
|
self.assertEqual(default_events_register_stream, True)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(bot['default_all_public_streams'], True)
|
|
|
|
|
|
|
|
def test_patch_bot_default_all_public_streams_false(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-02-13 19:39:54 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2014-02-13 19:39:54 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'default_all_public_streams': ujson.dumps(False),
|
|
|
|
}
|
|
|
|
result = self.client_patch("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
default_events_register_stream = ujson.loads(result.content)['default_all_public_streams']
|
|
|
|
self.assertEqual(default_events_register_stream, False)
|
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual(bot['default_all_public_streams'], False)
|
|
|
|
|
2013-10-21 18:54:57 +02:00
|
|
|
def test_patch_bot_via_post(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-10-21 18:54:57 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots", bot_info)
|
2013-10-21 18:54:57 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
'method': 'PATCH'
|
|
|
|
}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/bots/hambot-bot@zulip.com", bot_info)
|
2013-10-21 18:54:57 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
full_name = ujson.loads(result.content)['full_name']
|
|
|
|
self.assertEqual('Fred', full_name)
|
2013-07-16 22:25:34 +02:00
|
|
|
|
|
|
|
bot = self.get_bot()
|
|
|
|
self.assertEqual('Fred', bot['full_name'])
|
|
|
|
|
|
|
|
def test_patch_bogus_bot(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
"""Deleting a bogus bot will succeed silently."""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-07-16 22:25:34 +02:00
|
|
|
self.create_bot()
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
}
|
2013-08-01 19:35:39 +02:00
|
|
|
result = self.client_patch("/json/bots/nonexistent-bot@zulip.com", bot_info)
|
2013-07-16 22:25:34 +02:00
|
|
|
self.assert_json_error(result, 'No such user')
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class ChangeSettingsTest(ZulipTestCase):
|
2012-12-21 01:06:53 +01:00
|
|
|
|
|
|
|
def check_well_formed_change_settings_response(self, result):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2012-12-21 01:06:53 +01:00
|
|
|
self.assertIn("full_name", result)
|
|
|
|
|
2016-12-21 05:15:55 +01:00
|
|
|
# DEPRECATED, to be deleted after all uses of check_for_toggle_param
|
|
|
|
# are converted into check_for_toggle_param_patch.
|
2016-06-15 19:32:16 +02:00
|
|
|
def check_for_toggle_param(self, pattern, param):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: (str, str) -> None
|
2016-06-15 23:25:58 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2016-06-15 23:22:51 +02:00
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2016-07-28 00:30:22 +02:00
|
|
|
json_result = self.client_post(pattern,
|
2016-06-15 19:32:16 +02:00
|
|
|
{param: ujson.dumps(True)})
|
|
|
|
self.assert_json_success(json_result)
|
2016-06-15 23:57:34 +02:00
|
|
|
# refetch user_profile object to correctly handle caching
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2016-06-15 23:22:51 +02:00
|
|
|
self.assertEqual(getattr(user_profile, param), True)
|
2016-06-15 19:32:16 +02:00
|
|
|
|
2016-07-28 00:30:22 +02:00
|
|
|
json_result = self.client_post(pattern,
|
2016-06-15 19:32:16 +02:00
|
|
|
{param: ujson.dumps(False)})
|
|
|
|
self.assert_json_success(json_result)
|
2016-06-15 23:57:34 +02:00
|
|
|
# refetch user_profile object to correctly handle caching
|
2016-06-15 23:22:51 +02:00
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
|
|
|
self.assertEqual(getattr(user_profile, param), False)
|
2016-06-15 19:32:16 +02:00
|
|
|
|
2016-12-21 05:15:55 +01:00
|
|
|
# TODO: requires method consolidation, right now, there's no alternative
|
|
|
|
# for check_for_toggle_param for PATCH.
|
2016-12-24 11:56:07 +01:00
|
|
|
def check_for_toggle_param_patch(self, pattern, param):
|
2016-12-21 05:15:55 +01:00
|
|
|
# type: (str, str) -> None
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
|
|
|
json_result = self.client_patch(pattern,
|
2016-12-24 11:56:07 +01:00
|
|
|
{param: ujson.dumps(True)})
|
2016-12-21 05:15:55 +01:00
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2016-12-24 11:56:07 +01:00
|
|
|
self.assertEqual(getattr(user_profile, param), True)
|
2016-12-21 05:15:55 +01:00
|
|
|
|
|
|
|
json_result = self.client_patch(pattern,
|
2016-12-24 11:56:07 +01:00
|
|
|
{param: ujson.dumps(False)})
|
2016-12-21 05:15:55 +01:00
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
|
|
|
user_profile = get_user_profile_by_email("hamlet@zulip.com")
|
2016-12-24 11:56:07 +01:00
|
|
|
self.assertEqual(getattr(user_profile, param), False)
|
2016-12-21 05:15:55 +01:00
|
|
|
|
2012-12-21 16:59:08 +01:00
|
|
|
def test_successful_change_settings(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2012-12-21 01:06:53 +01:00
|
|
|
"""
|
|
|
|
A call to /json/settings/change with valid parameters changes the user's
|
|
|
|
settings correctly and returns correct values.
|
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2016-12-03 00:04:17 +01:00
|
|
|
json_result = self.client_post(
|
|
|
|
"/json/settings/change",
|
2016-07-23 22:01:47 +02:00
|
|
|
dict(
|
|
|
|
full_name='Foo Bar',
|
|
|
|
old_password=initial_password('hamlet@zulip.com'),
|
|
|
|
new_password='foobar1',
|
|
|
|
confirm_password='foobar1',
|
2016-12-03 00:04:17 +01:00
|
|
|
))
|
2012-12-21 01:06:53 +01:00
|
|
|
self.assert_json_success(json_result)
|
2013-06-18 23:55:55 +02:00
|
|
|
result = ujson.loads(json_result.content)
|
2012-12-21 01:06:53 +01:00
|
|
|
self.check_well_formed_change_settings_response(result)
|
2013-07-24 20:41:09 +02:00
|
|
|
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
|
2016-12-03 00:04:17 +01:00
|
|
|
full_name, "Foo Bar")
|
2016-07-28 00:30:22 +02:00
|
|
|
self.client_post('/accounts/logout/')
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com", "foobar1")
|
|
|
|
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
2012-12-21 01:06:53 +01:00
|
|
|
|
2016-09-16 01:52:32 +02:00
|
|
|
def test_illegal_name_changes(self):
|
|
|
|
# type: () -> None
|
|
|
|
email = 'hamlet@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
full_name = user.full_name
|
|
|
|
|
|
|
|
with self.settings(NAME_CHANGES_DISABLED=True):
|
|
|
|
json_result = self.client_post("/json/settings/change",
|
2016-12-03 00:04:17 +01:00
|
|
|
dict(full_name='Foo Bar'))
|
2016-09-16 01:52:32 +02:00
|
|
|
|
|
|
|
# We actually fail silently here, since this only happens if
|
|
|
|
# somebody is trying to game our API, and there's no reason to
|
|
|
|
# give them the courtesy of an error reason.
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(user.full_name, full_name)
|
|
|
|
|
|
|
|
# Now try a too-long name
|
|
|
|
json_result = self.client_post("/json/settings/change",
|
2016-12-03 00:04:17 +01:00
|
|
|
dict(full_name='x' * 1000))
|
2016-09-16 01:52:32 +02:00
|
|
|
self.assert_json_error(json_result, 'Name too long!')
|
|
|
|
|
2017-01-16 04:18:42 +01:00
|
|
|
def test_illegal_characters_in_name_changes(self):
|
|
|
|
# type: () -> None
|
|
|
|
email = 'hamlet@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
# Now try a name with invalid characters
|
|
|
|
json_result = self.client_post("/json/settings/change",
|
|
|
|
dict(full_name='Opheli*'))
|
|
|
|
self.assert_json_error(json_result, 'Invalid characters in name!')
|
|
|
|
|
2016-06-15 23:25:58 +02:00
|
|
|
# This is basically a don't-explode test.
|
2013-10-25 22:41:35 +02:00
|
|
|
def test_notify_settings(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-12-22 18:29:14 +01:00
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_desktop_notifications")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_stream_desktop_notifications")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_stream_sounds")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_sounds")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_offline_email_notifications")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_offline_push_notifications")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_online_push_notifications")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "enable_digest_emails")
|
2016-12-07 17:29:12 +01:00
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications", "pm_content_in_desktop_notifications")
|
2013-10-25 22:41:35 +02:00
|
|
|
|
2013-12-03 21:01:37 +01:00
|
|
|
def test_ui_settings(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-12-22 03:36:18 +01:00
|
|
|
self.check_for_toggle_param_patch("/json/settings/ui", "autoscroll_forever")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/ui", "default_desktop_notifications")
|
2014-01-16 22:48:50 +01:00
|
|
|
|
2016-06-15 19:57:01 +02:00
|
|
|
def test_toggling_left_side_userlist(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-12-24 11:44:41 +01:00
|
|
|
self.check_for_toggle_param_patch("/json/settings/display", "left_side_userlist")
|
2016-06-15 19:57:01 +02:00
|
|
|
|
2017-03-02 08:30:53 +01:00
|
|
|
def test_toggling_emoji_alt_code(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/display", "emoji_alt_code")
|
|
|
|
|
2016-06-15 20:01:24 +02:00
|
|
|
def test_time_setting(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-12-21 05:15:55 +01:00
|
|
|
self.check_for_toggle_param_patch("/json/settings/display", "twenty_four_hour_time")
|
2016-06-15 20:01:24 +02:00
|
|
|
|
2016-06-15 20:03:28 +02:00
|
|
|
def test_enter_sends_setting(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2016-06-15 20:03:28 +02:00
|
|
|
self.check_for_toggle_param('/json/users/me/enter-sends', "enter_sends")
|
|
|
|
|
2012-12-21 01:06:53 +01:00
|
|
|
def test_mismatching_passwords(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2012-12-21 01:06:53 +01:00
|
|
|
"""
|
|
|
|
new_password and confirm_password must match
|
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2016-12-03 00:04:17 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/settings/change",
|
2016-07-23 22:01:47 +02:00
|
|
|
dict(
|
|
|
|
new_password="mismatched_password",
|
|
|
|
confirm_password="not_the_same",
|
2016-12-03 00:04:17 +01:00
|
|
|
))
|
2012-12-21 01:06:53 +01:00
|
|
|
self.assert_json_error(result,
|
2016-12-03 00:04:17 +01:00
|
|
|
"New password must match confirmation password!")
|
2012-12-21 01:06:53 +01:00
|
|
|
|
|
|
|
def test_wrong_old_password(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2012-12-21 01:06:53 +01:00
|
|
|
"""
|
|
|
|
new_password and confirm_password must match
|
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2016-12-03 00:04:17 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/settings/change",
|
2016-07-23 22:01:47 +02:00
|
|
|
dict(
|
|
|
|
old_password='bad_password',
|
|
|
|
new_password="ignored",
|
|
|
|
confirm_password="ignored",
|
2016-12-03 00:04:17 +01:00
|
|
|
))
|
2012-12-21 01:06:53 +01:00
|
|
|
self.assert_json_error(result, "Wrong password!")
|
|
|
|
|
2016-07-23 22:22:25 +02:00
|
|
|
def test_changing_nothing_returns_error(self):
|
2016-07-23 22:07:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
"""
|
2016-07-23 22:22:25 +02:00
|
|
|
We need to supply at least one non-empty parameter
|
|
|
|
to this API, or it should fail. (Eventually, we should
|
|
|
|
probably use a patch interface for these changes.)
|
2016-07-23 22:07:22 +02:00
|
|
|
"""
|
|
|
|
self.login("hamlet@zulip.com")
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/settings/change",
|
2016-12-03 00:04:17 +01:00
|
|
|
dict(old_password='ignored',))
|
2016-07-23 22:22:25 +02:00
|
|
|
self.assert_json_error(result, "No new data supplied")
|
2016-07-23 22:07:22 +02:00
|
|
|
|
2016-07-31 10:02:42 +02:00
|
|
|
def test_change_default_language(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Test changing the default language of the user.
|
|
|
|
"""
|
|
|
|
email = "hamlet@zulip.com"
|
|
|
|
self.login(email)
|
|
|
|
german = "de"
|
|
|
|
data = dict(default_language=ujson.dumps(german))
|
2016-12-22 09:54:27 +01:00
|
|
|
result = self.client_patch("/json/settings/display", data)
|
2016-07-31 10:02:42 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(user_profile.default_language, german)
|
|
|
|
|
|
|
|
# Test to make sure invalid languages are not accepted
|
|
|
|
# and saved in the db.
|
|
|
|
invalid_lang = "invalid_lang"
|
|
|
|
data = dict(default_language=ujson.dumps(invalid_lang))
|
2016-12-22 09:54:27 +01:00
|
|
|
result = self.client_patch("/json/settings/display", data)
|
2016-07-31 10:02:42 +02:00
|
|
|
self.assert_json_error(result, "Invalid language '%s'" % (invalid_lang,))
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
self.assertNotEqual(user_profile.default_language, invalid_lang)
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class GetProfileTest(ZulipTestCase):
|
2013-01-22 20:07:51 +01:00
|
|
|
|
|
|
|
def common_update_pointer(self, email, pointer):
|
2016-12-04 18:38:56 +01:00
|
|
|
# type: (Text, int) -> None
|
2013-01-22 20:07:51 +01:00
|
|
|
self.login(email)
|
2016-12-31 08:54:00 +01:00
|
|
|
result = self.client_post("/json/users/me/pointer", {"pointer": pointer})
|
2013-01-22 20:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
def common_get_profile(self, email):
|
2016-12-04 18:38:56 +01:00
|
|
|
# type: (str) -> Dict[Text, Any]
|
2013-07-02 21:50:05 +02:00
|
|
|
user_profile = get_user_profile_by_email(email)
|
2013-07-02 18:13:26 +02:00
|
|
|
self.send_message(email, "Verona", Recipient.STREAM, "hello")
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/api/v1/users/me", **self.api_auth(email))
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2013-07-02 16:15:10 +02:00
|
|
|
max_id = most_recent_message(user_profile).id
|
2013-01-22 20:07:51 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-01-22 20:07:51 +01:00
|
|
|
|
|
|
|
self.assertIn("client_id", json)
|
|
|
|
self.assertIn("max_message_id", json)
|
|
|
|
self.assertIn("pointer", json)
|
|
|
|
|
2013-01-17 18:12:02 +01:00
|
|
|
self.assertEqual(json["max_message_id"], max_id)
|
2013-01-22 20:07:51 +01:00
|
|
|
return json
|
|
|
|
|
2016-09-14 02:14:45 +02:00
|
|
|
def test_get_pointer(self):
|
|
|
|
# type: () -> None
|
|
|
|
email = "hamlet@zulip.com"
|
|
|
|
self.login(email)
|
|
|
|
result = self.client_get("/json/users/me/pointer")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
self.assertIn("pointer", json)
|
|
|
|
|
2013-09-28 01:05:08 +02:00
|
|
|
def test_cache_behavior(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-09-28 01:05:08 +02:00
|
|
|
with queries_captured() as queries:
|
|
|
|
with simulated_empty_cache() as cache_queries:
|
|
|
|
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
|
2016-09-25 21:30:10 +02:00
|
|
|
self.assert_max_length(queries, 1)
|
|
|
|
self.assert_length(cache_queries, 1)
|
2013-09-28 01:05:08 +02:00
|
|
|
self.assertEqual(user_profile.email, 'hamlet@zulip.com')
|
|
|
|
|
2016-12-13 19:17:49 +01:00
|
|
|
def test_get_user_profile(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login('hamlet@zulip.com')
|
|
|
|
result = ujson.loads(self.client_get('/json/users/me').content)
|
|
|
|
self.assertEqual(result['short_name'], 'hamlet')
|
|
|
|
self.assertEqual(result['email'], 'hamlet@zulip.com')
|
|
|
|
self.assertEqual(result['full_name'], 'King Hamlet')
|
|
|
|
self.assertIn("user_id", result)
|
|
|
|
self.assertFalse(result['is_bot'])
|
|
|
|
self.assertFalse(result['is_admin'])
|
|
|
|
self.login('iago@zulip.com')
|
|
|
|
result = ujson.loads(self.client_get('/json/users/me').content)
|
|
|
|
self.assertEqual(result['short_name'], 'iago')
|
|
|
|
self.assertEqual(result['email'], 'iago@zulip.com')
|
|
|
|
self.assertEqual(result['full_name'], 'Iago')
|
|
|
|
self.assertFalse(result['is_bot'])
|
|
|
|
self.assertTrue(result['is_admin'])
|
|
|
|
|
2013-01-22 20:07:51 +01:00
|
|
|
def test_api_get_empty_profile(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-01-22 20:07:51 +01:00
|
|
|
"""
|
2016-04-02 20:07:28 +02:00
|
|
|
Ensure GET /users/me returns a max message id and returns successfully
|
2013-01-22 20:07:51 +01:00
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
json = self.common_get_profile("othello@zulip.com")
|
2013-01-17 18:12:02 +01:00
|
|
|
self.assertEqual(json["pointer"], -1)
|
2013-01-22 20:07:51 +01:00
|
|
|
|
|
|
|
def test_profile_with_pointer(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-01-22 20:07:51 +01:00
|
|
|
"""
|
2016-04-02 20:07:28 +02:00
|
|
|
Ensure GET /users/me returns a proper pointer id after the pointer is updated
|
2013-01-22 20:07:51 +01:00
|
|
|
"""
|
2013-08-19 21:05:23 +02:00
|
|
|
|
2013-08-28 21:29:55 +02:00
|
|
|
id1 = self.send_message("othello@zulip.com", "Verona", Recipient.STREAM)
|
|
|
|
id2 = self.send_message("othello@zulip.com", "Verona", Recipient.STREAM)
|
2013-08-19 21:05:23 +02:00
|
|
|
|
2013-07-24 20:41:09 +02:00
|
|
|
json = self.common_get_profile("hamlet@zulip.com")
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2013-08-19 21:05:23 +02:00
|
|
|
self.common_update_pointer("hamlet@zulip.com", id2)
|
2013-07-24 20:41:09 +02:00
|
|
|
json = self.common_get_profile("hamlet@zulip.com")
|
2013-08-19 21:05:23 +02:00
|
|
|
self.assertEqual(json["pointer"], id2)
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2013-08-19 21:05:23 +02:00
|
|
|
self.common_update_pointer("hamlet@zulip.com", id1)
|
2013-07-24 20:41:09 +02:00
|
|
|
json = self.common_get_profile("hamlet@zulip.com")
|
2013-08-19 21:05:23 +02:00
|
|
|
self.assertEqual(json["pointer"], id2) # pointer does not move backwards
|
|
|
|
|
2016-12-31 08:54:00 +01:00
|
|
|
result = self.client_post("/json/users/me/pointer", {"pointer": 99999999})
|
2013-08-19 21:05:23 +02:00
|
|
|
self.assert_json_error(result, "Invalid message ID")
|
2012-12-19 20:19:46 +01:00
|
|
|
|
2014-07-18 06:16:14 +02:00
|
|
|
def test_get_all_profiles_avatar_urls(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2014-07-18 06:16:14 +02:00
|
|
|
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/api/v1/users", **self.api_auth('hamlet@zulip.com'))
|
2014-07-18 06:16:14 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
|
|
|
|
for user in json['members']:
|
|
|
|
if user['email'] == 'hamlet@zulip.com':
|
|
|
|
self.assertEqual(
|
|
|
|
user['avatar_url'],
|
2017-02-16 22:35:57 +01:00
|
|
|
avatar_url(user_profile),
|
2014-07-18 06:16:14 +02:00
|
|
|
)
|
|
|
|
|
2017-01-06 18:56:36 +01:00
|
|
|
class AuthorsPageTest(ZulipTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# type: () -> None
|
2017-01-14 11:19:26 +01:00
|
|
|
""" Manual installation which did not execute `tools/provision`
|
2017-01-06 18:56:36 +01:00
|
|
|
would not have the `static/generated/github-contributors.json` fixture
|
|
|
|
file.
|
|
|
|
"""
|
2017-03-05 08:17:36 +01:00
|
|
|
# This block has unreliable test coverage due to the implicit
|
|
|
|
# caching here, so we exclude it from coverage.
|
2017-01-06 18:56:36 +01:00
|
|
|
if not os.path.exists(settings.CONTRIBUTORS_DATA):
|
|
|
|
# Copy the fixture file in `zerver/fixtures` to `static/generated`
|
|
|
|
update_script = os.path.join(os.path.dirname(__file__),
|
2017-03-05 08:17:36 +01:00
|
|
|
'../../tools/update-authors-json') # nocoverage
|
|
|
|
subprocess.check_call([update_script, '--use-fixture']) # nocoverage
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
def test_endpoint(self):
|
|
|
|
# type: () -> None
|
|
|
|
result = self.client_get('/authors/')
|
|
|
|
self.assert_in_success_response(
|
|
|
|
['Contributors', 'Statistic last Updated:', 'commits',
|
|
|
|
'@timabbott'],
|
|
|
|
result
|
|
|
|
)
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class MutedTopicsTests(ZulipTestCase):
|
2013-09-10 00:06:24 +02:00
|
|
|
def test_json_set(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2013-09-10 00:06:24 +02:00
|
|
|
email = 'hamlet@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
url = '/json/set_muted_topics'
|
|
|
|
data = {'muted_topics': '[["stream", "topic"]]'}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post(url, data)
|
2013-09-10 00:06:24 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(ujson.loads(user.muted_topics), [["stream", "topic"]])
|
|
|
|
|
|
|
|
url = '/json/set_muted_topics'
|
|
|
|
data = {'muted_topics': '[["stream2", "topic2"]]'}
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post(url, data)
|
2013-09-10 00:06:24 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(ujson.loads(user.muted_topics), [["stream2", "topic2"]])
|
|
|
|
|
2014-02-14 19:29:42 +01:00
|
|
|
class ExtractedRecipientsTest(TestCase):
|
|
|
|
def test_extract_recipients(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
|
|
|
|
2014-02-14 19:39:11 +01:00
|
|
|
# JSON list w/dups, empties, and trailing whitespace
|
2014-02-14 19:29:42 +01:00
|
|
|
s = ujson.dumps([' alice@zulip.com ', ' bob@zulip.com ', ' ', 'bob@zulip.com'])
|
2016-07-10 00:24:19 +02:00
|
|
|
self.assertEqual(sorted(extract_recipients(s)), ['alice@zulip.com', 'bob@zulip.com'])
|
2014-02-14 19:39:11 +01:00
|
|
|
|
|
|
|
# simple string with one name
|
2014-02-14 19:29:42 +01:00
|
|
|
s = 'alice@zulip.com '
|
2016-07-10 00:24:19 +02:00
|
|
|
self.assertEqual(extract_recipients(s), ['alice@zulip.com'])
|
2014-02-14 19:29:42 +01:00
|
|
|
|
2014-02-14 19:39:11 +01:00
|
|
|
# JSON-encoded string
|
|
|
|
s = '"alice@zulip.com"'
|
2016-07-10 00:24:19 +02:00
|
|
|
self.assertEqual(extract_recipients(s), ['alice@zulip.com'])
|
2014-02-14 19:39:11 +01:00
|
|
|
|
2014-02-14 19:56:55 +01:00
|
|
|
# bare comma-delimited string
|
|
|
|
s = 'bob@zulip.com, alice@zulip.com'
|
2016-07-10 00:24:19 +02:00
|
|
|
self.assertEqual(sorted(extract_recipients(s)), ['alice@zulip.com', 'bob@zulip.com'])
|
2014-02-14 19:56:55 +01:00
|
|
|
|
2014-02-14 19:39:11 +01:00
|
|
|
# JSON-encoded, comma-delimited string
|
|
|
|
s = '"bob@zulip.com,alice@zulip.com"'
|
2016-07-10 00:24:19 +02:00
|
|
|
self.assertEqual(sorted(extract_recipients(s)), ['alice@zulip.com', 'bob@zulip.com'])
|
2014-02-14 19:39:11 +01:00
|
|
|
|
2014-02-14 19:29:42 +01:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestMissedMessages(ZulipTestCase):
|
2016-06-22 10:36:22 +02:00
|
|
|
def normalize_string(self, s):
|
2016-12-04 18:38:56 +01:00
|
|
|
# type: (Text) -> Text
|
2016-06-22 10:36:22 +02:00
|
|
|
s = s.strip()
|
|
|
|
return re.sub(r'\s+', ' ', s)
|
|
|
|
|
2016-10-18 11:14:12 +02:00
|
|
|
def _get_tokens(self):
|
|
|
|
# type: () -> List[str]
|
|
|
|
return [str(random.getrandbits(32)) for _ in range(30)]
|
2014-07-15 21:03:51 +02:00
|
|
|
|
2016-10-18 11:14:12 +02:00
|
|
|
def _test_cases(self, tokens, msg_id, body, send_as_user):
|
|
|
|
# type: (List[str], int, str, bool) -> None
|
2016-06-22 10:36:22 +02:00
|
|
|
othello = get_user_profile_by_email('othello@zulip.com')
|
2014-07-15 21:03:51 +02:00
|
|
|
hamlet = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
handle_missedmessage_emails(hamlet.id, [{'message_id': msg_id}])
|
2016-10-18 11:14:12 +02:00
|
|
|
reply_to_addresses = [settings.EMAIL_GATEWAY_PATTERN % (u'mm' + t) for t in tokens]
|
2017-03-06 08:45:59 +01:00
|
|
|
msg = mail.outbox[0]
|
2016-06-22 10:36:22 +02:00
|
|
|
sender = 'Zulip <{}>'.format(settings.NOREPLY_EMAIL_ADDRESS)
|
2016-10-18 11:14:12 +02:00
|
|
|
from_email = sender
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2016-10-18 11:14:12 +02:00
|
|
|
if send_as_user:
|
|
|
|
from_email = '"%s" <%s>' % (othello.full_name, othello.email)
|
|
|
|
self.assertEqual(msg.extra_headers['Sender'], sender)
|
|
|
|
else:
|
|
|
|
self.assertNotIn("Sender", msg.extra_headers)
|
|
|
|
self.assertEqual(msg.from_email, from_email)
|
2016-06-22 10:36:22 +02:00
|
|
|
self.assertIn(msg.extra_headers['Reply-To'], reply_to_addresses)
|
2016-10-18 11:14:12 +02:00
|
|
|
self.assertIn(body, self.normalize_string(msg.body))
|
|
|
|
|
|
|
|
@patch('zerver.lib.email_mirror.generate_random_token')
|
|
|
|
def _extra_context_in_missed_stream_messages(self, send_as_user, mock_random_token):
|
|
|
|
# type: (bool, MagicMock) -> None
|
|
|
|
tokens = self._get_tokens()
|
|
|
|
mock_random_token.side_effect = tokens
|
|
|
|
|
|
|
|
for i in range(0, 11):
|
|
|
|
self.send_message("othello@zulip.com", "Denmark", Recipient.STREAM, str(i))
|
|
|
|
self.send_message("othello@zulip.com", "Denmark", Recipient.STREAM, '11', subject='test2')
|
|
|
|
msg_id = self.send_message("othello@zulip.com", "denmark", Recipient.STREAM, '@**hamlet**')
|
|
|
|
body = 'Denmark > test Othello, the Moor of Venice 1 2 3 4 5 6 7 8 9 10 @**hamlet**'
|
|
|
|
self._test_cases(tokens, msg_id, body, send_as_user)
|
2015-10-15 16:13:52 +02:00
|
|
|
|
2016-06-22 10:36:22 +02:00
|
|
|
@patch('zerver.lib.email_mirror.generate_random_token')
|
2016-10-18 11:14:12 +02:00
|
|
|
def _extra_context_in_personal_missed_stream_messages(self, send_as_user, mock_random_token):
|
|
|
|
# type: (bool, MagicMock) -> None
|
|
|
|
tokens = self._get_tokens()
|
2016-06-22 10:36:22 +02:00
|
|
|
mock_random_token.side_effect = tokens
|
|
|
|
|
|
|
|
msg_id = self.send_message("othello@zulip.com", "hamlet@zulip.com",
|
|
|
|
Recipient.PERSONAL,
|
|
|
|
'Extremely personal message!')
|
2016-10-18 11:14:12 +02:00
|
|
|
body = 'You and Othello, the Moor of Venice Extremely personal message!'
|
|
|
|
self._test_cases(tokens, msg_id, body, send_as_user)
|
2016-06-22 10:36:22 +02:00
|
|
|
|
|
|
|
@patch('zerver.lib.email_mirror.generate_random_token')
|
2016-10-18 11:14:12 +02:00
|
|
|
def _extra_context_in_huddle_missed_stream_messages(self, send_as_user, mock_random_token):
|
|
|
|
# type: (bool, MagicMock) -> None
|
|
|
|
tokens = self._get_tokens()
|
2016-06-22 10:36:22 +02:00
|
|
|
mock_random_token.side_effect = tokens
|
|
|
|
|
|
|
|
msg_id = self.send_message("othello@zulip.com",
|
|
|
|
["hamlet@zulip.com", "iago@zulip.com"],
|
|
|
|
Recipient.PERSONAL,
|
|
|
|
'Group personal message!')
|
|
|
|
|
|
|
|
body = ('You and Iago, Othello, the Moor of Venice Othello,'
|
|
|
|
' the Moor of Venice Group personal message')
|
2016-10-18 11:14:12 +02:00
|
|
|
self._test_cases(tokens, msg_id, body, send_as_user)
|
|
|
|
|
|
|
|
@override_settings(SEND_MISSED_MESSAGE_EMAILS_AS_USER=True)
|
|
|
|
def test_extra_context_in_missed_stream_messages_as_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._extra_context_in_missed_stream_messages(True)
|
|
|
|
|
|
|
|
def test_extra_context_in_missed_stream_messages(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._extra_context_in_missed_stream_messages(False)
|
|
|
|
|
|
|
|
@override_settings(SEND_MISSED_MESSAGE_EMAILS_AS_USER=True)
|
|
|
|
def test_extra_context_in_personal_missed_stream_messages_as_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._extra_context_in_personal_missed_stream_messages(True)
|
|
|
|
|
|
|
|
def test_extra_context_in_personal_missed_stream_messages(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._extra_context_in_personal_missed_stream_messages(False)
|
|
|
|
|
|
|
|
@override_settings(SEND_MISSED_MESSAGE_EMAILS_AS_USER=True)
|
|
|
|
def test_extra_context_in_huddle_missed_stream_messages_as_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._extra_context_in_huddle_missed_stream_messages(True)
|
|
|
|
|
|
|
|
def test_extra_context_in_huddle_missed_stream_messages(self):
|
|
|
|
# type: () -> None
|
|
|
|
self._extra_context_in_huddle_missed_stream_messages(False)
|
2016-06-22 10:36:22 +02:00
|
|
|
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestOpenRealms(ZulipTestCase):
|
2015-10-15 16:13:52 +02:00
|
|
|
def test_open_realm_logic(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2017-01-27 00:06:55 +01:00
|
|
|
realm = get_realm('simple')
|
|
|
|
do_deactivate_realm(realm)
|
|
|
|
|
2017-03-04 09:19:37 +01:00
|
|
|
mit_realm = get_realm("zephyr")
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(get_unique_open_realm(), None)
|
2015-10-15 16:13:52 +02:00
|
|
|
mit_realm.restricted_to_domain = False
|
|
|
|
mit_realm.save()
|
2016-12-24 02:10:26 +01:00
|
|
|
self.assertTrue(completely_open(mit_realm))
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(get_unique_open_realm(), None)
|
2017-01-04 09:20:23 +01:00
|
|
|
with self.settings(SYSTEM_ONLY_REALMS={"zulip"}):
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(get_unique_open_realm(), mit_realm)
|
2015-10-15 16:13:52 +02:00
|
|
|
mit_realm.restricted_to_domain = True
|
|
|
|
mit_realm.save()
|
2016-10-10 12:51:59 +02:00
|
|
|
|
|
|
|
class TestLoginPage(ZulipTestCase):
|
2017-01-13 07:52:15 +01:00
|
|
|
def test_login_page_wrong_subdomain_error(self):
|
2016-10-10 12:51:59 +02:00
|
|
|
# type: () -> None
|
|
|
|
result = self.client_get("/login/?subdomain=1")
|
|
|
|
self.assertIn(WRONG_SUBDOMAIN_ERROR, result.content.decode('utf8'))
|
2016-12-20 10:41:46 +01:00
|
|
|
|
2017-01-10 10:44:56 +01:00
|
|
|
@patch('django.http.HttpRequest.get_host')
|
|
|
|
def test_login_page_redirects_for_root_alias(self, mock_get_host):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
mock_get_host.return_value = 'www.testserver'
|
|
|
|
with self.settings(REALMS_HAVE_SUBDOMAINS=True,
|
|
|
|
ROOT_SUBDOMAIN_ALIASES=['www']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual(result.url, '/find_my_team/')
|
|
|
|
|
|
|
|
@patch('django.http.HttpRequest.get_host')
|
|
|
|
def test_login_page_redirects_for_root_domain(self, mock_get_host):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
mock_get_host.return_value = 'testserver'
|
|
|
|
with self.settings(REALMS_HAVE_SUBDOMAINS=True,
|
|
|
|
ROOT_SUBDOMAIN_ALIASES=['www']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual(result.url, '/find_my_team/')
|
|
|
|
|
|
|
|
mock_get_host.return_value = 'www.testserver.com'
|
|
|
|
with self.settings(REALMS_HAVE_SUBDOMAINS=True,
|
|
|
|
EXTERNAL_HOST='www.testserver.com',
|
|
|
|
ROOT_SUBDOMAIN_ALIASES=['test']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual(result.url, '/find_my_team/')
|
|
|
|
|
|
|
|
@patch('django.http.HttpRequest.get_host')
|
|
|
|
def test_login_page_works_without_subdomains(self, mock_get_host):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
mock_get_host.return_value = 'www.testserver'
|
|
|
|
with self.settings(ROOT_SUBDOMAIN_ALIASES=['www']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
mock_get_host.return_value = 'testserver'
|
|
|
|
with self.settings(ROOT_SUBDOMAIN_ALIASES=['www']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2017-01-06 04:48:12 +01:00
|
|
|
class TestFindMyTeam(ZulipTestCase):
|
2016-12-20 10:41:46 +01:00
|
|
|
def test_template(self):
|
|
|
|
# type: () -> None
|
2017-01-06 03:31:48 +01:00
|
|
|
result = self.client_get('/find_my_team/')
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertIn("Find your team", result.content.decode('utf8'))
|
|
|
|
|
|
|
|
def test_result(self):
|
|
|
|
# type: () -> None
|
2017-01-06 03:31:48 +01:00
|
|
|
url = '/find_my_team/?emails=iago@zulip.com,cordelia@zulip.com'
|
2016-12-20 10:41:46 +01:00
|
|
|
result = self.client_get(url)
|
|
|
|
content = result.content.decode('utf8')
|
2017-01-05 03:42:53 +01:00
|
|
|
self.assertIn("Emails sent! You will only receive emails", content)
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertIn("iago@zulip.com", content)
|
|
|
|
self.assertIn("cordelia@zulip.com", content)
|
|
|
|
|
|
|
|
def test_find_team_zero_emails(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': ''}
|
2017-01-06 03:31:48 +01:00
|
|
|
result = self.client_post('/find_my_team/', data)
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertIn('This field is required', result.content.decode('utf8'))
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_find_team_one_email(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': 'hamlet@zulip.com'}
|
2017-01-06 03:31:48 +01:00
|
|
|
result = self.client_post('/find_my_team/', data)
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-01-06 03:31:48 +01:00
|
|
|
self.assertEqual(result.url, '/find_my_team/?emails=hamlet%40zulip.com')
|
2016-12-20 10:41:46 +01:00
|
|
|
|
|
|
|
def test_find_team_multiple_emails(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': 'hamlet@zulip.com,iago@zulip.com'}
|
2017-01-06 03:31:48 +01:00
|
|
|
result = self.client_post('/find_my_team/', data)
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-01-06 03:31:48 +01:00
|
|
|
expected = '/find_my_team/?emails=hamlet%40zulip.com%2Ciago%40zulip.com'
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertEqual(result.url, expected)
|
|
|
|
|
|
|
|
def test_find_team_more_than_ten_emails(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': ','.join(['hamlet-{}@zulip.com'.format(i) for i in range(11)])}
|
2017-01-06 03:31:48 +01:00
|
|
|
result = self.client_post('/find_my_team/', data)
|
2016-12-20 10:41:46 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assertIn("Please enter at most 10", result.content.decode('utf8'))
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
class UtilsUnitTest(TestCase):
|
|
|
|
def test_split_by(self):
|
|
|
|
# type: () -> None
|
|
|
|
flat_list = [1, 2, 3, 4, 5, 6, 7]
|
|
|
|
expected_result = [[1, 2], [3, 4], [5, 6], [7, None]]
|
|
|
|
self.assertEqual(split_by(flat_list, 2, None), expected_result)
|