-
Notifications
You must be signed in to change notification settings - Fork 14.6k
/
Copy pathtest_config_endpoint.py
363 lines (318 loc) · 13.4 KB
/
test_config_endpoint.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import textwrap
from unittest.mock import patch
import pytest
from airflow.security import permissions
from tests.test_utils.api_connexion_utils import assert_401, create_user, delete_user
from tests.test_utils.config import conf_vars
pytestmark = [pytest.mark.db_test, pytest.mark.skip_if_database_isolation_mode]
MOCK_CONF = {
"core": {
"parallelism": "1024",
},
"smtp": {
"smtp_host": "localhost",
"smtp_mail_from": "[email protected]",
},
}
MOCK_CONF_WITH_SENSITIVE_VALUE = {
"core": {"parallelism": "1024", "sql_alchemy_conn": "mock_conn"},
"smtp": {
"smtp_host": "localhost",
"smtp_mail_from": "[email protected]",
},
}
@pytest.fixture(scope="module")
def configured_app(minimal_app_for_api):
app = minimal_app_for_api
create_user(
app, # type:ignore
username="test",
role_name="Test",
permissions=[(permissions.ACTION_CAN_READ, permissions.RESOURCE_CONFIG)], # type: ignore
)
create_user(app, username="test_no_permissions", role_name="TestNoPermissions") # type: ignore
with conf_vars({("webserver", "expose_config"): "True"}):
yield minimal_app_for_api
delete_user(app, username="test") # type: ignore
delete_user(app, username="test_no_permissions") # type: ignore
class TestGetConfig:
@pytest.fixture(autouse=True)
def setup_attrs(self, configured_app) -> None:
self.app = configured_app
self.client = self.app.test_client() # type:ignore
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_200_text_plain(self, mock_as_dict):
response = self.client.get(
"/api/v1/config", headers={"Accept": "text/plain"}, environ_overrides={"REMOTE_USER": "test"}
)
mock_as_dict.assert_called_with(display_source=False, display_sensitive=True)
assert response.status_code == 200
expected = textwrap.dedent(
"""\
[core]
parallelism = 1024
[smtp]
smtp_host = localhost
smtp_mail_from = [email protected]
"""
)
assert expected == response.data.decode()
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
@conf_vars({("webserver", "expose_config"): "non-sensitive-only"})
def test_should_respond_200_text_plain_with_non_sensitive_only(self, mock_as_dict):
response = self.client.get(
"/api/v1/config", headers={"Accept": "text/plain"}, environ_overrides={"REMOTE_USER": "test"}
)
mock_as_dict.assert_called_with(display_source=False, display_sensitive=False)
assert response.status_code == 200
expected = textwrap.dedent(
"""\
[core]
parallelism = 1024
[smtp]
smtp_host = localhost
smtp_mail_from = [email protected]
"""
)
assert expected == response.data.decode()
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_200_application_json(self, mock_as_dict):
response = self.client.get(
"/api/v1/config",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
mock_as_dict.assert_called_with(display_source=False, display_sensitive=True)
assert response.status_code == 200
expected = {
"sections": [
{
"name": "core",
"options": [
{"key": "parallelism", "value": "1024"},
],
},
{
"name": "smtp",
"options": [
{"key": "smtp_host", "value": "localhost"},
{"key": "smtp_mail_from", "value": "[email protected]"},
],
},
]
}
assert expected == response.json
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_200_single_section_as_text_plain(self, mock_as_dict):
response = self.client.get(
"/api/v1/config?section=smtp",
headers={"Accept": "text/plain"},
environ_overrides={"REMOTE_USER": "test"},
)
mock_as_dict.assert_called_with(display_source=False, display_sensitive=True)
assert response.status_code == 200
expected = textwrap.dedent(
"""\
[smtp]
smtp_host = localhost
smtp_mail_from = [email protected]
"""
)
assert expected == response.data.decode()
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_200_single_section_as_json(self, mock_as_dict):
response = self.client.get(
"/api/v1/config?section=smtp",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
mock_as_dict.assert_called_with(display_source=False, display_sensitive=True)
assert response.status_code == 200
expected = {
"sections": [
{
"name": "smtp",
"options": [
{"key": "smtp_host", "value": "localhost"},
{"key": "smtp_mail_from", "value": "[email protected]"},
],
},
]
}
assert expected == response.json
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_404_when_section_not_exist(self, mock_as_dict):
response = self.client.get(
"/api/v1/config?section=smtp1",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 404
assert "section=smtp1 not found." in response.json["detail"]
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_406(self, mock_as_dict):
response = self.client.get(
"/api/v1/config",
headers={"Accept": "application/octet-stream"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 406
def test_should_raises_401_unauthenticated(self):
response = self.client.get("/api/v1/config", headers={"Accept": "application/json"})
assert_401(response)
def test_should_raises_403_unauthorized(self):
response = self.client.get(
"/api/v1/config",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test_no_permissions"},
)
assert response.status_code == 403
@conf_vars({("webserver", "expose_config"): "False"})
def test_should_respond_403_when_expose_config_off(self):
response = self.client.get(
"/api/v1/config",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 403
assert "chose not to expose" in response.json["detail"]
@pytest.mark.parametrize(
"set_auto_role_public, expected_status_code",
(("Public", 403), ("Admin", 200)),
indirect=["set_auto_role_public"],
)
def test_with_auth_role_public_set(self, set_auto_role_public, expected_status_code):
response = self.client.get("/api/v1/config", headers={"Accept": "application/json"})
assert response.status_code == expected_status_code
class TestGetValue:
@pytest.fixture(autouse=True)
def setup_attrs(self, configured_app) -> None:
self.app = configured_app
self.client = self.app.test_client() # type:ignore
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_200_text_plain(self, mock_as_dict):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from",
headers={"Accept": "text/plain"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 200
expected = textwrap.dedent(
"""\
[smtp]
smtp_mail_from = [email protected]
"""
)
assert expected == response.data.decode()
@patch(
"airflow.api_connexion.endpoints.config_endpoint.conf.as_dict",
return_value=MOCK_CONF_WITH_SENSITIVE_VALUE,
)
@conf_vars({("webserver", "expose_config"): "non-sensitive-only"})
@pytest.mark.parametrize(
"section, option",
[
("core", "sql_alchemy_conn"),
("core", "SQL_ALCHEMY_CONN"),
("corE", "sql_alchemy_conn"),
("CORE", "sql_alchemy_conn"),
],
)
def test_should_respond_200_text_plain_with_non_sensitive_only(self, mock_as_dict, section, option):
response = self.client.get(
f"/api/v1/config/section/{section}/option/{option}",
headers={"Accept": "text/plain"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 200
expected = textwrap.dedent(
f"""\
[{section}]
{option} = < hidden >
"""
)
assert expected == response.data.decode()
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_200_application_json(self, mock_as_dict):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 200
expected = {
"sections": [
{
"name": "smtp",
"options": [
{"key": "smtp_mail_from", "value": "[email protected]"},
],
},
]
}
assert expected == response.json
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_404_when_option_not_exist(self, mock_as_dict):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from1",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 404
assert "The option [smtp/smtp_mail_from1] is not found in config." in response.json["detail"]
@patch("airflow.api_connexion.endpoints.config_endpoint.conf.as_dict", return_value=MOCK_CONF)
def test_should_respond_406(self, mock_as_dict):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from",
headers={"Accept": "application/octet-stream"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 406
def test_should_raises_401_unauthenticated(self):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from", headers={"Accept": "application/json"}
)
assert_401(response)
def test_should_raises_403_unauthorized(self):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test_no_permissions"},
)
assert response.status_code == 403
@conf_vars({("webserver", "expose_config"): "False"})
def test_should_respond_403_when_expose_config_off(self):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from",
headers={"Accept": "application/json"},
environ_overrides={"REMOTE_USER": "test"},
)
assert response.status_code == 403
assert "chose not to expose" in response.json["detail"]
@pytest.mark.parametrize(
"set_auto_role_public, expected_status_code",
(("Public", 403), ("Admin", 200)),
indirect=["set_auto_role_public"],
)
def test_with_auth_role_public_set(self, set_auto_role_public, expected_status_code):
response = self.client.get(
"/api/v1/config/section/smtp/option/smtp_mail_from", headers={"Accept": "application/json"}
)
assert response.status_code == expected_status_code