Skip to content

Commit

Permalink
fix(test): missing auth on tests (apache#10842)
Browse files Browse the repository at this point in the history
* fix(test): missing auth on tests

* fix mock

* make test login more inline with other tests
  • Loading branch information
dpgaspar authored and auxten committed Nov 20, 2020
1 parent f4ed8d4 commit b50f3dc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions tests/base_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

class Model1Api(BaseSupersetModelRestApi):
datamodel = SQLAInterface(Dashboard)
allow_browser_login = True
class_permission_name = "DashboardModelView"
method_permission_name = {
"get_list": "list",
Expand Down
60 changes: 33 additions & 27 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_login(self):
self.assertIn("User confirmation needed", resp)

def test_dashboard_endpoint(self):
self.login()
resp = self.client.get("/superset/dashboard/-1/")
assert resp.status_code == 404

Expand Down Expand Up @@ -190,6 +191,7 @@ def test_annotation_json_endpoint(self):
db.session.add(annotation)
db.session.commit()

self.login()
resp_annotations = json.loads(
self.get_resp("annotationlayermodelview/api/read")
)
Expand Down Expand Up @@ -562,6 +564,7 @@ def test_databaseview_edit(self, username="admin"):
db.session.commit()

def test_warm_up_cache(self):
self.login()
slc = self.get_slice("Girls", db.session)
data = self.get_json_resp("/superset/warm_up_cache?slice_id={}".format(slc.id))
self.assertEqual(
Expand Down Expand Up @@ -629,7 +632,7 @@ def test_gamma(self):
assert "Dashboards" in self.get_resp("/dashboard/list/")

def test_csv_endpoint(self):
self.login("admin")
self.login()
sql = """
SELECT name
FROM birth_names
Expand All @@ -654,7 +657,7 @@ def test_csv_endpoint(self):
self.logout()

def test_extra_table_metadata(self):
self.login("admin")
self.login()
example_db = utils.get_example_database()
schema = "default" if example_db.backend in {"presto", "hive"} else "superset"
self.get_json_resp(
Expand Down Expand Up @@ -695,7 +698,7 @@ def test_templated_sql_json(self):
if utils.get_example_database().backend == "presto":
# TODO: make it work for presto
return
self.login("admin")
self.login()
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}' as test"
data = self.run_sql(sql, "fdaklj3ws")
self.assertEqual(data["data"][0]["test"], "2017-01-01T00:00:00")
Expand Down Expand Up @@ -764,7 +767,7 @@ def test_custom_template_processors_ignored(self) -> None:
def test_custom_templated_sql_json(self, sql_lab_mock, mock_dt) -> None:
"""Test sqllab receives macros expanded query."""
mock_dt.utcnow = mock.Mock(return_value=datetime.datetime(1970, 1, 1))
self.login("admin")
self.login()
sql = "SELECT '$DATE()' as test"
resp = {
"status": utils.QueryStatus.SUCCESS,
Expand Down Expand Up @@ -952,44 +955,47 @@ def test_get_select_star_not_allowed(self):

@mock.patch("superset.views.core.results_backend_use_msgpack", False)
@mock.patch("superset.views.core.results_backend")
@mock.patch("superset.views.core.db")
def test_display_limit(self, mock_superset_db, mock_results_backend):
query_mock = mock.Mock()
query_mock.sql = "SELECT *"
query_mock.database = 1
query_mock.schema = "superset"
mock_superset_db.session.query().filter_by().one_or_none.return_value = (
query_mock
)
def test_display_limit(self, mock_results_backend):
self.login()

data = [{"col_0": i} for i in range(100)]
payload = {
"status": utils.QueryStatus.SUCCESS,
"query": {"rows": 100},
"data": data,
}
# limit results to 1
expected_key = {"status": "success", "query": {"rows": 100}, "data": data}
limited_data = data[:1]
expected_limited = {
"status": "success",
"query": {"rows": 100},
"data": limited_data,
"displayLimitReached": True,
}

query_mock = mock.Mock()
query_mock.sql = "SELECT *"
query_mock.database = 1
query_mock.schema = "superset"

# do not apply msgpack serialization
use_msgpack = app.config["RESULTS_BACKEND_USE_MSGPACK"]
app.config["RESULTS_BACKEND_USE_MSGPACK"] = False
serialized_payload = sql_lab._serialize_payload(payload, False)
compressed = utils.zlib_compress(serialized_payload)
mock_results_backend.get.return_value = compressed

# get all results
result = json.loads(self.get_resp("/superset/results/key/"))
expected = {"status": "success", "query": {"rows": 100}, "data": data}
self.assertEqual(result, expected)
with mock.patch("superset.views.core.db") as mock_superset_db:
mock_superset_db.session.query().filter_by().one_or_none.return_value = (
query_mock
)
# get all results
result_key = json.loads(self.get_resp("/superset/results/key/"))
result_limited = json.loads(self.get_resp("/superset/results/key/?rows=1"))

# limit results to 1
limited_data = data[:1]
result = json.loads(self.get_resp("/superset/results/key/?rows=1"))
expected = {
"status": "success",
"query": {"rows": 100},
"data": limited_data,
"displayLimitReached": True,
}
self.assertEqual(result, expected)
self.assertEqual(result_key, expected_key)
self.assertEqual(result_limited, expected_limited)

app.config["RESULTS_BACKEND_USE_MSGPACK"] = use_msgpack

Expand Down

0 comments on commit b50f3dc

Please sign in to comment.