Skip to content

Commit 5b26d3f

Browse files
Distinguish unshared files from sharing API failures.
1 parent aa3cd6c commit 5b26d3f

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎pythonanywhere_core/files.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,21 @@ def sharing_get(self, path: str) -> str:
137137
"""Checks sharing status for a `path`.
138138
139139
Returns url with sharing link if file is shared or an empty
140-
string otherwise."""
140+
string on HTTP 404 (not shared). Raises on other API errors."""
141141

142142
url = f"{self.sharing_endpoint}?path={path}"
143143

144144
result = call_api(url, "GET")
145145
if result.ok:
146146
sharing_url_suffix = result.json()["url"]
147147
return self._make_sharing_url(sharing_url_suffix)
148-
else:
148+
if result.status_code == 404:
149149
return ""
150150

151+
raise PythonAnywhereApiException(
152+
f"GET sharing status for {url} failed, got {result}{self._error_msg(result)}"
153+
)
154+
151155
def sharing_delete(self, path: str) -> int:
152156
"""Stops sharing file at `path`.
153157

‎tests/test_files.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,23 @@ def test_sharing_get_returns_empty_string_when_path_not_shared(
327327
assert Files().sharing_get(valid_path) == ""
328328

329329

330+
@pytest.mark.parametrize("status_code", [400, 403, 429, 500])
331+
def test_sharing_get_raises_on_errors_other_than_not_found(
332+
api_token, api_responses, base_url, home_dir_path, status_code
333+
):
334+
path = f"{home_dir_path}/README.txt"
335+
url = urljoin(base_url, f"sharing/?path={path}")
336+
api_responses.add(
337+
responses.GET, url=url, status=status_code, json={"detail": "request failed"}
338+
)
339+
340+
with pytest.raises(PythonAnywhereApiException) as exc:
341+
Files().sharing_get(path)
342+
343+
assert str(status_code) in str(exc.value)
344+
assert "request failed" in str(exc.value)
345+
346+
330347
def test_returns_204_on_sucessful_unshare(
331348
api_token, api_responses, base_url, home_dir_path, username
332349
):

0 commit comments

Comments
 (0)