-
Notifications
You must be signed in to change notification settings - Fork 88
/
serve_test.py
215 lines (167 loc) · 6.56 KB
/
serve_test.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import importlib
import pytest
import serve
import sys
import os
from werkzeug import serving
class ObjectStub:
def __init__(self, **kwds):
self.__dict__.update(kwds)
@pytest.fixture
def mock_werkzeug(monkeypatch):
stub = ObjectStub(lastcall=None)
def mock_serving(host, port, app, **kwargs):
stub.lastcall = ObjectStub(host=host, port=port, app=app, kwargs=kwargs)
monkeypatch.setattr(serving, "run_simple", mock_serving)
return stub
@pytest.fixture
def mock_importlib(monkeypatch):
app = ObjectStub()
app.app = ObjectStub()
def mock_import_module(module):
if app.app:
app.app.module = module
return app
monkeypatch.setattr(importlib, "import_module", mock_import_module)
return app
@pytest.fixture
def mock_path(monkeypatch):
path = []
monkeypatch.setattr(sys, "path", path)
return path
@pytest.fixture
def mock_os_path_exists(monkeypatch):
mock_path = ObjectStub()
mock_path.file_names_that_exist = []
def mock_exists(file_name):
return file_name in mock_path.file_names_that_exist
monkeypatch.setattr(os.path, 'exists', mock_exists)
return mock_path
def test_serve(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000")
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "localhost"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
"processes": 1,
"ssl_context": None,
}
def test_serve_alternative_hostname(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000", "0.0.0.0")
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "0.0.0.0"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
"processes": 1,
"ssl_context": None,
}
def test_serve_disable_threading(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000", "0.0.0.0", threaded=False)
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "0.0.0.0"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": False,
"processes": 1,
"ssl_context": None,
}
def test_serve_multiple_processes(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000", "0.0.0.0", processes=10)
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "0.0.0.0"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
"processes": 10,
"ssl_context": None,
}
def test_serve_ssl(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp1", "app.app", "5000", "0.0.0.0", threaded=False, ssl=True)
assert len(mock_path) == 1
assert mock_path[0] == "/tmp1"
assert mock_werkzeug.lastcall.host == "0.0.0.0"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": False,
"processes": 1,
"ssl_context": "adhoc",
}
def test_serve_from_subdir(mock_path, mock_importlib, mock_werkzeug):
serve.serve("/tmp2", "subdir/app.app", "5000")
assert len(mock_path) == 2
assert mock_path[0] == "/tmp2/subdir"
assert mock_path[1] == "/tmp2"
assert mock_werkzeug.lastcall.host == "localhost"
assert mock_werkzeug.lastcall.port == 5000
assert mock_werkzeug.lastcall.app.module == "app"
assert mock_werkzeug.lastcall.app.debug
assert mock_werkzeug.lastcall.kwargs == {
"use_reloader": True,
"use_debugger": True,
"use_evalex": True,
"threaded": True,
"processes": 1,
"ssl_context": None,
}
def test_serve_non_debuggable_app(mock_path, mock_importlib, mock_werkzeug):
mock_importlib.app = None
serve.serve("/tmp1", "app.app", "5000")
assert mock_werkzeug.lastcall.app is None
def test_validate_ssl_keys_with_no_keys_passed():
return_value = serve._validate_ssl_keys(None, None)
assert return_value is None
def test_validate_ssl_keys_with_sys_exit_for_missing_key():
with pytest.raises(SystemExit) as pytest_wrapped_e:
serve._validate_ssl_keys('test.pem', None)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == "Missing either cert file or private key file (hint: --ssl-pub <file> and --ssl-pri <file>)"
def test_validate_ssl_keys_with_non_existant_cert_file(mock_os_path_exists):
with pytest.raises(SystemExit) as pytest_wrapped_e:
serve._validate_ssl_keys('test.pem', 'test-key.pem')
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == "Cert file can't be found"
def test_validate_ssl_keys_with_non_existant_key_file(mock_os_path_exists):
mock_os_path_exists.file_names_that_exist = ['test.pem']
with pytest.raises(SystemExit) as pytest_wrapped_e:
serve._validate_ssl_keys('test.pem', 'test-key.pem')
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == "Private key file can't be found"
def test_validate_ssl_keys_with_actual_keys(mock_os_path_exists):
mock_os_path_exists.file_names_that_exist = ['test.pem', 'test-key.pem']
return_value = serve._validate_ssl_keys('test.pem', 'test-key.pem')
assert type(return_value) is tuple
assert return_value[0] == 'test.pem'
assert return_value[1] == 'test-key.pem'