diff --git a/tests/core/test_core.py b/tests/core/test_core.py index c1e76de504..bcb751894a 100644 --- a/tests/core/test_core.py +++ b/tests/core/test_core.py @@ -244,17 +244,20 @@ def test_show_all_mentions(self, mocker, controller, index_all_mentions): msg_ids = {widget.original_widget.message['id'] for widget in widgets} assert msg_ids == id_list - def test_view_in_browser(self, mocker, controller): + @pytest.mark.parametrize('server_url', [ + 'https://chat.zulip.org/', + 'https://foo.zulipchat.com/', + ]) + def test_view_in_browser(self, mocker, controller, message_fixture, + server_url): # Set DISPLAY environ to be able to run test in Travis os.environ['DISPLAY'] = ':0' - mock_open = mocker.patch('webbrowser.open', mocker.Mock()) - message_id = 123456 - controller.model.server_url = 'https://foo.zulipchat.com/' - controller.view_in_browser(message_id) - assert mock_open.call_count == 1 - url = mock_open.call_args[0][0] - assert url.startswith(controller.model.server_url) - assert url.endswith('/{}'.format(message_id)) + controller.model.server_url = server_url + mocked_open = mocker.patch(CORE + '.webbrowser.open') + + controller.view_in_browser(message_fixture) + + mocked_open.assert_called_once_with(controller.url) def test_main(self, mocker, controller): ret_mock = mocker.Mock() diff --git a/tests/ui/test_ui_tools.py b/tests/ui/test_ui_tools.py index 7d3adf9a0e..ea52b7d24e 100644 --- a/tests/ui/test_ui_tools.py +++ b/tests/ui/test_ui_tools.py @@ -1526,7 +1526,7 @@ def test_keypress_view_in_browser(self, message_fixture, key): size = (200, 20) self.msg_info_view.keypress(size, key) (self.controller.view_in_browser. - assert_called_once_with(message_fixture['id'])) + assert_called_once_with(message_fixture)) def test_height_noreactions(self): expected_height = 4 diff --git a/zulipterminal/core.py b/zulipterminal/core.py index 1d61235b7f..3fd0528996 100644 --- a/zulipterminal/core.py +++ b/zulipterminal/core.py @@ -12,7 +12,9 @@ import zulip from zulipterminal.config.themes import ThemeSpec -from zulipterminal.helper import MACOS, WSL, Message, asynch, suppress_output +from zulipterminal.helper import ( + MACOS, WSL, Message, asynch, near_message_url, suppress_output, +) from zulipterminal.model import Model from zulipterminal.ui import Screen, View from zulipterminal.ui_tools.utils import create_msg_box_list @@ -298,15 +300,16 @@ def show_all_mentions(self, button: Any) -> None: anchor=None, mentioned=True) - def view_in_browser(self, message_id: int) -> None: - url = '{}#narrow/near/{}'.format(self.model.server_url, message_id) + def view_in_browser(self, message: Message) -> None: + # Truncate extra '/' from the server url. + self.url = near_message_url(self.model.server_url[:-1], message) if (not MACOS and not WSL and not os.environ.get('DISPLAY') and os.environ.get('TERM')): # Don't try to open web browser if running without a GUI return with suppress_output(): # Suppress anything on stdout or stderr when opening the browser - webbrowser.open(url) + webbrowser.open(self.url) def deregister_client(self) -> None: queue_id = self.model.queue_id diff --git a/zulipterminal/ui_tools/views.py b/zulipterminal/ui_tools/views.py index 1b405c5e94..9885955810 100644 --- a/zulipterminal/ui_tools/views.py +++ b/zulipterminal/ui_tools/views.py @@ -1143,7 +1143,7 @@ def __init__(self, controller: Any, msg: Message, title: str, def keypress(self, size: urwid_Size, key: str) -> str: if is_command_key('VIEW_IN_BROWSER', key): - self.controller.view_in_browser(self.msg['id']) + self.controller.view_in_browser(self.msg) return super().keypress(size, key)