2016-11-10 02:08:22 -05:00
|
|
|
"""Unit tests for Superset"""
|
2016-04-20 18:08:10 -04:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
import csv
|
2016-02-10 12:34:09 -05:00
|
|
|
import doctest
|
2016-08-30 00:55:31 -04:00
|
|
|
import json
|
|
|
|
import io
|
2016-09-11 10:39:07 -04:00
|
|
|
import random
|
2015-09-26 18:55:33 -04:00
|
|
|
import unittest
|
2016-03-28 11:09:46 -04:00
|
|
|
|
|
|
|
from flask import escape
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-12-01 22:53:23 -05:00
|
|
|
from superset import db, models, utils, appbuilder, sm, jinja_context, sql_lab
|
2016-11-10 02:08:22 -05:00
|
|
|
from superset.views import DatabaseView
|
2016-03-28 11:09:46 -04:00
|
|
|
|
2016-11-10 02:08:22 -05:00
|
|
|
from .base_tests import SupersetTestCase
|
2016-12-13 19:05:05 -05:00
|
|
|
import logging
|
2016-03-28 11:09:46 -04:00
|
|
|
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-11-10 02:08:22 -05:00
|
|
|
class CoreTests(SupersetTestCase):
|
2016-04-20 18:08:10 -04:00
|
|
|
|
2016-10-02 21:03:19 -04:00
|
|
|
requires_examples = True
|
|
|
|
|
2016-04-20 18:08:10 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(CoreTests, self).__init__(*args, **kwargs)
|
2016-06-12 14:01:16 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-08-30 00:55:31 -04:00
|
|
|
cls.table_ids = {tbl.table_name: tbl.id for tbl in (
|
2016-04-20 18:08:10 -04:00
|
|
|
db.session
|
|
|
|
.query(models.SqlaTable)
|
|
|
|
.all()
|
|
|
|
)}
|
2015-09-26 18:55:33 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-08-30 00:55:31 -04:00
|
|
|
db.session.query(models.Query).delete()
|
2016-09-22 12:53:14 -04:00
|
|
|
db.session.query(models.DatasourceAccessRequest).delete()
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-03-28 11:09:46 -04:00
|
|
|
def tearDown(self):
|
2016-11-01 23:48:31 -04:00
|
|
|
db.session.query(models.Query).delete()
|
2015-09-22 14:36:54 -04:00
|
|
|
|
2016-10-07 19:24:39 -04:00
|
|
|
def test_welcome(self):
|
|
|
|
self.login()
|
2016-11-10 02:08:22 -05:00
|
|
|
resp = self.client.get('/superset/welcome')
|
2016-10-07 19:24:39 -04:00
|
|
|
assert 'Welcome' in resp.data.decode('utf-8')
|
|
|
|
|
|
|
|
def test_slice_endpoint(self):
|
|
|
|
self.login(username='admin')
|
|
|
|
slc = self.get_slice("Girls", db.session)
|
2016-11-10 02:08:22 -05:00
|
|
|
resp = self.get_resp('/superset/slice/{}/'.format(slc.id))
|
2016-10-07 19:24:39 -04:00
|
|
|
assert 'Time Column' in resp
|
|
|
|
assert 'List Roles' in resp
|
|
|
|
|
|
|
|
# Testing overrides
|
|
|
|
resp = self.get_resp(
|
2016-11-10 02:08:22 -05:00
|
|
|
'/superset/slice/{}/?standalone=true'.format(slc.id))
|
2016-10-07 19:24:39 -04:00
|
|
|
assert 'List Roles' not in resp
|
|
|
|
|
|
|
|
def test_endpoints_for_a_slice(self):
|
|
|
|
self.login(username='admin')
|
|
|
|
slc = self.get_slice("Girls", db.session)
|
|
|
|
|
|
|
|
resp = self.get_resp(slc.viz.csv_endpoint)
|
|
|
|
assert 'Jennifer,' in resp
|
|
|
|
|
|
|
|
resp = self.get_resp(slc.viz.json_endpoint)
|
|
|
|
assert '"Jennifer"' in resp
|
|
|
|
|
2016-09-22 12:53:14 -04:00
|
|
|
def test_admin_only_permissions(self):
|
|
|
|
def assert_admin_permission_in(role_name, assert_func):
|
|
|
|
role = sm.find_role(role_name)
|
|
|
|
permissions = [p.permission.name for p in role.permissions]
|
|
|
|
assert_func('can_sync_druid_source', permissions)
|
|
|
|
assert_func('can_approve', permissions)
|
|
|
|
|
|
|
|
assert_admin_permission_in('Admin', self.assertIn)
|
|
|
|
assert_admin_permission_in('Alpha', self.assertNotIn)
|
|
|
|
assert_admin_permission_in('Gamma', self.assertNotIn)
|
|
|
|
|
|
|
|
def test_admin_only_menu_views(self):
|
|
|
|
def assert_admin_view_menus_in(role_name, assert_func):
|
|
|
|
role = sm.find_role(role_name)
|
|
|
|
view_menus = [p.view_menu.name for p in role.permissions]
|
|
|
|
assert_func('ResetPasswordView', view_menus)
|
|
|
|
assert_func('RoleModelView', view_menus)
|
|
|
|
assert_func('Security', view_menus)
|
|
|
|
assert_func('UserDBModelView', view_menus)
|
2016-09-23 20:41:24 -04:00
|
|
|
assert_func('SQL Lab',
|
2016-09-22 12:53:14 -04:00
|
|
|
view_menus)
|
|
|
|
assert_func('AccessRequestsModelView', view_menus)
|
|
|
|
|
|
|
|
assert_admin_view_menus_in('Admin', self.assertIn)
|
|
|
|
assert_admin_view_menus_in('Alpha', self.assertNotIn)
|
|
|
|
assert_admin_view_menus_in('Gamma', self.assertNotIn)
|
|
|
|
|
2016-04-20 18:08:10 -04:00
|
|
|
def test_save_slice(self):
|
2016-06-02 22:17:34 -04:00
|
|
|
self.login(username='admin')
|
2016-11-17 14:58:33 -05:00
|
|
|
slice_name = "Energy Sankey"
|
|
|
|
slice_id = self.get_slice(slice_name, db.session).id
|
|
|
|
db.session.commit()
|
2016-04-20 18:08:10 -04:00
|
|
|
copy_name = "Test Sankey Save"
|
|
|
|
tbl_id = self.table_ids.get('energy_usage')
|
2016-08-30 00:55:31 -04:00
|
|
|
url = (
|
2016-11-10 02:08:22 -05:00
|
|
|
"/superset/explore/table/{}/?viz_type=sankey&groupby=source&"
|
2016-08-30 00:55:31 -04:00
|
|
|
"groupby=target&metric=sum__value&row_limit=5000&where=&having=&"
|
|
|
|
"flt_col_0=source&flt_op_0=in&flt_eq_0=&slice_id={}&slice_name={}&"
|
|
|
|
"collapsed_fieldsets=&action={}&datasource_name=energy_usage&"
|
|
|
|
"datasource_id=1&datasource_type=table&previous_viz_type=sankey")
|
2016-04-20 18:08:10 -04:00
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
# Changing name
|
2016-10-02 21:03:19 -04:00
|
|
|
resp = self.get_resp(url.format(tbl_id, slice_id, copy_name, 'save'))
|
|
|
|
assert copy_name in resp
|
2016-11-17 14:58:33 -05:00
|
|
|
|
|
|
|
# Setting the name back to its original name
|
|
|
|
resp = self.get_resp(url.format(tbl_id, slice_id, slice_name, 'save'))
|
|
|
|
assert slice_name in resp
|
|
|
|
|
|
|
|
# Doing a basic overwrite
|
2016-10-02 21:03:19 -04:00
|
|
|
assert 'Energy' in self.get_resp(
|
|
|
|
url.format(tbl_id, slice_id, copy_name, 'overwrite'))
|
2016-04-20 18:08:10 -04:00
|
|
|
|
2016-12-16 17:23:48 -05:00
|
|
|
def test_filter_endpoint(self):
|
|
|
|
self.login(username='admin')
|
|
|
|
slice_name = "Energy Sankey"
|
|
|
|
slice_id = self.get_slice(slice_name, db.session).id
|
|
|
|
db.session.commit()
|
|
|
|
tbl_id = self.table_ids.get('energy_usage')
|
|
|
|
table = db.session.query(models.SqlaTable).filter(models.SqlaTable.id == tbl_id)
|
|
|
|
table.filter_select_enabled = True
|
|
|
|
url = (
|
|
|
|
"/superset/filter/table/{}/target/?viz_type=sankey&groupby=source"
|
|
|
|
"&metric=sum__value&flt_col_0=source&flt_op_0=in&flt_eq_0=&"
|
|
|
|
"slice_id={}&datasource_name=energy_usage&"
|
|
|
|
"datasource_id=1&datasource_type=table")
|
|
|
|
|
|
|
|
# Changing name
|
|
|
|
resp = self.get_resp(url.format(tbl_id, slice_id))
|
|
|
|
assert len(resp) > 0
|
|
|
|
assert 'Carbon Dioxide' in resp
|
|
|
|
|
2015-09-26 18:55:33 -04:00
|
|
|
def test_slices(self):
|
2016-06-20 12:18:03 -04:00
|
|
|
# Testing by hitting the two supported end points for all slices
|
2016-06-02 22:17:34 -04:00
|
|
|
self.login(username='admin')
|
2015-09-26 18:55:33 -04:00
|
|
|
Slc = models.Slice
|
2016-03-28 11:09:46 -04:00
|
|
|
urls = []
|
2015-09-26 18:55:33 -04:00
|
|
|
for slc in db.session.query(Slc).all():
|
2016-03-28 11:09:46 -04:00
|
|
|
urls += [
|
2016-06-20 12:18:03 -04:00
|
|
|
(slc.slice_name, 'slice_url', slc.slice_url),
|
2016-06-09 21:05:58 -04:00
|
|
|
(slc.slice_name, 'json_endpoint', slc.viz.json_endpoint),
|
|
|
|
(slc.slice_name, 'csv_endpoint', slc.viz.csv_endpoint),
|
2016-10-12 16:50:47 -04:00
|
|
|
(slc.slice_name, 'slice_id_url', slc.slice_id_url),
|
2016-03-28 11:09:46 -04:00
|
|
|
]
|
2016-06-09 21:05:58 -04:00
|
|
|
for name, method, url in urls:
|
2016-12-13 19:05:05 -05:00
|
|
|
logging.info("[{name}]/[{method}]: {url}".format(**locals()))
|
2016-03-28 11:09:46 -04:00
|
|
|
self.client.get(url)
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-12-12 19:42:38 -05:00
|
|
|
def test_slices_V2(self):
|
|
|
|
# Add explore-v2-beta role to admin user
|
|
|
|
# Test all slice urls as user with with explore-v2-beta role
|
|
|
|
sm.add_role('explore-v2-beta')
|
|
|
|
|
|
|
|
appbuilder.sm.add_user(
|
|
|
|
'explore_beta', 'explore_beta', ' user', 'explore_beta@airbnb.com',
|
|
|
|
appbuilder.sm.find_role('explore-v2-beta'),
|
|
|
|
password='general')
|
|
|
|
self.login(username='explore_beta', password='general')
|
|
|
|
|
|
|
|
Slc = models.Slice
|
|
|
|
urls = []
|
|
|
|
for slc in db.session.query(Slc).all():
|
|
|
|
urls += [
|
|
|
|
(slc.slice_name, 'slice_url', slc.slice_url),
|
|
|
|
]
|
|
|
|
for name, method, url in urls:
|
|
|
|
print("[{name}]/[{method}]: {url}".format(**locals()))
|
|
|
|
response = self.client.get(url)
|
|
|
|
|
2015-09-26 18:55:33 -04:00
|
|
|
def test_dashboard(self):
|
2016-06-02 22:17:34 -04:00
|
|
|
self.login(username='admin')
|
2016-03-28 11:09:46 -04:00
|
|
|
urls = {}
|
2015-09-26 18:55:33 -04:00
|
|
|
for dash in db.session.query(models.Dashboard).all():
|
2016-03-28 11:09:46 -04:00
|
|
|
urls[dash.dashboard_title] = dash.url
|
|
|
|
for title, url in urls.items():
|
2016-03-29 11:40:18 -04:00
|
|
|
assert escape(title) in self.client.get(url).data.decode('utf-8')
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-02-10 12:34:09 -05:00
|
|
|
def test_doctests(self):
|
2016-12-01 22:53:23 -05:00
|
|
|
modules = [utils, models, sql_lab]
|
2016-02-10 12:34:09 -05:00
|
|
|
for mod in modules:
|
|
|
|
failed, tests = doctest.testmod(mod)
|
|
|
|
if failed:
|
|
|
|
raise Exception("Failed a doctest")
|
|
|
|
|
2016-03-28 11:09:46 -04:00
|
|
|
def test_misc(self):
|
2016-10-02 21:03:19 -04:00
|
|
|
assert self.get_resp('/health') == "OK"
|
|
|
|
assert self.get_resp('/ping') == "OK"
|
2016-03-27 17:23:33 -04:00
|
|
|
|
2016-09-17 15:32:41 -04:00
|
|
|
def test_testconn(self):
|
2016-10-19 12:17:08 -04:00
|
|
|
database = self.get_main_database(db.session)
|
2016-09-19 18:14:00 -04:00
|
|
|
|
|
|
|
# validate that the endpoint works with the password-masked sqlalchemy uri
|
|
|
|
data = json.dumps({
|
|
|
|
'uri': database.safe_sqlalchemy_uri(),
|
|
|
|
'name': 'main'
|
|
|
|
})
|
2016-11-10 02:08:22 -05:00
|
|
|
response = self.client.post('/superset/testconn', data=data, content_type='application/json')
|
2016-09-17 15:32:41 -04:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
2016-09-19 18:14:00 -04:00
|
|
|
# validate that the endpoint works with the decrypted sqlalchemy uri
|
2016-09-17 15:32:41 -04:00
|
|
|
data = json.dumps({
|
2016-09-19 18:14:00 -04:00
|
|
|
'uri': database.sqlalchemy_uri_decrypted,
|
2016-09-17 15:32:41 -04:00
|
|
|
'name': 'main'
|
|
|
|
})
|
2016-11-10 02:08:22 -05:00
|
|
|
response = self.client.post('/superset/testconn', data=data, content_type='application/json')
|
2016-09-17 15:32:41 -04:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
2016-10-11 19:49:40 -04:00
|
|
|
def test_databaseview_edit(self, username='admin'):
|
|
|
|
# validate that sending a password-masked uri does not over-write the decrypted uri
|
|
|
|
self.login(username=username)
|
2016-10-19 12:17:08 -04:00
|
|
|
database = self.get_main_database(db.session)
|
2016-10-11 19:49:40 -04:00
|
|
|
sqlalchemy_uri_decrypted = database.sqlalchemy_uri_decrypted
|
|
|
|
url = 'databaseview/edit/{}'.format(database.id)
|
|
|
|
data = {k: database.__getattribute__(k) for k in DatabaseView.add_columns}
|
|
|
|
data['sqlalchemy_uri'] = database.safe_sqlalchemy_uri()
|
2016-10-19 12:17:08 -04:00
|
|
|
self.client.post(url, data=data)
|
|
|
|
database = self.get_main_database(db.session)
|
2016-10-11 19:49:40 -04:00
|
|
|
self.assertEqual(sqlalchemy_uri_decrypted, database.sqlalchemy_uri_decrypted)
|
2016-09-19 18:14:00 -04:00
|
|
|
|
2016-09-06 16:58:09 -04:00
|
|
|
def test_warm_up_cache(self):
|
|
|
|
slice = db.session.query(models.Slice).first()
|
2016-10-19 12:17:08 -04:00
|
|
|
data = self.get_json_resp(
|
2016-11-10 02:08:22 -05:00
|
|
|
'/superset/warm_up_cache?slice_id={}'.format(slice.id))
|
2016-09-06 16:58:09 -04:00
|
|
|
assert data == [{'slice_id': slice.id, 'slice_name': slice.slice_name}]
|
|
|
|
|
2016-10-19 12:17:08 -04:00
|
|
|
data = self.get_json_resp(
|
2016-11-10 02:08:22 -05:00
|
|
|
'/superset/warm_up_cache?table_name=energy_usage&db_name=main')
|
2016-09-06 16:58:09 -04:00
|
|
|
assert len(data) == 3
|
|
|
|
|
2016-04-20 18:08:10 -04:00
|
|
|
def test_shortner(self):
|
2016-06-02 22:17:34 -04:00
|
|
|
self.login(username='admin')
|
2016-08-30 00:55:31 -04:00
|
|
|
data = (
|
2016-11-10 02:08:22 -05:00
|
|
|
"//superset/explore/table/1/?viz_type=sankey&groupby=source&"
|
2016-08-30 00:55:31 -04:00
|
|
|
"groupby=target&metric=sum__value&row_limit=5000&where=&having=&"
|
|
|
|
"flt_col_0=source&flt_op_0=in&flt_eq_0=&slice_id=78&slice_name="
|
|
|
|
"Energy+Sankey&collapsed_fieldsets=&action=&datasource_name="
|
|
|
|
"energy_usage&datasource_id=1&datasource_type=table&"
|
2016-09-22 12:53:14 -04:00
|
|
|
"previous_viz_type=sankey"
|
|
|
|
)
|
2016-04-20 18:08:10 -04:00
|
|
|
resp = self.client.post('/r/shortner/', data=data)
|
|
|
|
assert '/r/' in resp.data.decode('utf-8')
|
|
|
|
|
2016-06-02 22:17:34 -04:00
|
|
|
def test_save_dash(self, username='admin'):
|
|
|
|
self.login(username=username)
|
2016-08-30 00:55:31 -04:00
|
|
|
dash = db.session.query(models.Dashboard).filter_by(
|
|
|
|
slug="births").first()
|
2016-06-02 22:17:34 -04:00
|
|
|
positions = []
|
|
|
|
for i, slc in enumerate(dash.slices):
|
|
|
|
d = {
|
|
|
|
'col': 0,
|
|
|
|
'row': i * 4,
|
|
|
|
'size_x': 4,
|
|
|
|
'size_y': 4,
|
|
|
|
'slice_id': '{}'.format(slc.id)}
|
|
|
|
positions.append(d)
|
|
|
|
data = {
|
|
|
|
'css': '',
|
|
|
|
'expanded_slices': {},
|
|
|
|
'positions': positions,
|
|
|
|
}
|
2016-11-10 02:08:22 -05:00
|
|
|
url = '/superset/save_dash/{}/'.format(dash.id)
|
2016-06-02 22:17:34 -04:00
|
|
|
resp = self.client.post(url, data=dict(data=json.dumps(data)))
|
2016-04-20 18:08:10 -04:00
|
|
|
assert "SUCCESS" in resp.data.decode('utf-8')
|
|
|
|
|
2016-11-28 11:34:14 -05:00
|
|
|
def test_copy_dash(self, username='admin'):
|
|
|
|
self.login(username=username)
|
|
|
|
dash = db.session.query(models.Dashboard).filter_by(
|
|
|
|
slug="births").first()
|
|
|
|
positions = []
|
|
|
|
for i, slc in enumerate(dash.slices):
|
|
|
|
d = {
|
|
|
|
'col': 0,
|
|
|
|
'row': i * 4,
|
|
|
|
'size_x': 4,
|
|
|
|
'size_y': 4,
|
|
|
|
'slice_id': '{}'.format(slc.id)}
|
|
|
|
positions.append(d)
|
|
|
|
data = {
|
|
|
|
'css': '',
|
|
|
|
'expanded_slices': {},
|
|
|
|
'positions': positions,
|
|
|
|
'dashboard_title': 'Copy Of Births',
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save changes to Births dashboard and retrieve updated dash
|
|
|
|
dash_id = dash.id
|
|
|
|
url = '/superset/save_dash/{}/'.format(dash_id)
|
|
|
|
self.client.post(url, data=dict(data=json.dumps(data)))
|
|
|
|
dash = db.session.query(models.Dashboard).filter_by(
|
|
|
|
id=dash_id).first()
|
|
|
|
orig_json_data = json.loads(dash.json_data)
|
|
|
|
|
|
|
|
# Verify that copy matches original
|
|
|
|
url = '/superset/copy_dash/{}/'.format(dash_id)
|
|
|
|
resp = self.get_json_resp(url, data=dict(data=json.dumps(data)))
|
|
|
|
self.assertEqual(resp['dashboard_title'], 'Copy Of Births')
|
|
|
|
self.assertEqual(resp['position_json'], orig_json_data['position_json'])
|
|
|
|
self.assertEqual(resp['metadata'], orig_json_data['metadata'])
|
|
|
|
self.assertEqual(resp['slices'], orig_json_data['slices'])
|
|
|
|
|
2016-07-08 00:40:33 -04:00
|
|
|
def test_add_slices(self, username='admin'):
|
|
|
|
self.login(username=username)
|
2016-08-30 00:55:31 -04:00
|
|
|
dash = db.session.query(models.Dashboard).filter_by(
|
|
|
|
slug="births").first()
|
|
|
|
new_slice = db.session.query(models.Slice).filter_by(
|
|
|
|
slice_name="Mapbox Long/Lat").first()
|
|
|
|
existing_slice = db.session.query(models.Slice).filter_by(
|
|
|
|
slice_name="Name Cloud").first()
|
2016-07-08 00:40:33 -04:00
|
|
|
data = {
|
2016-08-30 00:55:31 -04:00
|
|
|
"slice_ids": [new_slice.data["slice_id"],
|
|
|
|
existing_slice.data["slice_id"]]
|
2016-07-08 00:40:33 -04:00
|
|
|
}
|
2016-11-10 02:08:22 -05:00
|
|
|
url = '/superset/add_slices/{}/'.format(dash.id)
|
2016-07-08 00:40:33 -04:00
|
|
|
resp = self.client.post(url, data=dict(data=json.dumps(data)))
|
|
|
|
assert "SLICES ADDED" in resp.data.decode('utf-8')
|
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
dash = db.session.query(models.Dashboard).filter_by(
|
|
|
|
slug="births").first()
|
|
|
|
new_slice = db.session.query(models.Slice).filter_by(
|
|
|
|
slice_name="Mapbox Long/Lat").first()
|
2016-07-08 00:40:33 -04:00
|
|
|
assert new_slice in dash.slices
|
|
|
|
assert len(set(dash.slices)) == len(dash.slices)
|
|
|
|
|
2016-10-02 21:03:19 -04:00
|
|
|
# cleaning up
|
|
|
|
dash = db.session.query(models.Dashboard).filter_by(
|
|
|
|
slug="births").first()
|
|
|
|
dash.slices = [
|
|
|
|
o for o in dash.slices if o.slice_name != "Mapbox Long/Lat"]
|
2016-09-09 20:12:09 -04:00
|
|
|
db.session.commit()
|
|
|
|
|
2016-04-26 19:44:51 -04:00
|
|
|
def test_gamma(self):
|
2016-06-02 22:17:34 -04:00
|
|
|
self.login(username='gamma')
|
2016-10-02 21:03:19 -04:00
|
|
|
assert "List Slice" in self.get_resp('/slicemodelview/list/')
|
|
|
|
assert "List Dashboard" in self.get_resp('/dashboardmodelview/list/')
|
2016-04-26 19:44:51 -04:00
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
def test_csv_endpoint(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.login('admin')
|
2016-09-11 10:39:07 -04:00
|
|
|
sql = """
|
|
|
|
SELECT first_name, last_name
|
|
|
|
FROM ab_user
|
|
|
|
WHERE first_name='admin'
|
|
|
|
"""
|
|
|
|
client_id = "{}".format(random.getrandbits(64))[:10]
|
2016-11-17 14:58:33 -05:00
|
|
|
self.run_sql(sql, client_id)
|
2016-07-30 01:39:33 -04:00
|
|
|
|
2016-11-10 02:08:22 -05:00
|
|
|
resp = self.get_resp('/superset/csv/{}'.format(client_id))
|
2016-10-02 21:03:19 -04:00
|
|
|
data = csv.reader(io.StringIO(resp))
|
2016-09-11 10:39:07 -04:00
|
|
|
expected_data = csv.reader(
|
|
|
|
io.StringIO("first_name,last_name\nadmin, user\n"))
|
2016-08-30 00:55:31 -04:00
|
|
|
|
|
|
|
self.assertEqual(list(expected_data), list(data))
|
|
|
|
self.logout()
|
|
|
|
|
2016-05-04 01:31:37 -04:00
|
|
|
def test_public_user_dashboard_access(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
table = (
|
|
|
|
db.session
|
|
|
|
.query(models.SqlaTable)
|
|
|
|
.filter_by(table_name='birth_names')
|
|
|
|
.one()
|
|
|
|
)
|
2016-05-04 01:31:37 -04:00
|
|
|
# Try access before adding appropriate permissions.
|
2016-11-17 14:58:33 -05:00
|
|
|
self.revoke_public_access_to_table(table)
|
2016-06-02 22:17:34 -04:00
|
|
|
self.logout()
|
|
|
|
|
2016-10-02 21:03:19 -04:00
|
|
|
resp = self.get_resp('/slicemodelview/list/')
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertNotIn('birth_names</a>', resp)
|
2016-05-04 01:31:37 -04:00
|
|
|
|
2016-10-02 21:03:19 -04:00
|
|
|
resp = self.get_resp('/dashboardmodelview/list/')
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertNotIn('/superset/dashboard/births/', resp)
|
2016-05-04 01:31:37 -04:00
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
self.grant_public_access_to_table(table)
|
2016-05-04 01:31:37 -04:00
|
|
|
|
|
|
|
# Try access after adding appropriate permissions.
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertIn('birth_names', self.get_resp('/slicemodelview/list/'))
|
2016-05-04 01:31:37 -04:00
|
|
|
|
2016-10-02 21:03:19 -04:00
|
|
|
resp = self.get_resp('/dashboardmodelview/list/')
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertIn("/superset/dashboard/births/", resp)
|
2016-05-04 01:31:37 -04:00
|
|
|
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertIn('Births', self.get_resp('/superset/dashboard/births/'))
|
2016-05-04 01:31:37 -04:00
|
|
|
|
|
|
|
# Confirm that public doesn't have access to other datasets.
|
2016-10-02 21:03:19 -04:00
|
|
|
resp = self.get_resp('/slicemodelview/list/')
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertNotIn('wb_health_population</a>', resp)
|
2016-08-30 00:55:31 -04:00
|
|
|
|
2016-10-02 21:03:19 -04:00
|
|
|
resp = self.get_resp('/dashboardmodelview/list/')
|
2016-11-17 14:58:33 -05:00
|
|
|
self.assertNotIn("/superset/dashboard/world_health/", resp)
|
2016-05-04 01:31:37 -04:00
|
|
|
|
2016-10-11 11:04:00 -04:00
|
|
|
def test_dashboard_with_created_by_can_be_accessed_by_public_users(self):
|
|
|
|
self.logout()
|
2016-11-17 14:58:33 -05:00
|
|
|
table = (
|
|
|
|
db.session
|
|
|
|
.query(models.SqlaTable)
|
|
|
|
.filter_by(table_name='birth_names')
|
|
|
|
.one()
|
|
|
|
)
|
|
|
|
self.grant_public_access_to_table(table)
|
2016-10-11 11:04:00 -04:00
|
|
|
|
|
|
|
dash = db.session.query(models.Dashboard).filter_by(dashboard_title="Births").first()
|
|
|
|
dash.owners = [appbuilder.sm.find_user('admin')]
|
|
|
|
dash.created_by = appbuilder.sm.find_user('admin')
|
|
|
|
db.session.merge(dash)
|
|
|
|
db.session.commit()
|
|
|
|
|
2016-11-10 02:08:22 -05:00
|
|
|
assert 'Births' in self.get_resp('/superset/dashboard/births/')
|
2016-10-11 11:04:00 -04:00
|
|
|
|
2016-06-02 22:17:34 -04:00
|
|
|
def test_only_owners_can_save(self):
|
|
|
|
dash = (
|
|
|
|
db.session
|
|
|
|
.query(models.Dashboard)
|
|
|
|
.filter_by(slug="births")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
dash.owners = []
|
|
|
|
db.session.merge(dash)
|
|
|
|
db.session.commit()
|
|
|
|
self.test_save_dash('admin')
|
|
|
|
|
|
|
|
self.logout()
|
|
|
|
self.assertRaises(
|
2016-06-21 12:42:54 -04:00
|
|
|
AssertionError, self.test_save_dash, 'alpha')
|
2016-06-02 22:17:34 -04:00
|
|
|
|
|
|
|
alpha = appbuilder.sm.find_user('alpha')
|
|
|
|
|
|
|
|
dash = (
|
|
|
|
db.session
|
|
|
|
.query(models.Dashboard)
|
|
|
|
.filter_by(slug="births")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
dash.owners = [alpha]
|
|
|
|
db.session.merge(dash)
|
|
|
|
db.session.commit()
|
|
|
|
self.test_save_dash('alpha')
|
2016-04-20 18:08:10 -04:00
|
|
|
|
2016-10-19 12:17:08 -04:00
|
|
|
def test_extra_table_metadata(self):
|
|
|
|
self.login('admin')
|
|
|
|
dbid = self.get_main_database(db.session).id
|
|
|
|
self.get_json_resp(
|
2016-11-10 02:08:22 -05:00
|
|
|
'/superset/extra_table_metadata/{dbid}/'
|
2016-10-19 12:17:08 -04:00
|
|
|
'ab_permission_view/panoramix/'.format(**locals()))
|
|
|
|
|
2016-10-26 14:09:27 -04:00
|
|
|
def test_process_template(self):
|
2016-11-02 16:22:07 -04:00
|
|
|
maindb = self.get_main_database(db.session)
|
2016-10-26 14:09:27 -04:00
|
|
|
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}'"
|
2016-11-02 16:22:07 -04:00
|
|
|
tp = jinja_context.get_template_processor(database=maindb)
|
|
|
|
rendered = tp.process_template(sql)
|
2016-10-26 14:09:27 -04:00
|
|
|
self.assertEqual("SELECT '2017-01-01T00:00:00'", rendered)
|
|
|
|
|
|
|
|
def test_templated_sql_json(self):
|
2016-11-17 14:58:33 -05:00
|
|
|
self.login('admin')
|
2016-10-26 14:09:27 -04:00
|
|
|
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}' as test"
|
2016-11-17 14:58:33 -05:00
|
|
|
data = self.run_sql(sql, "fdaklj3ws")
|
2016-10-26 14:09:27 -04:00
|
|
|
self.assertEqual(data['data'][0]['test'], "2017-01-01T00:00:00")
|
|
|
|
|
2016-11-01 02:52:37 -04:00
|
|
|
def test_table_metadata(self):
|
|
|
|
maindb = self.get_main_database(db.session)
|
|
|
|
data = self.get_json_resp(
|
2016-11-10 02:08:22 -05:00
|
|
|
"/superset/table/{}/ab_user/null/".format(maindb.id))
|
2016-11-01 02:52:37 -04:00
|
|
|
self.assertEqual(data['name'], 'ab_user')
|
|
|
|
assert len(data['columns']) > 5
|
|
|
|
assert data.get('selectStar').startswith('SELECT')
|
|
|
|
|
|
|
|
# Engine specific tests
|
|
|
|
backend = maindb.backend
|
|
|
|
if backend in ('mysql', 'postgresql'):
|
|
|
|
self.assertEqual(data.get('primaryKey').get('type'), 'pk')
|
|
|
|
self.assertEqual(
|
|
|
|
data.get('primaryKey').get('column_names')[0], 'id')
|
|
|
|
self.assertEqual(len(data.get('foreignKeys')), 2)
|
|
|
|
if backend == 'mysql':
|
|
|
|
self.assertEqual(len(data.get('indexes')), 7)
|
|
|
|
elif backend == 'postgresql':
|
|
|
|
self.assertEqual(len(data.get('indexes')), 5)
|
|
|
|
|
2016-11-08 18:55:49 -05:00
|
|
|
def test_fetch_datasource_metadata(self):
|
|
|
|
self.login(username='admin')
|
2016-11-18 20:14:26 -05:00
|
|
|
url = '/superset/fetch_datasource_metadata?datasource_type=table&' \
|
|
|
|
'datasource_id=1'
|
2016-11-08 18:55:49 -05:00
|
|
|
resp = json.loads(self.get_resp(url))
|
2016-11-23 12:51:19 -05:00
|
|
|
self.assertEqual(len(resp['field_options']), 21)
|
2016-11-01 02:52:37 -04:00
|
|
|
|
2016-11-18 20:14:26 -05:00
|
|
|
def test_fetch_all_tables(self):
|
|
|
|
self.login(username='admin')
|
|
|
|
database = self.get_main_database(db.session)
|
|
|
|
url = '/superset/all_tables/{}'.format(database.id)
|
|
|
|
resp = json.loads(self.get_resp(url))
|
|
|
|
self.assertIn('tables', resp)
|
|
|
|
self.assertIn('views', resp)
|
|
|
|
|
2016-11-20 00:23:44 -05:00
|
|
|
def test_user_profile(self):
|
|
|
|
self.login(username='admin')
|
2016-12-01 18:21:18 -05:00
|
|
|
slc = self.get_slice("Girls", db.session)
|
|
|
|
|
|
|
|
# Setting some faves
|
|
|
|
url = '/superset/favstar/Slice/{}/select/'.format(slc.id)
|
|
|
|
resp = self.get_json_resp(url)
|
|
|
|
self.assertEqual(resp['count'], 1)
|
|
|
|
|
|
|
|
dash = (
|
|
|
|
db.session
|
|
|
|
.query(models.Dashboard)
|
|
|
|
.filter_by(slug="births")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
url = '/superset/favstar/Dashboard/{}/select/'.format(dash.id)
|
|
|
|
resp = self.get_json_resp(url)
|
|
|
|
self.assertEqual(resp['count'], 1)
|
|
|
|
|
2016-11-20 00:23:44 -05:00
|
|
|
userid = appbuilder.sm.find_user('admin').id
|
|
|
|
resp = self.get_resp('/superset/profile/admin/')
|
|
|
|
self.assertIn('"app"', resp)
|
|
|
|
data = self.get_json_resp('/superset/recent_activity/{}/'.format(userid))
|
|
|
|
self.assertNotIn('message', data)
|
|
|
|
data = self.get_json_resp('/superset/created_slices/{}/'.format(userid))
|
|
|
|
self.assertNotIn('message', data)
|
|
|
|
data = self.get_json_resp('/superset/created_dashboards/{}/'.format(userid))
|
|
|
|
self.assertNotIn('message', data)
|
|
|
|
data = self.get_json_resp('/superset/fave_slices/{}/'.format(userid))
|
|
|
|
self.assertNotIn('message', data)
|
|
|
|
data = self.get_json_resp('/superset/fave_dashboards/{}/'.format(userid))
|
|
|
|
self.assertNotIn('message', data)
|
|
|
|
|
2016-11-01 02:52:37 -04:00
|
|
|
|
2015-09-26 18:55:33 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2016-10-28 17:12:53 -04:00
|
|
|
|