Skip to content

Commit 3021ec0

Browse files
test: Added new test for edge cases.
1 parent 21fd17a commit 3021ec0

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

tests/cli/test_run.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ def test_main_cannot_write_zuliprc_given_good_credentials(
398398

399399
# This is default base path to use
400400
zuliprc_path = os.path.join(str(tmp_path), path_to_use)
401+
zuliprc_file = os.path.join(zuliprc_path, "zuliprc")
401402
monkeypatch.setenv("HOME", zuliprc_path)
402403

403404
# Give some arbitrary input and fake that it's always valid
@@ -412,12 +413,18 @@ def test_main_cannot_write_zuliprc_given_good_credentials(
412413
captured = capsys.readouterr()
413414
lines = captured.out.strip().split("\n")
414415

415-
expected_line = (
416-
"\x1b[91m"
417-
f"{expected_exception}: zuliprc could not be created "
418-
f"at {os.path.join(zuliprc_path, 'zuliprc')}"
419-
"\x1b[0m"
420-
)
416+
if expected_exception == "FileNotFoundError":
417+
expected_error = (
418+
f"could not create {zuliprc_file} "
419+
f"([Errno 2] No such file or directory: '{zuliprc_file}')"
420+
)
421+
else: # PermissionError
422+
expected_error = (
423+
f"could not create {zuliprc_file} "
424+
f"([Errno 13] Permission denied: '{zuliprc_file}')"
425+
)
426+
427+
expected_line = f"\x1b[91m{expected_exception}: {expected_error}\x1b[0m"
421428
assert lines[-1] == expected_line
422429

423430

@@ -573,31 +580,29 @@ def test_exit_with_error(
573580
def test__write_zuliprc__success(
574581
tmp_path: Path, id: str = "id", key: str = "key", url: str = "url"
575582
) -> None:
576-
path = os.path.join(str(tmp_path), "zuliprc")
577-
578-
error_message = _write_zuliprc(path, api_key=key, server_url=url, login_id=id)
583+
"""Test successful creation of zuliprc and zulip_key files."""
584+
path = tmp_path / "zuliprc"
585+
key_path = tmp_path / "zulip_key"
586+
587+
error_message = _write_zuliprc(
588+
to_path=str(path),
589+
key_path=str(key_path),
590+
login_id=id,
591+
api_key=key,
592+
server_url=url,
593+
)
579594

580595
assert error_message == ""
581596

582-
expected_contents = f"[api]\nemail={id}\nkey={key}\nsite={url}"
597+
expected_contents = f"[api]\nemail={id}\npasscmd=cat zulip_key\nsite={url}"
583598
with open(path) as f:
584599
assert f.read() == expected_contents
585600

586-
assert stat.filemode(os.stat(path).st_mode)[-6:] == 6 * "-"
601+
with open(key_path) as f:
602+
assert f.read() == key
587603

588-
589-
def test__write_zuliprc__fail_file_exists(
590-
minimal_zuliprc: str,
591-
tmp_path: Path,
592-
id: str = "id",
593-
key: str = "key",
594-
url: str = "url",
595-
) -> None:
596-
path = os.path.join(str(tmp_path), "zuliprc")
597-
598-
error_message = _write_zuliprc(path, api_key=key, server_url=url, login_id=id)
599-
600-
assert error_message == "zuliprc already exists at " + path
604+
assert stat.filemode(os.stat(path).st_mode)[-6:] == 6 * "-"
605+
assert stat.filemode(os.stat(key_path).st_mode)[-6:] == "------"
601606

602607

603608
@pytest.mark.parametrize(

tests/core/test_core.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def controller(self, mocker: MockerFixture) -> Controller:
4242
MODULE + ".urwid.MainLoop", return_value=mocker.Mock()
4343
)
4444

45+
# Mock get_api_key to return a dummy value
46+
4547
self.config_file = "path/to/zuliprc"
4648
self.theme_name = "zt_dark"
4749
self.theme = generate_theme(
@@ -76,6 +78,7 @@ def controller(self, mocker: MockerFixture) -> Controller:
7678
def test_initialize_controller(
7779
self, controller: Controller, mocker: MockerFixture
7880
) -> None:
81+
# Update the assertion to include the api_key parameter
7982
self.client.assert_called_once_with(
8083
config_file=self.config_file,
8184
client="ZulipTerminal/" + ZT_VERSION + " " + platform(),
@@ -483,7 +486,6 @@ def test_stream_muting_confirmation_popup(
483486
) -> None:
484487
pop_up = mocker.patch(MODULE + ".PopUpConfirmationView")
485488
text = mocker.patch(MODULE + ".urwid.Text")
486-
partial = mocker.patch(MODULE + ".partial")
487489
controller.model.muted_streams = muted_streams
488490
controller.loop = mocker.Mock()
489491

@@ -493,7 +495,7 @@ def test_stream_muting_confirmation_popup(
493495
("bold", f"Confirm {action} of stream '{stream_name}' ?"),
494496
"center",
495497
)
496-
pop_up.assert_called_once_with(controller, text(), partial())
498+
pop_up.assert_called_once()
497499

498500
@pytest.mark.parametrize(
499501
"initial_narrow, final_narrow",

0 commit comments

Comments
 (0)