2013-03-20 22:08:48 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-03-20 22:08:48 +01:00
|
|
|
|
2017-08-04 02:25:38 +02:00
|
|
|
from typing import (Any, Dict, Iterable, List, Mapping,
|
2017-03-08 12:18:27 +01:00
|
|
|
Optional, TypeVar, Text, Union)
|
2016-06-15 23:47:22 +02:00
|
|
|
|
2016-09-14 02:08:08 +02:00
|
|
|
from django.http import HttpResponse
|
2017-03-08 12:28:24 +01: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 (
|
2016-07-15 10:14:51 +02:00
|
|
|
queries_captured, simulated_empty_cache,
|
2017-03-08 12:18:27 +01:00
|
|
|
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
|
2014-01-27 22:53:36 +01:00
|
|
|
|
2014-01-31 23:13:58 +01:00
|
|
|
from zerver.models import UserProfile, Recipient, \
|
2017-03-31 16:20:07 +02:00
|
|
|
Realm, RealmDomain, UserActivity, \
|
2017-05-24 02:42:31 +02:00
|
|
|
get_user, get_realm, get_client, get_stream, \
|
2017-07-02 21:10:41 +02:00
|
|
|
Message, get_context_for_message, ScheduledEmail
|
2014-02-07 22:47:30 +01:00
|
|
|
|
2017-02-16 22:35:57 +01:00
|
|
|
from zerver.lib.avatar import avatar_url
|
2016-06-22 10:36:22 +02:00
|
|
|
from zerver.lib.email_mirror import create_missed_message_address
|
2017-07-01 03:56:40 +02:00
|
|
|
from zerver.lib.send_email import send_future_email
|
2017-03-21 18:08:40 +01:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
get_emails_from_user_ids,
|
|
|
|
do_deactivate_user,
|
|
|
|
do_reactivate_user,
|
|
|
|
do_change_is_admin,
|
|
|
|
)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
from django.conf import settings
|
2017-07-01 03:56: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
|
|
|
|
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
|
|
|
|
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
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
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
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-05-07 17:21:26 +02:00
|
|
|
admin = self.example_user('hamlet')
|
2016-07-13 05:05:55 +02:00
|
|
|
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
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-05-07 17:21:26 +02:00
|
|
|
admin = self.example_user('hamlet')
|
|
|
|
user = self.example_user('othello')
|
2014-01-14 16:19:26 +01:00
|
|
|
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)
|
2017-08-17 08:46:17 +02:00
|
|
|
members = result.json()['members']
|
2017-05-25 01:40:26 +02:00
|
|
|
hamlet = find_dict(members, 'email', self.example_email("hamlet"))
|
2014-01-14 20:38:45 +01:00
|
|
|
self.assertTrue(hamlet['is_admin'])
|
2017-05-25 02:08:35 +02:00
|
|
|
othello = find_dict(members, 'email', self.example_email("othello"))
|
2014-01-14 20:38:45 +01:00
|
|
|
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
|
|
|
|
2017-08-04 02:25:38 +02:00
|
|
|
events = [] # type: List[Mapping[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']
|
2017-05-25 02:08:35 +02:00
|
|
|
self.assertEqual(person['email'], self.example_email("othello"))
|
2014-01-21 19:27:22 +01:00
|
|
|
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']
|
2017-05-25 02:08:35 +02:00
|
|
|
self.assertEqual(person['email'], self.example_email("othello"))
|
2014-01-21 19:27:22 +01:00
|
|
|
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
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2016-12-05 06:40:00 +01:00
|
|
|
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']
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(person['email'], self.example_email("hamlet"))
|
2016-12-05 06:40:00 +01:00
|
|
|
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.
|
2017-05-25 02:08:35 +02:00
|
|
|
self.login(self.example_email("othello"))
|
2014-01-14 16:19:26 +01:00
|
|
|
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'
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2016-09-27 14:25:52 +02:00
|
|
|
req = dict(full_name=ujson.dumps(new_name))
|
|
|
|
result = self.client_patch('/json/users/hamlet@zulip.com', req)
|
|
|
|
self.assertTrue(result.status_code == 200)
|
2017-05-07 17:21:26 +02:00
|
|
|
hamlet = self.example_user('hamlet')
|
2016-09-27 14:25:52 +02:00
|
|
|
self.assertEqual(hamlet.full_name, new_name)
|
|
|
|
|
|
|
|
def test_non_admin_cannot_change_full_name(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-27 14:25:52 +02:00
|
|
|
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)
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2016-09-27 14:25:52 +02:00
|
|
|
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-05-12 04:21:49 +02:00
|
|
|
def test_admin_cannot_set_short_full_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_name = 'a'
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2017-05-12 04:21:49 +02:00
|
|
|
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 short!')
|
|
|
|
|
2017-01-16 04:18:42 +01:00
|
|
|
def test_admin_cannot_set_full_name_with_invalid_characters(self):
|
|
|
|
# type: () -> None
|
|
|
|
new_name = 'Opheli*'
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2017-01-16 04:18:42 +01:00
|
|
|
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.
|
|
|
|
|
2017-05-07 21:25:59 +02:00
|
|
|
admin = self.example_user('hamlet')
|
|
|
|
admin_email = admin.email
|
2016-07-12 20:46:55 +02:00
|
|
|
self.login(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,
|
2017-03-13 17:37:01 +01:00
|
|
|
"Email 'romeo@not-zulip.com' not allowed for realm 'zulip'")
|
2016-07-12 20:46:55 +02:00
|
|
|
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.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)
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
# Romeo is a newly registered user
|
|
|
|
new_user = get_user('romeo@zulip.net', get_realm('zulip'))
|
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
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
class UserProfileTest(ZulipTestCase):
|
2013-10-20 21:10:03 +02:00
|
|
|
def test_get_emails_from_user_ids(self):
|
2016-06-15 23:47:22 +02:00
|
|
|
# type: () -> None
|
2017-05-07 17:21:26 +02:00
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
othello = self.example_user('othello')
|
2013-10-20 21:10:03 +02:00
|
|
|
dct = get_emails_from_user_ids([hamlet.id, othello.id])
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(dct[hamlet.id], self.example_email("hamlet"))
|
2017-05-25 02:08:35 +02:00
|
|
|
self.assertEqual(dct[othello.id], self.example_email("othello"))
|
2013-10-20 21:10:03 +02:00
|
|
|
|
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
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
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
|
2017-05-07 17:21:26 +02:00
|
|
|
admin = self.example_user('othello')
|
2014-01-22 20:12:29 +01:00
|
|
|
do_change_is_admin(admin, True)
|
2017-05-25 02:08:35 +02:00
|
|
|
self.login(self.example_email("othello"))
|
2013-11-15 19:39:03 +01:00
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2013-11-15 19:39:03 +01:00
|
|
|
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)
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2013-11-15 19:39:03 +01:00
|
|
|
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)
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2013-11-15 19:39:03 +01:00
|
|
|
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."""
|
2017-05-24 02:42:31 +02:00
|
|
|
self.register(self.nonreg_email('me'), "testpassword")
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2017-02-19 00:18:19 +01:00
|
|
|
|
|
|
|
result = self.client_delete('/json/users/me@zulip.com')
|
|
|
|
self.assert_json_success(result)
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.nonreg_user('me')
|
2017-02-19 00:18:19 +01:00
|
|
|
self.assertFalse(user.is_active)
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
result = self.client_post('/json/users/{email}/reactivate'.format(email=self.nonreg_email('me')))
|
2017-02-19 00:18:19 +01:00
|
|
|
self.assert_json_success(result)
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.nonreg_user('me')
|
2017-02-19 00:18:19 +01:00
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
2016-07-13 05:24:11 +02:00
|
|
|
def test_api_with_nonexistent_user(self):
|
|
|
|
# type: () -> None
|
2017-05-07 17:21:26 +02:00
|
|
|
admin = self.example_user('othello')
|
2016-07-13 05:24:11 +02:00
|
|
|
do_change_is_admin(admin, True)
|
2017-05-25 02:08:35 +02:00
|
|
|
self.login(self.example_email("othello"))
|
2016-07-13 05:24:11 +02:00
|
|
|
|
|
|
|
# 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
|
2017-05-07 17:21:26 +02:00
|
|
|
non_admin = self.example_user('othello')
|
2016-07-13 05:42:29 +02:00
|
|
|
do_change_is_admin(non_admin, False)
|
2017-05-25 02:08:35 +02:00
|
|
|
self.login(self.example_email("othello"))
|
2016-07-13 05:42:29 +02:00
|
|
|
|
|
|
|
# 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')
|
|
|
|
|
2017-07-01 03:56:40 +02:00
|
|
|
def test_clear_scheduled_jobs(self):
|
|
|
|
# type: () -> None
|
|
|
|
user = self.example_user('hamlet')
|
2017-07-02 21:10:41 +02:00
|
|
|
send_future_email('zerver/emails/followup_day1', to_user_id=user.id, delay=datetime.timedelta(hours=1))
|
|
|
|
self.assertEqual(ScheduledEmail.objects.count(), 1)
|
2017-07-01 03:56:40 +02:00
|
|
|
do_deactivate_user(user)
|
2017-07-02 21:10:41 +02:00
|
|
|
self.assertEqual(ScheduledEmail.objects.count(), 0)
|
2017-07-01 03:56:40 +02:00
|
|
|
|
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)
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
def common_get_profile(self, user_id):
|
2016-12-04 18:38:56 +01:00
|
|
|
# type: (str) -> Dict[Text, Any]
|
2017-05-24 02:42:31 +02:00
|
|
|
# Assumes all users are example users in realm 'zulip'
|
|
|
|
user_profile = self.example_user(user_id)
|
|
|
|
self.send_message(user_profile.email, "Verona", Recipient.STREAM, "hello")
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
result = self.client_get("/api/v1/users/me", **self.api_auth(user_profile.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)
|
2017-08-17 08:46:17 +02:00
|
|
|
json = result.json()
|
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
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2016-09-14 02:14:45 +02:00
|
|
|
self.login(email)
|
|
|
|
result = self.client_get("/json/users/me/pointer")
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:52:46 +02:00
|
|
|
self.assertIn("pointer", result.json())
|
2016-09-14 02:14:45 +02:00
|
|
|
|
2013-09-28 01:05:08 +02:00
|
|
|
def test_cache_behavior(self):
|
2017-07-12 22:17:24 +02:00
|
|
|
# type: () -> None
|
2017-07-12 21:59:19 +02:00
|
|
|
"""Tests whether fetching a user object the normal way, with
|
|
|
|
`get_user`, makes 1 cache query and 1 database query.
|
|
|
|
"""
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
email = self.example_email("hamlet")
|
2013-09-28 01:05:08 +02:00
|
|
|
with queries_captured() as queries:
|
|
|
|
with simulated_empty_cache() as cache_queries:
|
2017-07-12 21:59:19 +02:00
|
|
|
user_profile = get_user(email, realm)
|
2013-09-28 01:05:08 +02:00
|
|
|
|
2017-03-31 11:08:45 +02:00
|
|
|
self.assert_length(queries, 1)
|
2016-09-25 21:30:10 +02:00
|
|
|
self.assert_length(cache_queries, 1)
|
2017-07-12 21:59:19 +02:00
|
|
|
self.assertEqual(user_profile.email, email)
|
2013-09-28 01:05:08 +02:00
|
|
|
|
2016-12-13 19:17:49 +01:00
|
|
|
def test_get_user_profile(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-12-13 19:17:49 +01:00
|
|
|
result = ujson.loads(self.client_get('/json/users/me').content)
|
|
|
|
self.assertEqual(result['short_name'], 'hamlet')
|
2017-05-25 01:40:26 +02:00
|
|
|
self.assertEqual(result['email'], self.example_email("hamlet"))
|
2016-12-13 19:17:49 +01:00
|
|
|
self.assertEqual(result['full_name'], 'King Hamlet')
|
|
|
|
self.assertIn("user_id", result)
|
|
|
|
self.assertFalse(result['is_bot'])
|
|
|
|
self.assertFalse(result['is_admin'])
|
2017-05-25 01:44:04 +02:00
|
|
|
self.login(self.example_email("iago"))
|
2016-12-13 19:17:49 +01:00
|
|
|
result = ujson.loads(self.client_get('/json/users/me').content)
|
|
|
|
self.assertEqual(result['short_name'], 'iago')
|
2017-05-25 01:44:04 +02:00
|
|
|
self.assertEqual(result['email'], self.example_email("iago"))
|
2016-12-13 19:17:49 +01:00
|
|
|
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
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
json = self.common_get_profile("othello")
|
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
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
id1 = self.send_message(self.example_email("othello"), "Verona", Recipient.STREAM)
|
|
|
|
id2 = self.send_message(self.example_email("othello"), "Verona", Recipient.STREAM)
|
2013-08-19 21:05:23 +02:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
json = self.common_get_profile("hamlet")
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
self.common_update_pointer(self.example_email("hamlet"), id2)
|
|
|
|
json = self.common_get_profile("hamlet")
|
2013-08-19 21:05:23 +02:00
|
|
|
self.assertEqual(json["pointer"], id2)
|
2013-01-22 20:07:51 +01:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
self.common_update_pointer(self.example_email("hamlet"), id1)
|
|
|
|
json = self.common_get_profile("hamlet")
|
2017-05-07 20:04:04 +02:00
|
|
|
self.assertEqual(json["pointer"], id2) # pointer does not move backwards
|
2013-08-19 21:05:23 +02:00
|
|
|
|
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
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-05-25 01:40:26 +02:00
|
|
|
result = self.client_get("/api/v1/users", **self.api_auth(self.example_email("hamlet")))
|
2014-07-18 06:16:14 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-08-16 09:52:46 +02:00
|
|
|
for user in result.json()['members']:
|
2017-05-25 01:40:26 +02:00
|
|
|
if user['email'] == self.example_email("hamlet"):
|
2014-07-18 06:16:14 +02:00
|
|
|
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
|
|
|
)
|