redash/tests/controllers.py

272 lines
9.5 KiB
Python
Raw Normal View History

2014-01-31 16:50:25 +00:00
from contextlib import contextmanager
2014-02-05 09:01:06 +00:00
import json
from tests import BaseTestCase
from tests.factories import dashboard_factory, widget_factory, visualization_factory, query_factory
2014-02-05 09:01:06 +00:00
from redash import app, models
from redash.utils import json_dumps
2014-01-31 16:50:25 +00:00
@contextmanager
2014-02-05 09:01:06 +00:00
def authenticated_user(c, user='test@example.com', name='John Test'):
2014-01-31 16:50:25 +00:00
with c.session_transaction() as sess:
2014-02-05 09:01:06 +00:00
sess['openid'] = {'email': user, 'name': name}
2014-01-31 16:50:25 +00:00
yield
2014-02-05 09:01:06 +00:00
def json_request(method, path, data=None):
if data:
response = method(path, data=json_dumps(data))
2014-02-05 09:01:06 +00:00
else:
response = method(path)
if response.data:
response.json = json.loads(response.data)
else:
response.json = None
return response
2014-01-31 16:50:25 +00:00
class AuthenticationTestMixin():
def test_redirects_when_not_authenticated(self):
with app.test_client() as c:
for path in self.paths:
rv = c.get(path)
self.assertEquals(302, rv.status_code)
def test_returns_content_when_authenticated(self):
2014-02-05 09:01:06 +00:00
with app.test_client() as c, authenticated_user(c):
for path in self.paths:
rv = c.get(path)
self.assertEquals(200, rv.status_code)
2014-01-31 16:50:25 +00:00
2014-02-05 09:01:06 +00:00
class PingTest(BaseTestCase):
2014-01-31 16:50:25 +00:00
def test_ping(self):
with app.test_client() as c:
rv = c.get('/ping')
self.assertEquals(200, rv.status_code)
self.assertEquals('PONG.', rv.data)
2014-02-05 09:01:06 +00:00
class IndexTest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
self.paths = ['/', '/dashboard/example', '/queries/1', '/admin/status']
2014-02-05 09:01:06 +00:00
super(IndexTest, self).setUp()
2014-01-31 16:50:25 +00:00
2014-02-05 09:01:06 +00:00
class StatusTest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
self.paths = ['/status.json']
2014-02-05 09:01:06 +00:00
super(StatusTest, self).setUp()
2014-01-31 16:50:25 +00:00
2014-02-05 09:01:06 +00:00
class DashboardAPITest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
self.paths = ['/api/dashboards']
2014-02-05 09:01:06 +00:00
super(DashboardAPITest, self).setUp()
def test_get_dashboard(self):
d1 = dashboard_factory.create()
2014-02-05 09:01:06 +00:00
with app.test_client() as c, authenticated_user(c):
rv = c.get('/api/dashboards/{0}'.format(d1.slug))
self.assertEquals(rv.status_code, 200)
self.assertDictEqual(json.loads(rv.data), d1.to_dict(with_widgets=True))
def test_get_non_existint_dashbaord(self):
with app.test_client() as c, authenticated_user(c):
rv = c.get('/api/dashboards/not_existing')
self.assertEquals(rv.status_code, 404)
def test_create_new_dashboard(self):
user_email = 'test@everything.me'
with app.test_client() as c, authenticated_user(c, user=user_email):
dashboard_name = 'Test Dashboard'
rv = json_request(c.post, '/api/dashboards', data={'name': dashboard_name})
self.assertEquals(rv.status_code, 200)
self.assertEquals(rv.json['name'], 'Test Dashboard')
self.assertEquals(rv.json['user'], user_email)
self.assertEquals(rv.json['layout'], [])
def test_update_dashboard(self):
d = dashboard_factory.create()
2014-02-05 09:01:06 +00:00
new_name = 'New Name'
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/dashboards/{0}'.format(d.id),
data={'name': new_name, 'layout': '[]'})
self.assertEquals(rv.status_code, 200)
self.assertEquals(rv.json['name'], new_name)
def test_delete_dashboard(self):
d = dashboard_factory.create()
2014-02-05 09:01:06 +00:00
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.delete, '/api/dashboards/{0}'.format(d.slug))
self.assertEquals(rv.status_code, 200)
d = models.Dashboard.get_by_slug(d.slug)
self.assertTrue(d.is_archived)
class WidgetAPITest(BaseTestCase):
def create_widget(self, dashboard, visualization, width=1):
data = {
'visualization_id': visualization.id,
'dashboard_id': dashboard.id,
'options': {},
'width': width
}
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/widgets', data=data)
return rv
def test_create_widget(self):
dashboard = dashboard_factory.create()
vis = visualization_factory.create()
rv = self.create_widget(dashboard, vis)
self.assertEquals(rv.status_code, 200)
dashboard = models.Dashboard.get(models.Dashboard.id == dashboard.id)
self.assertEquals(unicode(rv.json['layout']), dashboard.layout)
self.assertEquals(dashboard.widgets, 1)
self.assertEquals(rv.json['layout'], [[rv.json['widget']['id']]])
self.assertEquals(rv.json['new_row'], True)
rv2 = self.create_widget(dashboard, vis)
self.assertEquals(dashboard.widgets, 2)
self.assertEquals(rv2.json['layout'],
[[rv.json['widget']['id'], rv2.json['widget']['id']]])
self.assertEquals(rv2.json['new_row'], False)
rv3 = self.create_widget(dashboard, vis)
self.assertEquals(rv3.json['new_row'], True)
rv4 = self.create_widget(dashboard, vis, width=2)
self.assertEquals(rv4.json['layout'],
[[rv.json['widget']['id'], rv2.json['widget']['id']],
[rv3.json['widget']['id']],
[rv4.json['widget']['id']]])
self.assertEquals(rv4.json['new_row'], True)
def test_delete_widget(self):
widget = widget_factory.create()
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.delete, '/api/widgets/{0}'.format(widget.id))
self.assertEquals(rv.status_code, 200)
dashboard = models.Dashboard.get_by_slug(widget.dashboard.slug)
self.assertEquals(dashboard.widgets.count(), 0)
self.assertEquals(dashboard.layout, '[]')
# TODO: test how it updates the layout
2014-02-05 09:01:06 +00:00
class QueryAPITest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
self.paths = ['/api/queries']
2014-02-05 09:01:06 +00:00
super(QueryAPITest, self).setUp()
2014-01-31 16:50:25 +00:00
def test_update_query(self):
query = query_factory.create()
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/queries/{0}'.format(query.id), data={'name': 'Testing'})
self.assertEqual(rv.status_code, 200)
self.assertEquals(rv.json['name'], 'Testing')
def test_create_query(self):
user = 'test@everything.me'
query_data = {
'name': 'Testing',
'description': 'Description',
'query': 'SELECT 1',
'ttl': 3600
}
with app.test_client() as c, authenticated_user(c, user=user):
rv = json_request(c.post, '/api/queries', data=query_data)
self.assertEquals(rv.status_code, 200)
self.assertDictContainsSubset(query_data, rv.json)
self.assertEquals(rv.json['user'], user)
self.assertIsNotNone(rv.json['api_key'])
self.assertIsNotNone(rv.json['query_hash'])
def test_get_query(self):
query = query_factory.create()
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.get, '/api/queries/{0}'.format(query.id))
self.assertEquals(rv.status_code, 200)
d = query.to_dict(with_visualizations=True)
d.pop('created_at')
self.assertDictContainsSubset(d, rv.json)
def test_get_all_queries(self):
queries = [query_factory.create() for _ in range(10)]
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.get, '/api/queries')
self.assertEquals(rv.status_code, 200)
self.assertEquals(len(rv.json), 10)
2014-02-06 18:49:05 +00:00
class VisualizationAPITest(BaseTestCase):
def test_create_visualization(self):
query = query_factory.create()
data = {
'query_id': query.id,
'name': 'Chart',
'description':'',
'options': {},
'type': 'CHART'
}
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/visualizations', data=data)
self.assertEquals(rv.status_code, 200)
data.pop('query_id')
self.assertDictContainsSubset(data, rv.json)
def test_delete_visualization(self):
visualization = visualization_factory.create()
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.delete, '/api/visualizations/{0}'.format(visualization.id))
self.assertEquals(rv.status_code, 200)
self.assertEquals(models.Visualization.select().count(), 0)
def test_update_visualization(self):
visualization = visualization_factory.create()
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/visualizations/{0}'.format(visualization.id),
data={'name': 'After Update'})
self.assertEquals(rv.status_code, 200)
self.assertEquals(rv.json['name'], 'After Update')
2014-01-31 16:50:25 +00:00
2014-02-05 09:01:06 +00:00
class QueryResultAPITest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
self.paths = []
2014-02-05 09:01:06 +00:00
super(QueryResultAPITest, self).setUp()
2014-01-31 16:50:25 +00:00
2014-02-05 09:01:06 +00:00
class JobAPITest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
self.paths = []
2014-02-05 09:01:06 +00:00
super(JobAPITest, self).setUp()
2014-01-31 16:50:25 +00:00
2014-02-05 09:01:06 +00:00
class CsvQueryResultAPITest(BaseTestCase, AuthenticationTestMixin):
2014-01-31 16:50:25 +00:00
def setUp(self):
2014-02-05 09:01:06 +00:00
self.paths = []
super(CsvQueryResultAPITest, self).setUp()