Skip to content

Commit 50846c5

Browse files
makes sharing_get return files sharing link.
1 parent eca2fdf commit 50846c5

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

pythonanywhere_core/files.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def _error_msg(self, result: Response) -> str:
5555
return f": {msg}"
5656
return ""
5757

58+
def _make_sharing_url(self, sharing_url_suffix):
59+
return urljoin(self.base_url.split("api")[0], sharing_url_suffix)
60+
5861
def path_get(self, path: str) -> Union[dict, bytes]:
5962
"""Returns dictionary of directory contents when `path` is an
6063
absolute path to of an existing directory or file contents if
@@ -140,8 +143,11 @@ def sharing_get(self, path: str) -> str:
140143
url = f"{self.sharing_endpoint}?path={path}"
141144

142145
result = call_api(url, "GET")
143-
144-
return result.json()["url"] if result.ok else ""
146+
if result.ok:
147+
sharing_url_suffix = result.json()["url"]
148+
return self._make_sharing_url(sharing_url_suffix)
149+
else:
150+
return ""
145151

146152
def sharing_delete(self, path: str) -> int:
147153
"""Stops sharing file at `path`.

tests/test_files.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_path_delete_raises_when_wrong_path_provided(
248248

249249

250250
def test_sharing_post_returns_url_when_path_successfully_shared_or_has_been_shared_before(
251-
api_token, api_responses, base_url, home_dir_path
251+
api_token, api_responses, base_url, home_dir_path, username
252252
):
253253
valid_path = f"{home_dir_path}/README.txt"
254254
shared_url = f"/user/{username}/shares/asdf1234/"
@@ -297,22 +297,23 @@ def test_sharing_post_raises_exception_when_path_not_provided(
297297

298298

299299
def test_sharing_get_returns_sharing_url_when_path_is_shared(
300-
api_token, api_responses, base_url, home_dir_path
300+
api_token, api_responses, base_url, home_dir_path, username
301301
):
302302
valid_path = f"{home_dir_path}/README.txt"
303-
sharing_url = urljoin(base_url, f"sharing/")
303+
sharing_api_url = urljoin(base_url, "sharing/")
304304
get_url = urljoin(base_url, f"sharing/?path={valid_path}")
305-
shared_url = f"/user/{username}/shares/asdf1234/"
305+
shared_url_suffix = f"/user/{username}/shares/asdf1234/"
306+
sharing_url = urljoin(base_url.split("api")[0], shared_url_suffix)
306307
partial_response = dict(
307-
body=bytes(f'{{"url": "{shared_url}"}}', "utf"),
308+
body=bytes(f'{{"url": "{shared_url_suffix}"}}', "utf"),
308309
headers={"Content-Type": "application/json"},
309310
)
310-
api_responses.add(**partial_response, method=responses.POST, url=sharing_url, status=201)
311+
api_responses.add(**partial_response, method=responses.POST, url=sharing_api_url, status=201)
311312
api_responses.add(**partial_response, method=responses.GET, url=get_url, status=200)
312313
files = Files()
313314
files.sharing_post(valid_path)
314315

315-
assert files.sharing_get(valid_path) == shared_url
316+
assert files.sharing_get(valid_path) == sharing_url
316317

317318

318319
def test_sharing_get_returns_empty_string_when_path_not_shared(
@@ -326,7 +327,7 @@ def test_sharing_get_returns_empty_string_when_path_not_shared(
326327

327328

328329
def test_returns_204_on_sucessful_unshare(
329-
api_token, api_responses, base_url, home_dir_path
330+
api_token, api_responses, base_url, home_dir_path, username
330331
):
331332
valid_path = f"{home_dir_path}/README.txt"
332333
url = urljoin(base_url, f"sharing/?path={valid_path}")

0 commit comments

Comments
 (0)