Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 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())
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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())
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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))
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
 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'], [])
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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)
Ejemplo n.º 20
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')
Ejemplo n.º 21
0
    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))
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
    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, '[]')
Ejemplo n.º 24
0
    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)
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
    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)
Ejemplo n.º 27
0
    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, '[]')
Ejemplo n.º 28
0
    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)
Ejemplo n.º 29
0
    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'])
Ejemplo n.º 30
0
 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'], [])
Ejemplo n.º 31
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')
Ejemplo n.º 32
0
    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)
Ejemplo n.º 33
0
 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)
Ejemplo n.º 34
0
    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
Ejemplo n.º 35
0
    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)
Ejemplo n.º 36
0
    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
Ejemplo n.º 37
0
    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))
Ejemplo n.º 38
0
    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)
Ejemplo n.º 39
0
    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)
Ejemplo n.º 40
0
    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)
Ejemplo n.º 41
0
    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)
Ejemplo n.º 42
0
    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.')
Ejemplo n.º 43
0
    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.')
Ejemplo n.º 44
0
    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)
Ejemplo n.º 45
0
    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)
Ejemplo n.º 46
0
 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)
Ejemplo n.º 47
0
 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)
Ejemplo n.º 48
0
 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)
Ejemplo n.º 49
0
 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)
Ejemplo n.º 50
0
 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)
Ejemplo n.º 51
0
 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)
Ejemplo n.º 52
0
 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)