2016-04-20 18:08:10 -04:00
|
|
|
"""Unit tests for Caravel"""
|
|
|
|
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-04-08 02:01:40 -04:00
|
|
|
import imp
|
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-08-30 00:55:31 -04:00
|
|
|
|
2016-03-28 11:09:46 -04:00
|
|
|
|
|
|
|
from flask import escape
|
2016-05-04 01:31:37 -04:00
|
|
|
from flask_appbuilder.security.sqla import models as ab_models
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-03-29 00:55:58 -04:00
|
|
|
import caravel
|
2016-09-23 00:19:03 -04:00
|
|
|
from caravel import app, db, models, utils, appbuilder, sm
|
|
|
|
from caravel.source_registry import SourceRegistry
|
2016-08-30 00:55:31 -04:00
|
|
|
from caravel.models import DruidDatasource
|
2016-03-28 11:09:46 -04:00
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
from .base_tests import CaravelTestCase
|
2016-03-28 11:09:46 -04:00
|
|
|
|
2015-09-26 18:55:33 -04:00
|
|
|
BASE_DIR = app.config.get("BASE_DIR")
|
2016-03-29 00:55:58 -04:00
|
|
|
cli = imp.load_source('cli', BASE_DIR + "/bin/caravel")
|
2015-09-26 18:55:33 -04:00
|
|
|
|
2016-04-20 18:08:10 -04:00
|
|
|
class CoreTests(CaravelTestCase):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-08-30 00:55:31 -04:00
|
|
|
# Load examples first, so that we setup proper permission-view
|
|
|
|
# relations for all example data sources.
|
2016-04-20 18:08:10 -04:00
|
|
|
super(CoreTests, self).__init__(*args, **kwargs)
|
2016-06-12 14:01:16 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cli.load_examples(load_test_data=True)
|
|
|
|
utils.init(caravel)
|
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):
|
|
|
|
pass
|
2015-09-22 14:36:54 -04:00
|
|
|
|
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-04-26 19:44:51 -04:00
|
|
|
|
2016-06-12 14:01:16 -04:00
|
|
|
slc = (
|
2016-04-26 19:44:51 -04:00
|
|
|
db.session.query(models.Slice.id)
|
|
|
|
.filter_by(slice_name="Energy Sankey")
|
2016-06-12 14:01:16 -04:00
|
|
|
.first())
|
|
|
|
slice_id = slc.id
|
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 = (
|
|
|
|
"/caravel/explore/table/{}/?viz_type=sankey&groupby=source&"
|
|
|
|
"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
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
resp = self.client.get(
|
|
|
|
url.format(tbl_id, slice_id, copy_name, 'save'),
|
|
|
|
follow_redirects=True)
|
|
|
|
assert copy_name in resp.data.decode('utf-8')
|
|
|
|
resp = self.client.get(
|
|
|
|
url.format(tbl_id, slice_id, copy_name, 'overwrite'),
|
|
|
|
follow_redirects=True)
|
|
|
|
assert 'Energy' in resp.data.decode('utf-8')
|
|
|
|
|
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-08-31 19:45:03 -04:00
|
|
|
(slc.slice_name, 'slice_id_url',
|
|
|
|
"/caravel/{slc.datasource_type}/{slc.datasource_id}/{slc.id}/".format(slc=slc)),
|
2016-03-28 11:09:46 -04:00
|
|
|
]
|
2016-06-09 21:05:58 -04:00
|
|
|
for name, method, url in urls:
|
2016-06-12 14:01:16 -04:00
|
|
|
print("[{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
|
|
|
|
|
|
|
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-07-01 17:44:25 -04:00
|
|
|
modules = [utils, models]
|
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-03-29 11:40:18 -04:00
|
|
|
assert self.client.get('/health').data.decode('utf-8') == "OK"
|
|
|
|
assert self.client.get('/ping').data.decode('utf-8') == "OK"
|
2016-03-27 17:23:33 -04:00
|
|
|
|
2016-09-17 15:32:41 -04:00
|
|
|
def test_testconn(self):
|
2016-09-19 18:14:00 -04:00
|
|
|
database = (
|
|
|
|
db.session
|
|
|
|
.query(models.Database)
|
|
|
|
.filter_by(database_name='main')
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
# validate that the endpoint works with the password-masked sqlalchemy uri
|
|
|
|
data = json.dumps({
|
|
|
|
'uri': database.safe_sqlalchemy_uri(),
|
|
|
|
'name': 'main'
|
|
|
|
})
|
2016-09-17 15:32:41 -04:00
|
|
|
response = self.client.post('/caravel/testconn', data=data, content_type='application/json')
|
|
|
|
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'
|
|
|
|
})
|
|
|
|
response = self.client.post('/caravel/testconn', data=data, content_type='application/json')
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
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()
|
|
|
|
resp = self.client.get(
|
|
|
|
'/caravel/warm_up_cache?slice_id={}'.format(slice.id),
|
|
|
|
follow_redirects=True)
|
|
|
|
data = json.loads(resp.data.decode('utf-8'))
|
|
|
|
assert data == [{'slice_id': slice.id, 'slice_name': slice.slice_name}]
|
|
|
|
|
|
|
|
resp = self.client.get(
|
|
|
|
'/caravel/warm_up_cache?table_name=energy_usage&db_name=main',
|
|
|
|
follow_redirects=True)
|
|
|
|
data = json.loads(resp.data.decode('utf-8'))
|
|
|
|
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 = (
|
|
|
|
"//caravel/explore/table/1/?viz_type=sankey&groupby=source&"
|
|
|
|
"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-04-20 18:08:10 -04:00
|
|
|
url = '/caravel/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-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
|
|
|
}
|
|
|
|
url = '/caravel/add_slices/{}/'.format(dash.id)
|
|
|
|
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-09-22 12:53:14 -04:00
|
|
|
def test_approve(self):
|
|
|
|
session = db.session
|
|
|
|
sm.add_role('table_role')
|
|
|
|
self.login('admin')
|
|
|
|
|
|
|
|
def prepare_request(ds_type, ds_name, role):
|
2016-09-23 00:19:03 -04:00
|
|
|
ds_class = SourceRegistry.sources[ds_type]
|
2016-09-22 12:53:14 -04:00
|
|
|
# TODO: generalize datasource names
|
|
|
|
if ds_type == 'table':
|
|
|
|
ds = session.query(ds_class).filter(
|
|
|
|
ds_class.table_name == ds_name).first()
|
|
|
|
else:
|
|
|
|
ds = session.query(ds_class).filter(
|
|
|
|
ds_class.datasource_name == ds_name).first()
|
|
|
|
ds_perm_view = sm.find_permission_view_menu(
|
|
|
|
'datasource_access', ds.perm)
|
|
|
|
sm.add_permission_role(sm.find_role(role), ds_perm_view)
|
|
|
|
access_request = models.DatasourceAccessRequest(
|
|
|
|
datasource_id=ds.id,
|
|
|
|
datasource_type=ds_type,
|
|
|
|
created_by_fk=sm.find_user(username='gamma').id,
|
|
|
|
)
|
|
|
|
session.add(access_request)
|
|
|
|
session.commit()
|
|
|
|
return access_request
|
|
|
|
|
|
|
|
EXTEND_ROLE_REQUEST = (
|
|
|
|
'/caravel/approve?datasource_type={}&datasource_id={}&'
|
|
|
|
'created_by={}&role_to_extend={}')
|
|
|
|
GRANT_ROLE_REQUEST = (
|
|
|
|
'/caravel/approve?datasource_type={}&datasource_id={}&'
|
|
|
|
'created_by={}&role_to_grant={}')
|
|
|
|
|
|
|
|
# Case 1. Grant new role to the user.
|
|
|
|
|
|
|
|
access_request1 = prepare_request(
|
|
|
|
'table', 'unicode_test', 'table_role')
|
|
|
|
ds_1_id = access_request1.datasource_id
|
|
|
|
self.client.get(GRANT_ROLE_REQUEST.format(
|
|
|
|
'table', ds_1_id, 'gamma', 'table_role'))
|
|
|
|
access_requests = self.get_access_requests('gamma', 'table', ds_1_id)
|
|
|
|
# request was removed
|
|
|
|
self.assertFalse(access_requests)
|
|
|
|
# user was granted table_role
|
|
|
|
user_roles = [r.name for r in sm.find_user('gamma').roles]
|
|
|
|
self.assertIn('table_role', user_roles)
|
|
|
|
|
|
|
|
# Case 2. Extend the role to have access to the table
|
|
|
|
|
|
|
|
access_request2 = prepare_request('table', 'long_lat', 'table_role')
|
|
|
|
ds_2_id = access_request2.datasource_id
|
|
|
|
long_lat_perm = access_request2.datasource.perm
|
|
|
|
|
|
|
|
self.client.get(EXTEND_ROLE_REQUEST.format(
|
|
|
|
'table', access_request2.datasource_id, 'gamma', 'table_role'))
|
|
|
|
access_requests = self.get_access_requests('gamma', 'table', ds_2_id)
|
|
|
|
# request was removed
|
|
|
|
self.assertFalse(access_requests)
|
|
|
|
# table_role was extended to grant access to the long_lat table/
|
|
|
|
table_role = sm.find_role('table_role')
|
|
|
|
perm_view = sm.find_permission_view_menu(
|
|
|
|
'datasource_access', long_lat_perm)
|
|
|
|
self.assertIn(perm_view, table_role.permissions)
|
|
|
|
|
|
|
|
# Case 3. Grant new role to the user to access the druid datasource.
|
|
|
|
|
|
|
|
sm.add_role('druid_role')
|
|
|
|
access_request3 = prepare_request('druid', 'druid_ds_1', 'druid_role')
|
|
|
|
self.client.get(GRANT_ROLE_REQUEST.format(
|
|
|
|
'druid', access_request3.datasource_id, 'gamma', 'druid_role'))
|
|
|
|
|
|
|
|
# user was granted table_role
|
|
|
|
user_roles = [r.name for r in sm.find_user('gamma').roles]
|
|
|
|
self.assertIn('druid_role', user_roles)
|
|
|
|
|
|
|
|
# Case 4. Extend the role to have access to the druid datasource
|
|
|
|
|
|
|
|
access_request4 = prepare_request('druid', 'druid_ds_2', 'druid_role')
|
|
|
|
druid_ds_2_perm = access_request4.datasource.perm
|
|
|
|
|
|
|
|
self.client.get(EXTEND_ROLE_REQUEST.format(
|
|
|
|
'druid', access_request4.datasource_id, 'gamma', 'druid_role'))
|
|
|
|
# druid_role was extended to grant access to the druid_access_ds_2
|
|
|
|
druid_role = sm.find_role('druid_role')
|
|
|
|
perm_view = sm.find_permission_view_menu(
|
|
|
|
'datasource_access', druid_ds_2_perm)
|
|
|
|
self.assertIn(perm_view, druid_role.permissions)
|
|
|
|
|
|
|
|
# cleanup
|
|
|
|
gamma_user = sm.find_user(username='gamma')
|
|
|
|
gamma_user.roles.remove(sm.find_role('druid_role'))
|
|
|
|
gamma_user.roles.remove(sm.find_role('table_role'))
|
|
|
|
session.delete(sm.find_role('druid_role'))
|
|
|
|
session.delete(sm.find_role('table_role'))
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
def test_request_access(self):
|
|
|
|
session = db.session
|
|
|
|
self.login(username='gamma')
|
|
|
|
gamma_user = sm.find_user(username='gamma')
|
|
|
|
sm.add_role('dummy_role')
|
|
|
|
gamma_user.roles.append(sm.find_role('dummy_role'))
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
ACCESS_REQUEST = (
|
|
|
|
'/caravel/request_access?datasource_type={}&datasource_id={}')
|
|
|
|
ROLE_EXTEND_LINK = (
|
|
|
|
'<a href="/caravel/approve?datasource_type={}&datasource_id={}&'
|
|
|
|
'created_by={}&role_to_extend={}">Extend {} Role</a>')
|
|
|
|
ROLE_GRANT_LINK = (
|
|
|
|
'<a href="/caravel/approve?datasource_type={}&datasource_id={}&'
|
|
|
|
'created_by={}&role_to_grant={}">Grant {} Role</a>')
|
|
|
|
|
|
|
|
# Case 1. Request table access, there are no roles have this table.
|
|
|
|
|
|
|
|
table1 = session.query(models.SqlaTable).filter_by(
|
|
|
|
table_name='random_time_series').first()
|
|
|
|
table_1_id = table1.id
|
|
|
|
|
|
|
|
# request access to the table
|
|
|
|
self.client.get(ACCESS_REQUEST.format('table', table_1_id))
|
|
|
|
|
|
|
|
access_request1 = self.get_access_requests(
|
|
|
|
'gamma', 'table', table_1_id)[0]
|
|
|
|
approve_link_1 = ROLE_EXTEND_LINK.format(
|
|
|
|
'table', table_1_id, 'gamma', 'dummy_role', 'dummy_role')
|
|
|
|
self.assertEqual(
|
|
|
|
access_request1.user_roles,
|
|
|
|
'<ul><li>Gamma Role</li><li>{}</li></ul>'.format(approve_link_1))
|
|
|
|
self.assertEqual(access_request1.roles_with_datasource, '<ul></ul>')
|
|
|
|
|
|
|
|
# Case 2. Duplicate request.
|
|
|
|
|
|
|
|
self.client.get(ACCESS_REQUEST.format('table', table_1_id))
|
|
|
|
access_requests_2 = self.get_access_requests(
|
|
|
|
'gamma', 'table', table_1_id)
|
|
|
|
self.assertEqual(len(access_requests_2), 1)
|
|
|
|
|
|
|
|
# Case 3. Request access, roles exist that contains the table.
|
|
|
|
|
|
|
|
# add table to the existing roles
|
|
|
|
table3 = session.query(models.SqlaTable).filter_by(
|
|
|
|
table_name='energy_usage').first()
|
|
|
|
table_3_id = table3.id
|
|
|
|
table3_perm = table3.perm
|
|
|
|
|
|
|
|
sm.add_role('energy_usage_role')
|
|
|
|
alpha_role = sm.find_role('Alpha')
|
|
|
|
sm.add_permission_role(
|
|
|
|
alpha_role,
|
|
|
|
sm.find_permission_view_menu('datasource_access', table3_perm))
|
|
|
|
sm.add_permission_role(
|
|
|
|
sm.find_role("energy_usage_role"),
|
|
|
|
sm.find_permission_view_menu('datasource_access', table3_perm))
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
self.client.get(ACCESS_REQUEST.format('table', table_3_id))
|
|
|
|
|
|
|
|
access_request3 = self.get_access_requests(
|
|
|
|
'gamma', 'table', table_3_id)[0]
|
|
|
|
approve_link_3 = ROLE_GRANT_LINK.format(
|
|
|
|
'table', table_3_id, 'gamma', 'energy_usage_role',
|
|
|
|
'energy_usage_role')
|
|
|
|
self.assertEqual(access_request3.roles_with_datasource,
|
|
|
|
'<ul><li>{}</li></ul>'.format(approve_link_3))
|
|
|
|
|
|
|
|
# Case 4. Request druid access, there are no roles have this table.
|
|
|
|
druid_ds_4 = session.query(models.DruidDatasource).filter_by(
|
|
|
|
datasource_name='druid_ds_1').first()
|
|
|
|
druid_ds_4_id = druid_ds_4.id
|
|
|
|
|
|
|
|
# request access to the table
|
|
|
|
self.client.get(ACCESS_REQUEST.format('druid', druid_ds_4_id))
|
|
|
|
access_request4 = self.get_access_requests(
|
|
|
|
'gamma', 'druid', druid_ds_4_id)[0]
|
|
|
|
approve_link_4 = ROLE_EXTEND_LINK.format(
|
|
|
|
'druid', druid_ds_4_id, 'gamma', 'dummy_role', 'dummy_role')
|
|
|
|
self.assertEqual(
|
|
|
|
access_request4.user_roles,
|
|
|
|
'<ul><li>Gamma Role</li><li>{}</li></ul>'.format(approve_link_4))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
access_request4.roles_with_datasource,
|
|
|
|
'<ul></ul>'.format(access_request4.id))
|
|
|
|
|
|
|
|
# Case 5. Roles exist that contains the druid datasource.
|
|
|
|
# add druid ds to the existing roles
|
|
|
|
druid_ds_5 = session.query(models.DruidDatasource).filter_by(
|
|
|
|
datasource_name='druid_ds_2').first()
|
|
|
|
druid_ds_5_id = druid_ds_5.id
|
|
|
|
druid_ds_5_perm = druid_ds_5.perm
|
|
|
|
|
|
|
|
druid_ds_2_role = sm.add_role('druid_ds_2_role')
|
|
|
|
admin_role = sm.find_role('Admin')
|
|
|
|
sm.add_permission_role(
|
|
|
|
admin_role,
|
|
|
|
sm.find_permission_view_menu('datasource_access', druid_ds_5_perm))
|
|
|
|
sm.add_permission_role(
|
|
|
|
druid_ds_2_role,
|
|
|
|
sm.find_permission_view_menu('datasource_access', druid_ds_5_perm))
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
self.client.get(ACCESS_REQUEST.format('druid', druid_ds_5_id))
|
|
|
|
access_request5 = self.get_access_requests(
|
|
|
|
'gamma', 'druid', druid_ds_5_id)[0]
|
|
|
|
approve_link_5 = ROLE_GRANT_LINK.format(
|
|
|
|
'druid', druid_ds_5_id, 'gamma', 'druid_ds_2_role',
|
|
|
|
'druid_ds_2_role')
|
|
|
|
|
|
|
|
self.assertEqual(access_request5.roles_with_datasource,
|
|
|
|
'<ul><li>{}</li></ul>'.format(approve_link_5))
|
|
|
|
|
|
|
|
# cleanup
|
|
|
|
gamma_user = sm.find_user(username='gamma')
|
|
|
|
gamma_user.roles.remove(sm.find_role('dummy_role'))
|
|
|
|
session.commit()
|
2016-08-02 02:06:19 -04:00
|
|
|
|
2016-09-07 20:27:35 -04:00
|
|
|
def test_druid_sync_from_config(self):
|
2016-09-22 12:53:14 -04:00
|
|
|
self.login()
|
2016-09-07 20:27:35 -04:00
|
|
|
cluster = models.DruidCluster(cluster_name="new_druid")
|
|
|
|
db.session.add(cluster)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
cfg = {
|
2016-09-13 20:30:36 -04:00
|
|
|
"user": "admin",
|
|
|
|
"cluster": "new_druid",
|
|
|
|
"config": {
|
|
|
|
"name": "test_click",
|
|
|
|
"dimensions": ["affiliate_id", "campaign", "first_seen"],
|
|
|
|
"metrics_spec": [{"type": "count", "name": "count"},
|
|
|
|
{"type": "sum", "name": "sum"}],
|
|
|
|
"batch_ingestion": {
|
|
|
|
"sql": "SELECT * FROM clicks WHERE d='{{ ds }}'",
|
|
|
|
"ts_column": "d",
|
|
|
|
"sources": [{
|
|
|
|
"table": "clicks",
|
|
|
|
"partition": "d='{{ ds }}'"
|
|
|
|
}]
|
|
|
|
}
|
2016-09-07 20:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 20:30:36 -04:00
|
|
|
resp = self.client.post('/caravel/sync_druid/', data=json.dumps(cfg))
|
2016-09-07 20:27:35 -04:00
|
|
|
|
|
|
|
druid_ds = db.session.query(DruidDatasource).filter_by(
|
|
|
|
datasource_name="test_click").first()
|
|
|
|
assert set([c.column_name for c in druid_ds.columns]) == set(
|
|
|
|
["affiliate_id", "campaign", "first_seen"])
|
|
|
|
assert set([m.metric_name for m in druid_ds.metrics]) == set(
|
|
|
|
["count", "sum"])
|
|
|
|
assert resp.status_code == 201
|
|
|
|
|
2016-09-13 20:30:36 -04:00
|
|
|
# datasource exists, not changes required
|
|
|
|
resp = self.client.post('/caravel/sync_druid/', data=json.dumps(cfg))
|
2016-09-07 20:27:35 -04:00
|
|
|
druid_ds = db.session.query(DruidDatasource).filter_by(
|
|
|
|
datasource_name="test_click").first()
|
|
|
|
assert set([c.column_name for c in druid_ds.columns]) == set(
|
|
|
|
["affiliate_id", "campaign", "first_seen"])
|
|
|
|
assert set([m.metric_name for m in druid_ds.metrics]) == set(
|
|
|
|
["count", "sum"])
|
|
|
|
assert resp.status_code == 201
|
|
|
|
|
2016-09-13 20:30:36 -04:00
|
|
|
# datasource exists, add new metrics and dimentions
|
2016-09-07 20:27:35 -04:00
|
|
|
cfg = {
|
2016-09-13 20:30:36 -04:00
|
|
|
"user": "admin",
|
|
|
|
"cluster": "new_druid",
|
|
|
|
"config": {
|
|
|
|
"name": "test_click",
|
|
|
|
"dimensions": ["affiliate_id", "second_seen"],
|
|
|
|
"metrics_spec": [
|
|
|
|
{"type": "bla", "name": "sum"},
|
|
|
|
{"type": "unique", "name": "unique"}
|
|
|
|
],
|
|
|
|
}
|
2016-09-07 20:27:35 -04:00
|
|
|
}
|
2016-09-13 20:30:36 -04:00
|
|
|
resp = self.client.post('/caravel/sync_druid/', data=json.dumps(cfg))
|
2016-09-07 20:27:35 -04:00
|
|
|
druid_ds = db.session.query(DruidDatasource).filter_by(
|
|
|
|
datasource_name="test_click").first()
|
|
|
|
# columns and metrics are not deleted if config is changed as
|
|
|
|
# user could define his own dimensions / metrics and want to keep them
|
|
|
|
assert set([c.column_name for c in druid_ds.columns]) == set(
|
|
|
|
["affiliate_id", "campaign", "first_seen", "second_seen"])
|
|
|
|
assert set([m.metric_name for m in druid_ds.metrics]) == set(
|
|
|
|
["count", "sum", "unique"])
|
|
|
|
# metric type will not be overridden, sum stays instead of bla
|
|
|
|
assert set([m.metric_type for m in druid_ds.metrics]) == set(
|
|
|
|
["longSum", "sum", "unique"])
|
|
|
|
assert resp.status_code == 201
|
|
|
|
|
2016-09-09 20:12:09 -04:00
|
|
|
def test_filter_druid_datasource(self):
|
|
|
|
gamma_ds = DruidDatasource(
|
|
|
|
datasource_name="datasource_for_gamma",
|
|
|
|
)
|
|
|
|
db.session.add(gamma_ds)
|
|
|
|
no_gamma_ds = DruidDatasource(
|
|
|
|
datasource_name="datasource_not_for_gamma",
|
|
|
|
)
|
|
|
|
db.session.add(no_gamma_ds)
|
|
|
|
db.session.commit()
|
|
|
|
utils.merge_perm(sm, 'datasource_access', gamma_ds.perm)
|
|
|
|
utils.merge_perm(sm, 'datasource_access', no_gamma_ds.perm)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
gamma_ds_permission_view = (
|
|
|
|
db.session.query(ab_models.PermissionView)
|
|
|
|
.join(ab_models.ViewMenu)
|
|
|
|
.filter(ab_models.ViewMenu.name == gamma_ds.perm)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
sm.add_permission_role(sm.find_role('Gamma'), gamma_ds_permission_view)
|
|
|
|
|
|
|
|
self.login(username='gamma')
|
|
|
|
url = '/druiddatasourcemodelview/list/'
|
|
|
|
resp = self.client.get(url, follow_redirects=True)
|
|
|
|
assert 'datasource_for_gamma' in resp.data.decode('utf-8')
|
|
|
|
assert 'datasource_not_for_gamma' not in resp.data.decode('utf-8')
|
|
|
|
|
2016-09-12 16:41:05 -04:00
|
|
|
def test_add_filter(self, username='admin'):
|
|
|
|
# navigate to energy_usage slice with "Electricity,heat" in filter values
|
|
|
|
data = (
|
|
|
|
"/caravel/explore/table/1/?viz_type=table&groupby=source&metric=count&flt_col_1=source&flt_op_1=in&flt_eq_1=%27Electricity%2Cheat%27"
|
|
|
|
"&userid=1&datasource_name=energy_usage&datasource_id=1&datasource_type=tablerdo_save=saveas")
|
|
|
|
resp = self.client.get(
|
|
|
|
data,
|
|
|
|
follow_redirects=True)
|
|
|
|
assert ("source" in resp.data.decode('utf-8'))
|
|
|
|
|
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-04-26 19:44:51 -04:00
|
|
|
resp = self.client.get('/slicemodelview/list/')
|
|
|
|
assert "List Slice" in resp.data.decode('utf-8')
|
|
|
|
|
|
|
|
resp = self.client.get('/dashboardmodelview/list/')
|
|
|
|
assert "List Dashboard" in resp.data.decode('utf-8')
|
|
|
|
|
2016-09-28 16:55:06 -04:00
|
|
|
def run_sql(self, sql, user_name, client_id):
|
2016-08-09 20:53:23 -04:00
|
|
|
self.login(username=user_name)
|
2016-07-30 01:39:33 -04:00
|
|
|
dbid = (
|
|
|
|
db.session.query(models.Database)
|
2016-08-30 00:55:31 -04:00
|
|
|
.filter_by(database_name='main')
|
2016-07-30 01:39:33 -04:00
|
|
|
.first().id
|
|
|
|
)
|
|
|
|
resp = self.client.post(
|
|
|
|
'/caravel/sql_json/',
|
2016-09-28 16:55:06 -04:00
|
|
|
data=dict(database_id=dbid, sql=sql, select_as_create_as=False,
|
|
|
|
client_id=client_id),
|
2016-07-30 01:39:33 -04:00
|
|
|
)
|
2016-08-09 20:53:23 -04:00
|
|
|
self.logout()
|
2016-07-30 01:39:33 -04:00
|
|
|
return json.loads(resp.data.decode('utf-8'))
|
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
def test_sql_json(self):
|
2016-09-28 16:55:06 -04:00
|
|
|
data = self.run_sql('SELECT * FROM ab_user', 'admin', "1")
|
2016-08-30 00:55:31 -04:00
|
|
|
assert len(data['data']) > 0
|
|
|
|
|
2016-09-28 16:55:06 -04:00
|
|
|
data = self.run_sql('SELECT * FROM unexistant_table', 'admin', "2")
|
2016-08-30 00:55:31 -04:00
|
|
|
assert len(data['error']) > 0
|
2016-08-09 20:53:23 -04:00
|
|
|
|
|
|
|
def test_sql_json_has_access(self):
|
|
|
|
main_db = (
|
2016-08-30 00:55:31 -04:00
|
|
|
db.session.query(models.Database).filter_by(database_name="main").first()
|
2016-08-09 20:53:23 -04:00
|
|
|
)
|
|
|
|
utils.merge_perm(sm, 'database_access', main_db.perm)
|
|
|
|
db.session.commit()
|
|
|
|
main_db_permission_view = (
|
|
|
|
db.session.query(ab_models.PermissionView)
|
2016-08-30 00:55:31 -04:00
|
|
|
.join(ab_models.ViewMenu)
|
|
|
|
.filter(ab_models.ViewMenu.name == '[main].(id:1)')
|
|
|
|
.first()
|
2016-08-09 20:53:23 -04:00
|
|
|
)
|
|
|
|
astronaut = sm.add_role("Astronaut")
|
|
|
|
sm.add_permission_role(astronaut, main_db_permission_view)
|
2016-08-30 00:55:31 -04:00
|
|
|
# Astronaut role is Gamma + main db permissions
|
2016-08-09 20:53:23 -04:00
|
|
|
for gamma_perm in sm.find_role('Gamma').permissions:
|
|
|
|
sm.add_permission_role(astronaut, gamma_perm)
|
|
|
|
|
|
|
|
gagarin = appbuilder.sm.find_user('gagarin')
|
|
|
|
if not gagarin:
|
|
|
|
appbuilder.sm.add_user(
|
|
|
|
'gagarin', 'Iurii', 'Gagarin', 'gagarin@cosmos.ussr',
|
|
|
|
appbuilder.sm.find_role('Astronaut'),
|
|
|
|
password='general')
|
2016-09-28 16:55:06 -04:00
|
|
|
data = self.run_sql('SELECT * FROM ab_user', 'gagarin', "3")
|
2016-08-30 00:55:31 -04:00
|
|
|
db.session.query(models.Query).delete()
|
|
|
|
db.session.commit()
|
2016-08-09 20:53:23 -04:00
|
|
|
assert len(data['data']) > 0
|
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
def test_csv_endpoint(self):
|
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]
|
|
|
|
self.run_sql(sql, 'admin', client_id)
|
2016-07-30 01:39:33 -04:00
|
|
|
|
2016-08-30 00:55:31 -04:00
|
|
|
self.login('admin')
|
2016-09-11 10:39:07 -04:00
|
|
|
resp = self.client.get('/caravel/csv/{}'.format(client_id))
|
2016-08-30 00:55:31 -04:00
|
|
|
data = csv.reader(io.StringIO(resp.data.decode('utf-8')))
|
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()
|
|
|
|
|
|
|
|
def test_queries_endpoint(self):
|
|
|
|
resp = self.client.get('/caravel/queries/{}'.format(0))
|
|
|
|
self.assertEquals(403, resp.status_code)
|
|
|
|
|
|
|
|
self.login('admin')
|
|
|
|
resp = self.client.get('/caravel/queries/{}'.format(0))
|
|
|
|
data = json.loads(resp.data.decode('utf-8'))
|
|
|
|
self.assertEquals(0, len(data))
|
|
|
|
self.logout()
|
|
|
|
|
|
|
|
self.run_sql("SELECT * FROM ab_user", 'admin', client_id='client_id_1')
|
|
|
|
self.run_sql("SELECT * FROM ab_user1", 'admin', client_id='client_id_2')
|
|
|
|
self.login('admin')
|
|
|
|
resp = self.client.get('/caravel/queries/{}'.format(0))
|
|
|
|
data = json.loads(resp.data.decode('utf-8'))
|
|
|
|
self.assertEquals(2, len(data))
|
|
|
|
|
|
|
|
query = db.session.query(models.Query).filter_by(
|
|
|
|
sql='SELECT * FROM ab_user').first()
|
|
|
|
query.changed_on = utils.EPOCH
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
resp = self.client.get('/caravel/queries/{}'.format(123456000))
|
|
|
|
data = json.loads(resp.data.decode('utf-8'))
|
|
|
|
self.assertEquals(1, len(data))
|
|
|
|
|
|
|
|
self.logout()
|
|
|
|
resp = self.client.get('/caravel/queries/{}'.format(0))
|
|
|
|
self.assertEquals(403, resp.status_code)
|
2016-07-30 01:39:33 -04:00
|
|
|
|
2016-09-23 20:41:24 -04:00
|
|
|
def test_search_query_endpoint(self):
|
|
|
|
userId = 'userId=null'
|
|
|
|
databaseId = 'databaseId=null'
|
|
|
|
searchText = 'searchText=null'
|
|
|
|
status = 'status=success'
|
|
|
|
params = [userId, databaseId, searchText, status]
|
|
|
|
resp = self.client.get('/caravel/search_queries?'+'&'.join(params))
|
|
|
|
self.assertEquals(200, resp.status_code)
|
|
|
|
|
2016-05-04 01:31:37 -04:00
|
|
|
def test_public_user_dashboard_access(self):
|
|
|
|
# Try access before adding appropriate permissions.
|
2016-06-02 22:17:34 -04:00
|
|
|
self.revoke_public_access('birth_names')
|
|
|
|
self.logout()
|
|
|
|
|
2016-05-04 01:31:37 -04:00
|
|
|
resp = self.client.get('/slicemodelview/list/')
|
|
|
|
data = resp.data.decode('utf-8')
|
|
|
|
|
2016-06-02 22:17:34 -04:00
|
|
|
assert 'birth_names</a>' not in data
|
2016-05-04 01:31:37 -04:00
|
|
|
|
2016-06-02 22:17:34 -04:00
|
|
|
resp = self.client.get('/dashboardmodelview/list/')
|
2016-05-04 01:31:37 -04:00
|
|
|
data = resp.data.decode('utf-8')
|
2016-06-02 22:17:34 -04:00
|
|
|
assert '/caravel/dashboard/births/' not in data
|
2016-05-04 01:31:37 -04:00
|
|
|
|
|
|
|
self.setup_public_access_for_dashboard('birth_names')
|
|
|
|
|
|
|
|
# Try access after adding appropriate permissions.
|
|
|
|
resp = self.client.get('/slicemodelview/list/')
|
|
|
|
data = resp.data.decode('utf-8')
|
2016-06-12 14:01:16 -04:00
|
|
|
assert 'birth_names' in data
|
2016-05-04 01:31:37 -04:00
|
|
|
|
|
|
|
resp = self.client.get('/dashboardmodelview/list/')
|
|
|
|
data = resp.data.decode('utf-8')
|
2016-06-02 22:17:34 -04:00
|
|
|
assert "/caravel/dashboard/births/" in data
|
2016-05-04 01:31:37 -04:00
|
|
|
|
|
|
|
resp = self.client.get('/caravel/dashboard/births/')
|
|
|
|
data = resp.data.decode('utf-8')
|
2016-06-02 22:17:34 -04:00
|
|
|
assert 'Births' in data
|
2016-05-04 01:31:37 -04:00
|
|
|
|
|
|
|
# Confirm that public doesn't have access to other datasets.
|
2016-08-30 00:55:31 -04:00
|
|
|
resp = self.client.get('/slicemodelview/list/')
|
|
|
|
data = resp.data.decode('utf-8')
|
|
|
|
assert 'wb_health_population</a>' not in data
|
|
|
|
|
2016-05-04 01:31:37 -04:00
|
|
|
resp = self.client.get('/dashboardmodelview/list/')
|
|
|
|
data = resp.data.decode('utf-8')
|
2016-06-02 22:17:34 -04:00
|
|
|
assert "/caravel/dashboard/world_health/" not in data
|
2016-05-04 01:31:37 -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
|
|
|
|
2015-09-26 18:55:33 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|