2017-03-08 12:12:02 +01:00
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from mock import patch
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from zerver.lib.initial_password import initial_password
|
|
|
|
from zerver.lib.sessions import get_session_dict_user
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-07-14 00:30:55 +02:00
|
|
|
from zerver.models import get_realm, get_user, UserProfile
|
2017-03-08 12:12:02 +01:00
|
|
|
|
|
|
|
class ChangeSettingsTest(ZulipTestCase):
|
|
|
|
|
|
|
|
def check_well_formed_change_settings_response(self, result):
|
|
|
|
# type: (Dict[str, Any]) -> None
|
|
|
|
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.
|
|
|
|
def check_for_toggle_param(self, pattern, param):
|
|
|
|
# type: (str, str) -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("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.
|
|
|
|
def check_for_toggle_param_patch(self, pattern, param):
|
|
|
|
# type: (str, str) -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("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)
|
|
|
|
|
|
|
|
def test_successful_change_settings(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
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.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
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',
|
2017-05-25 01:40:26 +02:00
|
|
|
old_password=initial_password(self.example_email("hamlet")),
|
2017-03-08 12:12:02 +01:00
|
|
|
new_password='foobar1',
|
|
|
|
confirm_password='foobar1',
|
|
|
|
))
|
|
|
|
self.assert_json_success(json_result)
|
|
|
|
result = ujson.loads(json_result.content)
|
|
|
|
self.check_well_formed_change_settings_response(result)
|
2017-05-07 17:21:26 +02:00
|
|
|
self.assertEqual(self.example_user('hamlet').
|
2017-03-08 12:12:02 +01:00
|
|
|
full_name, "Foo Bar")
|
2017-04-18 03:23:32 +02:00
|
|
|
self.logout()
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"), "foobar1")
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
|
|
|
|
|
|
|
def test_illegal_name_changes(self):
|
|
|
|
# type: () -> None
|
2017-05-07 21:25:59 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
2017-03-08 12:12:02 +01:00
|
|
|
self.login(email)
|
|
|
|
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-03-08 12:12:02 +01:00
|
|
|
def test_illegal_characters_in_name_changes(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-03-08 12:12:02 +01:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
# 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!')
|
|
|
|
|
|
|
|
# This is basically a don't-explode test.
|
|
|
|
def test_notify_settings(self):
|
|
|
|
# type: () -> None
|
2017-07-14 00:30:55 +02:00
|
|
|
for notification_setting in UserProfile.notification_setting_types:
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/notifications",
|
|
|
|
notification_setting)
|
2017-03-08 12:12:02 +01:00
|
|
|
|
|
|
|
def test_ui_settings(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/ui", "autoscroll_forever")
|
|
|
|
self.check_for_toggle_param_patch("/json/settings/ui", "default_desktop_notifications")
|
|
|
|
|
2017-07-14 00:30:55 +02:00
|
|
|
def test_toggling_boolean_user_display_settings(self):
|
2017-03-08 12:12:02 +01:00
|
|
|
# type: () -> 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
|
|
|
|
|
|
|
def test_enter_sends_setting(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.check_for_toggle_param('/json/users/me/enter-sends', "enter_sends")
|
|
|
|
|
|
|
|
def test_mismatching_passwords(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
new_password and confirm_password must match
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(
|
|
|
|
"/json/settings",
|
2017-03-08 12:12:02 +01:00
|
|
|
dict(
|
|
|
|
new_password="mismatched_password",
|
|
|
|
confirm_password="not_the_same",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result,
|
|
|
|
"New password must match confirmation password!")
|
|
|
|
|
|
|
|
def test_wrong_old_password(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
new_password and confirm_password must match
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("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",
|
|
|
|
confirm_password="ignored",
|
|
|
|
))
|
|
|
|
self.assert_json_error(result, "Wrong password!")
|
|
|
|
|
|
|
|
def test_changing_nothing_returns_error(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
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.)
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch("/json/settings",
|
|
|
|
dict(old_password='ignored',))
|
2017-03-08 12:12:02 +01:00
|
|
|
self.assert_json_error(result, "No new data supplied")
|
|
|
|
|
2017-07-14 00:30:55 +02:00
|
|
|
def do_test_change_user_display_setting(self, setting_name):
|
|
|
|
# type: (str) -> None
|
2017-03-08 12:12:02 +01:00
|
|
|
|
2017-07-14 00:30:55 +02:00
|
|
|
test_changes = dict(
|
|
|
|
default_language = 'de',
|
|
|
|
emojiset = 'apple',
|
|
|
|
timezone = 'US/Mountain',
|
|
|
|
) # type: Dict[str, Any]
|
2017-03-08 12:20:56 +01:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
email = self.example_email('hamlet')
|
2017-03-14 10:53:09 +01:00
|
|
|
self.login(email)
|
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:
|
|
|
|
raise AssertionError('No test created for %s' % (setting_name))
|
|
|
|
invalid_value = 'invalid_' + setting_name
|
|
|
|
|
|
|
|
data = {setting_name: ujson.dumps(test_value)}
|
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)}
|
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.
|
|
|
|
self.assert_json_error(result, "Invalid %s '%s'" % (setting_name.split('_')[-1],
|
|
|
|
invalid_value))
|
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-07-14 00:30:55 +02:00
|
|
|
def test_change_user_display_setting(self):
|
2017-04-02 21:05:33 +02:00
|
|
|
# type: () -> 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
|
|
|
|
|
|
|
|
2017-03-08 12:20:56 +01:00
|
|
|
class UserChangesTest(ZulipTestCase):
|
|
|
|
def test_update_api_key(self):
|
|
|
|
# type: () -> None
|
2017-05-07 21:25:59 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
2017-03-08 12:20:56 +01:00
|
|
|
self.login(email)
|
|
|
|
old_api_key = user.api_key
|
|
|
|
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']
|
2017-03-08 12:20:56 +01:00
|
|
|
self.assertNotEqual(old_api_key, new_api_key)
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-03-08 12:20:56 +01:00
|
|
|
self.assertEqual(new_api_key, user.api_key)
|