sqllab_tests.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. # isort:skip_file
  18. """Unit tests for Sql Lab"""
  19. import json
  20. from datetime import datetime, timedelta
  21. from random import random
  22. import prison
  23. import tests.test_app
  24. from superset import db, security_manager
  25. from superset.connectors.sqla.models import SqlaTable
  26. from superset.dataframe import df_to_records
  27. from superset.db_engine_specs import BaseEngineSpec
  28. from superset.models.sql_lab import Query
  29. from superset.result_set import SupersetResultSet
  30. from superset.utils.core import datetime_to_epoch, get_example_database
  31. from .base_tests import SupersetTestCase
  32. QUERY_1 = "SELECT * FROM birth_names LIMIT 1"
  33. QUERY_2 = "SELECT * FROM NO_TABLE"
  34. QUERY_3 = "SELECT * FROM birth_names LIMIT 10"
  35. class SqlLabTests(SupersetTestCase):
  36. """Testings for Sql Lab"""
  37. def __init__(self, *args, **kwargs):
  38. super(SqlLabTests, self).__init__(*args, **kwargs)
  39. def run_some_queries(self):
  40. db.session.query(Query).delete()
  41. db.session.commit()
  42. self.run_sql(QUERY_1, client_id="client_id_1", user_name="admin")
  43. self.run_sql(QUERY_2, client_id="client_id_3", user_name="admin")
  44. self.run_sql(QUERY_3, client_id="client_id_2", user_name="gamma_sqllab")
  45. self.logout()
  46. def tearDown(self):
  47. self.logout()
  48. db.session.query(Query).delete()
  49. db.session.commit()
  50. db.session.close()
  51. def test_sql_json(self):
  52. self.login("admin")
  53. data = self.run_sql("SELECT * FROM birth_names LIMIT 10", "1")
  54. self.assertLess(0, len(data["data"]))
  55. data = self.run_sql("SELECT * FROM unexistant_table", "2")
  56. self.assertLess(0, len(data["error"]))
  57. def test_multi_sql(self):
  58. self.login("admin")
  59. multi_sql = """
  60. SELECT * FROM birth_names LIMIT 1;
  61. SELECT * FROM birth_names LIMIT 2;
  62. """
  63. data = self.run_sql(multi_sql, "2234")
  64. self.assertLess(0, len(data["data"]))
  65. def test_explain(self):
  66. self.login("admin")
  67. data = self.run_sql("EXPLAIN SELECT * FROM birth_names", "1")
  68. self.assertLess(0, len(data["data"]))
  69. def test_sql_json_has_access(self):
  70. examples_db = get_example_database()
  71. examples_db_permission_view = security_manager.add_permission_view_menu(
  72. "database_access", examples_db.perm
  73. )
  74. astronaut = security_manager.add_role("Astronaut")
  75. security_manager.add_permission_role(astronaut, examples_db_permission_view)
  76. # Astronaut role is Gamma + sqllab + db permissions
  77. for perm in security_manager.find_role("Gamma").permissions:
  78. security_manager.add_permission_role(astronaut, perm)
  79. for perm in security_manager.find_role("sql_lab").permissions:
  80. security_manager.add_permission_role(astronaut, perm)
  81. gagarin = security_manager.find_user("gagarin")
  82. if not gagarin:
  83. security_manager.add_user(
  84. "gagarin",
  85. "Iurii",
  86. "Gagarin",
  87. "gagarin@cosmos.ussr",
  88. astronaut,
  89. password="general",
  90. )
  91. data = self.run_sql(QUERY_1, "3", user_name="gagarin")
  92. db.session.query(Query).delete()
  93. db.session.commit()
  94. self.assertLess(0, len(data["data"]))
  95. def test_queries_endpoint(self):
  96. self.run_some_queries()
  97. # Not logged in, should error out
  98. resp = self.client.get("/superset/queries/0")
  99. # Redirects to the login page
  100. self.assertEqual(403, resp.status_code)
  101. # Admin sees queries
  102. self.login("admin")
  103. data = self.get_json_resp("/superset/queries/0")
  104. self.assertEqual(2, len(data))
  105. # Run 2 more queries
  106. self.run_sql("SELECT * FROM birth_names LIMIT 1", client_id="client_id_4")
  107. self.run_sql("SELECT * FROM birth_names LIMIT 2", client_id="client_id_5")
  108. self.login("admin")
  109. data = self.get_json_resp("/superset/queries/0")
  110. self.assertEqual(4, len(data))
  111. now = datetime.now() + timedelta(days=1)
  112. query = (
  113. db.session.query(Query)
  114. .filter_by(sql="SELECT * FROM birth_names LIMIT 1")
  115. .first()
  116. )
  117. query.changed_on = now
  118. db.session.commit()
  119. data = self.get_json_resp(
  120. "/superset/queries/{}".format(int(datetime_to_epoch(now)) - 1000)
  121. )
  122. self.assertEqual(1, len(data))
  123. self.logout()
  124. resp = self.client.get("/superset/queries/0")
  125. # Redirects to the login page
  126. self.assertEqual(403, resp.status_code)
  127. def test_search_query_on_db_id(self):
  128. self.run_some_queries()
  129. self.login("admin")
  130. examples_dbid = get_example_database().id
  131. # Test search queries on database Id
  132. data = self.get_json_resp(
  133. f"/superset/search_queries?database_id={examples_dbid}"
  134. )
  135. self.assertEqual(3, len(data))
  136. db_ids = [k["dbId"] for k in data]
  137. self.assertEqual([examples_dbid for i in range(3)], db_ids)
  138. resp = self.get_resp("/superset/search_queries?database_id=-1")
  139. data = json.loads(resp)
  140. self.assertEqual(0, len(data))
  141. def test_search_query_on_user(self):
  142. self.run_some_queries()
  143. self.login("admin")
  144. # Test search queries on user Id
  145. user_id = security_manager.find_user("admin").id
  146. data = self.get_json_resp("/superset/search_queries?user_id={}".format(user_id))
  147. self.assertEqual(2, len(data))
  148. user_ids = {k["userId"] for k in data}
  149. self.assertEqual(set([user_id]), user_ids)
  150. user_id = security_manager.find_user("gamma_sqllab").id
  151. resp = self.get_resp("/superset/search_queries?user_id={}".format(user_id))
  152. data = json.loads(resp)
  153. self.assertEqual(1, len(data))
  154. self.assertEqual(data[0]["userId"], user_id)
  155. def test_search_query_on_status(self):
  156. self.run_some_queries()
  157. self.login("admin")
  158. # Test search queries on status
  159. resp = self.get_resp("/superset/search_queries?status=success")
  160. data = json.loads(resp)
  161. self.assertEqual(2, len(data))
  162. states = [k["state"] for k in data]
  163. self.assertEqual(["success", "success"], states)
  164. resp = self.get_resp("/superset/search_queries?status=failed")
  165. data = json.loads(resp)
  166. self.assertEqual(1, len(data))
  167. self.assertEqual(data[0]["state"], "failed")
  168. def test_search_query_on_text(self):
  169. self.run_some_queries()
  170. self.login("admin")
  171. url = "/superset/search_queries?search_text=birth"
  172. data = self.get_json_resp(url)
  173. self.assertEqual(2, len(data))
  174. self.assertIn("birth", data[0]["sql"])
  175. def test_search_query_on_time(self):
  176. self.run_some_queries()
  177. self.login("admin")
  178. first_query_time = (
  179. db.session.query(Query).filter_by(sql=QUERY_1).one()
  180. ).start_time
  181. second_query_time = (
  182. db.session.query(Query).filter_by(sql=QUERY_3).one()
  183. ).start_time
  184. # Test search queries on time filter
  185. from_time = "from={}".format(int(first_query_time))
  186. to_time = "to={}".format(int(second_query_time))
  187. params = [from_time, to_time]
  188. resp = self.get_resp("/superset/search_queries?" + "&".join(params))
  189. data = json.loads(resp)
  190. self.assertEqual(2, len(data))
  191. def test_search_query_only_owned(self) -> None:
  192. """
  193. Test a search query with a user that does not have can_access_all_queries.
  194. """
  195. # Test search_queries for Alpha user
  196. self.run_some_queries()
  197. self.login("gamma_sqllab")
  198. user_id = security_manager.find_user("gamma_sqllab").id
  199. data = self.get_json_resp("/superset/search_queries")
  200. self.assertEqual(1, len(data))
  201. user_ids = {k["userId"] for k in data}
  202. self.assertEqual(set([user_id]), user_ids)
  203. def test_alias_duplicate(self):
  204. self.run_sql(
  205. "SELECT name as col, gender as col FROM birth_names LIMIT 10",
  206. client_id="2e2df3",
  207. user_name="admin",
  208. raise_on_error=True,
  209. )
  210. def test_ps_conversion_no_dict(self):
  211. cols = [["string_col", "string"], ["int_col", "int"], ["float_col", "float"]]
  212. data = [["a", 4, 4.0]]
  213. results = SupersetResultSet(data, cols, BaseEngineSpec)
  214. self.assertEqual(len(data), results.size)
  215. self.assertEqual(len(cols), len(results.columns))
  216. def test_pa_conversion_tuple(self):
  217. cols = ["string_col", "int_col", "list_col", "float_col"]
  218. data = [("Text", 111, [123], 1.0)]
  219. results = SupersetResultSet(data, cols, BaseEngineSpec)
  220. self.assertEqual(len(data), results.size)
  221. self.assertEqual(len(cols), len(results.columns))
  222. def test_pa_conversion_dict(self):
  223. cols = ["string_col", "dict_col", "int_col"]
  224. data = [["a", {"c1": 1, "c2": 2, "c3": 3}, 4]]
  225. results = SupersetResultSet(data, cols, BaseEngineSpec)
  226. self.assertEqual(len(data), results.size)
  227. self.assertEqual(len(cols), len(results.columns))
  228. def test_sqllab_viz(self):
  229. self.login("admin")
  230. examples_dbid = get_example_database().id
  231. payload = {
  232. "chartType": "dist_bar",
  233. "datasourceName": f"test_viz_flow_table_{random()}",
  234. "schema": "superset",
  235. "columns": [
  236. {"is_date": False, "type": "STRING", "name": f"viz_type_{random()}"},
  237. {"is_date": False, "type": "OBJECT", "name": f"ccount_{random()}"},
  238. ],
  239. "sql": """\
  240. SELECT *
  241. FROM birth_names
  242. LIMIT 10""",
  243. "dbId": examples_dbid,
  244. }
  245. data = {"data": json.dumps(payload)}
  246. resp = self.get_json_resp("/superset/sqllab_viz/", data=data)
  247. self.assertIn("table_id", resp)
  248. # ensure owner is set correctly
  249. table_id = resp["table_id"]
  250. table = db.session.query(SqlaTable).filter_by(id=table_id).one()
  251. self.assertEqual([owner.username for owner in table.owners], ["admin"])
  252. def test_sql_limit(self):
  253. self.login("admin")
  254. test_limit = 1
  255. data = self.run_sql("SELECT * FROM birth_names", client_id="sql_limit_1")
  256. self.assertGreater(len(data["data"]), test_limit)
  257. data = self.run_sql(
  258. "SELECT * FROM birth_names", client_id="sql_limit_2", query_limit=test_limit
  259. )
  260. self.assertEqual(len(data["data"]), test_limit)
  261. data = self.run_sql(
  262. "SELECT * FROM birth_names LIMIT {}".format(test_limit),
  263. client_id="sql_limit_3",
  264. query_limit=test_limit + 1,
  265. )
  266. self.assertEqual(len(data["data"]), test_limit)
  267. data = self.run_sql(
  268. "SELECT * FROM birth_names LIMIT {}".format(test_limit + 1),
  269. client_id="sql_limit_4",
  270. query_limit=test_limit,
  271. )
  272. self.assertEqual(len(data["data"]), test_limit)
  273. def test_queryview_filter(self) -> None:
  274. """
  275. Test queryview api without can_only_access_owned_queries perm added to
  276. Admin and make sure all queries show up.
  277. """
  278. self.run_some_queries()
  279. self.login(username="admin")
  280. url = "/queryview/api/read"
  281. data = self.get_json_resp(url)
  282. admin = security_manager.find_user("admin")
  283. gamma_sqllab = security_manager.find_user("gamma_sqllab")
  284. self.assertEqual(3, len(data["result"]))
  285. user_queries = [result.get("username") for result in data["result"]]
  286. assert admin.username in user_queries
  287. assert gamma_sqllab.username in user_queries
  288. def test_queryview_can_access_all_queries(self) -> None:
  289. """
  290. Test queryview api with can_access_all_queries perm added to
  291. gamma and make sure all queries show up.
  292. """
  293. session = db.session
  294. # Add all_query_access perm to Gamma user
  295. all_queries_view = security_manager.find_permission_view_menu(
  296. "all_query_access", "all_query_access"
  297. )
  298. security_manager.add_permission_role(
  299. security_manager.find_role("gamma_sqllab"), all_queries_view
  300. )
  301. session.commit()
  302. # Test search_queries for Admin user
  303. self.run_some_queries()
  304. self.login("gamma_sqllab")
  305. url = "/queryview/api/read"
  306. data = self.get_json_resp(url)
  307. self.assertEqual(3, len(data["result"]))
  308. # Remove all_query_access from gamma sqllab
  309. all_queries_view = security_manager.find_permission_view_menu(
  310. "all_query_access", "all_query_access"
  311. )
  312. security_manager.del_permission_role(
  313. security_manager.find_role("gamma_sqllab"), all_queries_view
  314. )
  315. session.commit()
  316. def test_queryview_admin_can_access_all_queries(self) -> None:
  317. """
  318. Test queryview api with all_query_access perm added to
  319. Admin and make sure only Admin queries show up. This is the default
  320. """
  321. # Test search_queries for Admin user
  322. self.run_some_queries()
  323. self.login("admin")
  324. url = "/queryview/api/read"
  325. data = self.get_json_resp(url)
  326. admin = security_manager.find_user("admin")
  327. self.assertEqual(3, len(data["result"]))
  328. def test_api_database(self):
  329. self.login("admin")
  330. self.create_fake_db()
  331. arguments = {
  332. "keys": [],
  333. "filters": [{"col": "expose_in_sqllab", "opr": "eq", "value": True}],
  334. "order_column": "database_name",
  335. "order_direction": "asc",
  336. "page": 0,
  337. "page_size": -1,
  338. }
  339. url = f"api/v1/database/?q={prison.dumps(arguments)}"
  340. self.assertEqual(
  341. {"examples", "fake_db_100"},
  342. {r.get("database_name") for r in self.get_json_resp(url)["result"]},
  343. )
  344. self.delete_fake_db()