Skip to content

Commit b92996b

Browse files
Validate file sharing creation statuses.
1 parent 4e1f9dd commit b92996b

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎pythonanywhere_core/files.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ def sharing_post(self, path: str) -> Tuple[str, str]:
117117
"""Starts sharing a file at `path`.
118118
119119
Returns a tuple with a message and sharing link on
120-
success, raises otherwise. Message is "successfully shared" on success,
121-
"was already shared" if file has been already shared."""
120+
HTTP 200/201, raises otherwise. Message is "successfully shared" on 201,
121+
"was already shared" on 200."""
122122

123123
url = self.sharing_endpoint
124124

125125
result = call_api(url, "POST", json={"path": path})
126126

127-
if result.ok:
128-
msg = {200: "was already shared", 201: "successfully shared"}[result.status_code]
127+
messages = {200: "was already shared", 201: "successfully shared"}
128+
if result.status_code in messages:
129+
msg = messages[result.status_code]
129130
sharing_url_suffix = result.json()["url"]
130131
return msg, self._make_sharing_url(sharing_url_suffix)
131132

‎tests/test_files.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ def test_sharing_post_returns_url_when_path_successfully_shared_or_has_been_shar
274274
assert second_share[1] == sharing_url
275275

276276

277+
@pytest.mark.parametrize("status_code", [202, 204, 400, 403, 500])
278+
def test_sharing_post_rejects_unexpected_statuses(
279+
api_token, api_responses, base_url, home_dir_path, status_code
280+
):
281+
url = urljoin(base_url, "sharing/")
282+
if status_code == 204:
283+
api_responses.add(responses.POST, url, status=status_code, body="")
284+
else:
285+
api_responses.add(
286+
responses.POST, url, status=status_code, json={"detail": "Sharing failed"}
287+
)
288+
289+
with pytest.raises(PythonAnywhereApiException) as exc:
290+
Files().sharing_post(f"{home_dir_path}/README.txt")
291+
292+
assert str(status_code) in str(exc.value)
293+
if status_code != 204:
294+
assert "Sharing failed" in str(exc.value)
295+
assert len(api_responses.calls) == 1
296+
297+
277298
@pytest.mark.skip(reason="not implemented in the api yet")
278299
def test_sharing_post_raises_exception_when_path_not_provided(
279300
api_token, api_responses, base_url, home_dir_path

0 commit comments

Comments
 (0)