Skip to content

Commit f20d94e

Browse files
committed
refactor: run: Fetch server settings in get_api_key, not get_login_id.
This centralizes the requests calls for this logic into only get_api_key. Tests updated (now simplified).
1 parent f1343bc commit f20d94e

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

tests/cli/test_run.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,13 @@ def test_in_color(color: str, code: str, text: str = "some text") -> None:
5555
],
5656
)
5757
def test_get_login_id(mocker: MockerFixture, json: Dict[str, bool], label: str) -> None:
58-
response = mocker.Mock(json=lambda: json)
59-
mocked_get = mocker.patch("requests.get", return_value=response)
6058
mocked_styled_input = mocker.patch(
6159
MODULE + ".styled_input", return_value="input return value"
6260
)
6361

64-
result = get_login_id("REALM_URL")
62+
result = get_login_id(json)
6563

6664
assert result == "input return value"
67-
mocked_get.assert_called_with(url="REALM_URL/api/v1/server_settings")
6865
mocked_styled_input.assert_called_with(label + ": ")
6966

7067

zulipterminal/cli/run.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import traceback
1212
from enum import Enum
1313
from os import path, remove
14-
from typing import Dict, List, NamedTuple, Optional, Tuple
14+
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
1515

1616
import requests
1717
from urwid import display_common, set_encoding
@@ -206,10 +206,9 @@ def styled_input(label: str) -> str:
206206
return input(in_color("blue", label))
207207

208208

209-
def get_login_id(realm_url: str) -> str:
210-
res_json = requests.get(url=f"{realm_url}/api/v1/server_settings").json()
211-
require_email_format_usernames = res_json["require_email_format_usernames"]
212-
email_auth_enabled = res_json["email_auth_enabled"]
209+
def get_login_id(server_properties: Dict[str, Any]) -> str:
210+
require_email_format_usernames = server_properties["require_email_format_usernames"]
211+
email_auth_enabled = server_properties["email_auth_enabled"]
213212

214213
if not require_email_format_usernames and email_auth_enabled:
215214
label = "Email or Username: "
@@ -225,8 +224,11 @@ def get_login_id(realm_url: str) -> str:
225224
def get_api_key(realm_url: str) -> Optional[Tuple[str, str]]:
226225
from getpass import getpass
227226

228-
login_id = get_login_id(realm_url)
227+
server_properties = requests.get(url=f"{realm_url}/api/v1/server_settings").json()
228+
229+
login_id = get_login_id(server_properties)
229230
password = getpass(in_color("blue", "Password: "))
231+
230232
response = requests.post(
231233
url=f"{realm_url}/api/v1/fetch_api_key",
232234
data={

0 commit comments

Comments
 (0)