mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	test_settings: Refactor to use UserProfile property_types framework.
This commit is contained in:
		@@ -10,7 +10,7 @@ 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
 | 
			
		||||
from zerver.models import get_realm, get_user
 | 
			
		||||
from zerver.models import get_realm, get_user, UserProfile
 | 
			
		||||
 | 
			
		||||
class ChangeSettingsTest(ZulipTestCase):
 | 
			
		||||
 | 
			
		||||
@@ -125,32 +125,21 @@ class ChangeSettingsTest(ZulipTestCase):
 | 
			
		||||
    # This is basically a don't-explode test.
 | 
			
		||||
    def test_notify_settings(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_desktop_notifications")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_stream_desktop_notifications")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_stream_sounds")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_sounds")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_offline_email_notifications")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_offline_push_notifications")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_online_push_notifications")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "enable_digest_emails")
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/notifications", "pm_content_in_desktop_notifications")
 | 
			
		||||
        for notification_setting in UserProfile.notification_setting_types:
 | 
			
		||||
            self.check_for_toggle_param_patch("/json/settings/notifications",
 | 
			
		||||
                                              notification_setting)
 | 
			
		||||
 | 
			
		||||
    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")
 | 
			
		||||
 | 
			
		||||
    def test_toggling_left_side_userlist(self):
 | 
			
		||||
    def test_toggling_boolean_user_display_settings(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/display", "left_side_userlist")
 | 
			
		||||
 | 
			
		||||
    def test_toggling_emoji_alt_code(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/display", "emoji_alt_code")
 | 
			
		||||
 | 
			
		||||
    def test_time_setting(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        self.check_for_toggle_param_patch("/json/settings/display", "twenty_four_hour_time")
 | 
			
		||||
        """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)
 | 
			
		||||
 | 
			
		||||
    def test_enter_sends_setting(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
@@ -198,74 +187,47 @@ class ChangeSettingsTest(ZulipTestCase):
 | 
			
		||||
                                  dict(old_password='ignored',))
 | 
			
		||||
        self.assert_json_error(result, "No new data supplied")
 | 
			
		||||
 | 
			
		||||
    def test_change_default_language(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        """
 | 
			
		||||
        Test changing the default language of the user.
 | 
			
		||||
        """
 | 
			
		||||
    def do_test_change_user_display_setting(self, setting_name):
 | 
			
		||||
        # type: (str) -> None
 | 
			
		||||
 | 
			
		||||
        test_changes = dict(
 | 
			
		||||
            default_language = 'de',
 | 
			
		||||
            emojiset = 'apple',
 | 
			
		||||
            timezone = 'US/Mountain',
 | 
			
		||||
        )  # type: Dict[str, Any]
 | 
			
		||||
 | 
			
		||||
        email = self.example_email('hamlet')
 | 
			
		||||
        self.login(email)
 | 
			
		||||
        german = "de"
 | 
			
		||||
        data = dict(default_language=ujson.dumps(german))
 | 
			
		||||
        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)}
 | 
			
		||||
        result = self.client_patch("/json/settings/display", data)
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        self.assertEqual(user_profile.default_language, german)
 | 
			
		||||
        self.assertEqual(getattr(user_profile, setting_name), test_value)
 | 
			
		||||
 | 
			
		||||
        # Test to make sure invalid languages are not accepted
 | 
			
		||||
        # Test to make sure invalid settings are not accepted
 | 
			
		||||
        # and saved in the db.
 | 
			
		||||
        invalid_lang = "invalid_lang"
 | 
			
		||||
        data = dict(default_language=ujson.dumps(invalid_lang))
 | 
			
		||||
        data = {setting_name: ujson.dumps(invalid_value)}
 | 
			
		||||
        result = self.client_patch("/json/settings/display", data)
 | 
			
		||||
        self.assert_json_error(result, "Invalid language '%s'" % (invalid_lang,))
 | 
			
		||||
        # 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))
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        self.assertNotEqual(user_profile.default_language, invalid_lang)
 | 
			
		||||
        self.assertNotEqual(getattr(user_profile, setting_name), invalid_value)
 | 
			
		||||
 | 
			
		||||
    def test_change_timezone(self):
 | 
			
		||||
    def test_change_user_display_setting(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        """
 | 
			
		||||
        Test changing the timezone of the user.
 | 
			
		||||
        """
 | 
			
		||||
        email = self.example_email('hamlet')
 | 
			
		||||
        self.login(email)
 | 
			
		||||
        usa_pacific = 'US/Pacific'
 | 
			
		||||
        data = dict(timezone=ujson.dumps(usa_pacific))
 | 
			
		||||
        result = self.client_patch("/json/settings/display", data)
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        self.assertEqual(user_profile.timezone, usa_pacific)
 | 
			
		||||
        """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)
 | 
			
		||||
 | 
			
		||||
        # Test to make sure invalid timezones are not accepted
 | 
			
		||||
        # and saved in the db.
 | 
			
		||||
        invalid_timezone = "invalid_timezone"
 | 
			
		||||
        data = dict(timezone=ujson.dumps(invalid_timezone))
 | 
			
		||||
        result = self.client_patch("/json/settings/display", data)
 | 
			
		||||
        self.assert_json_error(result, "Invalid timezone '%s'" % (invalid_timezone,))
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        self.assertNotEqual(user_profile.timezone, invalid_timezone)
 | 
			
		||||
 | 
			
		||||
    def test_change_emojiset(self):
 | 
			
		||||
        # type: () -> None
 | 
			
		||||
        """
 | 
			
		||||
        Test changing the emojiset.
 | 
			
		||||
        """
 | 
			
		||||
        email = self.example_email('hamlet')
 | 
			
		||||
        self.login(email)
 | 
			
		||||
        emojiset = 'apple'
 | 
			
		||||
        data = dict(emojiset=ujson.dumps(emojiset))
 | 
			
		||||
        result = self.client_patch("/json/settings/display", data)
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        self.assertEqual(user_profile.emojiset, emojiset)
 | 
			
		||||
 | 
			
		||||
        # Test to make sure invalid emojisets are not accepted
 | 
			
		||||
        # and saved in the db.
 | 
			
		||||
        invalid_emojiset = "invalid_emojiset"
 | 
			
		||||
        data = dict(emojiset=ujson.dumps(invalid_emojiset))
 | 
			
		||||
        result = self.client_patch("/json/settings/display", data)
 | 
			
		||||
        self.assert_json_error(result, "Invalid emojiset '%s'" % (invalid_emojiset,))
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        self.assertNotEqual(user_profile.emojiset, invalid_emojiset)
 | 
			
		||||
 | 
			
		||||
class UserChangesTest(ZulipTestCase):
 | 
			
		||||
    def test_update_api_key(self):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user