2013-03-20 22:08:48 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2013-03-20 22:08:48 +01:00
|
|
|
|
2012-08-31 20:11:15 +02:00
|
|
|
from django.test import TestCase
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2014-01-27 22:53:36 +01:00
|
|
|
from zerver.lib.test_helpers import (
|
2014-01-31 21:08:40 +01:00
|
|
|
queries_captured, simulated_empty_cache,
|
2014-01-31 16:44:45 +01:00
|
|
|
simulated_queue_client, tornado_redirected_to_list, AuthedTestCase,
|
2014-01-31 23:23:39 +01:00
|
|
|
most_recent_usermessage, most_recent_message,
|
2014-01-27 22:53:36 +01:00
|
|
|
)
|
|
|
|
|
2014-01-31 23:13:58 +01:00
|
|
|
from zerver.models import UserProfile, Recipient, \
|
2014-01-31 21:08:40 +01:00
|
|
|
Realm, Client, UserActivity, \
|
2014-01-31 16:44:45 +01:00
|
|
|
get_user_profile_by_email, split_email_to_domain, get_realm, \
|
2014-01-31 23:23:39 +01:00
|
|
|
get_client
|
2014-02-07 22:47:30 +01:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.initial_password import initial_password
|
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-01-31 23:23:39 +01:00
|
|
|
do_change_is_admin, \
|
2014-01-31 16:44:45 +01:00
|
|
|
do_set_realm_name, get_realm_name, do_deactivate_realm
|
2013-09-03 22:41:17 +02:00
|
|
|
from zerver.lib.alert_words import alert_words_in_realm, user_alert_words, \
|
|
|
|
add_user_alert_words, remove_user_alert_words
|
2013-12-26 15:01:46 +01:00
|
|
|
from zerver.middleware import is_slow_query
|
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
|
2013-09-20 18:36:40 +02:00
|
|
|
import datetime
|
2013-06-19 20:08:48 +02:00
|
|
|
import os
|
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
|
2013-03-14 22:12:25 +01:00
|
|
|
|
2013-02-12 21:04:30 +01:00
|
|
|
def bail(msg):
|
|
|
|
print '\nERROR: %s\n' % (msg,)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-01-10 19:41:49 +01:00
|
|
|
try:
|
|
|
|
settings.TEST_SUITE
|
|
|
|
except:
|
2013-08-06 22:51:47 +02:00
|
|
|
bail('Test suite only runs correctly with --settings=zproject.test_settings')
|
2013-02-12 21:04:30 +01:00
|
|
|
|
2013-03-28 16:27:24 +01:00
|
|
|
# Even though we don't use pygments directly in this file, we need
|
|
|
|
# this import.
|
2013-02-12 21:04:30 +01:00
|
|
|
try:
|
|
|
|
import pygments
|
|
|
|
except ImportError:
|
|
|
|
bail('The Pygments library is required to run the backend test suite.')
|
2012-09-27 19:58:42 +02:00
|
|
|
|
2014-01-14 20:38:45 +01:00
|
|
|
def find_dict(lst, k, v):
|
|
|
|
for dct in lst:
|
|
|
|
if dct[k] == v:
|
|
|
|
return dct
|
|
|
|
raise Exception('Cannot find element in list where key %s == %s' % (k, v))
|
|
|
|
|
2013-12-26 15:01:46 +01:00
|
|
|
class SlowQueryTest(TestCase):
|
|
|
|
def test_is_slow_query(self):
|
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
|
|
|
|
2014-01-28 17:29:00 +01:00
|
|
|
class RealmTest(AuthedTestCase):
|
2014-01-28 15:11:10 +01:00
|
|
|
def assert_user_profile_cache_gets_new_name(self, email, new_realm_name):
|
|
|
|
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):
|
2014-01-28 15:11:10 +01:00
|
|
|
# The main complicated thing about setting realm names is fighting the
|
|
|
|
# cache, and we start by populating the cache for Hamlet, and we end
|
|
|
|
# by checking the cache to ensure that the new value is there.
|
|
|
|
get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
realm = Realm.objects.get(domain='zulip.com')
|
|
|
|
new_name = 'Zed You Elle Eye Pea'
|
|
|
|
do_set_realm_name(realm, new_name)
|
|
|
|
self.assertEqual(get_realm_name(realm.domain), new_name)
|
|
|
|
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):
|
|
|
|
realm = Realm.objects.get(domain='zulip.com')
|
|
|
|
new_name = 'Puliz'
|
|
|
|
events = []
|
|
|
|
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,
|
|
|
|
))
|
|
|
|
|
2014-01-28 23:22:50 +01:00
|
|
|
def test_realm_name_api(self):
|
|
|
|
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)
|
|
|
|
|
|
|
|
req = dict(name=ujson.dumps(new_name))
|
|
|
|
result = self.client_patch('/json/realm', req)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
realm = get_realm('zulip.com')
|
|
|
|
self.assertEqual(realm.name, new_name)
|
|
|
|
|
2014-01-28 23:29:09 +01:00
|
|
|
def test_admin_restrictions_for_changing_realm_name(self):
|
|
|
|
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):
|
|
|
|
# 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().
|
|
|
|
get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
realm = Realm.objects.get(domain='zulip.com')
|
|
|
|
do_deactivate_realm(realm)
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertTrue(user.realm.deactivated)
|
|
|
|
|
2014-01-14 16:19:26 +01:00
|
|
|
class PermissionTest(AuthedTestCase):
|
2013-11-02 15:36:17 +01:00
|
|
|
def test_get_admin_users(self):
|
|
|
|
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
|
|
|
|
2014-01-14 16:19:26 +01:00
|
|
|
def test_admin_api(self):
|
|
|
|
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
|
|
|
|
result = self.client.get('/json/users')
|
|
|
|
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
|
|
|
|
|
|
|
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.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
|
|
|
|
|
|
|
# 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')
|
|
|
|
|
2013-10-17 17:48:19 +02:00
|
|
|
class WorkerTest(TestCase):
|
|
|
|
class FakeClient:
|
|
|
|
def __init__(self):
|
|
|
|
self.consumers = {}
|
|
|
|
self.queue = []
|
|
|
|
|
|
|
|
def register_json_consumer(self, queue_name, callback):
|
|
|
|
self.consumers[queue_name] = callback
|
|
|
|
|
|
|
|
def start_consuming(self):
|
|
|
|
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):
|
|
|
|
fake_client = self.FakeClient()
|
|
|
|
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
UserActivity.objects.filter(
|
|
|
|
user_profile = user.id,
|
2013-12-03 20:31:47 +01:00
|
|
|
client = get_client('ios')
|
2013-10-17 17:48:19 +02:00
|
|
|
).delete()
|
|
|
|
|
|
|
|
data = dict(
|
|
|
|
user_profile_id = user.id,
|
2013-12-03 20:31:47 +01:00
|
|
|
client = 'ios',
|
2013-10-17 17:48:19 +02:00
|
|
|
time = time.time(),
|
|
|
|
query = 'send_message'
|
|
|
|
)
|
|
|
|
fake_client.queue.append(('user_activity', data))
|
|
|
|
|
|
|
|
with simulated_queue_client(lambda: fake_client):
|
|
|
|
worker = queue_processors.UserActivityWorker()
|
|
|
|
worker.start()
|
|
|
|
activity_records = UserActivity.objects.filter(
|
|
|
|
user_profile = user.id,
|
2013-12-03 20:31:47 +01:00
|
|
|
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):
|
|
|
|
processed = []
|
|
|
|
|
|
|
|
@queue_processors.assign_queue('flake')
|
|
|
|
class FlakyWorker(queue_processors.QueueProcessingWorker):
|
2013-10-30 22:03:50 +01:00
|
|
|
def consume(self, data):
|
2013-10-29 20:03:42 +01:00
|
|
|
if data == 'freak out':
|
|
|
|
raise Exception('Freaking out!')
|
|
|
|
processed.append(data)
|
|
|
|
|
|
|
|
def _log_problem(self):
|
|
|
|
# keep the tests quiet
|
|
|
|
pass
|
|
|
|
|
|
|
|
fake_client = self.FakeClient()
|
|
|
|
for msg in ['good', 'fine', 'freak out', 'back to normal']:
|
|
|
|
fake_client.queue.append(('flake', msg))
|
|
|
|
|
|
|
|
fn = os.path.join(settings.QUEUE_ERROR_DIR, 'flake.errors')
|
|
|
|
try:
|
|
|
|
os.remove(fn)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
with simulated_queue_client(lambda: fake_client):
|
|
|
|
worker = FlakyWorker()
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
self.assertEqual(processed, ['good', 'fine', 'back to normal'])
|
|
|
|
line = open(fn).readline().strip()
|
|
|
|
event = ujson.loads(line.split('\t')[1])
|
|
|
|
self.assertEqual(event, 'freak out')
|
|
|
|
|
2013-09-30 22:52:04 +02:00
|
|
|
class ActivityTest(AuthedTestCase):
|
|
|
|
def test_activity(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
client, _ = Client.objects.get_or_create(name='website')
|
|
|
|
query = '/json/update_pointer'
|
|
|
|
last_visit = datetime.datetime.now()
|
|
|
|
count=150
|
|
|
|
for user_profile in UserProfile.objects.all():
|
|
|
|
UserActivity.objects.get_or_create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
client=client,
|
|
|
|
query=query,
|
|
|
|
count=count,
|
|
|
|
last_visit=last_visit
|
|
|
|
)
|
|
|
|
with queries_captured() as queries:
|
|
|
|
self.client.get('/activity')
|
2013-10-22 18:05:12 +02:00
|
|
|
|
2014-01-27 20:46:38 +01:00
|
|
|
self.assert_length(queries, 12)
|
2013-09-30 22:52:04 +02:00
|
|
|
|
2013-10-20 21:10:03 +02:00
|
|
|
class UserProfileTest(TestCase):
|
|
|
|
def test_get_emails_from_user_ids(self):
|
|
|
|
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')
|
|
|
|
|
2013-08-08 16:49:32 +02:00
|
|
|
class UserChangesTest(AuthedTestCase):
|
|
|
|
def test_update_api_key(self):
|
|
|
|
email = "hamlet@zulip.com"
|
|
|
|
self.login(email)
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
old_api_key = user.api_key
|
|
|
|
result = self.client.post('/json/users/me/api_key/regenerate')
|
|
|
|
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)
|
|
|
|
|
2013-11-15 19:39:03 +01:00
|
|
|
class ActivateTest(AuthedTestCase):
|
2013-11-15 18:57:44 +01:00
|
|
|
def test_basics(self):
|
|
|
|
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):
|
|
|
|
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)
|
|
|
|
|
|
|
|
result = self.client.post('/json/users/hamlet@zulip.com/reactivate')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
user = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
2013-07-03 21:34:32 +02:00
|
|
|
class BotTest(AuthedTestCase):
|
|
|
|
def assert_num_bots_equal(self, count):
|
|
|
|
result = self.client.post("/json/get_bots")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
self.assertEqual(count, len(json['bots']))
|
|
|
|
|
|
|
|
def create_bot(self):
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client.post("/json/create_bot", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2013-07-03 21:54:10 +02:00
|
|
|
def deactivate_bot(self):
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_delete("/json/users/hambot-bot@zulip.com")
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2013-07-03 21:34:32 +02:00
|
|
|
def test_add_bot(self):
|
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)
|
|
|
|
self.create_bot()
|
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
2013-07-03 21:54:10 +02:00
|
|
|
def test_deactivate_bot(self):
|
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):
|
|
|
|
# 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)
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_delete("/json/users/bogus-bot@zulip.com")
|
2013-07-08 23:34:43 +02:00
|
|
|
self.assert_json_error(result, 'No such user')
|
2013-07-03 21:54:10 +02:00
|
|
|
self.assert_num_bots_equal(1)
|
|
|
|
|
|
|
|
def test_bot_deactivation_attacks(self):
|
|
|
|
# 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
|
|
|
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_delete("/json/users/hamlet@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
|
|
|
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_delete("/json/users/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)
|
|
|
|
|
2013-07-19 17:45:06 +02:00
|
|
|
def test_bot_permissions(self):
|
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
|
|
|
|
2013-07-24 20:41:09 +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):
|
|
|
|
result = self.client.post("/json/get_bots")
|
|
|
|
bots = ujson.loads(result.content)['bots']
|
|
|
|
return bots[0]
|
|
|
|
|
2013-07-19 17:45:06 +02:00
|
|
|
def test_update_api_key(self):
|
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']
|
2013-07-24 20:41:09 +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'])
|
|
|
|
|
2013-07-16 22:25:34 +02:00
|
|
|
def test_patch_bot_full_name(self):
|
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',
|
|
|
|
}
|
|
|
|
result = self.client.post("/json/create_bot", bot_info)
|
|
|
|
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'])
|
|
|
|
|
|
|
|
def test_patch_bot_via_post(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'The Bot of Hamlet',
|
|
|
|
'short_name': 'hambot',
|
|
|
|
}
|
|
|
|
result = self.client.post("/json/create_bot", bot_info)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
bot_info = {
|
|
|
|
'full_name': 'Fred',
|
|
|
|
'method': 'PATCH'
|
|
|
|
}
|
|
|
|
result = self.client.post("/json/bots/hambot-bot@zulip.com", bot_info)
|
|
|
|
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):
|
|
|
|
# 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)
|
|
|
|
|
2012-12-21 01:06:53 +01:00
|
|
|
class ChangeSettingsTest(AuthedTestCase):
|
|
|
|
|
|
|
|
def post_with_params(self, modified_params):
|
|
|
|
post_params = {"full_name": "Foo Bar",
|
2013-07-24 20:41:09 +02:00
|
|
|
"old_password": initial_password("hamlet@zulip.com"),
|
2012-12-21 01:06:53 +01:00
|
|
|
"new_password": "foobar1", "confirm_password": "foobar1",
|
2013-10-25 22:41:35 +02:00
|
|
|
}
|
2012-12-21 01:06:53 +01:00
|
|
|
post_params.update(modified_params)
|
|
|
|
return self.client.post("/json/settings/change", dict(post_params))
|
|
|
|
|
|
|
|
def check_well_formed_change_settings_response(self, result):
|
|
|
|
self.assertIn("full_name", result)
|
|
|
|
|
2012-12-21 16:59:08 +01:00
|
|
|
def test_successful_change_settings(self):
|
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")
|
2012-12-21 01:06:53 +01:00
|
|
|
json_result = self.post_with_params({})
|
|
|
|
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").
|
2012-12-21 01:06:53 +01:00
|
|
|
full_name, "Foo Bar")
|
|
|
|
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')
|
2013-03-29 17:39:53 +01:00
|
|
|
self.assertEqual(self.client.session['_auth_user_id'], user_profile.id)
|
2012-12-21 01:06:53 +01:00
|
|
|
|
2013-10-25 22:41:35 +02:00
|
|
|
def test_notify_settings(self):
|
|
|
|
# This is basically a don't-explode test.
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
json_result = self.client.post("/json/notify_settings/change", {})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
|
|
|
|
enable_desktop_notifications, False)
|
|
|
|
|
2013-12-03 21:01:37 +01:00
|
|
|
def test_ui_settings(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
2014-01-16 22:48:50 +01:00
|
|
|
json_result = self.client.post("/json/ui_settings/change", {"autoscroll_forever": "on"})
|
2013-12-03 21:01:37 +01:00
|
|
|
self.assert_json_success(json_result)
|
|
|
|
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
|
|
|
|
enable_desktop_notifications, True)
|
|
|
|
|
|
|
|
json_result = self.client.post("/json/ui_settings/change", {})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
|
|
|
|
autoscroll_forever, False)
|
|
|
|
|
2014-01-16 22:48:50 +01:00
|
|
|
json_result = self.client.post("/json/ui_settings/change", {"default_desktop_notifications": "on"})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
|
|
|
|
default_desktop_notifications, True)
|
|
|
|
|
|
|
|
json_result = self.client.post("/json/ui_settings/change", {})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").
|
|
|
|
default_desktop_notifications, False)
|
|
|
|
|
2012-12-21 16:59:08 +01:00
|
|
|
def test_missing_params(self):
|
|
|
|
"""
|
2013-11-04 23:42:31 +01:00
|
|
|
full_name is a required POST parameter for json_change_settings.
|
|
|
|
(enable_desktop_notifications is false by default, and password is
|
|
|
|
only required if you are changing it)
|
2012-12-21 16:59:08 +01:00
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-11-04 23:42:31 +01:00
|
|
|
|
|
|
|
result = self.client.post("/json/settings/change", {})
|
|
|
|
self.assert_json_error(result,
|
|
|
|
"Missing '%s' argument" % ("full_name",))
|
2012-12-21 16:59:08 +01:00
|
|
|
|
2012-12-21 01:06:53 +01:00
|
|
|
def test_mismatching_passwords(self):
|
|
|
|
"""
|
|
|
|
new_password and confirm_password must match
|
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2012-12-21 01:06:53 +01:00
|
|
|
result = self.post_with_params({"new_password": "mismatched_password"})
|
|
|
|
self.assert_json_error(result,
|
|
|
|
"New password must match confirmation password!")
|
|
|
|
|
|
|
|
def test_wrong_old_password(self):
|
|
|
|
"""
|
|
|
|
new_password and confirm_password must match
|
|
|
|
"""
|
2013-07-24 20:41:09 +02:00
|
|
|
self.login("hamlet@zulip.com")
|
2012-12-21 01:06:53 +01:00
|
|
|
result = self.post_with_params({"old_password": "bad_password"})
|
|
|
|
self.assert_json_error(result, "Wrong password!")
|
|
|
|
|
2013-01-22 20:07:51 +01:00
|
|
|
class GetProfileTest(AuthedTestCase):
|
|
|
|
|
|
|
|
def common_update_pointer(self, email, pointer):
|
|
|
|
self.login(email)
|
2013-08-19 21:05:23 +02:00
|
|
|
result = self.client.post("/json/update_pointer", {"pointer": pointer})
|
2013-01-22 20:07:51 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
def common_get_profile(self, email):
|
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
|
|
|
|
2013-12-11 20:41:44 +01: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
|
|
|
|
|
2013-09-28 01:05:08 +02:00
|
|
|
def test_cache_behavior(self):
|
|
|
|
with queries_captured() as queries:
|
|
|
|
with simulated_empty_cache() as cache_queries:
|
|
|
|
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
|
|
|
|
2014-01-27 20:46:38 +01:00
|
|
|
self.assert_length(queries, 1)
|
|
|
|
self.assert_length(cache_queries, 1, exact=True)
|
2013-09-28 01:05:08 +02:00
|
|
|
self.assertEqual(user_profile.email, 'hamlet@zulip.com')
|
|
|
|
|
2013-01-22 20:07:51 +01:00
|
|
|
def test_api_get_empty_profile(self):
|
|
|
|
"""
|
|
|
|
Ensure get_profile returns a max message id and returns successfully
|
|
|
|
"""
|
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):
|
|
|
|
"""
|
|
|
|
Ensure get_profile returns a proper pointer id after the pointer is updated
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
|
result = self.client.post("/json/update_pointer", {"pointer": 99999999})
|
|
|
|
self.assert_json_error(result, "Invalid message ID")
|
2012-12-19 20:19:46 +01:00
|
|
|
|
2013-02-11 17:23:01 +01:00
|
|
|
class UserPresenceTests(AuthedTestCase):
|
|
|
|
def test_get_empty(self):
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
result = self.client.post("/json/get_active_statuses")
|
2013-02-11 17:23:01 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-02-11 17:23:01 +01:00
|
|
|
for email, presence in json['presences'].items():
|
|
|
|
self.assertEqual(presence, {})
|
|
|
|
|
|
|
|
def test_set_idle(self):
|
2013-07-24 20:41:09 +02:00
|
|
|
email = "hamlet@zulip.com"
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login(email)
|
2013-02-11 17:23:01 +01:00
|
|
|
client = 'website'
|
|
|
|
|
|
|
|
def test_result(result):
|
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'idle')
|
|
|
|
self.assertIn('timestamp', json['presences'][email][client])
|
|
|
|
self.assertIsInstance(json['presences'][email][client]['timestamp'], int)
|
2013-07-24 20:41:09 +02:00
|
|
|
self.assertEqual(json['presences'].keys(), ['hamlet@zulip.com'])
|
2013-02-11 17:23:01 +01:00
|
|
|
return json['presences'][email][client]['timestamp']
|
|
|
|
|
2013-12-19 20:17:13 +01:00
|
|
|
result = self.client.post("/json/update_active_status", {'status': 'idle'})
|
2013-02-11 17:23:01 +01:00
|
|
|
test_result(result)
|
|
|
|
|
2013-12-19 20:17:13 +01:00
|
|
|
result = self.client.post("/json/get_active_statuses", {})
|
2013-02-11 17:23:01 +01:00
|
|
|
timestamp = test_result(result)
|
|
|
|
|
2013-07-24 20:41:09 +02:00
|
|
|
email = "othello@zulip.com"
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login(email)
|
|
|
|
self.client.post("/json/update_active_status", {'status': 'idle'})
|
|
|
|
result = self.client.post("/json/get_active_statuses", {})
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'idle')
|
2013-07-24 20:41:09 +02:00
|
|
|
self.assertEqual(json['presences']['hamlet@zulip.com'][client]['status'], 'idle')
|
|
|
|
self.assertEqual(json['presences'].keys(), ['hamlet@zulip.com', 'othello@zulip.com'])
|
2013-02-11 17:23:01 +01:00
|
|
|
newer_timestamp = json['presences'][email][client]['timestamp']
|
|
|
|
self.assertGreaterEqual(newer_timestamp, timestamp)
|
|
|
|
|
|
|
|
def test_set_active(self):
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
2013-02-11 17:23:01 +01:00
|
|
|
client = 'website'
|
|
|
|
|
2013-12-19 20:17:13 +01:00
|
|
|
self.client.post("/json/update_active_status", {'status': 'idle'})
|
|
|
|
result = self.client.post("/json/get_active_statuses", {})
|
2013-02-11 17:23:01 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-12-19 20:17:13 +01:00
|
|
|
self.assertEqual(json['presences']["hamlet@zulip.com"][client]['status'], 'idle')
|
2013-02-11 17:23:01 +01:00
|
|
|
|
2013-07-24 20:41:09 +02:00
|
|
|
email = "othello@zulip.com"
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login("othello@zulip.com")
|
|
|
|
self.client.post("/json/update_active_status", {'status': 'idle'})
|
|
|
|
result = self.client.post("/json/get_active_statuses", {})
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'idle')
|
2013-07-24 20:41:09 +02:00
|
|
|
self.assertEqual(json['presences']['hamlet@zulip.com'][client]['status'], 'idle')
|
2013-02-11 17:23:01 +01:00
|
|
|
|
2013-12-19 20:17:13 +01:00
|
|
|
self.client.post("/json/update_active_status", {'status': 'active'})
|
|
|
|
result = self.client.post("/json/get_active_statuses", {})
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assertEqual(json['presences'][email][client]['status'], 'active')
|
2013-07-24 20:41:09 +02:00
|
|
|
self.assertEqual(json['presences']['hamlet@zulip.com'][client]['status'], 'idle')
|
2013-02-11 17:23:01 +01:00
|
|
|
|
2013-02-12 18:42:14 +01:00
|
|
|
def test_no_mit(self):
|
|
|
|
# MIT never gets a list of users
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login("espuser@mit.edu")
|
|
|
|
result = self.client.post("/json/update_active_status", {'status': 'idle'})
|
2013-02-12 18:42:14 +01:00
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-02-12 18:42:14 +01:00
|
|
|
self.assertEqual(json['presences'], {})
|
|
|
|
|
2013-02-11 17:23:01 +01:00
|
|
|
def test_same_realm(self):
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login("espuser@mit.edu")
|
|
|
|
self.client.post("/json/update_active_status", {'status': 'idle'})
|
2013-02-11 17:23:01 +01:00
|
|
|
result = self.client.post("/accounts/logout/")
|
|
|
|
|
2013-07-24 20:41:09 +02:00
|
|
|
# Ensure we don't see hamlet@zulip.com information leakage
|
2013-12-19 20:17:13 +01:00
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
result = self.client.post("/json/update_active_status", {'status': 'idle'})
|
2013-02-11 17:23:01 +01:00
|
|
|
self.assert_json_success(result)
|
2013-06-18 23:55:55 +02:00
|
|
|
json = ujson.loads(result.content)
|
2013-12-19 20:17:13 +01:00
|
|
|
self.assertEqual(json['presences']["hamlet@zulip.com"]["website"]['status'], 'idle')
|
2013-07-24 20:56:42 +02:00
|
|
|
# We only want @zulip.com emails
|
2013-02-11 17:23:01 +01:00
|
|
|
for email in json['presences'].keys():
|
2013-11-22 23:48:00 +01:00
|
|
|
self.assertEqual(split_email_to_domain(email), 'zulip.com')
|
2013-02-11 17:23:01 +01:00
|
|
|
|
2013-09-03 22:41:17 +02:00
|
|
|
class AlertWordTests(AuthedTestCase):
|
2013-11-06 15:42:26 +01:00
|
|
|
interesting_alert_word_list = ['alert', 'multi-word word', '☃'.decode("utf-8")]
|
|
|
|
|
2013-12-18 17:30:33 +01:00
|
|
|
def test_internal_endpoint(self):
|
|
|
|
email = "cordelia@zulip.com"
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'alert_words': ujson.dumps(['milk', 'cookies'])
|
|
|
|
}
|
|
|
|
result = self.client.post('/json/set_alert_words', params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
words = user_alert_words(user)
|
|
|
|
self.assertEqual(words, ['milk', 'cookies'])
|
|
|
|
|
|
|
|
|
2013-09-03 22:41:17 +02:00
|
|
|
def test_default_no_words(self):
|
2013-11-06 15:42:26 +01:00
|
|
|
"""
|
|
|
|
Users start out with no alert words.
|
|
|
|
"""
|
2013-09-03 22:41:17 +02:00
|
|
|
email = "cordelia@zulip.com"
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
|
|
|
|
words = user_alert_words(user)
|
|
|
|
|
|
|
|
self.assertEqual(words, [])
|
|
|
|
|
|
|
|
def test_add_word(self):
|
2013-11-06 15:42:26 +01:00
|
|
|
"""
|
|
|
|
add_user_alert_words can add multiple alert words at once.
|
|
|
|
"""
|
2013-09-03 22:41:17 +02:00
|
|
|
email = "cordelia@zulip.com"
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
# Add several words, including multi-word and non-ascii words.
|
|
|
|
add_user_alert_words(user, self.interesting_alert_word_list)
|
2013-09-03 22:41:17 +02:00
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
words = user_alert_words(user)
|
|
|
|
self.assertEqual(words, self.interesting_alert_word_list)
|
2013-09-03 22:41:17 +02:00
|
|
|
|
|
|
|
def test_remove_word(self):
|
2013-11-06 15:42:26 +01:00
|
|
|
"""
|
|
|
|
Removing alert words works via remove_user_alert_words, even
|
|
|
|
for multi-word and non-ascii words.
|
|
|
|
"""
|
2013-09-03 22:41:17 +02:00
|
|
|
email = "cordelia@zulip.com"
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
add_user_alert_words(user, self.interesting_alert_word_list)
|
|
|
|
|
|
|
|
theoretical_remaining_alerts = self.interesting_alert_word_list[:]
|
2013-09-03 22:41:17 +02:00
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
for alert_word in self.interesting_alert_word_list:
|
|
|
|
remove_user_alert_words(user, alert_word)
|
|
|
|
theoretical_remaining_alerts.remove(alert_word)
|
|
|
|
actual_remaining_alerts = user_alert_words(user)
|
|
|
|
self.assertEqual(actual_remaining_alerts,
|
|
|
|
theoretical_remaining_alerts)
|
2013-09-03 22:41:17 +02:00
|
|
|
|
|
|
|
def test_realm_words(self):
|
2013-11-06 15:42:26 +01:00
|
|
|
"""
|
|
|
|
We can gather alert words for an entire realm via
|
|
|
|
alert_words_in_realm. Alerts added for one user do not impact other
|
|
|
|
users.
|
|
|
|
"""
|
2013-09-03 22:41:17 +02:00
|
|
|
email = "cordelia@zulip.com"
|
|
|
|
user1 = get_user_profile_by_email(email)
|
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
add_user_alert_words(user1, self.interesting_alert_word_list)
|
2013-09-03 22:41:17 +02:00
|
|
|
|
|
|
|
email = "othello@zulip.com"
|
|
|
|
user2 = get_user_profile_by_email(email)
|
|
|
|
add_user_alert_words(user2, ['another'])
|
|
|
|
|
|
|
|
realm_words = alert_words_in_realm(user2.realm)
|
|
|
|
self.assertEqual(len(realm_words), 2)
|
2013-10-09 20:48:05 +02:00
|
|
|
self.assertEqual(realm_words.keys(), [user1.id, user2.id])
|
2013-11-06 15:42:26 +01:00
|
|
|
self.assertEqual(realm_words[user1.id],
|
|
|
|
self.interesting_alert_word_list)
|
2013-10-09 20:48:05 +02:00
|
|
|
self.assertEqual(realm_words[user2.id], ['another'])
|
2013-09-03 22:41:17 +02:00
|
|
|
|
|
|
|
def test_json_list_default(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
|
|
|
result = self.client.get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
data = ujson.loads(result.content)
|
|
|
|
self.assertEqual(data['alert_words'], [])
|
|
|
|
|
|
|
|
def test_json_list_add(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
|
|
|
result = self.client_patch('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
|
|
|
|
result = self.client.get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
data = ujson.loads(result.content)
|
|
|
|
self.assertEqual(data['alert_words'], ['one', 'two', 'three'])
|
|
|
|
|
|
|
|
def test_json_list_remove(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
|
|
|
result = self.client_patch('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_delete('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one'])})
|
2013-09-03 22:41:17 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client.get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
data = ujson.loads(result.content)
|
|
|
|
self.assertEqual(data['alert_words'], ['two', 'three'])
|
|
|
|
|
|
|
|
def test_json_list_set(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
|
|
|
result = self.client_patch('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2013-12-11 20:02:32 +01:00
|
|
|
result = self.client_put('/json/users/me/alert_words', {'alert_words': ujson.dumps(['a', 'b', 'c'])})
|
2013-09-03 22:41:17 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client.get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
data = ujson.loads(result.content)
|
|
|
|
self.assertEqual(data['alert_words'], ['a', 'b', 'c'])
|
|
|
|
|
2013-10-09 20:09:48 +02:00
|
|
|
def message_does_alert(self, user_profile, message):
|
|
|
|
# Send a bunch of messages as othello, so Hamlet is notified
|
|
|
|
self.send_message("othello@zulip.com", "Denmark", Recipient.STREAM, message)
|
|
|
|
message = most_recent_usermessage(user_profile)
|
|
|
|
return 'has_alert_word' in message.flags_list()
|
|
|
|
|
|
|
|
def test_alert_flags(self):
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
user_profile_hamlet = get_user_profile_by_email("hamlet@zulip.com")
|
|
|
|
|
|
|
|
result = self.client_patch('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client.get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
data = ujson.loads(result.content)
|
|
|
|
self.assertEqual(data['alert_words'], ['one', 'two', 'three'])
|
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
# Alerts in the middle of messages work.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Normal alert one time"))
|
2013-11-06 15:42:26 +01:00
|
|
|
# Alerts at the end of messages work.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Normal alert one"))
|
2013-11-06 15:42:26 +01:00
|
|
|
# Alerts at the beginning of messages work.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "two normal alerts"))
|
2013-11-06 15:42:26 +01:00
|
|
|
# Alerts with surrounding punctuation work.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "This one? should alert"))
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Definitely time for three."))
|
2013-11-06 15:42:26 +01:00
|
|
|
# Multiple alerts in a message work.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "One two three o'clock"))
|
2013-11-06 15:42:26 +01:00
|
|
|
# Alerts are case-insensitive.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "One o'clock"))
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Case of ONE, won't stop me"))
|
|
|
|
|
2013-11-06 15:42:26 +01:00
|
|
|
# We don't cause alerts for matches in URLs.
|
2013-10-09 20:09:48 +02:00
|
|
|
self.assertFalse(self.message_does_alert(user_profile_hamlet, "Don't alert on http://t.co/one/ urls"))
|
|
|
|
self.assertFalse(self.message_does_alert(user_profile_hamlet, "Don't alert on http://t.co/one urls"))
|
|
|
|
|
2013-09-10 00:06:24 +02:00
|
|
|
class MutedTopicsTests(AuthedTestCase):
|
|
|
|
def test_json_set(self):
|
|
|
|
email = 'hamlet@zulip.com'
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
url = '/json/set_muted_topics'
|
|
|
|
data = {'muted_topics': '[["stream", "topic"]]'}
|
|
|
|
result = self.client.post(url, data)
|
|
|
|
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"]]'}
|
|
|
|
result = self.client.post(url, data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
self.assertEqual(ujson.loads(user.muted_topics), [["stream2", "topic2"]])
|
|
|
|
|