Skip to content

Commit 547b786

Browse files
Use realistic webapp API responses in Django integration tests.
1 parent 12f1da2 commit 547b786

4 files changed

Lines changed: 44 additions & 28 deletions

File tree

‎tests/conftest.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ def api_responses(monkeypatch):
8787
yield r
8888

8989

90+
@pytest.fixture
91+
def webapp_api(api_responses):
92+
"""HTTP contract for the webapp used by Django setup integration tests."""
93+
from pythonanywhere_core.webapp import Webapp
94+
95+
webapp = Webapp("mydomain.com")
96+
api_responses.add(responses.GET, webapp.domain_url, status=404)
97+
api_responses.add(responses.POST, webapp.webapps_url, status=201, json={"status": "OK"})
98+
api_responses.add(responses.PATCH, webapp.domain_url, status=200, json={})
99+
api_responses.add(responses.DELETE, webapp.domain_url, status=204)
100+
api_responses.add(responses.POST, f"{webapp.domain_url}static_files/", status=201, json={})
101+
api_responses.add(responses.POST, f"{webapp.domain_url}reload/", status=200, json={"status": "OK"})
102+
# DELETE is only used by the replacement test; the other routes may repeat.
103+
api_responses.assert_all_requests_are_fired = False
104+
# These tests also GET the local Django development server.
105+
api_responses.add_passthru("http://localhost:8000/")
106+
return api_responses
107+
108+
90109
@pytest.fixture(scope="function")
91110
def api_token():
92111
old_token = os.environ.get("API_TOKEN")

‎tests/test_cli_django.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ def mock_update_wsgi_file(mocker):
2323
return mocker.patch("cli.django.DjangoProject.update_wsgi_file")
2424

2525

