2014-02-06 09:10:04 +00:00
|
|
|
import redash.models
|
2016-11-21 15:34:44 +00:00
|
|
|
from redash.models import db
|
2015-06-03 04:58:28 +00:00
|
|
|
from redash.utils import gen_query_hash, utcnow
|
2016-02-18 15:32:49 +00:00
|
|
|
from redash.utils.configuration import ConfigurationContainer
|
2016-10-24 13:58:30 +00:00
|
|
|
from redash.permissions import ACCESS_TYPE_MODIFY
|
2014-02-06 09:10:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModelFactory(object):
|
|
|
|
def __init__(self, model, **kwargs):
|
|
|
|
self.model = model
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
def _get_kwargs(self, override_kwargs):
|
|
|
|
kwargs = self.kwargs.copy()
|
|
|
|
kwargs.update(override_kwargs)
|
|
|
|
|
|
|
|
for key, arg in kwargs.items():
|
|
|
|
if callable(arg):
|
|
|
|
kwargs[key] = arg()
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def create(self, **override_kwargs):
|
|
|
|
kwargs = self._get_kwargs(override_kwargs)
|
2016-11-21 15:34:44 +00:00
|
|
|
obj = self.model(**kwargs)
|
|
|
|
db.session.add(obj)
|
2016-11-28 16:55:41 +00:00
|
|
|
db.session.commit()
|
2016-11-21 15:34:44 +00:00
|
|
|
return obj
|
2014-02-06 09:10:04 +00:00
|
|
|
|
2014-02-22 12:42:08 +00:00
|
|
|
|
|
|
|
class Sequence(object):
|
|
|
|
def __init__(self, string):
|
|
|
|
self.sequence = 0
|
|
|
|
self.string = string
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
self.sequence += 1
|
|
|
|
|
|
|
|
return self.string.format(self.sequence)
|
|
|
|
|
2014-05-08 18:29:50 +00:00
|
|
|
|
2014-02-22 12:42:08 +00:00
|
|
|
user_factory = ModelFactory(redash.models.User,
|
|
|
|
name='John Doe', email=Sequence('test{}@example.com'),
|
2016-11-21 15:34:44 +00:00
|
|
|
group_ids=[2],
|
|
|
|
org_id=1)
|
2014-02-22 12:42:08 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
org_factory = ModelFactory(redash.models.Organization,
|
|
|
|
name=Sequence("Org {}"),
|
2016-01-03 14:05:29 +00:00
|
|
|
slug=Sequence("org{}.example.com"),
|
2015-12-01 13:44:08 +00:00
|
|
|
settings={})
|
2014-02-22 12:42:08 +00:00
|
|
|
|
2014-03-19 10:53:39 +00:00
|
|
|
data_source_factory = ModelFactory(redash.models.DataSource,
|
2015-04-18 19:46:19 +00:00
|
|
|
name=Sequence('Test {}'),
|
2014-03-19 10:53:39 +00:00
|
|
|
type='pg',
|
2016-05-30 15:30:05 +00:00
|
|
|
# If we don't use lambda here it will reuse the same options between tests:
|
|
|
|
options=lambda: ConfigurationContainer.from_json('{"dbname": "test"}'),
|
2016-11-28 16:55:41 +00:00
|
|
|
org_id=1)
|
2014-03-19 10:53:39 +00:00
|
|
|
|
2014-02-06 09:10:04 +00:00
|
|
|
dashboard_factory = ModelFactory(redash.models.Dashboard,
|
2016-11-29 15:53:19 +00:00
|
|
|
name='test',
|
|
|
|
user=user_factory.create,
|
|
|
|
layout='[]',
|
|
|
|
is_draft=False,
|
|
|
|
org=1)
|
2014-02-06 09:10:04 +00:00
|
|
|
|
2016-02-21 12:08:49 +00:00
|
|
|
api_key_factory = ModelFactory(redash.models.ApiKey,
|
|
|
|
object=dashboard_factory.create)
|
|
|
|
|
2014-02-06 09:10:04 +00:00
|
|
|
query_factory = ModelFactory(redash.models.Query,
|
2016-10-05 11:14:26 +00:00
|
|
|
name='Query',
|
2014-02-06 09:10:04 +00:00
|
|
|
description='',
|
2016-11-28 14:41:29 +00:00
|
|
|
query_text='SELECT 1',
|
2014-03-19 10:53:39 +00:00
|
|
|
user=user_factory.create,
|
2014-09-25 14:42:33 +00:00
|
|
|
is_archived=False,
|
2016-10-14 22:25:30 +00:00
|
|
|
is_draft=False,
|
2015-04-01 08:23:26 +00:00
|
|
|
schedule=None,
|
2015-12-01 13:44:08 +00:00
|
|
|
data_source=data_source_factory.create,
|
2016-11-23 18:35:18 +00:00
|
|
|
org_id=1)
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2016-05-05 01:15:49 +00:00
|
|
|
query_with_params_factory = ModelFactory(redash.models.Query,
|
|
|
|
name='New Query with Params',
|
|
|
|
description='',
|
2016-11-28 14:41:29 +00:00
|
|
|
query_text='SELECT {{param1}}',
|
2016-05-05 01:15:49 +00:00
|
|
|
user=user_factory.create,
|
|
|
|
is_archived=False,
|
2016-10-14 22:25:30 +00:00
|
|
|
is_draft=False,
|
2016-05-05 01:15:49 +00:00
|
|
|
schedule=None,
|
|
|
|
data_source=data_source_factory.create,
|
2016-11-23 18:35:18 +00:00
|
|
|
org_id=1)
|
2016-05-05 01:15:49 +00:00
|
|
|
|
2016-06-09 13:06:03 +00:00
|
|
|
access_permission_factory = ModelFactory(redash.models.AccessPermission,
|
|
|
|
object_id=query_factory.create,
|
|
|
|
object_type=redash.models.Query.__name__,
|
2016-10-24 13:58:30 +00:00
|
|
|
access_type=ACCESS_TYPE_MODIFY,
|
2016-06-09 13:06:03 +00:00
|
|
|
grantor=user_factory.create,
|
|
|
|
grantee=user_factory.create)
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
alert_factory = ModelFactory(redash.models.Alert,
|
|
|
|
name=Sequence('Alert {}'),
|
|
|
|
query=query_factory.create,
|
|
|
|
user=user_factory.create,
|
|
|
|
options={})
|
2014-02-06 09:10:04 +00:00
|
|
|
|
2014-02-06 18:49:05 +00:00
|
|
|
query_result_factory = ModelFactory(redash.models.QueryResult,
|
2014-02-09 16:50:28 +00:00
|
|
|
data='{"columns":{}, "rows":[]}',
|
2014-02-06 18:49:05 +00:00
|
|
|
runtime=1,
|
2015-06-03 04:58:28 +00:00
|
|
|
retrieved_at=utcnow,
|
2016-11-28 14:41:29 +00:00
|
|
|
query_text="SELECT 1",
|
2014-03-19 11:48:11 +00:00
|
|
|
query_hash=gen_query_hash('SELECT 1'),
|
2015-12-01 13:44:08 +00:00
|
|
|
data_source=data_source_factory.create,
|
2016-11-23 18:35:18 +00:00
|
|
|
org_id=1)
|
2014-02-06 18:49:05 +00:00
|
|
|
|
2014-02-06 09:10:04 +00:00
|
|
|
visualization_factory = ModelFactory(redash.models.Visualization,
|
|
|
|
type='CHART',
|
2016-11-28 16:55:41 +00:00
|
|
|
query_rel=query_factory.create,
|
2014-02-06 09:10:04 +00:00
|
|
|
name='Chart',
|
|
|
|
description='',
|
|
|
|
options='{}')
|
|
|
|
|
|
|
|
widget_factory = ModelFactory(redash.models.Widget,
|
|
|
|
type='chart',
|
|
|
|
width=1,
|
|
|
|
options='{}',
|
|
|
|
dashboard=dashboard_factory.create,
|
2015-04-01 08:23:26 +00:00
|
|
|
visualization=visualization_factory.create)
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2016-02-25 15:02:45 +00:00
|
|
|
destination_factory = ModelFactory(redash.models.NotificationDestination,
|
2016-11-23 18:35:18 +00:00
|
|
|
org_id=1,
|
2016-02-25 15:02:45 +00:00
|
|
|
user=user_factory.create,
|
|
|
|
name='Destination',
|
|
|
|
type='slack',
|
|
|
|
options=ConfigurationContainer.from_json('{"url": "https://www.slack.com"}'))
|
|
|
|
|
2016-03-14 19:11:13 +00:00
|
|
|
alert_subscription_factory = ModelFactory(redash.models.AlertSubscription,
|
|
|
|
user=user_factory.create,
|
|
|
|
destination=destination_factory.create,
|
|
|
|
alert=alert_factory.create)
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
class Factory(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.org, self.admin_group, self.default_group = redash.models.init_db()
|
2016-10-26 13:09:55 +00:00
|
|
|
self._data_source = None
|
|
|
|
self._user = None
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2016-10-26 13:09:55 +00:00
|
|
|
@property
|
|
|
|
def user(self):
|
|
|
|
if self._user is None:
|
|
|
|
self._user = self.create_user()
|
2016-11-29 04:30:35 +00:00
|
|
|
# Test setup creates users, they need to be in the db by the time
|
|
|
|
# the handler's db transaction starts.
|
|
|
|
db.session.commit()
|
2016-10-26 13:09:55 +00:00
|
|
|
return self._user
|
|
|
|
|
|
|
|
@property
|
|
|
|
def data_source(self):
|
|
|
|
if self._data_source is None:
|
|
|
|
self._data_source = data_source_factory.create(org=self.org)
|
2016-11-21 15:34:44 +00:00
|
|
|
db.session.add(redash.models.DataSourceGroup(
|
|
|
|
group=self.default_group,
|
|
|
|
data_source=self._data_source))
|
2016-10-26 13:09:55 +00:00
|
|
|
|
|
|
|
return self._data_source
|
|
|
|
|
|
|
|
def _init_org(self):
|
|
|
|
if self._org is None:
|
|
|
|
self._org, self._admin_group, self._default_group = redash.models.init_db()
|
2016-11-21 15:34:44 +00:00
|
|
|
self.org.domain = 'org0.example.org'
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
def create_org(self, **kwargs):
|
|
|
|
org = org_factory.create(**kwargs)
|
|
|
|
self.create_group(org=org, type=redash.models.Group.BUILTIN_GROUP, name="default")
|
2016-11-21 15:34:44 +00:00
|
|
|
self.create_group(org=org, type=redash.models.Group.BUILTIN_GROUP, name="admin",
|
|
|
|
permissions=["admin"])
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
return org
|
|
|
|
|
|
|
|
def create_user(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'org': self.org,
|
2016-11-21 15:34:44 +00:00
|
|
|
'group_ids': [self.default_group.id]
|
2015-12-01 13:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if 'org' in kwargs:
|
2016-11-21 15:34:44 +00:00
|
|
|
args['group_ids'] = [kwargs['org'].default_group.id]
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
return user_factory.create(**args)
|
|
|
|
|
|
|
|
def create_admin(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'org': self.org,
|
2016-11-21 15:34:44 +00:00
|
|
|
'group_ids': [self.admin_group.id, self.default_group.id]
|
2015-12-01 13:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if 'org' in kwargs:
|
2016-11-21 15:34:44 +00:00
|
|
|
args['group_ids'] = [kwargs['org'].default_group.id, kwargs['org'].admin_group.id]
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
return user_factory.create(**args)
|
|
|
|
|
|
|
|
def create_group(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'name': 'Group',
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
|
2016-11-21 15:34:44 +00:00
|
|
|
g = redash.models.Group(**args)
|
|
|
|
return g
|
|
|
|
|
|
|
|
def create_group_hack(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'name': 'Group',
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
|
|
|
|
g_id = redash.models.create_group_hack(**args)
|
|
|
|
return g_id
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
def create_alert(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'user': self.user,
|
|
|
|
'query': self.create_query()
|
|
|
|
}
|
|
|
|
|
|
|
|
args.update(**kwargs)
|
|
|
|
return alert_factory.create(**args)
|
|
|
|
|
2016-06-14 08:04:41 +00:00
|
|
|
def create_alert_subscription(self, **kwargs):
|
|
|
|
args = {
|
2016-06-14 08:21:05 +00:00
|
|
|
'user': self.user,
|
|
|
|
'alert': self.create_alert()
|
2016-06-14 08:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args.update(**kwargs)
|
|
|
|
return alert_subscription_factory.create(**args)
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def create_data_source(self, **kwargs):
|
2016-11-23 18:35:18 +00:00
|
|
|
group = None
|
|
|
|
if 'group' in kwargs:
|
|
|
|
group = kwargs.pop('group')
|
2015-12-01 13:44:08 +00:00
|
|
|
args = {
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
|
2016-11-23 18:35:18 +00:00
|
|
|
if group and 'org' not in kwargs:
|
|
|
|
args['org'] = group.org
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
data_source = data_source_factory.create(**args)
|
|
|
|
|
2016-11-23 18:35:18 +00:00
|
|
|
if group:
|
2016-03-09 13:15:03 +00:00
|
|
|
view_only = kwargs.pop('view_only', False)
|
2016-11-21 15:34:44 +00:00
|
|
|
db.session.add(redash.models.DataSourceGroup(
|
2016-11-23 18:35:18 +00:00
|
|
|
group=group,
|
2016-11-21 15:34:44 +00:00
|
|
|
data_source=data_source,
|
|
|
|
view_only=view_only))
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
return data_source
|
|
|
|
|
|
|
|
def create_dashboard(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'user': self.user,
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return dashboard_factory.create(**args)
|
|
|
|
|
|
|
|
def create_query(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'user': self.user,
|
|
|
|
'data_source': self.data_source,
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return query_factory.create(**args)
|
|
|
|
|
2016-05-05 01:15:49 +00:00
|
|
|
def create_query_with_params(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'user': self.user,
|
|
|
|
'data_source': self.data_source,
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return query_with_params_factory.create(**args)
|
|
|
|
|
2016-06-09 13:06:03 +00:00
|
|
|
def create_access_permission(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'grantor': self.user
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return access_permission_factory.create(**args)
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def create_query_result(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'data_source': self.data_source,
|
|
|
|
}
|
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
|
|
|
|
if 'data_source' in args and 'org' not in args:
|
2016-11-23 18:35:18 +00:00
|
|
|
args['org'] = args['data_source'].org
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
return query_result_factory.create(**args)
|
|
|
|
|
|
|
|
def create_visualization(self, **kwargs):
|
|
|
|
args = {
|
2016-11-29 04:30:35 +00:00
|
|
|
'query_rel': self.create_query()
|
2015-12-01 13:44:08 +00:00
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return visualization_factory.create(**args)
|
|
|
|
|
2016-05-05 01:15:49 +00:00
|
|
|
def create_visualization_with_params(self, **kwargs):
|
|
|
|
args = {
|
2016-11-29 04:30:35 +00:00
|
|
|
'query_rel': self.create_query_with_params()
|
2016-05-05 01:15:49 +00:00
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return visualization_factory.create(**args)
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def create_widget(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'dashboard': self.create_dashboard(),
|
|
|
|
'visualization': self.create_visualization()
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return widget_factory.create(**args)
|
2016-02-21 12:08:49 +00:00
|
|
|
|
|
|
|
def create_api_key(self, **kwargs):
|
|
|
|
args = {
|
|
|
|
'org': self.org
|
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return api_key_factory.create(**args)
|
2016-03-24 13:55:46 +00:00
|
|
|
|
|
|
|
def create_destination(self, **kwargs):
|
|
|
|
return destination_factory.create(**kwargs)
|