diff --git a/tests/core/test_core.py b/tests/core/test_core.py index 4361ce02fb..9971635968 100644 --- a/tests/core/test_core.py +++ b/tests/core/test_core.py @@ -272,17 +272,20 @@ def test_narrow_to_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): controller.view.palette = { diff --git a/tests/ui_tools/test_popups.py b/tests/ui_tools/test_popups.py index bb21429307..8ea46e390b 100644 --- a/tests/ui_tools/test_popups.py +++ b/tests/ui_tools/test_popups.py @@ -511,7 +511,7 @@ def test_keypress_view_in_browser(self, widget_size, message_fixture, key): 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 6745e41585..fd405e8285 100644 --- a/zulipterminal/core.py +++ b/zulipterminal/core.py @@ -17,6 +17,7 @@ from zulipterminal.config.themes import ThemeSpec from zulipterminal.helper import MACOS, WSL, 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 ( @@ -365,15 +366,16 @@ def narrow_to_all_mentions(self) -> None: # (nothing currently requires narrowing around a message id) self._narrow_to(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 b13c16747f..32bf8a62eb 100644 --- a/zulipterminal/ui_tools/views.py +++ b/zulipterminal/ui_tools/views.py @@ -1307,7 +1307,7 @@ 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['id']) + self.controller.view_in_browser(self.msg) return super().keypress(size, key)