forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_api_tests.py
227 lines (199 loc) · 7.92 KB
/
base_api_tests.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
# 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.
# isort:skip_file
import json
from flask_appbuilder.models.sqla.interface import SQLAInterface
import prison
import tests.test_app
from superset import db, security_manager
from superset.extensions import appbuilder
from superset.models.dashboard import Dashboard
from superset.views.base_api import BaseSupersetModelRestApi
from .base_tests import SupersetTestCase
class Model1Api(BaseSupersetModelRestApi):
datamodel = SQLAInterface(Dashboard)
allow_browser_login = True
class_permission_name = "Dashboard"
method_permission_name = {
"get_list": "read",
"get": "read",
"export": "read",
"post": "write",
"put": "write",
"delete": "write",
"bulk_delete": "write",
"info": "read",
"related": "read",
}
appbuilder.add_api(Model1Api)
class TestOpenApiSpec(SupersetTestCase):
def test_open_api_spec(self):
"""
API: Test validate OpenAPI spec
:return:
"""
from openapi_spec_validator import validate_spec
self.login(username="admin")
uri = "api/v1/_openapi"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
validate_spec(response)
class TestBaseModelRestApi(SupersetTestCase):
def test_default_missing_declaration_get(self):
"""
API: Test default missing declaration on get
We want to make sure that not declared list_columns will
not render all columns by default but just the model's pk
"""
# Check get list response
self.login(username="admin")
uri = "api/v1/model1api/"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(response["list_columns"], ["id"])
for result in response["result"]:
self.assertEqual(list(result.keys()), ["id"])
# Check get response
dashboard = db.session.query(Dashboard).first()
uri = f"api/v1/model1api/{dashboard.id}"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(response["show_columns"], ["id"])
self.assertEqual(list(response["result"].keys()), ["id"])
def test_default_missing_declaration_put_spec(self):
"""
API: Test default missing declaration on put openapi spec
We want to make sure that not declared edit_columns will
not render all columns by default but just the model's pk
"""
self.login(username="admin")
uri = "api/v1/_openapi"
rv = self.client.get(uri)
# dashboard model accepts all fields are null
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
expected_mutation_spec = {
"properties": {"id": {"format": "int32", "type": "integer"}},
"type": "object",
}
self.assertEqual(
response["components"]["schemas"]["Model1Api.post"], expected_mutation_spec
)
self.assertEqual(
response["components"]["schemas"]["Model1Api.put"], expected_mutation_spec
)
def test_default_missing_declaration_post(self):
"""
API: Test default missing declaration on post
We want to make sure that not declared add_columns will
not accept all columns by default
"""
dashboard_data = {
"dashboard_title": "title1",
"slug": "slug1",
"position_json": '{"a": "A"}',
"css": "css",
"json_metadata": '{"b": "B"}',
"published": True,
}
self.login(username="admin")
uri = "api/v1/model1api/"
rv = self.client.post(uri, json=dashboard_data)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(rv.status_code, 422)
expected_response = {
"message": {
"css": ["Unknown field."],
"dashboard_title": ["Unknown field."],
"json_metadata": ["Unknown field."],
"position_json": ["Unknown field."],
"published": ["Unknown field."],
"slug": ["Unknown field."],
}
}
self.assertEqual(response, expected_response)
def test_default_missing_declaration_put(self):
"""
API: Test default missing declaration on put
We want to make sure that not declared edit_columns will
not accept all columns by default
"""
dashboard = db.session.query(Dashboard).first()
dashboard_data = {"dashboard_title": "CHANGED", "slug": "CHANGED"}
self.login(username="admin")
uri = f"api/v1/model1api/{dashboard.id}"
rv = self.client.put(uri, json=dashboard_data)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(rv.status_code, 422)
expected_response = {
"message": {
"dashboard_title": ["Unknown field."],
"slug": ["Unknown field."],
}
}
self.assertEqual(response, expected_response)
class ApiOwnersTestCaseMixin:
"""
Implements shared tests for owners related field
"""
resource_name: str = ""
def test_get_related_owners(self):
"""
API: Test get related owners
"""
self.login(username="admin")
uri = f"api/v1/{self.resource_name}/related/owners"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
users = db.session.query(security_manager.user_model).all()
expected_users = [str(user) for user in users]
self.assertEqual(response["count"], len(users))
# This needs to be implemented like this, because ordering varies between
# postgres and mysql
response_users = [result["text"] for result in response["result"]]
for expected_user in expected_users:
self.assertIn(expected_user, response_users)
def test_get_filter_related_owners(self):
"""
API: Test get filter related owners
"""
self.login(username="admin")
argument = {"filter": "gamma"}
uri = f"api/v1/{self.resource_name}/related/owners?q={prison.dumps(argument)}"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(3, response["count"])
sorted_results = sorted(response["result"], key=lambda value: value["text"])
expected_results = [
{"text": "gamma user", "value": 2},
{"text": "gamma2 user", "value": 3},
{"text": "gamma_sqllab user", "value": 4},
]
self.assertEqual(expected_results, sorted_results)
def test_get_related_fail(self):
"""
API: Test get related fail
"""
self.login(username="admin")
uri = f"api/v1/{self.resource_name}/related/owner"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 404)