2019-12-30 02:21:51 +01:00
|
|
|
import time
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict
|
|
|
|
from unittest import mock
|
2017-03-08 12:12:02 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import ujson
|
2017-03-08 12:12:02 +01:00
|
|
|
from django.http import HttpResponse
|
2018-05-29 07:25:08 +02:00
|
|
|
from django.test import override_settings
|
2017-03-08 12:12:02 +01:00
|
|
|
|
|
|
|
from zerver.lib.initial_password import initial_password
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.rate_limiter import add_ratelimit_rule, remove_ratelimit_rule
|
2017-03-08 12:12:02 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2019-10-16 18:12:59 +02:00
|
|
|
from zerver.lib.test_helpers import get_test_image_file
|
2018-08-01 10:53:40 +02:00
|
|
|
from zerver.lib.users import get_all_api_keys
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserProfile, get_user_profile_by_api_key
|
|
|
|
|
2017-03-08 12:12:02 +01:00
|
|
|
|
|
|
|
class ChangeSettingsTest(ZulipTestCase):
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def check_well_formed_change_settings_response(self, result: Dict[str, Any]) -> None:
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertIn("full_name", result)
|
|
|
|
|
|
|
|
# DEPRECATED, to be deleted after all uses of check_for_toggle_param
|
|
|
|
# are converted into check_for_toggle_param_patch.
|
2017-11-05 10:51:25 +01:00
|
|
|
def check_for_toggle_param(self, pattern: str, param: str) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
json_result = self.client_post(pattern,
|
|
|
|
{param: ujson.dumps(True)})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertEqual(getattr(user_profile, param), True)
|
|
|
|
|
|
|
|
json_result = self.client_post(pattern,
|
|
|
|
{param: ujson.dumps(False)})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertEqual(getattr(user_profile, param), False)
|
|
|
|
|
|
|
|
# TODO: requires method consolidation, right now, there's no alternative
|
|
|
|
# for check_for_toggle_param for PATCH.
|
2017-11-05 10:51:25 +01:00
|
|
|
def check_for_toggle_param_patch(self, pattern: str, param: str) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
json_result = self.client_patch(pattern,
|
|
|
|
{param: ujson.dumps(True)})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertEqual(getattr(user_profile, param), True)
|
|
|
|
|
|
|
|
json_result = self.client_patch(pattern,
|
|
|
|
{param: ujson.dumps(False)})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertEqual(getattr(user_profile, param), False)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_successful_change_settings(self) -> None:
|
2017-03-08 12:12:02 +01:00
|
|
|
"""
|
2017-07-31 20:44:52 +02:00
|
|
|
A call to /json/settings with valid parameters changes the user's
|
2017-03-08 12:12:02 +01:00
|
|
|
settings correctly and returns correct values.
|
|
|
|
"""
|
2020-03-06 18:40:46 +01:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.login_user(user)
|
2017-07-31 20:44:52 +02:00
|
|
|
json_result = self.client_patch(
|
|
|
|
"/json/settings",
|
2017-03-08 12:12:02 +01:00
|
|
|
dict(
|
|
|
|
full_name='Foo Bar',
|
2020-03-12 14:17:25 +01:00
|
|
|
old_password=initial_password(user.delivery_email),
|
2017-03-08 12:12:02 +01:00
|
|
|
new_password='foobar1',
|
|
|
|
))
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
result = ujson.loads(json_result.content)
|
|
|
|
self.check_well_formed_change_settings_response(result)
|
2020-03-06 18:40:46 +01:00
|
|
|
|
|
|
|
user.refresh_from_db()
|
|
|
|
self.assertEqual(user.full_name, "Foo Bar")
|
2017-04-18 03:23:32 +02:00
|
|
|
self.logout()
|
2020-03-06 18:40:46 +01:00
|
|
|
|
|
|
|
# This is one of the few places we log in directly
|
|
|
|
# with Django's client (to test the password change
|
|
|
|
# with as few moving parts as possible).
|
|
|
|
self.assertTrue(
|
|
|
|
self.client.login(
|
2020-03-12 14:17:25 +01:00
|
|
|
username=user.delivery_email,
|
2020-03-06 18:40:46 +01:00
|
|
|
password='foobar1',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
realm=user.realm,
|
|
|
|
),
|
2020-03-06 18:40:46 +01:00
|
|
|
)
|
|
|
|
self.assert_logged_in_user_id(user.id)
|
2017-03-08 12:12:02 +01:00
|
|
|
|
auth: Use zxcvbn to ensure password strength on server side.
For a long time, we've been only doing the zxcvbn password strength
checks on the browser, which is helpful, but means users could through
hackery (or a bug in the frontend validation code) manage to set a
too-weak password. We fix this by running our password strength
validation on the backend as well, using python-zxcvbn.
In theory, a bug in python-zxcvbn could result in it producing a
different opinion than the frontend version; if so, it'd be a pretty
bad bug in the library, and hopefully we'd hear about it from users,
report upstream, and get it fixed that way. Alternatively, we can
switch to shelling out to node like we do for KaTeX.
Fixes #6880.
2019-11-18 08:11:03 +01:00
|
|
|
def test_password_change_check_strength(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
auth: Use zxcvbn to ensure password strength on server side.
For a long time, we've been only doing the zxcvbn password strength
checks on the browser, which is helpful, but means users could through
hackery (or a bug in the frontend validation code) manage to set a
too-weak password. We fix this by running our password strength
validation on the backend as well, using python-zxcvbn.
In theory, a bug in python-zxcvbn could result in it producing a
different opinion than the frontend version; if so, it'd be a pretty
bad bug in the library, and hopefully we'd hear about it from users,
report upstream, and get it fixed that way. Alternatively, we can
switch to shelling out to node like we do for KaTeX.
Fixes #6880.
2019-11-18 08:11:03 +01:00
|
|
|
with self.settings(PASSWORD_MIN_LENGTH=3, PASSWORD_MIN_GUESSES=1000):
|
|
|
|
json_result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
full_name='Foo Bar',
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password='easy',
|
|
|
|
))
|
|
|
|
self.assert_json_error(json_result, "New password is too weak!")
|
|
|
|
|
|
|
|
json_result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
full_name='Foo Bar',
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password='f657gdGGk9',
|
|
|
|
))
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_illegal_name_changes(self) -> None:
|
2017-05-07 21:25:59 +02:00
|
|
|
user = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-08 12:12:02 +01:00
|
|
|
full_name = user.full_name
|
|
|
|
|
|
|
|
with self.settings(NAME_CHANGES_DISABLED=True):
|
2017-07-31 20:44:52 +02:00
|
|
|
json_result = self.client_patch("/json/settings",
|
|
|
|
dict(full_name='Foo Bar'))
|
2017-03-08 12:12:02 +01: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)
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertEqual(user.full_name, full_name)
|
|
|
|
|
|
|
|
# Now try a too-long name
|
2017-07-31 20:44:52 +02:00
|
|
|
json_result = self.client_patch("/json/settings",
|
|
|
|
dict(full_name='x' * 1000))
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assert_json_error(json_result, 'Name too long!')
|
|
|
|
|
2017-05-12 04:21:49 +02:00
|
|
|
# Now try a too-short name
|
2017-07-31 20:44:52 +02:00
|
|
|
json_result = self.client_patch("/json/settings",
|
|
|
|
dict(full_name='x'))
|
2017-05-12 04:21:49 +02:00
|
|
|
self.assert_json_error(json_result, 'Name too short!')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_illegal_characters_in_name_changes(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
|
|
|
|
# Now try a name with invalid characters
|
2017-07-31 20:44:52 +02:00
|
|
|
json_result = self.client_patch("/json/settings",
|
|
|
|
dict(full_name='Opheli*'))
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assert_json_error(json_result, 'Invalid characters in name!')
|
|
|
|
|
2018-05-04 18:15:54 +02:00
|
|
|
def test_change_email_to_disposable_email(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
self.login_user(hamlet)
|
|
|
|
realm = hamlet.realm
|
2018-05-04 18:15:54 +02:00
|
|
|
realm.disallow_disposable_email_addresses = True
|
2018-07-27 23:26:29 +02:00
|
|
|
realm.emails_restricted_to_domains = False
|
2018-05-04 18:15:54 +02:00
|
|
|
realm.save()
|
|
|
|
|
|
|
|
json_result = self.client_patch("/json/settings",
|
|
|
|
dict(email='hamlet@mailnator.com'))
|
|
|
|
self.assert_json_error(json_result, 'Please use your real email address.')
|
|
|
|
|
2017-03-08 12:12:02 +01:00
|
|
|
# This is basically a don't-explode test.
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_notify_settings(self) -> None:
|
2017-07-14 00:30:55 +02:00
|
|
|
for notification_setting in UserProfile.notification_setting_types:
|
2018-01-11 21:36:11 +01:00
|
|
|
# `notification_sound` is a string not a boolean, so this test
|
|
|
|
# doesn't work for it.
|
|
|
|
#
|
|
|
|
# TODO: Make this work more like do_test_realm_update_api
|
2019-02-01 14:08:45 +01:00
|
|
|
if notification_setting != 'notification_sound':
|
2018-01-11 21:36:11 +01:00
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications",
|
|
|
|
notification_setting)
|
|
|
|
|
|
|
|
def test_change_notification_sound(self) -> None:
|
|
|
|
pattern = "/json/settings/notifications"
|
|
|
|
param = "notification_sound"
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2018-01-11 21:36:11 +01:00
|
|
|
|
|
|
|
json_result = self.client_patch(pattern,
|
|
|
|
{param: ujson.dumps("invalid")})
|
|
|
|
self.assert_json_error(json_result, "Invalid notification sound 'invalid'")
|
|
|
|
|
|
|
|
json_result = self.client_patch(pattern,
|
|
|
|
{param: ujson.dumps("ding")})
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
|
|
|
|
# refetch user_profile object to correctly handle caching
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
self.assertEqual(getattr(user_profile, param), "ding")
|
|
|
|
|
|
|
|
json_result = self.client_patch(pattern,
|
|
|
|
{param: ujson.dumps('zulip')})
|
|
|
|
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
# refetch user_profile object to correctly handle caching
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
self.assertEqual(getattr(user_profile, param), 'zulip')
|
2017-03-08 12:12:02 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_toggling_boolean_user_display_settings(self) -> None:
|
2017-07-14 00:30:55 +02:00
|
|
|
"""Test updating each boolean setting in UserProfile property_types"""
|
|
|
|
boolean_settings = (s for s in UserProfile.property_types if UserProfile.property_types[s] is bool)
|
|
|
|
for display_setting in boolean_settings:
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/display", display_setting)
|
2017-03-08 12:12:02 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_enter_sends_setting(self) -> None:
|
2017-03-08 12:12:02 +01:00
|
|
|
self.check_for_toggle_param('/json/users/me/enter-sends', "enter_sends")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_wrong_old_password(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
2017-03-08 12:12:02 +01:00
|
|
|
dict(
|
|
|
|
old_password='bad_password',
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Wrong password!")
|
|
|
|
|
2019-12-30 02:21:51 +01:00
|
|
|
def test_wrong_old_password_rate_limiter(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-12-30 02:21:51 +01:00
|
|
|
with self.settings(RATE_LIMITING_AUTHENTICATE=True):
|
2019-12-30 21:17:11 +01:00
|
|
|
add_ratelimit_rule(10, 2, domain='authenticate_by_username')
|
2019-12-30 02:21:51 +01:00
|
|
|
start_time = time.time()
|
|
|
|
with mock.patch('time.time', return_value=start_time):
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password='bad_password',
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Wrong password!")
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password='bad_password',
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Wrong password!")
|
|
|
|
|
|
|
|
# We're over the limit, so we'll get blocked even with the correct password.
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "You're making too many attempts! Try again in 10 seconds.")
|
|
|
|
|
|
|
|
# After time passes, we should be able to succeed if we give the correct password.
|
|
|
|
with mock.patch('time.time', return_value=start_time + 11):
|
|
|
|
json_result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password='foobar1',
|
|
|
|
))
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
|
2019-12-30 21:17:11 +01:00
|
|
|
remove_ratelimit_rule(10, 2, domain='authenticate_by_username')
|
2019-12-30 02:21:51 +01:00
|
|
|
|
2018-05-29 07:25:08 +02:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',
|
|
|
|
'zproject.backends.EmailAuthBackend',
|
2019-10-16 18:12:59 +02:00
|
|
|
'zproject.backends.ZulipDummyBackend'))
|
2018-05-29 07:25:08 +02:00
|
|
|
def test_change_password_ldap_backend(self) -> None:
|
2019-10-16 18:12:59 +02:00
|
|
|
self.init_default_ldap_database()
|
|
|
|
ldap_user_attr_map = {'full_name': 'cn', 'short_name': 'sn'}
|
2018-05-29 07:25:08 +02:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-10-16 18:12:59 +02:00
|
|
|
|
2018-05-29 07:25:08 +02:00
|
|
|
with self.settings(LDAP_APPEND_DOMAIN="zulip.com",
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Your Zulip password is managed in LDAP")
|
|
|
|
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
2020-02-19 19:40:49 +01:00
|
|
|
old_password=self.ldap_password("hamlet"), # hamlet's password in ldap
|
2018-05-29 07:25:08 +02:00
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Your Zulip password is managed in LDAP")
|
|
|
|
|
|
|
|
with self.settings(LDAP_APPEND_DOMAIN="example.com",
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
with self.settings(LDAP_APPEND_DOMAIN=None,
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
|
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
|
|
|
dict(
|
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
|
|
|
new_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Your Zulip password is managed in LDAP")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_changing_nothing_returns_error(self) -> None:
|
2017-03-08 12:12:02 +01: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.)
|
|
|
|
"""
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch("/json/settings",
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
dict(old_password='ignored'))
|
2018-01-11 20:28:44 +01:00
|
|
|
self.assert_json_error(result, "Please fill out all fields.")
|
2017-03-08 12:12:02 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def do_test_change_user_display_setting(self, setting_name: str) -> None:
|
2017-03-08 12:12:02 +01:00
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
test_changes: Dict[str, Any] = dict(
|
2017-07-14 00:30:55 +02:00
|
|
|
default_language = 'de',
|
2018-07-20 09:28:32 +02:00
|
|
|
emojiset = 'google',
|
2017-07-14 00:30:55 +02:00
|
|
|
timezone = 'US/Mountain',
|
2019-03-17 14:48:51 +01:00
|
|
|
demote_inactive_streams = 2,
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
)
|
2017-03-08 12:20:56 +01:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-07-14 00:30:55 +02:00
|
|
|
test_value = test_changes.get(setting_name)
|
|
|
|
# Error if a setting in UserProfile.property_types does not have test values
|
|
|
|
if test_value is None:
|
2020-06-10 06:41:04 +02:00
|
|
|
raise AssertionError(f'No test created for {setting_name}')
|
2017-07-14 00:30:55 +02:00
|
|
|
|
2020-04-04 00:06:39 +02:00
|
|
|
if isinstance(test_value, int):
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
invalid_value: Any = 100
|
2019-03-17 14:48:51 +01:00
|
|
|
else:
|
|
|
|
invalid_value = 'invalid_' + setting_name
|
2017-07-14 00:30:55 +02:00
|
|
|
data = {setting_name: ujson.dumps(test_value)}
|
2019-03-17 14:48:51 +01:00
|
|
|
|
2017-03-14 10:53:09 +01:00
|
|
|
result = self.client_patch("/json/settings/display", data)
|
|
|
|
self.assert_json_success(result)
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-07-14 00:30:55 +02:00
|
|
|
self.assertEqual(getattr(user_profile, setting_name), test_value)
|
2017-03-14 10:53:09 +01:00
|
|
|
|
2017-07-14 00:30:55 +02:00
|
|
|
# Test to make sure invalid settings are not accepted
|
2017-03-14 10:53:09 +01:00
|
|
|
# and saved in the db.
|
2017-07-14 00:30:55 +02:00
|
|
|
data = {setting_name: ujson.dumps(invalid_value)}
|
2019-03-17 14:48:51 +01:00
|
|
|
|
2017-03-14 10:53:09 +01:00
|
|
|
result = self.client_patch("/json/settings/display", data)
|
2017-07-14 00:30:55 +02:00
|
|
|
# the json error for multiple word setting names (ex: default_language)
|
|
|
|
# displays as 'Invalid language'. Using setting_name.split('_') to format.
|
2020-06-10 06:41:04 +02:00
|
|
|
self.assert_json_error(result, f"Invalid {setting_name}")
|
2019-03-17 14:48:51 +01:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-07-14 00:30:55 +02:00
|
|
|
self.assertNotEqual(getattr(user_profile, setting_name), invalid_value)
|
2017-03-14 10:53:09 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_change_user_display_setting(self) -> None:
|
2017-07-14 00:30:55 +02:00
|
|
|
"""Test updating each non-boolean setting in UserProfile property_types"""
|
|
|
|
user_settings = (s for s in UserProfile.property_types if UserProfile.property_types[s] is not bool)
|
|
|
|
for setting in user_settings:
|
|
|
|
self.do_test_change_user_display_setting(setting)
|
2017-04-02 21:05:33 +02:00
|
|
|
|
2018-07-20 09:28:32 +02:00
|
|
|
def do_change_emojiset(self, emojiset: str) -> HttpResponse:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-07-20 09:28:32 +02:00
|
|
|
data = {'emojiset': ujson.dumps(emojiset)}
|
|
|
|
result = self.client_patch("/json/settings/display", data)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def test_emojiset(self) -> None:
|
|
|
|
"""Test banned emojisets are not accepted."""
|
2018-08-25 09:18:49 +02:00
|
|
|
banned_emojisets = ['apple', 'emojione']
|
|
|
|
valid_emojisets = ['google', 'google-blob', 'text', 'twitter']
|
2018-07-20 09:28:32 +02:00
|
|
|
|
|
|
|
for emojiset in banned_emojisets:
|
|
|
|
result = self.do_change_emojiset(emojiset)
|
2020-04-04 00:06:39 +02:00
|
|
|
self.assert_json_error(result, "Invalid emojiset")
|
2018-07-20 09:28:32 +02:00
|
|
|
|
|
|
|
for emojiset in valid_emojisets:
|
|
|
|
result = self.do_change_emojiset(emojiset)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2019-04-29 08:41:00 +02:00
|
|
|
def test_avatar_changes_disabled(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-04-29 08:41:00 +02:00
|
|
|
|
|
|
|
with self.settings(AVATAR_CHANGES_DISABLED=True):
|
|
|
|
result = self.client_delete("/json/users/me/avatar")
|
|
|
|
self.assert_json_error(result, "Avatar changes are disabled in this organization.", 400)
|
|
|
|
|
|
|
|
with self.settings(AVATAR_CHANGES_DISABLED=True):
|
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
result = self.client_post("/json/users/me/avatar", {'f1': fp1})
|
|
|
|
self.assert_json_error(result, "Avatar changes are disabled in this organization.", 400)
|
2017-04-02 21:05:33 +02:00
|
|
|
|
2017-03-08 12:20:56 +01:00
|
|
|
class UserChangesTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_api_key(self) -> None:
|
2017-05-07 21:25:59 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
2019-01-05 02:25:06 +01:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2018-08-01 10:53:40 +02:00
|
|
|
old_api_keys = get_all_api_keys(user)
|
2019-01-05 02:25:06 +01:00
|
|
|
# Ensure the old API keys are in the authentication cache, so
|
|
|
|
# that the below logic can test whether we have a cache-flushing bug.
|
|
|
|
for api_key in old_api_keys:
|
|
|
|
self.assertEqual(get_user_profile_by_api_key(api_key).email, email)
|
|
|
|
|
2017-03-08 12:20:56 +01:00
|
|
|
result = self.client_post('/json/users/me/api_key/regenerate')
|
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:44:10 +02:00
|
|
|
new_api_key = result.json()['api_key']
|
2018-08-01 10:53:40 +02:00
|
|
|
self.assertNotIn(new_api_key, old_api_keys)
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.example_user('hamlet')
|
2018-08-01 10:53:40 +02:00
|
|
|
current_api_keys = get_all_api_keys(user)
|
|
|
|
self.assertIn(new_api_key, current_api_keys)
|
2019-01-05 02:16:38 +01:00
|
|
|
|
|
|
|
for api_key in old_api_keys:
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
get_user_profile_by_api_key(api_key)
|
|
|
|
|
|
|
|
for api_key in current_api_keys:
|
|
|
|
self.assertEqual(get_user_profile_by_api_key(api_key).email, email)
|