redash/tests/test_handlers.py

454 lines
18 KiB
Python
Raw Normal View History

2014-02-05 09:01:06 +00:00
import json
from unittest import TestCase
from flask import url_for
2014-03-02 16:27:05 +00:00
from flask.ext.login import current_user
2014-03-02 15:59:08 +00:00
from mock import patch
from tests import BaseTestCase
2015-08-02 11:59:25 +00:00
from tests.handlers import authenticated_user, json_request
2014-02-09 16:50:28 +00:00
from tests.factories import dashboard_factory, widget_factory, visualization_factory, query_factory, \
2014-05-13 15:29:59 +00:00
query_result_factory, user_factory, data_source_factory
from redash import models, settings
from redash.wsgi import app
2014-01-31 16:50:25 +00:00
settings.GOOGLE_APPS_DOMAIN = "example.com"
2014-02-05 09:01:06 +00:00
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
class TestAuthentication(BaseTestCase):
def test_redirects_for_nonsigned_in_user(self):
with app.test_client() as c:
rv = c.get("/")
self.assertEquals(302, rv.status_code)
class PingTest(TestCase):
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-03-12 11:08:19 +00:00
class StatusTest(BaseTestCase):
def test_returns_data_for_admin(self):
2014-05-13 15:29:59 +00:00
admin = user_factory.create(groups=['admin', 'default'])
2014-03-12 11:08:19 +00:00
with app.test_client() as c, authenticated_user(c, user=admin):
rv = c.get('/status.json')
self.assertEqual(rv.status_code, 200)
def test_returns_403_for_non_admin(self):
with app.test_client() as c, authenticated_user(c):
rv = c.get('/status.json')
self.assertEqual(rv.status_code, 403)
def test_redirects_non_authenticated_user(self):
with app.test_client() as c:
rv = c.get('/status.json')
self.assertEqual(rv.status_code, 302)
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)
2015-03-22 12:42:06 +00:00
expected = d1.to_dict(with_widgets=True)
actual = json.loads(rv.data)
self.assertResponseEqual(expected, actual)
2014-02-05 09:01:06 +00:00
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):
2014-02-22 12:52:04 +00:00
user = user_factory.create()
with app.test_client() as c, authenticated_user(c, user=user):
2014-02-05 09:01:06 +00:00
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')
2014-02-22 12:52:04 +00:00
self.assertEquals(rv.json['user_id'], user.id)
2014-02-05 09:01:06 +00:00
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)
2014-04-29 07:36:56 +00:00
def test_create_text_widget(self):
dashboard = dashboard_factory.create()
data = {
'visualization_id': None,
'text': 'Sample text.',
'dashboard_id': dashboard.id,
'options': {},
'width': 2
}
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/widgets', data=data)
self.assertEquals(rv.status_code, 200)
self.assertEquals(rv.json['widget']['text'], 'Sample text.')
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()
other_user = user_factory.create()
with app.test_client() as c, authenticated_user(c, user=other_user):
rv = json_request(c.post, '/api/queries/{0}'.format(query.id), data={'name': 'Testing'})
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.json['name'], 'Testing')
self.assertEqual(rv.json['last_modified_by']['id'], other_user.id)
def test_create_query(self):
2014-02-22 12:52:04 +00:00
user = user_factory.create()
2014-03-19 10:53:51 +00:00
data_source = data_source_factory.create()
query_data = {
'name': 'Testing',
'query': 'SELECT 1',
'schedule': "3600",
2014-03-19 10:53:51 +00:00
'data_source_id': data_source.id
}
2014-02-22 12:52:04 +00:00
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)
2014-02-22 12:52:04 +00:00
self.assertEquals(rv.json['user']['id'], user.id)
self.assertIsNotNone(rv.json['api_key'])
self.assertIsNotNone(rv.json['query_hash'])
2014-02-06 19:12:02 +00:00
query = models.Query.get_by_id(rv.json['id'])
self.assertEquals(len(list(query.visualizations)), 1)
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)
2015-03-22 12:42:06 +00:00
self.assertResponseEqual(rv.json, query.to_dict(with_visualizations=True))
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)
# =1 because each query has a default table visualization.
self.assertEquals(models.Visualization.select().count(), 1)
2014-02-06 18:49:05 +00:00
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
def test_post_result_list(self):
data_source = data_source_factory.create()
query_result = query_result_factory.create()
query = query_factory.create()
with app.test_client() as c, authenticated_user(c):
rv = json_request(c.post, '/api/query_results',
data={'data_source_id': data_source.id,
'query': query.query})
self.assertEquals(rv.status_code, 200)
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-03-02 15:59:08 +00:00
class TestLogin(BaseTestCase):
def setUp(self):
settings.PASSWORD_LOGIN_ENABLED = True
super(TestLogin, self).setUp()
def test_redirects_to_google_login_if_password_disabled(self):
with app.test_client() as c, patch.object(settings, 'PASSWORD_LOGIN_ENABLED', False):
rv = c.get('/login')
self.assertEquals(rv.status_code, 302)
2014-09-21 07:11:03 +00:00
self.assertTrue(rv.location.endswith(url_for('google_oauth.authorize')))
2014-03-02 15:59:08 +00:00
def test_get_login_form(self):
with app.test_client() as c:
rv = c.get('/login')
self.assertEquals(rv.status_code, 200)
def test_submit_non_existing_user(self):
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
rv = c.post('/login', data={'email': 'arik', 'password': 'password'})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_submit_correct_user_and_password(self):
2014-05-13 15:29:59 +00:00
2014-03-02 15:59:08 +00:00
user = user_factory.create()
user.hash_password('password')
user.save()
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
rv = c.post('/login', data={'email': user.email, 'password': 'password'})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 302)
login_user_mock.assert_called_with(user, remember=False)
def test_submit_correct_user_and_password_and_remember_me(self):
user = user_factory.create()
user.hash_password('password')
user.save()
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
rv = c.post('/login', data={'email': user.email, 'password': 'password', 'remember': True})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 302)
login_user_mock.assert_called_with(user, remember=True)
def test_submit_correct_user_and_password_with_next(self):
user = user_factory.create()
user.hash_password('password')
user.save()
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
2014-03-02 15:59:08 +00:00
rv = c.post('/login?next=/test',
data={'email': user.email, 'password': 'password'})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 302)
self.assertEquals(rv.location, 'http://localhost/test')
login_user_mock.assert_called_with(user, remember=False)
def test_submit_incorrect_user(self):
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
rv = c.post('/login', data={'email': 'non-existing', 'password': 'password'})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_submit_incorrect_password(self):
user = user_factory.create()
user.hash_password('password')
user.save()
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
rv = c.post('/login', data={'email': user.email, 'password': 'badbadpassword'})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_submit_incorrect_password(self):
user = user_factory.create()
with app.test_client() as c, patch('redash.handlers.authentication.login_user') as login_user_mock:
rv = c.post('/login', data={'email': user.email, 'password': ''})
2014-03-02 15:59:08 +00:00
self.assertEquals(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_user_already_loggedin(self):
with app.test_client() as c, authenticated_user(c), patch('redash.handlers.authentication.login_user') as login_user_mock:
2014-03-02 15:59:08 +00:00
rv = c.get('/login')
self.assertEquals(rv.status_code, 302)
self.assertFalse(login_user_mock.called)
2014-03-02 16:27:05 +00:00
# TODO: brute force protection?
class TestLogout(BaseTestCase):
def test_logout_when_not_loggedin(self):
with app.test_client() as c:
rv = c.get('/logout')
self.assertEquals(rv.status_code, 302)
self.assertFalse(current_user.is_authenticated())
def test_logout_when_loggedin(self):
with app.test_client() as c, authenticated_user(c):
rv = c.get('/')
self.assertTrue(current_user.is_authenticated())
rv = c.get('/logout')
self.assertEquals(rv.status_code, 302)
self.assertFalse(current_user.is_authenticated())
class DataSourceTypesTest(BaseTestCase):
def test_returns_data_for_admin(self):
admin = user_factory.create(groups=['admin', 'default'])
with app.test_client() as c, authenticated_user(c, user=admin):
rv = c.get("/api/data_sources/types")
self.assertEqual(rv.status_code, 200)
def test_returns_403_for_non_admin(self):
with app.test_client() as c, authenticated_user(c):
rv = c.get("/api/data_sources/types")
self.assertEqual(rv.status_code, 403)
class DataSourceTest(BaseTestCase):
def test_returns_400_when_missing_fields(self):
admin = user_factory.create(groups=['admin', 'default'])
with app.test_client() as c, authenticated_user(c, user=admin):
rv = c.post("/api/data_sources")
self.assertEqual(rv.status_code, 400)
rv = json_request(c.post, '/api/data_sources', data={'name': 'DS 1'})
self.assertEqual(rv.status_code, 400)
def test_returns_400_when_configuration_invalid(self):
admin = user_factory.create(groups=['admin', 'default'])
with app.test_client() as c, authenticated_user(c, user=admin):
rv = json_request(c.post, '/api/data_sources',
data={'name': 'DS 1', 'type': 'pg', 'options': '{}'})
self.assertEqual(rv.status_code, 400)
def test_creates_data_source(self):
admin = user_factory.create(groups=['admin', 'default'])
with app.test_client() as c, authenticated_user(c, user=admin):
rv = json_request(c.post, '/api/data_sources',
data={'name': 'DS 1', 'type': 'pg', 'options': {"dbname": "redash"}})
self.assertEqual(rv.status_code, 200)