-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_cli_django.py
More file actions
271 lines (238 loc) · 7.08 KB
/
test_cli_django.py
File metadata and controls
271 lines (238 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import subprocess
import time
from platform import python_version
from unittest.mock import call
import pytest
import requests
from typer.testing import CliRunner
from cli.django import app
runner = CliRunner()
@pytest.fixture
def mock_django_project(mocker):
return mocker.patch("cli.django.DjangoProject")
@pytest.fixture
def mock_update_wsgi_file(mocker):
return mocker.patch("cli.django.DjangoProject.update_wsgi_file")
@pytest.fixture
def mock_call_api(mocker):
return mocker.patch("pythonanywhere_core.webapp.call_api")
@pytest.fixture
def running_python_version():
return ".".join(python_version().split(".")[:2])
def test_main_subcommand_without_args_prints_help():
result = runner.invoke(
app,
[],
)
assert result.exit_code == 2
assert "Show this message and exit." in result.stdout
def test_autoconfigure_calls_all_stuff_in_right_order(mock_django_project):
result = runner.invoke(
app,
[
"autoconfigure",
"repo.url",
"-d",
"www.domain.com",
"-p",
"python.version",
"--nuke",
],
)
mock_django_project.assert_called_once_with("www.domain.com", "python.version")
assert mock_django_project.return_value.method_calls == [
call.sanity_checks(nuke=True),
call.download_repo("repo.url", nuke=True),
call.ensure_branch("None"),
call.create_virtualenv(nuke=True),
call.create_webapp(nuke=True),
call.add_static_file_mappings(),
call.find_django_files(),
call.update_wsgi_file(),
call.update_settings_file(),
call.run_collectstatic(),
call.run_migrate(),
call.reload_webapp(),
call.start_bash(),
]
assert "Running sanity checks" in result.stdout
assert (
f"All done! Your site is now live at https://www.domain.com" in result.stdout
)
@pytest.mark.slowtest
def test_autoconfigure_actually_works_against_example_repo(
mocker,
mock_call_api,
mock_update_wsgi_file,
fake_home,
virtualenvs_folder,
api_token,
process_killer,
running_python_version,
):
git_ref = "non-nested-old" if running_python_version in ["3.8", "3.9"] else "master"
expected_django_version = "4.2.16" if running_python_version in ["3.8", "3.9"] else "5.1.3"
mocker.patch("cli.django.DjangoProject.start_bash")
repo = "https://github.com/pythonanywhere/example-django-project.git"
domain = "mydomain.com"
runner.invoke(
app,
[
"autoconfigure",
repo,
"-d",
domain,
"-p",
running_python_version,
"--branch",
git_ref,
],
)
expected_virtualenv = virtualenvs_folder / domain
expected_project_path = fake_home / domain
django_project_name = "myproject"
expected_settings_path = expected_project_path / django_project_name / "settings.py"
django_version = (
subprocess.check_output(
[
str(expected_virtualenv / "bin/python"),
"-c" "import django; print(django.get_version())",
]
)
.decode()
.strip()
)
assert django_version == expected_django_version
with expected_settings_path.open() as f:
lines = f.read().split("\n")
assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines
assert "ALLOWED_HOSTS = ['mydomain.com'] # type: List[str]" in lines
assert "base.css" in os.listdir(str(fake_home / domain / "static/admin/css"))
server = subprocess.Popen(
[
str(expected_virtualenv / "bin/python"),
str(expected_project_path / "manage.py"),
"runserver",
]
)
process_killer.append(server)
time.sleep(2)
response = requests.get("http://localhost:8000/", headers={"HOST": "mydomain.com"})
assert "Hello from an example django project" in response.text
def test_start_calls_all_stuff_in_right_order(mock_django_project):
result = runner.invoke(
app,
[
"start",
"-d",
"www.domain.com",
"-j",
"django.version",
"-p",
"python.version",
"--nuke",
],
)
assert mock_django_project.call_args == call("www.domain.com", "python.version")
assert mock_django_project.return_value.method_calls == [
call.sanity_checks(nuke=True),
call.create_virtualenv("django.version", nuke=True),
call.run_startproject(nuke=True),
call.find_django_files(),
call.update_settings_file(),
call.run_collectstatic(),
call.create_webapp(nuke=True),
call.add_static_file_mappings(),
call.update_wsgi_file(),
call.reload_webapp(),
]
assert "Running sanity checks" in result.stdout
assert (
f"All done! Your site is now live at https://www.domain.com" in result.stdout
)
@pytest.mark.slowtest
def test_start_actually_creates_django_project_in_virtualenv_with_hacked_settings_and_static_files(
mock_call_api,
mock_update_wsgi_file,
fake_home,
virtualenvs_folder,
api_token,
running_python_version,
new_django_version,
):
runner.invoke(
app,
[
"start",
"-d",
"mydomain.com",
"-j",
new_django_version,
"-p",
running_python_version,
],
)
django_version = (
subprocess.check_output(
[
str(virtualenvs_folder / "mydomain.com/bin/python"),
"-c" "import django; print(django.get_version())",
]
)
.decode()
.strip()
)
assert django_version == new_django_version
with (fake_home / "mydomain.com/mysite/settings.py").open() as f:
lines = f.read().split("\n")
assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
assert "base.css" in os.listdir(str(fake_home / "mydomain.com/static/admin/css"))
@pytest.mark.slowtest
def test_nuke_option_lets_you_run_twice(
mock_call_api,
mock_update_wsgi_file,
fake_home,
virtualenvs_folder,
api_token,
running_python_version,
old_django_version,
new_django_version,
):
runner.invoke(
app,
[
"start",
"-d",
"mydomain.com",
"-j",
old_django_version,
"-p",
running_python_version,
],
)
runner.invoke(
app,
[
"start",
"-d",
"mydomain.com",
"-j",
new_django_version,
"-p",
running_python_version,
"--nuke",
],
)
django_version = (
subprocess.check_output(
[
str(virtualenvs_folder / "mydomain.com/bin/python"),
"-c" "import django; print(django.get_version())",
]
)
.decode()
.strip()
)
assert django_version == new_django_version