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)
def test_returns_200_for_admin_changing_other_user(self): admin = user_factory.create(groups=['admin', 'default']) user = user_factory.create() with app.test_client() as c, authenticated_user(c, user=admin): rv = json_request(c.post, "/api/users/{}".format(user.id), data={"name": "New Name"}) self.assertEqual(rv.status_code, 200)
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())
def test_logout_when_loggedin(self): with app.test_client() as c, authenticated_user(c, user=self.factory.user): rv = c.get('/default/') self.assertTrue(current_user.is_authenticated) rv = c.get('/default/logout') self.assertEquals(rv.status_code, 302) self.assertFalse(current_user.is_authenticated)
def test_doesnt_use_cache_headers_for_non_specific_result(self): query_result = query_result_factory.create() query = query_factory.create(latest_query_data=query_result) with app.test_client() as c, authenticated_user(c): rv = json_request(c.get, '/api/queries/{}/results.json'.format(query.id)) self.assertNotIn('Cache-Control', rv.headers)
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: rv = c.get('/default/login') self.assertEquals(rv.status_code, 302) self.assertFalse(login_user_mock.called)
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)
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)
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) self.assertResponseEqual(rv.json, query.to_dict(with_visualizations=True))
def test_logout_when_loggedin(self): with app.test_client() as c, authenticated_user( c, user=self.factory.user): rv = c.get('/default/') self.assertTrue(current_user.is_authenticated) rv = c.get('/default/logout') self.assertEquals(rv.status_code, 302) self.assertFalse(current_user.is_authenticated)
def test_update_dashboard(self): d = dashboard_factory.create() 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() 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)
def test_create_new_dashboard(self): user = user_factory.create() with app.test_client() as c, authenticated_user(c, user=user): 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_id'], user.id) self.assertEquals(rv.json['layout'], [])
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)
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_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')
def test_get_dashboard(self): d1 = dashboard_factory.create() with app.test_client() as c, authenticated_user(c): rv = c.get('/api/dashboards/{0}'.format(d1.slug)) self.assertEquals(rv.status_code, 200) expected = d1.to_dict(with_widgets=True) actual = json.loads(rv.data) self.assertResponseEqual(expected, actual)
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, '[]')
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)
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/users") self.assertEqual(rv.status_code, 400) rv = json_request(c.post, '/api/users', data={'name': 'User'}) self.assertEqual(rv.status_code, 400)
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_creates_user(self): admin = user_factory.create(groups=['admin', 'default']) with app.test_client() as c, authenticated_user(c, user=admin): test_user = {'name': 'User', 'email': '*****@*****.**', 'password': '******'} rv = json_request(c.post, '/api/users', data=test_user) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.json['name'], test_user['name']) self.assertEqual(rv.json['email'], test_user['email'])
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')
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_update_dashboard(self): d = dashboard_factory.create() 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 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_changes_password(self): new_password = "******" old_password = "******" with app.test_client() as c, authenticated_user(c) as user: user.hash_password(old_password) user.save() rv = json_request(c.post, "/api/users/{}".format(user.id), data={"password": new_password, "old_password": old_password}) self.assertEqual(rv.status_code, 200) user = models.User.get_by_id(user.id) self.assertTrue(user.verify_password(new_password))
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)
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)
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_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_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_create_query(self): user = user_factory.create() data_source = data_source_factory.create() query_data = { 'name': 'Testing', 'query': 'SELECT 1', 'schedule': "3600", 'data_source_id': data_source.id } 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']['id'], user.id) self.assertIsNotNone(rv.json['api_key']) self.assertIsNotNone(rv.json['query_hash']) query = models.Query.get_by_id(rv.json['id']) self.assertEquals(len(list(query.visualizations)), 1)
def test_doesnt_return_api_key_for_other_user(self): other_user = user_factory.create() with app.test_client() as c, authenticated_user(c): rv = json_request(c.get, "/api/users/{}".format(other_user.id)) self.assertNotIn('api_key', rv.json)
def test_returns_403_for_non_admin(self): with app.test_client() as c, authenticated_user(c): rv = c.post("/api/users") self.assertEqual(rv.status_code, 403)
def test_returns_403_for_non_admin_changing_not_his_own(self): other_user = user_factory.create() with app.test_client() as c, authenticated_user(c): rv = c.post("/api/users/{}".format(other_user.id), data={"name": "New Name"}) self.assertEqual(rv.status_code, 403)
def test_returns_200_for_non_admin_changing_his_own(self): with app.test_client() as c, authenticated_user(c) as user: rv = json_request(c.post, "/api/users/{}".format(user.id), data={"name": "New Name"}) self.assertEqual(rv.status_code, 200)
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: rv = c.get('/default/login') self.assertEquals(rv.status_code, 302) self.assertFalse(login_user_mock.called)
def test_uses_cache_headers_for_specific_result(self): query_result = query_result_factory.create() query = query_factory.create(latest_query_data=query_result) with app.test_client() as c, authenticated_user(c): rv = json_request(c.get, '/api/queries/{}/results/{}.json'.format(query.id, query_result.id)) self.assertIn('Cache-Control', rv.headers)
def test_fails_password_change_with_incorrect_old_password(self): with app.test_client() as c, authenticated_user(c) as user: rv = json_request(c.post, "/api/users/{}".format(user.id), data={"password": "******", "old_password": "******"}) self.assertEqual(rv.status_code, 403)