mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 00:46:03 +00:00
zcommands: Add zcommand module and separate test module.
Move the zcommands from '/views/messages.py' to '/lib/zcommand'. Also, move the zcommand tests from '/tests/test_messages.py' to '/tests/test_zcommand'.
This commit is contained in:
32
zerver/lib/zcommand.py
Normal file
32
zerver/lib/zcommand.py
Normal file
@@ -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,))
|
||||||
@@ -999,39 +999,6 @@ class SewMessageAndReactionTest(ZulipTestCase):
|
|||||||
|
|
||||||
class MessagePOSTTest(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:
|
def test_message_to_self(self) -> None:
|
||||||
"""
|
"""
|
||||||
Sending a message to a stream to which you are subscribed is
|
Sending a message to a stream to which you are subscribed is
|
||||||
|
|||||||
51
zerver/tests/test_zcommand.py
Normal file
51
zerver/tests/test_zcommand.py
Normal file
@@ -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'])
|
||||||
@@ -13,12 +13,12 @@ from zerver.decorator import has_request_variables, \
|
|||||||
REQ, to_non_negative_int
|
REQ, to_non_negative_int
|
||||||
from django.utils.html import escape as escape_html
|
from django.utils.html import escape as escape_html
|
||||||
from zerver.lib import bugdown
|
from zerver.lib import bugdown
|
||||||
|
from zerver.lib.zcommand import process_zcommands
|
||||||
from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \
|
from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \
|
||||||
compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \
|
compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \
|
||||||
create_mirror_user_if_needed, check_send_message, do_update_message, \
|
create_mirror_user_if_needed, check_send_message, do_update_message, \
|
||||||
extract_recipients, truncate_body, render_incoming_message, do_delete_message, \
|
extract_recipients, truncate_body, render_incoming_message, do_delete_message, \
|
||||||
do_mark_all_as_read, do_mark_stream_messages_as_read, \
|
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
|
get_user_info_for_message_updates, check_schedule_message
|
||||||
from zerver.lib.queue import queue_json_publish
|
from zerver.lib.queue import queue_json_publish
|
||||||
from zerver.lib.message import (
|
from zerver.lib.message import (
|
||||||
@@ -684,29 +684,7 @@ def find_first_unread_anchor(sa_conn: Any,
|
|||||||
@has_request_variables
|
@has_request_variables
|
||||||
def zcommand_backend(request: HttpRequest, user_profile: UserProfile,
|
def zcommand_backend(request: HttpRequest, user_profile: UserProfile,
|
||||||
command: str=REQ('command')) -> HttpResponse:
|
command: str=REQ('command')) -> HttpResponse:
|
||||||
if command == 'ping':
|
return json_success(process_zcommands(command, user_profile))
|
||||||
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,))
|
|
||||||
|
|
||||||
@has_request_variables
|
@has_request_variables
|
||||||
def get_messages_backend(request: HttpRequest, user_profile: UserProfile,
|
def get_messages_backend(request: HttpRequest, user_profile: UserProfile,
|
||||||
|
|||||||
Reference in New Issue
Block a user