diff --git a/zerver/lib/zcommand.py b/zerver/lib/zcommand.py new file mode 100644 index 0000000000..8d198ffaa4 --- /dev/null +++ b/zerver/lib/zcommand.py @@ -0,0 +1,32 @@ +from typing import Any, Dict +from django.utils.translation import ugettext as _ + +from zerver.models import UserProfile +from zerver.lib.actions import do_set_user_display_setting +from zerver.lib.exceptions import JsonableError + +def process_zcommands(command: str, user_profile: UserProfile) -> Dict[str, Any]: + + if command == 'ping': + ret = dict() # type: Dict[str, Any] + return ret + + if command == 'night': + if user_profile.night_mode: + msg = 'You are still in night mode.' + else: + msg = 'Changed to night mode! To revert night mode, type `/day`.' + do_set_user_display_setting(user_profile, 'night_mode', True) + ret = dict(msg=msg) + return ret + + if command == 'day': + if user_profile.night_mode: + msg = 'Changed to day mode! To revert day mode, type `/night`.' + do_set_user_display_setting(user_profile, 'night_mode', False) + else: + msg = 'You are still in day mode.' + ret = dict(msg=msg) + return ret + + raise JsonableError(_('No such command: %s') % (command,)) diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index e46a1af70c..4f9db2f148 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -999,39 +999,6 @@ class SewMessageAndReactionTest(ZulipTestCase): class MessagePOSTTest(ZulipTestCase): - def test_zcommand(self) -> None: - self.login(self.example_email("hamlet")) - - payload = dict(command="boil-ocean") - result = self.client_post("/json/zcommand", payload) - self.assert_json_error(result, "No such command: boil-ocean") - - payload = dict(command="ping") - result = self.client_post("/json/zcommand", payload) - self.assert_json_success(result) - - user = self.example_user('hamlet') - user.night_mode = False - user.save() - - payload = dict(command="night") - result = self.client_post("/json/zcommand", payload) - self.assert_json_success(result) - self.assertIn('Changed to night', result.json()['msg']) - - result = self.client_post("/json/zcommand", payload) - self.assert_json_success(result) - self.assertIn('still in night mode', result.json()['msg']) - - payload = dict(command="day") - result = self.client_post("/json/zcommand", payload) - self.assert_json_success(result) - self.assertIn('Changed to day', result.json()['msg']) - - result = self.client_post("/json/zcommand", payload) - self.assert_json_success(result) - self.assertIn('still in day mode', result.json()['msg']) - def test_message_to_self(self) -> None: """ Sending a message to a stream to which you are subscribed is diff --git a/zerver/tests/test_zcommand.py b/zerver/tests/test_zcommand.py new file mode 100644 index 0000000000..3007fff91c --- /dev/null +++ b/zerver/tests/test_zcommand.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +from zerver.lib.test_classes import ( + ZulipTestCase, +) + +class ZcommandTest(ZulipTestCase): + + def test_invalid_zcommand(self) -> None: + self.login(self.example_email("hamlet")) + + payload = dict(command="boil-ocean") + result = self.client_post("/json/zcommand", payload) + self.assert_json_error(result, "No such command: boil-ocean") + + def test_ping_zcommand(self) -> None: + self.login(self.example_email("hamlet")) + + payload = dict(command="ping") + result = self.client_post("/json/zcommand", payload) + self.assert_json_success(result) + + def test_night_zcommand(self) -> None: + self.login(self.example_email("hamlet")) + user = self.example_user('hamlet') + user.night_mode = False + user.save() + + payload = dict(command="night") + result = self.client_post("/json/zcommand", payload) + self.assert_json_success(result) + self.assertIn('Changed to night', result.json()['msg']) + + result = self.client_post("/json/zcommand", payload) + self.assert_json_success(result) + self.assertIn('still in night mode', result.json()['msg']) + + def test_day_zcommand(self) -> None: + self.login(self.example_email("hamlet")) + user = self.example_user('hamlet') + user.night_mode = True + user.save() + + payload = dict(command="day") + result = self.client_post("/json/zcommand", payload) + self.assert_json_success(result) + self.assertIn('Changed to day', result.json()['msg']) + + result = self.client_post("/json/zcommand", payload) + self.assert_json_success(result) + self.assertIn('still in day mode', result.json()['msg']) diff --git a/zerver/views/messages.py b/zerver/views/messages.py index c6752a4f31..a1a8f1df2a 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -13,12 +13,12 @@ from zerver.decorator import has_request_variables, \ REQ, to_non_negative_int from django.utils.html import escape as escape_html from zerver.lib import bugdown +from zerver.lib.zcommand import process_zcommands from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \ compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \ create_mirror_user_if_needed, check_send_message, do_update_message, \ extract_recipients, truncate_body, render_incoming_message, do_delete_message, \ do_mark_all_as_read, do_mark_stream_messages_as_read, \ - do_set_user_display_setting, \ get_user_info_for_message_updates, check_schedule_message from zerver.lib.queue import queue_json_publish from zerver.lib.message import ( @@ -684,29 +684,7 @@ def find_first_unread_anchor(sa_conn: Any, @has_request_variables def zcommand_backend(request: HttpRequest, user_profile: UserProfile, command: str=REQ('command')) -> HttpResponse: - if command == 'ping': - ret = dict() # type: Dict[str, Any] - return json_success(ret) - - if command == 'night': - if user_profile.night_mode: - msg = 'You are still in night mode.' - else: - msg = 'Changed to night mode! To revert night mode, type `/day`.' - do_set_user_display_setting(user_profile, 'night_mode', True) - ret = dict(msg=msg) - return json_success(ret) - - if command == 'day': - if user_profile.night_mode: - msg = 'Changed to day mode! To revert day mode, type `/night`.' - do_set_user_display_setting(user_profile, 'night_mode', False) - else: - msg = 'You are still in day mode.' - ret = dict(msg=msg) - return json_success(ret) - - raise JsonableError(_('No such command: %s') % (command,)) + return json_success(process_zcommands(command, user_profile)) @has_request_variables def get_messages_backend(request: HttpRequest, user_profile: UserProfile,