示例#1
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)
示例#2
0
def authenticated_user(c, user=None):
    if not user:
        user = user_factory.create()
        db.session.commit()
    authenticate_request(c, user)

    yield user
示例#3
0
 def test_user_api_key(self):
     user = user_factory.create(api_key="user_key")
     path = '/api/queries/'
     with app.test_client() as c:
         signature = sign(user.api_key, path, self.expires)
         rv = c.get(path, query_string={'signature': signature, 'expires': self.expires, 'user_id': user.id})
         self.assertEqual(user.id, hmac_load_user_from_request(request).id)
示例#4
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)
示例#5
0
    def test_submit_incorrect_password(self):
        user = user_factory.create()

        with app.test_client() as c, patch('redash.controllers.login_user') as login_user_mock:
            rv = c.post('/login', data={'username': user.email, 'password': ''})
            self.assertEquals(rv.status_code, 200)
            self.assertFalse(login_user_mock.called)
示例#6
0
    def test_submit_incorrect_password(self):
        user = user_factory.create()

        with app.test_client() as c, patch('redash.controllers.login_user') as login_user_mock:
            rv = c.post('/login', data={'username': user.email, 'password': ''})
            self.assertEquals(rv.status_code, 200)
            self.assertFalse(login_user_mock.called)
示例#7
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)
示例#8
0
    def test_submit_incorrect_password(self):
        user = user_factory.create()

        with app.test_client() as c, patch("redash.controllers.login_user") as login_user_mock:
            rv = c.post("/login", data={"email": user.email, "password": ""})
            self.assertEquals(rv.status_code, 200)
            self.assertFalse(login_user_mock.called)
示例#9
0
def authenticated_user(c, user=None):
    if not user:
        user = user_factory.create()

    authenticate_request(c, user)

    yield user
示例#10
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)
示例#11
0
def authenticated_user(c, user=None):
    if not user:
        user = user_factory.create()

    with c.session_transaction() as sess:
        sess['user_id'] = user.id

    yield
    def test_logins_valid_user(self):
        user = user_factory.create(email='*****@*****.**')

        with patch.object(
                settings, 'GOOGLE_APPS_DOMAIN', 'example.com'), patch(
                    'redash.authentication.login_user') as login_user_mock:
            create_and_login_user(None, user)
            login_user_mock.assert_called_once_with(user, remember=True)
示例#13
0
def authenticated_user(c, user=None):
    if not user:
        user = user_factory.create()

    with c.session_transaction() as sess:
        sess['user_id'] = user.id

    yield
示例#14
0
    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.controllers.login_user") as login_user_mock:
            rv = c.post("/login", data={"email": user.email, "password": "******", "remember": True})
            self.assertEquals(rv.status_code, 302)
            login_user_mock.assert_called_with(user, remember=True)
示例#15
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"], [])
示例#16
0
    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': '******'})
            self.assertEquals(rv.status_code, 200)
            self.assertFalse(login_user_mock.called)
示例#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'], [])
示例#18
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'], [])
示例#19
0
    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.controllers.login_user') as login_user_mock:
            rv = c.post('/login', data={'username': user.email, 'password': '******', 'remember': True})
            self.assertEquals(rv.status_code, 302)
            login_user_mock.assert_called_with(user, remember=True)
示例#20
0
    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.controllers.login_user') as login_user_mock:
            rv = c.post('/login', data={'username': user.email, 'password': '******', 'remember': True})
            self.assertEquals(rv.status_code, 302)
            login_user_mock.assert_called_with(user, remember=True)
示例#21
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)
示例#22
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)
示例#23
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)
示例#24
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)
示例#25
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'])
示例#26
0
    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.controllers.login_user") as login_user_mock:
            rv = c.post("/login?next=/test", data={"email": user.email, "password": "******"})
            self.assertEquals(rv.status_code, 302)
            self.assertEquals(rv.location, "http://localhost/test")
            login_user_mock.assert_called_with(user, remember=False)
示例#27
0
    def test_submit_correct_user_and_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': '******'})
            self.assertEquals(rv.status_code, 302)
            login_user_mock.assert_called_with(user, remember=False)
示例#28
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)
示例#29
0
    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.controllers.login_user') as login_user_mock:
            rv = c.post('/login?next=/test',
                        data={'username': user.email, 'password': '******'})
            self.assertEquals(rv.status_code, 302)
            self.assertEquals(rv.location, 'http://localhost/test')
            login_user_mock.assert_called_with(user, remember=False)
示例#30
0
    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.controllers.login_user') as login_user_mock:
            rv = c.post('/login?next=/test',
                        data={'username': user.email, 'password': '******'})
            self.assertEquals(rv.status_code, 302)
            self.assertEquals(rv.location, 'http://localhost/test')
            login_user_mock.assert_called_with(user, remember=False)
示例#31
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)
示例#32
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)
示例#33
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)
示例#34
0
    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': '******'
                        })
            self.assertEquals(rv.status_code, 200)
            self.assertFalse(login_user_mock.called)
示例#35
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)
示例#36
0
    def test_submit_correct_user_and_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': '******'
                        })
            self.assertEquals(rv.status_code, 302)
            login_user_mock.assert_called_with(user, remember=False)
示例#37
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)
示例#38
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)
示例#39
0
 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('/status.json')
         self.assertEqual(rv.status_code, 200)
示例#40
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)
示例#41
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)
示例#42
0
 def test_user_api_key(self):
     user = user_factory.create(api_key="user_key")
     with app.test_client() as c:
         rv = c.get('/api/queries/', query_string={'api_key': user.api_key})
         self.assertEqual(user.id, api_key_load_user_from_request(request).id)
示例#43
0
 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('/status.json')
         self.assertEqual(rv.status_code, 200)
示例#44
0
    def test_logins_valid_user(self):
        user = user_factory.create(email='*****@*****.**')

        with patch('redash.google_oauth.login_user') as login_user_mock:
            create_and_login_user(user.name, user.email)
            login_user_mock.assert_called_once_with(user, remember=True)
示例#45
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)
示例#46
0
    def test_logins_valid_user(self):
        user = user_factory.create(email='*****@*****.**')

        with patch.object(settings, 'GOOGLE_APPS_DOMAIN', 'example.com'), patch('redash.authentication.login_user') as login_user_mock:
            create_and_login_user(None, user)
            login_user_mock.assert_called_once_with(user, remember=True)
示例#47
0
 def test_returns_api_key_for_other_user_when_admin(self):
     other_user = user_factory.create()
     admin = user_factory.create(groups=['admin', 'default'])
     with app.test_client() as c, authenticated_user(c, user=admin):
         rv = json_request(c.get, "/api/users/{}".format(other_user.id))
         self.assertIn('api_key', rv.json)