Skip to content

Commit

Permalink
refactor: core/views: Adapt open_in_browser to depend only on link.
Browse files Browse the repository at this point in the history
This commit refactors the open_in_browser function(previously
view_in_browser) to only depend on a provided url so that its use-case
can be extended beyond just the opening of messages in browser to
opening all kind of links in the browser.
This commit would pe a prep for adding support for handling opening
external links in the browser from the MsgLinkButton.

Tests amended.
  • Loading branch information
zee-bit committed Apr 28, 2021
1 parent d2f11b8 commit 58aa268
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
20 changes: 9 additions & 11 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,25 +272,23 @@ def test_narrow_to_all_mentions(
msg_ids = {widget.original_widget.message['id'] for widget in widgets}
assert msg_ids == id_list

@pytest.mark.parametrize('server_url', [
'https://chat.zulip.org/',
'https://foo.zulipchat.com/',
@pytest.mark.parametrize('url', [
'https://chat.zulip.org/#narrow/stream/test',
'https://chat.zulip.org/user_uploads/sent/abcd/efg.png',
'https://github.com/',
])
def test_view_in_browser(self, mocker, controller, message_fixture,
server_url):
def test_open_in_browser(self, mocker, controller, message_fixture, url):
# Set DISPLAY environ to be able to run test in CI
os.environ['DISPLAY'] = ':0'
controller.model.server_url = server_url
message_id = message_fixture['id']

mocked_get = mocker.patch(CORE + '.webbrowser.get')
mocked_open = mocker.patch(CORE + '.webbrowser.open')

controller.view_in_browser(message_fixture)
url = mocked_open.call_args[0][0]
controller.open_in_browser(url)
called_url = mocked_open.call_args[0][0]

assert mocked_open.call_count == 1
assert url.startswith(controller.model.server_url)
assert url.endswith('/{}'.format(message_id))
assert called_url == url

def test_main(self, mocker, controller):
controller.view.palette = {
Expand Down
7 changes: 4 additions & 3 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,15 @@ def test_keypress_exit_popup(self, key, widget_size):
assert self.controller.exit_popup.called

@pytest.mark.parametrize('key', keys_for_command('VIEW_IN_BROWSER'))
def test_keypress_view_in_browser(self, widget_size,
def test_keypress_view_in_browser(self, mocker, widget_size,
message_fixture, key):
size = widget_size(self.msg_info_view)
self.msg_info_view.server_url = 'https://chat.zulip.org/'
mocker.patch(VIEWS + '.near_message_url')

self.msg_info_view.keypress(size, key)

(self.controller.view_in_browser.
assert_called_once_with(message_fixture))
assert self.controller.open_in_browser.called

def test_height_noreactions(self):
expected_height = 4
Expand Down
6 changes: 1 addition & 5 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from zulipterminal.config.themes import ThemeSpec
from zulipterminal.helper import LINUX, asynch, suppress_output
from zulipterminal.model import Model
from zulipterminal.server_url import near_message_url
from zulipterminal.ui import Screen, View
from zulipterminal.ui_tools.utils import create_msg_box_list
from zulipterminal.ui_tools.views import (
Expand Down Expand Up @@ -255,10 +254,7 @@ def show_edit_history(
'area:msg'
)

def view_in_browser(self, message: Message) -> None:
# Truncate extra '/' from the server url.
url = near_message_url(self.model.server_url[:-1], message)

def open_in_browser(self, url: str) -> None:
# Don't try to open web browser if running without a GUI
if (LINUX and not os.environ.get('DISPLAY')
and os.environ.get('TERM')):
Expand Down
5 changes: 4 additions & 1 deletion zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from zulipterminal.config.ui_mappings import EDIT_MODE_CAPTIONS
from zulipterminal.helper import Message, asynch, match_stream, match_user
from zulipterminal.server_url import near_message_url
from zulipterminal.ui_tools.boxes import MessageBox, PanelSearchBox
from zulipterminal.ui_tools.buttons import (
HomeButton,
Expand Down Expand Up @@ -1223,6 +1224,7 @@ def __init__(self, controller: Any, msg: Message, title: str,
self.topic_links = topic_links
self.message_links = message_links
self.time_mentions = time_mentions
self.server_url = controller.model.server_url
date_and_time = controller.model.formatted_local_time(
msg['timestamp'], show_seconds=True, show_year=True
)
Expand Down Expand Up @@ -1347,7 +1349,8 @@ def keypress(self, size: urwid_Size, key: str) -> str:
time_mentions=self.time_mentions,
)
elif is_command_key('VIEW_IN_BROWSER', key):
self.controller.view_in_browser(self.msg)
url = near_message_url(self.server_url[:-1], self.msg)
self.controller.open_in_browser(url)
return super().keypress(size, key)


Expand Down

0 comments on commit 58aa268

Please sign in to comment.