|
| 1 | +import getpass |
| 2 | +import json |
| 3 | + |
| 4 | +import pytest |
| 5 | +import responses |
| 6 | + |
| 7 | +from pythonanywhere_core.base import get_api_endpoint |
| 8 | +from pythonanywhere_core.exceptions import PythonAnywhereApiException |
| 9 | +from pythonanywhere_core.schedule import Schedule |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def task_base_url(): |
| 14 | + return get_api_endpoint().format(username=getpass.getuser(), flavor="schedule") |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def task_specs(): |
| 19 | + username = getpass.getuser() |
| 20 | + return { |
| 21 | + "can_enable": False, |
| 22 | + "command": "echo foo", |
| 23 | + "enabled": True, |
| 24 | + "expiry": None, |
| 25 | + "extend_url": f"/user/{username}/schedule/task/123/extend", |
| 26 | + "hour": 16, |
| 27 | + "id": 123, |
| 28 | + "interval": "daily", |
| 29 | + "logfile": "/user/{username}/files/var/log/tasklog-126708-daily-at-1600-echo_foo.log", |
| 30 | + "minute": 0, |
| 31 | + "printable_time": "16:00", |
| 32 | + "url": f"/api/v0/user/{username}/schedule/123", |
| 33 | + "user": username, |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def base_task_params(): |
| 39 | + return { |
| 40 | + "command": "echo foo", |
| 41 | + "enabled": True, |
| 42 | + "minute": 0, |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def hourly_task_params(base_task_params): |
| 48 | + return { |
| 49 | + **base_task_params, |
| 50 | + "interval": "hourly", |
| 51 | + "minute": 0, |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def daily_task_params(base_task_params): |
| 57 | + return { |
| 58 | + **base_task_params, |
| 59 | + "interval": "daily", |
| 60 | + "hour": 16, |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | +def test_creates_daily_task( |
| 65 | + api_token, api_responses, task_specs, daily_task_params, task_base_url |
| 66 | +): |
| 67 | + api_responses.add( |
| 68 | + responses.POST, url=task_base_url, status=201, body=json.dumps(task_specs) |
| 69 | + ) |
| 70 | + |
| 71 | + assert Schedule().create(daily_task_params) == task_specs |
| 72 | + |
| 73 | + |
| 74 | +def test_creates_hourly_task( |
| 75 | + api_token, api_responses, task_specs, hourly_task_params, task_base_url |
| 76 | +): |
| 77 | + hourly_specs = {"hour": None, "interval": "hourly", "printable_time": "00 minutes past"} |
| 78 | + task_specs.update(hourly_specs) |
| 79 | + api_responses.add( |
| 80 | + responses.POST, url=task_base_url, status=201, body=json.dumps(task_specs) |
| 81 | + ) |
| 82 | + |
| 83 | + assert Schedule().create(hourly_task_params) == task_specs |
| 84 | + |
| 85 | + |
| 86 | +def test_raises_because_missing_params(api_token, api_responses, task_base_url): |
| 87 | + body = ( |
| 88 | + '{"interval":["This field is required."],"command":["This field is required."],' |
| 89 | + '"minute":["This field is required."]}' |
| 90 | + ) |
| 91 | + api_responses.add(responses.POST, url=task_base_url, status=400, body=body) |
| 92 | + |
| 93 | + with pytest.raises(PythonAnywhereApiException) as e: |
| 94 | + Schedule().create({}) |
| 95 | + |
| 96 | + expected_error_msg = ( |
| 97 | + f"POST to set new task via API failed, got <Response [400]>: {body}" |
| 98 | + ) |
| 99 | + assert str(e.value) == expected_error_msg |
| 100 | + |
| 101 | + |
| 102 | +def test_deletes_task(api_token, api_responses, task_base_url): |
| 103 | + url = f"{task_base_url}42/" |
| 104 | + api_responses.add(responses.DELETE, url=url, status=204) |
| 105 | + |
| 106 | + result = Schedule().delete(42) |
| 107 | + |
| 108 | + post = api_responses.calls[0] |
| 109 | + assert post.request.url == url |
| 110 | + assert post.request.body is None |
| 111 | + assert result is True |
| 112 | + |
| 113 | + |
| 114 | +def test_raises_because_attempt_to_delete_nonexisting_task(api_token, api_responses, task_base_url): |
| 115 | + body = '{"detail": "Not fount."}' |
| 116 | + api_responses.add( |
| 117 | + responses.DELETE, url=f"{task_base_url}42/", status=404, body=body |
| 118 | + ) |
| 119 | + |
| 120 | + with pytest.raises(PythonAnywhereApiException) as e: |
| 121 | + Schedule().delete(42) |
| 122 | + |
| 123 | + assert ( |
| 124 | + str(e.value) |
| 125 | + == f"DELETE via API on task 42 failed, got <Response [404]>: {body}" |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def test_returns_spec_dict(api_token, api_responses, task_base_url, task_specs): |
| 130 | + api_responses.add( |
| 131 | + responses.GET, |
| 132 | + url=f"{task_base_url}123/", |
| 133 | + status=200, |
| 134 | + body=json.dumps(task_specs), |
| 135 | + ) |
| 136 | + |
| 137 | + assert Schedule().get_specs(123) == task_specs |
| 138 | + |
| 139 | + |
| 140 | +def test_raises_because_attempt_to_get_nonexisting_task(api_token, api_responses, task_base_url): |
| 141 | + body = '{"detail":"Not found."}' |
| 142 | + api_responses.add( |
| 143 | + responses.GET, url=f"{task_base_url}42/", status=404, body=body |
| 144 | + ) |
| 145 | + |
| 146 | + with pytest.raises(PythonAnywhereApiException) as e: |
| 147 | + Schedule().get_specs(42) |
| 148 | + |
| 149 | + expected_error_msg = ( |
| 150 | + f"Could not get task with id 42. Got result <Response [404]>: {body}" |
| 151 | + ) |
| 152 | + assert str(e.value) == expected_error_msg |
| 153 | + |
| 154 | + |
| 155 | +def test_returns_tasks_list(api_token, api_responses, task_base_url): |
| 156 | + fake_specs = [{"fake": "specs"}, {"and": "more"}] |
| 157 | + api_responses.add( |
| 158 | + responses.GET, url=task_base_url, status=200, body=json.dumps(fake_specs), |
| 159 | + ) |
| 160 | + |
| 161 | + assert Schedule().get_list() == fake_specs |
| 162 | + |
| 163 | + |
| 164 | +def test_updates_daily_task( |
| 165 | + api_token, api_responses, task_specs, daily_task_params, task_base_url |
| 166 | +): |
| 167 | + api_responses.add( |
| 168 | + responses.PATCH, |
| 169 | + url=f"{task_base_url}123/", |
| 170 | + status=200, |
| 171 | + body=json.dumps(task_specs), |
| 172 | + ) |
| 173 | + |
| 174 | + assert Schedule().update(123, daily_task_params) == task_specs |
| 175 | + |
| 176 | + |
| 177 | +def test_raises_when_wrong_params( |
| 178 | + api_token, api_responses, task_specs, daily_task_params, task_base_url |
| 179 | +): |
| 180 | + body = '{"non_field_errors":["Hourly tasks must not have an hour."]}' |
| 181 | + api_responses.add( |
| 182 | + responses.PATCH, url=f"{task_base_url}1/", status=400, body=body |
| 183 | + ) |
| 184 | + |
| 185 | + with pytest.raises(PythonAnywhereApiException) as e: |
| 186 | + Schedule().update(1, {"hour": 23}) |
| 187 | + |
| 188 | + assert str(e.value) == f"Could not update task 1. Got <Response [400]>: {body}" |
0 commit comments