26-
@pytest.fixture
27-
def mock_call_api(mocker):
28-
return mocker.patch("pythonanywhere_core.webapp.call_api")
29-
30-
3126
@pytest.fixture
3227
def running_python_version():
3328
return ".".join(python_version().split(".")[:2])
@@ -80,7 +75,7 @@ def test_autoconfigure_calls_all_stuff_in_right_order(mock_django_project):
8075
@pytest.mark.slowtest
8176
def test_autoconfigure_actually_works_against_example_repo(
8277
mocker,
83-
mock_call_api,
78+
webapp_api,
8479
mock_update_wsgi_file,
8580
fake_home,
8681
virtualenvs_folder,
@@ -94,7 +89,7 @@ def test_autoconfigure_actually_works_against_example_repo(
9489
repo = "https://github.com/pythonanywhere/example-django-project.git"
9590
domain = "mydomain.com"
9691

97-
runner.invoke(
92+
result = runner.invoke(
9893
app,
9994
[
10095
"autoconfigure",
@@ -108,6 +103,8 @@ def test_autoconfigure_actually_works_against_example_repo(
108103
],
109104
)
110105

106+
assert result.exit_code == 0, result.exception
107+
111108
expected_virtualenv = virtualenvs_folder / domain
112109
expected_project_path = fake_home / domain
113110
django_project_name = "myproject"
@@ -180,15 +177,15 @@ def test_start_calls_all_stuff_in_right_order(mock_django_project):
180177

181178
@pytest.mark.slowtest
182179
def test_start_actually_creates_django_project_in_virtualenv_with_hacked_settings_and_static_files(
183-
mock_call_api,
180+
webapp_api,
184181
mock_update_wsgi_file,
185182
fake_home,
186183
virtualenvs_folder,
187184
api_token,
188185
running_python_version,
189186
new_django_version,
190187
):
191-
runner.invoke(
188+
result = runner.invoke(
192189
app,
193190
[
194191
"start",
@@ -200,6 +197,7 @@ def test_start_actually_creates_django_project_in_virtualenv_with_hacked_setting
200197
running_python_version,
201198
],
202199
)
200+
assert result.exit_code == 0, result.exception
203201

204202
django_version = (
205203
subprocess.check_output(
@@ -223,7 +221,7 @@ def test_start_actually_creates_django_project_in_virtualenv_with_hacked_setting
223221

224222
@pytest.mark.slowtest
225223
def test_nuke_option_lets_you_run_twice(
226-
mock_call_api,
224+
webapp_api,
227225
mock_update_wsgi_file,
228226
fake_home,
229227
virtualenvs_folder,
@@ -232,7 +230,7 @@ def test_nuke_option_lets_you_run_twice(
232230
old_django_version,
233231
new_django_version,
234232
):
235-
runner.invoke(
233+
result = runner.invoke(
236234
app,
237235
[
238236
"start",
@@ -244,7 +242,8 @@ def test_nuke_option_lets_you_run_twice(
244242
running_python_version,
245243
],
246244
)
247-
runner.invoke(
245+
assert result.exit_code == 0, result.exception
246+
result = runner.invoke(
248247
app,
249248
[
250249
"start",
@@ -257,6 +256,7 @@ def test_nuke_option_lets_you_run_twice(
257256
"--nuke",
258257
],
259258
)
259+
assert result.exit_code == 0, result.exception
260260

261261
django_version = (
262262
subprocess.check_output(

‎tests/test_pa_autoconfigure_django.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,20 @@ def test_calls_all_stuff_in_right_order(self):
3333

3434
@pytest.mark.slowtest
3535
def test_actually_works_against_example_repo(
36-
self, fake_home, virtualenvs_folder, api_token, process_killer, running_python_version, new_django_version
36+
self, fake_home, virtualenvs_folder, api_token, process_killer, running_python_version, new_django_version, webapp_api
3737
):
3838
git_ref = "non-nested-old" if running_python_version in ["3.8", "3.9"] else "master"
3939
repo = 'https://github.com/pythonanywhere/example-django-project.git'
4040
domain = 'mydomain.com'
4141
with patch('scripts.pa_autoconfigure_django.DjangoProject.update_wsgi_file'):
4242
with patch('scripts.pa_autoconfigure_django.DjangoProject.start_bash'):
43-
with patch('pythonanywhere_core.webapp.call_api'):
44-
main(
45-
repo_url=repo,
46-
branch=git_ref,
47-
domain=domain,
48-
python_version=running_python_version,
49-
nuke=False
50-
)
43+
main(
44+
repo_url=repo,
45+
branch=git_ref,
46+
domain=domain,
47+
python_version=running_python_version,
48+
nuke=False
49+
)
5150

5251
expected_virtualenv = virtualenvs_folder / domain
5352
expected_project_path = fake_home / domain

‎tests/test_pa_start_django_webapp_with_virtualenv.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ def test_calls_all_stuff_in_right_order(mocker):
3030

3131
@pytest.mark.slowtest
3232
def test_actually_creates_django_project_in_virtualenv_with_hacked_settings_and_static_files(
33-
fake_home, virtualenvs_folder, api_token, running_python_version, new_django_version
33+
fake_home, virtualenvs_folder, api_token, running_python_version, new_django_version, webapp_api
3434
):
3535
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject.update_wsgi_file"):
36-
with patch("pythonanywhere_core.webapp.call_api"):
37-
main("mydomain.com", new_django_version, running_python_version, nuke=False)
36+
main("mydomain.com", new_django_version, running_python_version, nuke=False)
3837

3938
output_django_version = (
4039
subprocess.check_output(
@@ -57,13 +56,12 @@ def test_actually_creates_django_project_in_virtualenv_with_hacked_settings_and_
5756

5857
@pytest.mark.slowtest
5958
def test_nuke_option_lets_you_run_twice(
60-
fake_home, virtualenvs_folder, api_token, running_python_version, new_django_version, old_django_version
59+
fake_home, virtualenvs_folder, api_token, running_python_version, new_django_version, old_django_version, webapp_api
6160
):
6261

6362
with patch("scripts.pa_start_django_webapp_with_virtualenv.DjangoProject.update_wsgi_file"):
64-
with patch("pythonanywhere_core.webapp.call_api"):
65-
main("mydomain.com", old_django_version, running_python_version, nuke=False)
66-
main("mydomain.com", new_django_version, running_python_version, nuke=True)
63+
main("mydomain.com", old_django_version, running_python_version, nuke=False)
64+
main("mydomain.com", new_django_version, running_python_version, nuke=True)
6765

6866
django_version = (
6967
subprocess.check_output(

0 commit comments

Comments
 (0)