Skip to content

Commit 4e1f9dd

Browse files
Require HTTP 204 for scheduled task deletion.
1 parent ed2b4c4 commit 4e1f9dd

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎pythonanywhere_core/schedule.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def delete(self, task_id: int) -> Literal[True]:
5252
"""Deletes scheduled task by id.
5353
5454
:param task_id: scheduled task to be deleted id number
55-
:returns: True when API response is 204"""
55+
:returns: True when API response is 204
56+
:raises PythonAnywhereApiException: if the response is not HTTP 204"""
5657

5758
result = call_api(
5859
f"{self.base_url}{task_id}/", "DELETE"
@@ -61,10 +62,9 @@ def delete(self, task_id: int) -> Literal[True]:
6162
if result.status_code == 204:
6263
return True
6364

64-
if not result.ok:
65-
raise PythonAnywhereApiException(
66-
f"DELETE via API on task {task_id} failed, got {result}: {result.text}"
67-
)
65+
raise PythonAnywhereApiException(
66+
f"DELETE via API on task {task_id} failed, got {result}: {result.text}"
67+
)
6868

6969
def get_list(self) -> List[dict]:
7070
"""Gets list of existing scheduled tasks.

‎tests/test_schedule.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ def test_deletes_task(api_token, api_responses, task_base_url):
133133
assert result is True
134134

135135

136+
@pytest.mark.parametrize("status_code", [200, 201, 202, 403, 404, 500])
137+
@pytest.mark.parametrize("body", ['{"detail": "Deletion failed"}', "Non-JSON error"])
138+
def test_delete_raises_unless_response_is_204(
139+
api_token, api_responses, task_base_url, status_code, body
140+
):
141+
api_responses.add(responses.DELETE, f"{task_base_url}42/", status=status_code, body=body)
142+
143+
with pytest.raises(PythonAnywhereApiException) as exc:
144+
Schedule().delete(42)
145+
146+
assert str(status_code) in str(exc.value)
147+
assert body in str(exc.value)
148+
assert len(api_responses.calls) == 1
149+
150+
136151
def test_raises_because_attempt_to_delete_nonexisting_task(api_token, api_responses, task_base_url):
137152
body = '{"detail": "Not fount."}'
138153
api_responses.add(

0 commit comments

Comments
 (0)