Skip to content

Commit 1772557

Browse files
Extracts static files mapping creation into separate method.
1 parent 2f76d52 commit 1772557

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

pythonanywhere_core/webapp.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,22 @@ def create(self, python_version: str, virtualenv_path: Path, project_path: Path,
8888
"PATCH to set virtualenv path and source directory via API failed," f"got {response}:{response.text}"
8989
)
9090

91+
def create_static_file_mapping(self, url_path: str, directory_path: Path) -> None:
92+
"""Create a static file mapping via the API.
93+
94+
:param url_path: URL path (e.g., '/static/')
95+
:param directory_path: Filesystem path to serve (as Path)
96+
"""
97+
url = f"{self.domain_url}static_files/"
98+
call_api(url, "post", json=dict(url=url_path, path=str(directory_path)))
99+
91100
def add_default_static_files_mappings(self, project_path: Path) -> None:
92101
"""Add default static files mappings for /static/ and /media/
93102
94103
:param project_path: path to the project
95104
"""
96-
url = f"{self.domain_url}static_files/"
97-
call_api(url, "post", json=dict(url="/static/", path=str(Path(project_path) / "static")))
98-
call_api(url, "post", json=dict(url="/media/", path=str(Path(project_path) / "media")))
105+
self.create_static_file_mapping("/static/", Path(project_path) / "static")
106+
self.create_static_file_mapping("/media/", Path(project_path) / "media")
99107

100108
def reload(self) -> None:
101109
"""Reload webapp

tests/test_webapp.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import getpass
22
import json
33
from datetime import datetime
4+
from pathlib import Path
45

56
import pytest
67
import responses
@@ -209,31 +210,37 @@ def test_ignores_404_from_delete_call_when_nuking(api_responses, api_token, base
209210
webapp.create("3.10", "/virtualenv/path", "/project/path", nuke=True)
210211

211212

212-
def test_does_two_posts_to_static_files_endpoint(api_token, api_responses, domain_url, webapp):
213+
def test_create_static_file_mapping_posts_correctly(api_token, api_responses, domain_url, webapp):
213214
static_files_url = f"{domain_url}static_files/"
214215
api_responses.add(responses.POST, static_files_url, status=201)
215-
api_responses.add(responses.POST, static_files_url, status=201)
216216

217-
webapp.add_default_static_files_mappings("/project/path")
217+
webapp.create_static_file_mapping("/assets/", "/project/assets")
218218

219-
post1 = api_responses.calls[0]
220-
assert post1.request.url == static_files_url
221-
assert post1.request.headers["content-type"] == "application/json"
222-
assert post1.request.headers["Authorization"] == f"Token {api_token}"
223-
assert json.loads(post1.request.body.decode("utf8")) == {
224-
"url": "/static/",
225-
"path": "/project/path/static",
226-
}
227-
post2 = api_responses.calls[1]
228-
assert post2.request.url == static_files_url
229-
assert post2.request.headers["content-type"] == "application/json"
230-
assert post2.request.headers["Authorization"] == f"Token {api_token}"
231-
assert json.loads(post2.request.body.decode("utf8")) == {
232-
"url": "/media/",
233-
"path": "/project/path/media",
219+
post = api_responses.calls[0]
220+
assert post.request.url == static_files_url
221+
assert post.request.headers["content-type"] == "application/json"
222+
assert post.request.headers["Authorization"] == f"Token {api_token}"
223+
assert json.loads(post.request.body.decode("utf8")) == {
224+
"url": "/assets/",
225+
"path": "/project/assets",
234226
}
235227

236228

229+
def test_adds_default_static_files_mappings(mocker, webapp):
230+
mock_create = mocker.patch.object(webapp, "create_static_file_mapping")
231+
232+
project_path = "/directory/path"
233+
webapp.add_default_static_files_mappings(project_path)
234+
235+
mock_create.assert_has_calls(
236+
[
237+
mocker.call("/static/", Path(project_path) / "static"),
238+
mocker.call("/media/", Path(project_path) / "media"),
239+
]
240+
)
241+
242+
243+
237244
def test_does_post_to_reload_url(api_responses, api_token, domain_url, webapp):
238245
reload_url = f"{domain_url}reload/"
239246
api_responses.add(responses.POST, reload_url, status=200)

0 commit comments

Comments
 (0)