Skip to content

Commit d6015ee

Browse files
committed
bugfix: run: Respect the server's choice of URL when fetching api key.
In the case where we connect to the server to fetch the api_key, but don't use the preferred realm_url (with protocol), the server gives a 405 error. This is interpreted by the current logic as that the email or password is incorrect, which is confusing to a user. Instead, use the server data that we already have available to adjust the entered value to that preferred by the server. The preferred URL is then passed back to be written in any created zuliprc. Test updated. This code could benefit from additional error handling, but this fixes #1318.
1 parent f20d94e commit d6015ee

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

tests/cli/test_run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ def test_main_cannot_write_zuliprc_given_good_credentials(
325325

326326
# Give some arbitrary input and fake that it's always valid
327327
mocker.patch.object(builtins, "input", lambda _: "text\n")
328-
mocker.patch(MODULE + ".get_api_key", return_value=("my_login", "my_api_key"))
328+
mocker.patch(
329+
MODULE + ".get_api_key", return_value=("my_site", "my_login", "my_api_key")
330+
)
329331

330332
with pytest.raises(SystemExit):
331333
main([])

zulipterminal/cli/run.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,27 @@ def get_login_id(server_properties: Dict[str, Any]) -> str:
221221
return styled_input(label)
222222

223223

224-
def get_api_key(realm_url: str) -> Optional[Tuple[str, str]]:
224+
def get_api_key(realm_url: str) -> Optional[Tuple[str, str, str]]:
225225
from getpass import getpass
226226

227227
server_properties = requests.get(url=f"{realm_url}/api/v1/server_settings").json()
228228

229+
# Assuming we connect to and get data from the server, use the realm_url it suggests
230+
# This avoids cases where there are redirects between http and https, for example
231+
preferred_realm_url = server_properties["realm_uri"]
232+
229233
login_id = get_login_id(server_properties)
230234
password = getpass(in_color("blue", "Password: "))
231235

232236
response = requests.post(
233-
url=f"{realm_url}/api/v1/fetch_api_key",
237+
url=f"{preferred_realm_url}/api/v1/fetch_api_key",
234238
data={
235239
"username": login_id,
236240
"password": password,
237241
},
238242
)
239243
if response.status_code == requests.codes.OK:
240-
return login_id, str(response.json()["api_key"])
244+
return preferred_realm_url, login_id, str(response.json()["api_key"])
241245
return None
242246

243247

@@ -267,12 +271,12 @@ def fetch_zuliprc(zuliprc_path: str) -> None:
267271
print(in_color("red", "\nIncorrect Email(or Username) or Password!\n"))
268272
login_data = get_api_key(realm_url)
269273

270-
login_id, api_key = login_data
274+
preferred_realm_url, login_id, api_key = login_data
271275
save_zuliprc_failure = _write_zuliprc(
272276
zuliprc_path,
273277
login_id=login_id,
274278
api_key=api_key,
275-
server_url=realm_url,
279+
server_url=preferred_realm_url,
276280
)
277281
if not save_zuliprc_failure:
278282
print(f"Generated API key saved at {zuliprc_path}")

0 commit comments

Comments
 (0)