Skip to content

Commit ea8321d

Browse files
committed
helper: Secure Linux & MacOS notifications & remove WSL for now.
Tests amended to updated lengths and xfail.
1 parent 2a9fa8b commit ea8321d

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

tests/helper/test_helper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ def test_invalid_color_format(mocker, color):
236236

237237

238238
@pytest.mark.parametrize('OS, is_notification_sent', [
239-
([True, False, False], True), # OS: [WSL, MACOS, LINUX]
239+
pytest.param([True, False, False], True, # OS: [WSL, MACOS, LINUX]
240+
marks=pytest.mark.xfail(reason="WSL notify disabled")),
240241
([False, True, False], True),
241242
([False, False, True], True),
242243
([False, False, False], False), # Unsupported OS
@@ -259,9 +260,10 @@ def test_notify(mocker, OS, is_notification_sent):
259260
"X", 'Spaced title', "'", '"'
260261
], ids=["X", "spaced_title", "single", "double"])
261262
@pytest.mark.parametrize('OS, cmd_length', [
262-
('LINUX', 3),
263-
('MACOS', 3),
264-
('WSL', 2)
263+
('LINUX', 4),
264+
('MACOS', 10),
265+
pytest.param('WSL', 2,
266+
marks=pytest.mark.xfail(reason="WSL notify disabled"))
265267
])
266268
def test_notify_quotes(monkeypatch, mocker,
267269
OS, cmd_length, title, text):

zulipterminal/helper.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import platform
3-
import shlex
43
import subprocess
54
import time
65
from collections import OrderedDict, defaultdict
@@ -591,39 +590,21 @@ def canonicalize_color(color: str) -> str:
591590
def notify(title: str, html_text: str) -> str:
592591
document = lxml.html.document_fromstring(html_text)
593592
text = document.text_content()
594-
quoted_text = shlex.quote(text)
595-
596-
quoted_title = shlex.quote(title)
597593

598594
command_list = None
599-
if WSL: # NOTE Tested and should work!
600-
# Escaping of quotes in powershell is done using ` instead of \
601-
escaped_text = text.replace('\'', '`\'').replace('\"', '`\"')
602-
quoted_text = '\"' + escaped_text + '\"'
595+
if MACOS:
603596
command_list = [
604-
'powershell.exe',
605-
"New-BurntToastNotification -Text {}, {}"
606-
.format(quoted_title, quoted_text)
597+
"osascript",
598+
"-e", "on run(argv)",
599+
"-e", "return display notification item 1 of argv with title "
600+
'item 2 of argv sound name "ZT_NOTIFICATION_SOUND"',
601+
"-e", "end",
602+
"--", text, title
607603
]
608-
expected_length = 2
609-
elif MACOS: # NOTE Tested and should work!
610-
command_list = shlex.split(
611-
"osascript -e "
612-
"'display notification \"\'{}\'\" with title \"\'{}\'\" "
613-
" sound name \"ZT_NOTIFICATION_SOUND\"'"
614-
.format(quoted_text, quoted_title)
615-
)
616-
expected_length = 3
617604
elif LINUX:
618-
command_list = shlex.split(
619-
'notify-send {} {}'.format(quoted_title, quoted_text)
620-
)
621-
expected_length = 3
605+
command_list = ["notify-send", "--", title, text]
622606

623607
if command_list is not None:
624-
# NOTE: We assert this in tests, but this signals unexpected breakage
625-
assert len(command_list) == expected_length
626-
627608
try:
628609
subprocess.run(command_list, stdout=subprocess.DEVNULL,
629610
stderr=subprocess.DEVNULL)

0 commit comments

Comments
 (0)