2018-02-25 18:06:11 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-11-01 23:48:31 -04:00
|
|
|
"""Unit tests for Sql Lab"""
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
from datetime import datetime, timedelta
|
2016-11-01 23:48:31 -04:00
|
|
|
import json
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from flask_appbuilder.security.sqla import models as ab_models
|
2017-03-10 12:11:51 -05:00
|
|
|
|
2017-11-07 23:23:40 -05:00
|
|
|
from superset import appbuilder, db, sm, utils
|
|
|
|
from superset.models.sql_lab import Query
|
2018-01-23 23:58:06 -05:00
|
|
|
from superset.sql_lab import convert_results_to_df
|
2016-11-10 02:08:22 -05:00
|
|
|
from .base_tests import SupersetTestCase
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
|
2016-11-10 02:08:22 -05:00
|
|
|
class SqlLabTests(SupersetTestCase):
|
2016-11-01 23:48:31 -04:00
|
|
|
"""Testings for Sql Lab"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(SqlLabTests, self).__init__(*args, **kwargs)
|
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
def run_some_queries(self):
|
2017-04-04 23:15:19 -04:00
|
|
|
db.session.query(Query).delete()
|
2016-11-17 14:58:33 -05:00
|
|
|
db.session.commit()
|
|
|
|
self.run_sql(
|
2017-11-14 00:06:51 -05:00
|
|
|
'SELECT * FROM ab_user',
|
2016-11-17 14:58:33 -05:00
|
|
|
client_id='client_id_1',
|
|
|
|
user_name='admin')
|
|
|
|
self.run_sql(
|
2017-11-14 00:06:51 -05:00
|
|
|
'SELECT * FROM NO_TABLE',
|
2016-11-17 14:58:33 -05:00
|
|
|
client_id='client_id_3',
|
|
|
|
user_name='admin')
|
|
|
|
self.run_sql(
|
2017-11-14 00:06:51 -05:00
|
|
|
'SELECT * FROM ab_permission',
|
2016-11-17 14:58:33 -05:00
|
|
|
client_id='client_id_2',
|
|
|
|
user_name='gamma_sqllab')
|
|
|
|
self.logout()
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2017-04-04 23:15:19 -04:00
|
|
|
db.session.query(Query).delete()
|
2016-11-17 14:58:33 -05:00
|
|
|
db.session.commit()
|
|
|
|
self.logout()
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
def test_sql_json(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.login('admin')
|
|
|
|
|
2017-11-14 00:06:51 -05:00
|
|
|
data = self.run_sql('SELECT * FROM ab_user', '1')
|
2016-11-01 23:48:31 -04:00
|
|
|
self.assertLess(0, len(data['data']))
|
|
|
|
|
2017-11-14 00:06:51 -05:00
|
|
|
data = self.run_sql('SELECT * FROM unexistant_table', '2')
|
2016-11-01 23:48:31 -04:00
|
|
|
self.assertLess(0, len(data['error']))
|
|
|
|
|
|
|
|
def test_sql_json_has_access(self):
|
|
|
|
main_db = self.get_main_database(db.session)
|
2016-11-17 14:58:33 -05:00
|
|
|
sm.add_permission_view_menu('database_access', main_db.perm)
|
2016-11-01 23:48:31 -04:00
|
|
|
db.session.commit()
|
|
|
|
main_db_permission_view = (
|
|
|
|
db.session.query(ab_models.PermissionView)
|
|
|
|
.join(ab_models.ViewMenu)
|
2017-05-22 19:41:32 -04:00
|
|
|
.join(ab_models.Permission)
|
2016-11-01 23:48:31 -04:00
|
|
|
.filter(ab_models.ViewMenu.name == '[main].(id:1)')
|
2017-05-22 19:41:32 -04:00
|
|
|
.filter(ab_models.Permission.name == 'database_access')
|
2016-11-01 23:48:31 -04:00
|
|
|
.first()
|
|
|
|
)
|
2017-11-14 00:06:51 -05:00
|
|
|
astronaut = sm.add_role('Astronaut')
|
2016-11-01 23:48:31 -04:00
|
|
|
sm.add_permission_role(astronaut, main_db_permission_view)
|
2016-11-17 14:58:33 -05:00
|
|
|
# Astronaut role is Gamma + sqllab + main db permissions
|
|
|
|
for perm in sm.find_role('Gamma').permissions:
|
|
|
|
sm.add_permission_role(astronaut, perm)
|
|
|
|
for perm in sm.find_role('sql_lab').permissions:
|
|
|
|
sm.add_permission_role(astronaut, perm)
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
gagarin = appbuilder.sm.find_user('gagarin')
|
|
|
|
if not gagarin:
|
|
|
|
appbuilder.sm.add_user(
|
|
|
|
'gagarin', 'Iurii', 'Gagarin', 'gagarin@cosmos.ussr',
|
2016-11-17 14:58:33 -05:00
|
|
|
astronaut,
|
2016-11-01 23:48:31 -04:00
|
|
|
password='general')
|
2017-11-14 00:06:51 -05:00
|
|
|
data = self.run_sql('SELECT * FROM ab_user', '3', user_name='gagarin')
|
2017-04-04 23:15:19 -04:00
|
|
|
db.session.query(Query).delete()
|
2016-11-01 23:48:31 -04:00
|
|
|
db.session.commit()
|
|
|
|
self.assertLess(0, len(data['data']))
|
|
|
|
|
|
|
|
def test_queries_endpoint(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_some_queries()
|
|
|
|
|
|
|
|
# Not logged in, should error out
|
|
|
|
resp = self.client.get('/superset/queries/0')
|
2016-12-15 08:38:34 -05:00
|
|
|
# Redirects to the login page
|
2016-11-01 23:48:31 -04:00
|
|
|
self.assertEquals(403, resp.status_code)
|
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
# Admin sees queries
|
2016-11-01 23:48:31 -04:00
|
|
|
self.login('admin')
|
2016-11-17 14:58:33 -05:00
|
|
|
data = self.get_json_resp('/superset/queries/0')
|
2016-11-01 23:48:31 -04:00
|
|
|
self.assertEquals(2, len(data))
|
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
# Run 2 more queries
|
2017-11-14 00:06:51 -05:00
|
|
|
self.run_sql('SELECT * FROM ab_user LIMIT 1', client_id='client_id_4')
|
|
|
|
self.run_sql('SELECT * FROM ab_user LIMIT 2', client_id='client_id_5')
|
2016-11-01 23:48:31 -04:00
|
|
|
self.login('admin')
|
2016-11-17 14:58:33 -05:00
|
|
|
data = self.get_json_resp('/superset/queries/0')
|
2016-11-01 23:48:31 -04:00
|
|
|
self.assertEquals(4, len(data))
|
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
now = datetime.now() + timedelta(days=1)
|
2017-04-04 23:15:19 -04:00
|
|
|
query = db.session.query(Query).filter_by(
|
2016-12-06 02:18:16 -05:00
|
|
|
sql='SELECT * FROM ab_user LIMIT 1').first()
|
2016-11-17 14:58:33 -05:00
|
|
|
query.changed_on = now
|
2016-11-01 23:48:31 -04:00
|
|
|
db.session.commit()
|
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
data = self.get_json_resp(
|
|
|
|
'/superset/queries/{}'.format(
|
2017-11-08 23:34:23 -05:00
|
|
|
int(utils.datetime_to_epoch(now)) - 1000))
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertEquals(1, len(data))
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
self.logout()
|
2016-11-17 14:58:33 -05:00
|
|
|
resp = self.client.get('/superset/queries/0')
|
2016-12-15 08:38:34 -05:00
|
|
|
# Redirects to the login page
|
2016-11-01 23:48:31 -04:00
|
|
|
self.assertEquals(403, resp.status_code)
|
|
|
|
|
|
|
|
def test_search_query_on_db_id(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_some_queries()
|
|
|
|
self.login('admin')
|
|
|
|
# Test search queries on database Id
|
2016-12-01 18:21:18 -05:00
|
|
|
data = self.get_json_resp('/superset/search_queries?database_id=1')
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertEquals(3, len(data))
|
2016-12-14 13:14:54 -05:00
|
|
|
db_ids = [k['dbId'] for k in data]
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertEquals([1, 1, 1], db_ids)
|
|
|
|
|
|
|
|
resp = self.get_resp('/superset/search_queries?database_id=-1')
|
|
|
|
data = json.loads(resp)
|
|
|
|
self.assertEquals(0, len(data))
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
def test_search_query_on_user(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_some_queries()
|
|
|
|
self.login('admin')
|
|
|
|
|
|
|
|
# Test search queries on user Id
|
2017-01-11 10:53:24 -05:00
|
|
|
user_id = appbuilder.sm.find_user('admin').id
|
2016-11-17 14:58:33 -05:00
|
|
|
data = self.get_json_resp(
|
2017-01-11 10:53:24 -05:00
|
|
|
'/superset/search_queries?user_id={}'.format(user_id))
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertEquals(2, len(data))
|
2016-12-14 13:14:54 -05:00
|
|
|
user_ids = {k['userId'] for k in data}
|
2017-01-11 10:53:24 -05:00
|
|
|
self.assertEquals(set([user_id]), user_ids)
|
2016-11-17 14:58:33 -05:00
|
|
|
|
2017-01-11 10:53:24 -05:00
|
|
|
user_id = appbuilder.sm.find_user('gamma_sqllab').id
|
2016-12-06 02:18:16 -05:00
|
|
|
resp = self.get_resp(
|
2017-01-11 10:53:24 -05:00
|
|
|
'/superset/search_queries?user_id={}'.format(user_id))
|
2016-11-17 14:58:33 -05:00
|
|
|
data = json.loads(resp)
|
|
|
|
self.assertEquals(1, len(data))
|
2017-11-08 23:34:23 -05:00
|
|
|
self.assertEquals(data[0]['userId'], user_id)
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
def test_search_query_on_status(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_some_queries()
|
|
|
|
self.login('admin')
|
|
|
|
# Test search queries on status
|
|
|
|
resp = self.get_resp('/superset/search_queries?status=success')
|
|
|
|
data = json.loads(resp)
|
|
|
|
self.assertEquals(2, len(data))
|
2016-12-14 13:14:54 -05:00
|
|
|
states = [k['state'] for k in data]
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertEquals(['success', 'success'], states)
|
|
|
|
|
|
|
|
resp = self.get_resp('/superset/search_queries?status=failed')
|
|
|
|
data = json.loads(resp)
|
|
|
|
self.assertEquals(1, len(data))
|
2016-12-14 13:14:54 -05:00
|
|
|
self.assertEquals(data[0]['state'], 'failed')
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
def test_search_query_on_text(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_some_queries()
|
|
|
|
self.login('admin')
|
2016-12-01 18:21:18 -05:00
|
|
|
url = '/superset/search_queries?search_text=permission'
|
|
|
|
data = self.get_json_resp(url)
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertEquals(1, len(data))
|
2016-12-14 13:14:54 -05:00
|
|
|
self.assertIn('permission', data[0]['sql'])
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
def test_search_query_on_time(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_some_queries()
|
|
|
|
self.login('admin')
|
|
|
|
first_query_time = (
|
2017-04-04 23:15:19 -04:00
|
|
|
db.session.query(Query)
|
2016-11-17 14:58:33 -05:00
|
|
|
.filter_by(sql='SELECT * FROM ab_user').one()
|
|
|
|
).start_time
|
|
|
|
second_query_time = (
|
2017-04-04 23:15:19 -04:00
|
|
|
db.session.query(Query)
|
2016-11-17 14:58:33 -05:00
|
|
|
.filter_by(sql='SELECT * FROM ab_permission').one()
|
|
|
|
).start_time
|
|
|
|
# Test search queries on time filter
|
|
|
|
from_time = 'from={}'.format(int(first_query_time))
|
|
|
|
to_time = 'to={}'.format(int(second_query_time))
|
|
|
|
params = [from_time, to_time]
|
2017-07-27 17:00:19 -04:00
|
|
|
resp = self.get_resp('/superset/search_queries?' + '&'.join(params))
|
2016-11-17 14:58:33 -05:00
|
|
|
data = json.loads(resp)
|
|
|
|
self.assertEquals(2, len(data))
|
2016-11-01 23:48:31 -04:00
|
|
|
|
2016-12-01 22:53:23 -05:00
|
|
|
def test_alias_duplicate(self):
|
|
|
|
self.run_sql(
|
2017-11-14 00:06:51 -05:00
|
|
|
'SELECT username as col, id as col, username FROM ab_user',
|
2016-12-01 22:53:23 -05:00
|
|
|
client_id='2e2df3',
|
|
|
|
user_name='admin',
|
|
|
|
raise_on_error=True)
|
|
|
|
|
2018-01-23 23:58:06 -05:00
|
|
|
def test_df_conversion_no_dict(self):
|
|
|
|
cols = [['string_col'], ['int_col']]
|
|
|
|
data = [['a', 4]]
|
|
|
|
cdf = convert_results_to_df(cols, data)
|
|
|
|
|
|
|
|
self.assertEquals(len(data), cdf.size)
|
|
|
|
self.assertEquals(len(cols), len(cdf.columns))
|
|
|
|
|
|
|
|
def test_df_conversion_dict(self):
|
|
|
|
cols = [['string_col'], ['dict_col'], ['int_col']]
|
|
|
|
data = [['a', {'c1': 1, 'c2': 2, 'c3': 3}, 4]]
|
|
|
|
cdf = convert_results_to_df(cols, data)
|
|
|
|
|
|
|
|
self.assertEquals(len(data), cdf.size)
|
|
|
|
self.assertEquals(len(cols), len(cdf.columns))
|
|
|
|
|
2016-11-01 23:48:31 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|