-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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'.
- Loading branch information
1 parent
76cb159
commit ad7e3a2
Showing
4 changed files
with
149 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from typing import Any, Dict | ||
from django.utils.translation import ugettext as _ | ||
from django.core.exceptions import ObjectDoesNotExist | ||
|
||
from zerver.models import UserProfile | ||
from zerver.lib.topic_mutes import topic_is_muted | ||
from zerver.lib.actions import do_set_user_display_setting, do_mute_topic | ||
from zerver.models import get_realm_stream, topic_exists, get_stream_recipient | ||
from zerver.lib.exceptions import JsonableError | ||
|
||
def process_zcommands(command: str, user_profile: UserProfile) -> Dict[str, Any]: | ||
tokens = command.split(' ') | ||
|
||
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 | ||
|
||
if tokens[0] == 'mute_topic': | ||
if len(tokens) < 3 or tokens[1] == 'help': | ||
return dict(msg="Usage: /mute_topic <stream_name> <topic_name>.") | ||
else: | ||
topic = ' '.join(tokens[2:]) | ||
stream_name = tokens[1] | ||
try: | ||
stream = get_realm_stream(stream_name=stream_name, | ||
realm_id=user_profile.realm.id) | ||
recipient = get_stream_recipient(stream.id) | ||
if not topic_exists(topic, recipient): | ||
msg = ("A valid topic name is required.") | ||
else: | ||
if topic_is_muted(user_profile, stream.id, topic): | ||
msg = ("The topic '%s' is already muted." % (topic)) | ||
else: | ||
do_mute_topic(user_profile, stream, recipient, topic) | ||
msg = ("The topic '%s' has been muted." % (topic)) | ||
except ObjectDoesNotExist: | ||
msg = ('A valid stream name is required.') | ||
ret = dict(msg=msg) | ||
return ret | ||
|
||
raise JsonableError(_('No such command: %s') % (command,)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- 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']) | ||
|
||
def test_mute_topic_zcommand(self) -> None: | ||
self.login(self.example_email("hamlet")) | ||
|
||
payload = dict(command="mute_topic help") | ||
result = self.client_post("/json/zcommand", payload) | ||
self.assert_json_success(result) | ||
self.assertIn('Usage: /mute_topic <stream_name> <topic_name>.', result.json()['msg']) | ||
|
||
payload = dict(command="mute_topic invalid") | ||
result = self.client_post("/json/zcommand", payload) | ||
self.assert_json_success(result) | ||
self.assertIn('Usage: /mute_topic <stream_name> <topic_name>.', result.json()['msg']) | ||
|
||
payload = dict(command="mute_topic invalid_stream topic") | ||
result = self.client_post("/json/zcommand", payload) | ||
self.assert_json_success(result) | ||
self.assertIn('A valid stream name is required.', result.json()['msg']) | ||
|
||
payload = dict(command="mute_topic Denmark invalid_topic") | ||
result = self.client_post("/json/zcommand", payload) | ||
self.assert_json_success(result) | ||
self.assertIn('A valid topic name is required.', result.json()['msg']) | ||
|
||
payload = dict(command="mute_topic Verona Verona1") | ||
result = self.client_post("/json/zcommand", payload) | ||
self.assert_json_success(result) | ||
self.assertIn("The topic 'Verona1' has been muted.", result.json()['msg']) | ||
|
||
payload = dict(command="mute_topic Verona Verona1") | ||
result = self.client_post("/json/zcommand", payload) | ||
self.assert_json_success(result) | ||
self.assertIn("The topic 'Verona1' is already muted.", result.json()['msg']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters