2018-10-17 18:55:58 +00:00
|
|
|
from passlib.apps import custom_app_context as pwd_context
|
2014-02-06 09:10:04 +00:00
|
|
|
import redash.models
|
2016-11-21 15:34:44 +00:00
|
|
|
from redash.models import db
|
2017-06-14 09:05:17 +00:00
|
|
|
from redash.permissions import ACCESS_TYPE_MODIFY
|
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
|
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
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
user_factory = ModelFactory(
|
|
|
|
redash.models.User,
|
|
|
|
name="John Doe",
|
|
|
|
email=Sequence("test{}@example.com"),
|
|
|
|
password_hash=pwd_context.encrypt("test1234"),
|
|
|
|
group_ids=[2],
|
|
|
|
org_id=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
org_factory = ModelFactory(
|
|
|
|
redash.models.Organization,
|
|
|
|
name=Sequence("Org {}"),
|
|
|
|
slug=Sequence("org{}.example.com"),
|
|
|
|
settings={},
|
|
|
|
)
|
|
|
|
|
|
|
|
data_source_factory = ModelFactory(
|
|
|
|
redash.models.DataSource,
|
|
|
|
name=Sequence("Test {}"),
|
|
|
|
type="pg",
|
|
|
|
# If we don't use lambda here it will reuse the same options between tests:
|
|
|
|
options=lambda: ConfigurationContainer.from_json('{"dbname": "test"}'),
|
|
|
|
org_id=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
dashboard_factory = ModelFactory(
|
|
|
|
redash.models.Dashboard,
|
|
|
|
name="test",
|
|
|
|
user=user_factory.create,
|
|
|
|
layout="[]",
|
|
|
|
is_draft=False,
|
|
|
|
org=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
api_key_factory = ModelFactory(redash.models.ApiKey, object=dashboard_factory.create)
|
|
|
|
|
|
|
|
query_factory = ModelFactory(
|
|
|
|
redash.models.Query,
|
|
|
|
name="Query",
|
|
|
|
description="",
|
|
|
|
query_text="SELECT 1",
|
|
|
|
user=user_factory.create,
|
|
|
|
is_archived=False,
|
|
|
|
is_draft=False,
|
|
|
|
schedule=None,
|
|
|
|
data_source=data_source_factory.create,
|
|
|
|
org_id=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
query_with_params_factory = ModelFactory(
|
|
|
|
redash.models.Query,
|
|
|
|
name="New Query with Params",
|
|
|
|
description="",
|
|
|
|
query_text="SELECT {{param1}}",
|
|
|
|
user=user_factory.create,
|
|
|
|
is_archived=False,
|
|
|
|
is_draft=False,
|
|
|
|
schedule={},
|
|
|
|
data_source=data_source_factory.create,
|
|
|
|
org_id=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
access_permission_factory = ModelFactory(
|
|
|
|
redash.models.AccessPermission,
|
|
|
|
object_id=query_factory.create,
|
|
|
|
object_type=redash.models.Query.__name__,
|
|
|
|
access_type=ACCESS_TYPE_MODIFY,
|
|
|
|
grantor=user_factory.create,
|
|
|
|
grantee=user_factory.create,
|
|
|
|
)
|
|
|
|
|
|
|
|
alert_factory = ModelFactory(
|
|
|
|
redash.models.Alert,
|
|
|
|
name=Sequence("Alert {}"),
|
|
|
|
query_rel=query_factory.create,
|
|
|
|
user=user_factory.create,
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
|
|
|
|
query_result_factory = ModelFactory(
|
|
|
|
redash.models.QueryResult,
|
|
|
|
data='{"columns":{}, "rows":[]}',
|
|
|
|
runtime=1,
|
|
|
|
retrieved_at=utcnow,
|
|
|
|
query_text="SELECT 1",
|
|
|
|
query_hash=gen_query_hash("SELECT 1"),
|
|
|
|
data_source=data_source_factory.create,
|
|
|
|
org_id=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
visualization_factory = ModelFactory(
|
|
|
|
redash.models.Visualization,
|
|
|
|
type="CHART",
|
|
|
|
query_rel=query_factory.create,
|
|
|
|
name="Chart",
|
|
|
|
description="",
|
|
|
|
options="{}",
|
|
|
|
)
|
|
|
|
|
|
|
|
widget_factory = ModelFactory(
|
|
|
|
redash.models.Widget,
|
|
|
|
width=1,
|
|
|
|
options="{}",
|
|
|
|
dashboard=dashboard_factory.create,
|
|
|
|
visualization=visualization_factory.create,
|
|
|
|
)
|
|
|
|
|
|
|
|
destination_factory = ModelFactory(
|
|
|
|
redash.models.NotificationDestination,
|
|
|
|
org_id=1,
|
|
|
|
user=user_factory.create,
|
|
|
|
name=Sequence("Destination {}"),
|
|
|
|
type="slack",
|
2020-05-24 19:22:01 +00:00
|
|
|
options=lambda: ConfigurationContainer.from_json('{"url": "https://www.slack.com"}'),
|
2019-12-11 11:54:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
alert_subscription_factory = ModelFactory(
|
|
|
|
redash.models.AlertSubscription,
|
|
|
|
user=user_factory.create,
|
|
|
|
destination=destination_factory.create,
|
|
|
|
alert=alert_factory.create,
|
|
|
|
)
|
|
|
|
|
|
|
|
query_snippet_factory = ModelFactory(
|
|
|
|
redash.models.QuerySnippet,
|
|
|
|
trigger=Sequence("trigger {}"),
|
|
|
|
description="description",
|
|
|
|
snippet="snippet",
|
|
|
|
)
|
2016-12-06 10:26:48 +00:00
|
|
|
|
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)
|
2019-12-11 11:54:29 +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
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def create_org(self, **kwargs):
|
|
|
|
org = org_factory.create(**kwargs)
|
2019-12-11 11:54:29 +00:00
|
|
|
self.create_group(
|
|
|
|
org=org, type=redash.models.Group.BUILTIN_GROUP, name="default"
|
|
|
|
)
|
|
|
|
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):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"org": self.org, "group_ids": [self.default_group.id]}
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
if "org" in kwargs:
|
|
|
|
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 = {
|
2019-12-11 11:54:29 +00:00
|
|
|
"org": self.org,
|
|
|
|
"group_ids": [self.admin_group.id, self.default_group.id],
|
2015-12-01 13:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
if "org" in kwargs:
|
|
|
|
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):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"name": "Group", "org": self.org}
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
|
2016-11-21 15:34:44 +00:00
|
|
|
g = redash.models.Group(**args)
|
|
|
|
return g
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def create_alert(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"user": self.user, "query_rel": self.create_query()}
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
args.update(**kwargs)
|
|
|
|
return alert_factory.create(**args)
|
|
|
|
|
2016-06-14 08:04:41 +00:00
|
|
|
def create_alert_subscription(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"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
|
2019-12-11 11:54:29 +00:00
|
|
|
if "group" in kwargs:
|
|
|
|
group = kwargs.pop("group")
|
|
|
|
args = {"org": self.org}
|
2015-12-01 13:44:08 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
if group and "org" not in kwargs:
|
|
|
|
args["org"] = group.org
|
2015-12-01 13:44:08 +00:00
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
view_only = args.pop("view_only", False)
|
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:
|
2019-12-11 11:54:29 +00:00
|
|
|
db.session.add(
|
|
|
|
redash.models.DataSourceGroup(
|
|
|
|
group=group, data_source=data_source, view_only=view_only
|
|
|
|
)
|
|
|
|
)
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
return data_source
|
|
|
|
|
|
|
|
def create_dashboard(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"user": self.user, "org": self.org}
|
2015-12-01 13:44:08 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return dashboard_factory.create(**args)
|
|
|
|
|
|
|
|
def create_query(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"user": self.user, "data_source": self.data_source, "org": self.org}
|
2015-12-01 13:44:08 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return query_factory.create(**args)
|
|
|
|
|
2016-05-05 01:15:49 +00:00
|
|
|
def create_query_with_params(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"user": self.user, "data_source": self.data_source, "org": self.org}
|
2016-05-05 01:15:49 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return query_with_params_factory.create(**args)
|
|
|
|
|
2016-06-09 13:06:03 +00:00
|
|
|
def create_access_permission(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"grantor": self.user}
|
2016-06-09 13:06:03 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return access_permission_factory.create(**args)
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def create_query_result(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"data_source": self.data_source}
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
args.update(kwargs)
|
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
if "data_source" in args and "org" not in args:
|
|
|
|
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):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"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):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"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 = {
|
2019-12-11 11:54:29 +00:00
|
|
|
"dashboard": self.create_dashboard(),
|
|
|
|
"visualization": self.create_visualization(),
|
2015-12-01 13:44:08 +00:00
|
|
|
}
|
|
|
|
args.update(kwargs)
|
|
|
|
return widget_factory.create(**args)
|
2016-02-21 12:08:49 +00:00
|
|
|
|
|
|
|
def create_api_key(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"org": self.org}
|
2016-02-21 12:08:49 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return api_key_factory.create(**args)
|
2016-03-24 13:55:46 +00:00
|
|
|
|
|
|
|
def create_destination(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"org": self.org}
|
2016-12-01 13:56:39 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return destination_factory.create(**args)
|
2016-12-06 10:26:48 +00:00
|
|
|
|
|
|
|
def create_query_snippet(self, **kwargs):
|
2019-12-11 11:54:29 +00:00
|
|
|
args = {"user": self.user, "org": self.org}
|
2016-12-06 10:26:48 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return query_snippet_factory.create(**args)
|