Skip to content

Commit 860dd6c

Browse files
Reject failed webapp existence checks.
1 parent b92996b commit 860dd6c

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎pythonanywhere_core/webapp.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def sanity_checks(self, nuke: bool) -> None:
5151
:param nuke: if True, skip the check for existing webapp
5252
5353
:raises SanityException: if API token is missing or webapp already exists
54+
:raises PythonAnywhereApiException: if the existence check returns neither 200 nor 404
5455
"""
5556
token = os.environ.get("API_TOKEN")
5657
if not token:
@@ -73,6 +74,11 @@ def sanity_checks(self, nuke: bool) -> None:
7374
f"You already have a webapp for {self.domain}.\n\nUse the --nuke option if you want to replace it."
7475
)
7576

77+
if response.status_code != 404:
78+
raise PythonAnywhereApiException(
79+
f"GET to check webapp existence via API failed, got {response}:{response.text}"
80+
)
81+
7682
def create(self, python_version: str, virtualenv_path: Path | None, project_path: Path, nuke: bool) -> None:
7783
"""Create a webapp for the given domain, using the given python version and virtualenv path
7884

‎tests/test_webapp.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ def test_does_not_complain_if_api_token_exists(api_token, api_responses, domain_
8888
webapp.sanity_checks(nuke=False) # should not raise
8989

9090

91-
def test_raises_if_no_api_token_exists(api_responses, no_api_token, webapp):
91+
@pytest.mark.parametrize("nuke", [False, True])
92+
def test_raises_if_no_api_token_exists(api_responses, no_api_token, webapp, nuke):
9293
with pytest.raises(SanityException) as e:
93-
webapp.sanity_checks(nuke=False)
94+
webapp.sanity_checks(nuke=nuke)
9495
assert "Could not find your API token" in str(e.value)
9596

9697

@@ -114,6 +115,27 @@ def test_does_not_raise_if_no_webapp(api_token, api_responses, domain_url, webap
114115
webapp.sanity_checks(nuke=False) # should not raise
115116

116117

118+
@pytest.mark.parametrize("status_code", [201, 202, 204, 403, 429, 500])
119+
@pytest.mark.parametrize("json_body", [False, True])
120+
def test_sanity_checks_raise_on_unexpected_status(
121+
api_token, api_responses, domain_url, webapp, status_code, json_body
122+
):
123+
body = "" if status_code == 204 else (
124+
'{"detail": "Preflight failed"}' if json_body else "Preflight failed"
125+
)
126+
api_responses.add(
127+
responses.GET, domain_url, status=status_code, body=body,
128+
content_type="application/json" if json_body else "text/plain",
129+
)
130+
131+
with pytest.raises(PythonAnywhereApiException) as exc:
132+
webapp.sanity_checks(nuke=False)
133+
134+
assert str(status_code) in str(exc.value)
135+
assert body in str(exc.value)
136+
assert len(api_responses.calls) == 1
137+
138+
117139
def test_nuke_option_overrides_all_but_token_check(
118140
api_token, api_responses, domain, fake_home, virtualenvs_folder, webapp
119141
):
@@ -122,6 +144,8 @@ def test_nuke_option_overrides_all_but_token_check(
122144

123145
webapp.sanity_checks(nuke=True) # should not raise
124146

147+
assert len(api_responses.calls) == 0
148+
125149

126150
# CREATE
127151
## /api/v0/user/{username}/webapps/ : POST

0 commit comments

Comments
 (0)