Skip to content

Commit

Permalink
buttons/helper: Add support for topic narrow in handle_narrow().
Browse files Browse the repository at this point in the history
Tests added.
  • Loading branch information
preetmishra committed Aug 5, 2020
1 parent b345bcd commit 47d85ea
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
15 changes: 14 additions & 1 deletion tests/helper/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import zulipterminal.helper
from zulipterminal.helper import (
canonicalize_color, classify_unread_counts, display_error_if_present,
index_messages, notify, powerset,
hash_util_decode, index_messages, notify, powerset,
)


Expand Down Expand Up @@ -298,3 +298,16 @@ def test_display_error_if_present(mocker, response, footer_updated):
set_footer_text.assert_called_once_with(response['msg'], 3)
else:
set_footer_text.assert_not_called()


@pytest.mark.parametrize('quoted_string, expected_unquoted_string', [
('(no.20topic)', '(no topic)'),
('.3Cstrong.3Exss.3C.2Fstrong.3E', '<strong>xss</strong>'),
('.23test-here.20.23T1.20.23T2.20.23T3', '#test-here #T1 #T2 #T3'),
('.2Edot', '.dot'),
('.3Aparty_parrot.3A', ':party_parrot:'),
])
def test_hash_util_decode(quoted_string, expected_unquoted_string):
return_value = hash_util_decode(quoted_string)

assert return_value == expected_unquoted_string
34 changes: 34 additions & 0 deletions tests/ui/test_ui_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2797,3 +2797,37 @@ def test_handle_narrow_to_stream(self, mocker, stream_dict, link,
assert self.controller.view.set_footer_text.called
assert not self.controller.narrow_to_stream.called
assert not self.controller.exit_popup.called

@pytest.mark.parametrize(['link', 'is_subscribed', 'stream_id',
'stream_name', 'topic_name'], [
(SERVER_URL + '/#narrow/stream/1-Stream-1/topic/Test', True, 1,
'Stream 1', 'Test'),
(SERVER_URL + '/#narrow/stream/2-Stream-2/topic/Topic.20test', True, 2,
'Stream 2', 'Topic test'),
(SERVER_URL + '/#narrow/stream/4-Stream-4/topic/Test', False, 4, None,
None),
])
def test_handle_narrow_to_topic(self, mocker, stream_dict, link,
is_subscribed, stream_id, stream_name,
topic_name):
self.controller.model.stream_dict = stream_dict
self.controller.view.set_footer_text = mocker.Mock()
self.controller.narrow_to_topic = mocker.Mock()
self.controller.exit_popup = mocker.Mock()
mocked_button = self.message_link_button(link=link)

mocked_button.handle_narrow()

assert mocked_button.stream_id == stream_id
if is_subscribed:
assert mocked_button.stream_name == stream_name
assert mocked_button.topic_name == topic_name
assert not self.controller.view.set_footer_text.called
assert self.controller.narrow_to_topic.called
assert self.controller.exit_popup.called
else:
assert not hasattr(mocked_button, 'stream_name')
assert not hasattr(mocked_button, 'topic_name')
assert self.controller.view.set_footer_text.called
assert not self.controller.narrow_to_topic.called
assert not self.controller.exit_popup.called
7 changes: 7 additions & 0 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Set, Tuple,
TypeVar, Union,
)
from urllib.parse import unquote

import lxml.html
from mypy_extensions import TypedDict
Expand Down Expand Up @@ -618,3 +619,9 @@ def display_error_if_present(response: Dict[str, Any], controller: Any
) -> None:
if response['result'] == 'error' and hasattr(controller, 'view'):
controller.view.set_footer_text(response['msg'], 3)


def hash_util_decode(string: str) -> str:
# Acknowledge custom string replacements in zulip/zulip's
# zerver/lib/url_encoding.py before unquote.
return unquote(string.replace('.', '%').replace('%2E', '.'))
13 changes: 10 additions & 3 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urwid

from zulipterminal.config.keys import is_command_key, keys_for_command
from zulipterminal.helper import hash_util_decode
from zulipterminal.urwid_types import urwid_Size


Expand Down Expand Up @@ -298,9 +299,8 @@ def handle_link(self, *_: Any) -> None:

def handle_narrow(self) -> None:
"""
Handles stream narrow.
Handles stream and topic narrow.
"""
# TODO: Handle topic narrow.
link_split = self.link.split('/')

self.stream_id = int(link_split[5].split('-')[0])
Expand All @@ -313,7 +313,14 @@ def handle_narrow(self) -> None:

self.stream_name = self.model.stream_dict[self.stream_id]['name']

if '/stream/' in self.link:
if '/topic/' in self.link:
encoded_topic = link_split[7]
self.topic_name = hash_util_decode(encoded_topic)
assert hasattr(self, 'stream_id')
assert hasattr(self, 'stream_name')
assert hasattr(self, 'topic_name')
self.controller.narrow_to_topic(self)
elif '/stream/' in self.link:
assert hasattr(self, 'stream_id')
assert hasattr(self, 'stream_name')
self.controller.narrow_to_stream(self)
Expand Down

0 comments on commit 47d85ea

Please sign in to comment.