redash/tests/handlers/test_queries.py
Nicolas Le Manchet 246eca1121 Migrate the application to Python 3 (#4251)
* Make core app compatible with Python 3

No backward compatibility with Python 2.7 is kept.
This commit mostly contains changes made with 2to3 and manual
tweaking when necessary.

* Use Python 3.7 as base docker image

Since it is not possible to change redash/base:debian to Python 3
without breaking future relases, its Dockerfile is temporarly
copied here.

* Upgrade some requirements to newest versions

Some of the older versions were not compatible with Python 3.

* Migrate tests to Python 3

* Build frontend on Python 3

* Make the HMAC sign function compatible with Python 3

In Python 3, HMAC only works with bytes so the strings and the
float used in the sign function need to be encoded.
Hopefully this is still backward compatible with already generated
signatures.

* Use assertCountEqual instead of assertItemsEqual

The latter is not available in Python 3.
See https://bugs.python.org/issue17866

* Remove redundant encoding header for Python 3 modules

* Remove redundant string encoding in CLI

* Rename list() functions in CLI

These functions shadow the builtin list function which is
problematic since 2to3 adds a fair amount of calls to the builtin
list when it finds dict.keys() and dict.values().

Only the Python function is renamed, from the perspective of the
CLI nothing changes.

* Replace usage of Exception.message in CLI

`message` is not available anymore, instead use the string
representation of the exception.

* Adapt test handlers to Python 3

* Fix test that relied on dict ordering

* Make sure test results are always uploaded (#4215)

* Support encoding memoryview to JSON

psycopg2 returns `buffer` objects in Python 2.7 and `memoryview`
in Python 3. See #3156

* Fix test relying on object address ordering

* Decode bytes returned from Redis

* Stop using e.message for most exceptions

Exception.message is not available in Python 3 anymore, except
for some exceptions defined by third-party libraries.

* Fix writing XLSX files in Python 3

The buffer for the file should be made of bytes and the actual
content written to it strings.

Note: I do not know why the diff is so large as it's only a two
lines change. Probably a white space or file encoding issue.

* Fix test by comparing strings to strings

* Fix another exception message unavailable in Python 3

* Fix export to CSV in Python 3

The UnicodeWriter is not used anymore. In Python 3, the interface
provided by the CSV module only deals with strings, in and out.
The encoding of the output is left to the user, in our case
it is given to Flask via `make_response`.

* (Python 3) Use Redis' decode_responses=True option (#4232)

* Fix test_outdated_queries_works_scheduled_queries_tracker (use utcnow)

* Make sure Redis connection uses decoded_responses option

* Remove unused imports.

* Use Redis' decode_responses option

* Remove cases of explicit Redis decoding

* Rename helper function and make sure it doesn't apply twice.

* Don't add decode_responses to Celery Redis connection URL

* Fix displaying error while connecting to SQLite

The exception message is always a string in Python 3, so no
need to try to decode things.

* Fix another missing exception message

* Handle JSON encoding for datasources returning bytes

SimpleJSON assumes the bytes it receives contain text data, so it
tries to UTF-8 encode them. It is sometimes not true, for instance
the SQLite datasource returns bytes for BLOB types, which typically
do not contain text but truly binary data.

This commit disables SimpleJSON auto encoding of bytes to str and
instead uses the same method as for memoryviews: generating a
hex representation of the data.

* Fix Python 3 compatibility with RQ

* Revert some changes 2to3 tends to do (#4261)

- Revert some changes 2to3 tends to do when it errs on the side of caution regarding dict view objects.

- Also fixed some naming issues with one character variables in list comprehensions.

- Fix Flask warning.

* Upgrade dependencies

* Remove useless `iter` added by 2to3

* Fix get_next_path tests (#4280)

* Removed setting SERVER_NAME in tests setup to avoid a warning.

* Change get_next_path to not return empty string in case of a domain only value.

* Fix redirect tests:

Since version 0.15 of Werkzeug it uses full path for fixing the location header instead of the root path.

* Remove explicit dependency for Werkzeug

* Switched pytz and certifi to unbinded versions.

* Switch to new library for getting country from IP

`python-geoip-geolite2` is not compatible with Python 3, instead
use `maxminddb-geolite2` which is very similar as it includes
the geolite2 database in the package .

* Python 3 RQ modifications (#4281)

* show current worker job (alongside with minor cosmetic column tweaks)

* avoid loading entire job data for queued jobs

* track general RQ queues (default, periodic and schemas)

* get all active RQ queues

* call get_celery_queues in another place

* merge dicts the Python 3 way

* extend the result_ttl of refresh_queries to 600 seconds to allow it to continue running periodically even after longer executions

* Remove legacy Python flake8 tests
2019-10-24 12:42:13 +03:00

458 lines
17 KiB
Python

from tests import BaseTestCase
from redash import models
from redash.models import db
from redash.serializers import serialize_query
from redash.permissions import ACCESS_TYPE_MODIFY
class TestQueryResourceGet(BaseTestCase):
def test_get_query(self):
query = self.factory.create_query()
rv = self.make_request('get', '/api/queries/{0}'.format(query.id))
self.assertEqual(rv.status_code, 200)
expected = serialize_query(query, with_visualizations=True)
expected['can_edit'] = True
expected['is_favorite'] = False
self.assertResponseEqual(expected, rv.json)
def test_get_all_queries(self):
[self.factory.create_query() for _ in range(10)]
rv = self.make_request('get', '/api/queries')
self.assertEqual(rv.status_code, 200)
self.assertEqual(len(rv.json['results']), 10)
def test_query_without_data_source_should_be_available_only_by_admin(self):
query = self.factory.create_query()
query.data_source = None
db.session.add(query)
rv = self.make_request('get', '/api/queries/{}'.format(query.id))
self.assertEqual(rv.status_code, 403)
rv = self.make_request('get', '/api/queries/{}'.format(query.id), user=self.factory.create_admin())
self.assertEqual(rv.status_code, 200)
def test_query_only_accessible_to_users_from_its_organization(self):
second_org = self.factory.create_org()
second_org_admin = self.factory.create_admin(org=second_org)
query = self.factory.create_query()
query.data_source = None
db.session.add(query)
rv = self.make_request('get', '/api/queries/{}'.format(query.id), user=second_org_admin)
self.assertEqual(rv.status_code, 404)
rv = self.make_request('get', '/api/queries/{}'.format(query.id), user=self.factory.create_admin())
self.assertEqual(rv.status_code, 200)
def test_query_search(self):
names = [
'Harder',
'Better',
'Faster',
'Stronger',
]
for name in names:
self.factory.create_query(name=name)
rv = self.make_request('get', '/api/queries?q=better')
self.assertEqual(rv.status_code, 200)
self.assertEqual(len(rv.json['results']), 1)
rv = self.make_request('get', '/api/queries?q=better OR faster')
self.assertEqual(rv.status_code, 200)
self.assertEqual(len(rv.json['results']), 2)
# test the old search API and that it redirects to the new one
rv = self.make_request('get', '/api/queries/search?q=stronger')
self.assertEqual(rv.status_code, 301)
self.assertIn('/api/queries?q=stronger', rv.headers['Location'])
rv = self.make_request('get', '/api/queries/search?q=stronger', follow_redirects=True)
self.assertEqual(rv.status_code, 200)
self.assertEqual(len(rv.json['results']), 1)
class TestQueryResourcePost(BaseTestCase):
def test_update_query(self):
admin = self.factory.create_admin()
query = self.factory.create_query()
new_ds = self.factory.create_data_source()
new_qr = self.factory.create_query_result()
data = {
'name': 'Testing',
'query': 'select 2',
'latest_query_data_id': new_qr.id,
'data_source_id': new_ds.id
}
rv = self.make_request('post', '/api/queries/{0}'.format(query.id), data=data, user=admin)
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.json['name'], data['name'])
self.assertEqual(rv.json['last_modified_by']['id'], admin.id)
self.assertEqual(rv.json['query'], data['query'])
self.assertEqual(rv.json['data_source_id'], data['data_source_id'])
self.assertEqual(rv.json['latest_query_data_id'], data['latest_query_data_id'])
def test_raises_error_in_case_of_conflict(self):
q = self.factory.create_query()
q.name = "Another Name"
db.session.add(q)
rv = self.make_request('post', '/api/queries/{0}'.format(q.id), data={'name': 'Testing', 'version': q.version - 1}, user=self.factory.user)
self.assertEqual(rv.status_code, 409)
def test_allows_association_with_authorized_dropdown_queries(self):
data_source = self.factory.create_data_source(group=self.factory.default_group)
other_query = self.factory.create_query(data_source=data_source)
db.session.add(other_query)
my_query = self.factory.create_query(data_source=data_source)
db.session.add(my_query)
options = {
'parameters': [{
'name': 'foo',
'type': 'query',
'queryId': other_query.id
}, {
'name': 'bar',
'type': 'query',
'queryId': other_query.id
}]
}
rv = self.make_request('post', '/api/queries/{0}'.format(my_query.id), data={'options': options}, user=self.factory.user)
self.assertEqual(rv.status_code, 200)
def test_prevents_association_with_unauthorized_dropdown_queries(self):
other_data_source = self.factory.create_data_source(group=self.factory.create_group())
other_query = self.factory.create_query(data_source=other_data_source)
db.session.add(other_query)
my_data_source = self.factory.create_data_source(group=self.factory.create_group())
my_query = self.factory.create_query(data_source=my_data_source)
db.session.add(my_query)
options = {
'parameters': [{
'type': 'query',
'queryId': other_query.id
}]
}
rv = self.make_request('post', '/api/queries/{0}'.format(my_query.id), data={'options': options}, user=self.factory.user)
self.assertEqual(rv.status_code, 403)
def test_prevents_association_with_non_existing_dropdown_queries(self):
my_data_source = self.factory.create_data_source(group=self.factory.create_group())
my_query = self.factory.create_query(data_source=my_data_source)
db.session.add(my_query)
options = {
'parameters': [{
'type': 'query',
'queryId': 100000
}]
}
rv = self.make_request('post', '/api/queries/{0}'.format(my_query.id), data={'options': options}, user=self.factory.user)
self.assertEqual(rv.status_code, 400)
def test_overrides_existing_if_no_version_specified(self):
q = self.factory.create_query()
q.name = "Another Name"
db.session.add(q)
rv = self.make_request('post', '/api/queries/{0}'.format(q.id), data={'name': 'Testing'}, user=self.factory.user)
self.assertEqual(rv.status_code, 200)
def test_works_for_non_owner_with_permission(self):
query = self.factory.create_query()
user = self.factory.create_user()
rv = self.make_request('post', '/api/queries/{0}'.format(query.id), data={'name': 'Testing'}, user=user)
self.assertEqual(rv.status_code, 403)
models.AccessPermission.grant(obj=query, access_type=ACCESS_TYPE_MODIFY, grantee=user, grantor=query.user)
rv = self.make_request('post', '/api/queries/{0}'.format(query.id), data={'name': 'Testing'}, user=user)
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.json['name'], 'Testing')
self.assertEqual(rv.json['last_modified_by']['id'], user.id)
class TestQueryListResourceGet(BaseTestCase):
def test_returns_queries(self):
q1 = self.factory.create_query()
q2 = self.factory.create_query()
q3 = self.factory.create_query()
rv = self.make_request('get', '/api/queries')
assert len(rv.json['results']) == 3
assert set([result['id'] for result in rv.json['results']]) == set([q1.id, q2.id, q3.id])
def test_filters_with_tags(self):
q1 = self.factory.create_query(tags=['test'])
self.factory.create_query()
self.factory.create_query()
rv = self.make_request('get', '/api/queries?tags=test')
assert len(rv.json['results']) == 1
assert set([result['id'] for result in rv.json['results']]) == set([q1.id])
def test_search_term(self):
q1 = self.factory.create_query(name="Sales")
q2 = self.factory.create_query(name="Q1 sales")
self.factory.create_query(name="Ops")
rv = self.make_request('get', '/api/queries?q=sales')
assert len(rv.json['results']) == 2
assert set([result['id'] for result in rv.json['results']]) == set([q1.id, q2.id])
class TestQueryListResourcePost(BaseTestCase):
def test_create_query(self):
query_data = {
'name': 'Testing',
'query': 'SELECT 1',
'schedule': {"interval": "3600"},
'data_source_id': self.factory.data_source.id
}
rv = self.make_request('post', '/api/queries', data=query_data)
self.assertEqual(rv.status_code, 200)
self.assertDictContainsSubset(query_data, rv.json)
self.assertEqual(rv.json['user']['id'], self.factory.user.id)
self.assertIsNotNone(rv.json['api_key'])
self.assertIsNotNone(rv.json['query_hash'])
query = models.Query.query.get(rv.json['id'])
self.assertEqual(len(list(query.visualizations)), 1)
self.assertTrue(query.is_draft)
def test_allows_association_with_authorized_dropdown_queries(self):
data_source = self.factory.create_data_source(group=self.factory.default_group)
other_query = self.factory.create_query(data_source=data_source)
db.session.add(other_query)
query_data = {
'name': 'Testing',
'query': 'SELECT 1',
'schedule': {"interval": "3600"},
'data_source_id': self.factory.data_source.id,
'options': {
'parameters': [{
'name': 'foo',
'type': 'query',
'queryId': other_query.id
}, {
'name': 'bar',
'type': 'query',
'queryId': other_query.id
}]
}
}
rv = self.make_request('post', '/api/queries', data=query_data)
self.assertEqual(rv.status_code, 200)
def test_prevents_association_with_unauthorized_dropdown_queries(self):
other_data_source = self.factory.create_data_source(group=self.factory.create_group())
other_query = self.factory.create_query(data_source=other_data_source)
db.session.add(other_query)
my_data_source = self.factory.create_data_source(group=self.factory.create_group())
query_data = {
'name': 'Testing',
'query': 'SELECT 1',
'schedule': {"interval": "3600"},
'data_source_id': my_data_source.id,
'options': {
'parameters': [{
'type': 'query',
'queryId': other_query.id
}]
}
}
rv = self.make_request('post', '/api/queries', data=query_data)
self.assertEqual(rv.status_code, 403)
def test_prevents_association_with_non_existing_dropdown_queries(self):
query_data = {
'name': 'Testing',
'query': 'SELECT 1',
'schedule': {"interval": "3600"},
'data_source_id': self.factory.data_source.id,
'options': {
'parameters': [{
'type': 'query',
'queryId': 100000
}]
}
}
rv = self.make_request('post', '/api/queries', data=query_data)
self.assertEqual(rv.status_code, 400)
class TestQueryArchiveResourceGet(BaseTestCase):
def test_returns_queries(self):
q1 = self.factory.create_query(is_archived=True)
q2 = self.factory.create_query(is_archived=True)
self.factory.create_query()
rv = self.make_request('get', '/api/queries/archive')
assert len(rv.json['results']) == 2
assert set([result['id'] for result in rv.json['results']]) == set([q1.id, q2.id])
def test_search_term(self):
q1 = self.factory.create_query(name="Sales", is_archived=True)
q2 = self.factory.create_query(name="Q1 sales", is_archived=True)
self.factory.create_query(name="Q2 sales")
rv = self.make_request('get', '/api/queries/archive?q=sales')
assert len(rv.json['results']) == 2
assert set([result['id'] for result in rv.json['results']]) == set([q1.id, q2.id])
class QueryRefreshTest(BaseTestCase):
def setUp(self):
super(QueryRefreshTest, self).setUp()
self.query = self.factory.create_query()
self.path = '/api/queries/{}/refresh'.format(self.query.id)
def test_refresh_regular_query(self):
response = self.make_request('post', self.path)
self.assertEqual(200, response.status_code)
def test_refresh_of_query_with_parameters(self):
self.query.query_text = "SELECT {{param}}"
db.session.add(self.query)
response = self.make_request('post', "{}?p_param=1".format(self.path))
self.assertEqual(200, response.status_code)
def test_refresh_of_query_with_parameters_without_parameters(self):
self.query.query_text = "SELECT {{param}}"
db.session.add(self.query)
response = self.make_request('post', "{}".format(self.path))
self.assertEqual(400, response.status_code)
def test_refresh_query_you_dont_have_access_to(self):
group = self.factory.create_group()
db.session.add(group)
db.session.commit()
user = self.factory.create_user(group_ids=[group.id])
response = self.make_request('post', self.path, user=user)
self.assertEqual(403, response.status_code)
def test_refresh_forbiden_with_query_api_key(self):
response = self.make_request('post', '{}?api_key={}'.format(self.path, self.query.api_key), user=False)
self.assertEqual(403, response.status_code)
response = self.make_request('post', '{}?api_key={}'.format(self.path, self.factory.user.api_key), user=False)
self.assertEqual(200, response.status_code)
class TestQueryRegenerateApiKey(BaseTestCase):
def test_non_admin_cannot_regenerate_api_key_of_other_user(self):
query_creator = self.factory.create_user()
query = self.factory.create_query(user=query_creator)
other_user = self.factory.create_user()
orig_api_key = query.api_key
rv = self.make_request('post', "/api/queries/{}/regenerate_api_key".format(query.id), user=other_user)
self.assertEqual(rv.status_code, 403)
reloaded_query = models.Query.query.get(query.id)
self.assertEqual(orig_api_key, reloaded_query.api_key)
def test_admin_can_regenerate_api_key_of_other_user(self):
query_creator = self.factory.create_user()
query = self.factory.create_query(user=query_creator)
admin_user = self.factory.create_admin()
orig_api_key = query.api_key
rv = self.make_request('post', "/api/queries/{}/regenerate_api_key".format(query.id), user=admin_user)
self.assertEqual(rv.status_code, 200)
reloaded_query = models.Query.query.get(query.id)
self.assertNotEqual(orig_api_key, reloaded_query.api_key)
def test_admin_can_regenerate_api_key_of_myself(self):
query_creator = self.factory.create_user()
admin_user = self.factory.create_admin()
query = self.factory.create_query(user=query_creator)
orig_api_key = query.api_key
rv = self.make_request('post', "/api/queries/{}/regenerate_api_key".format(query.id), user=admin_user)
self.assertEqual(rv.status_code, 200)
updated_query = models.Query.query.get(query.id)
self.assertNotEqual(orig_api_key, updated_query.api_key)
def test_user_can_regenerate_api_key_of_myself(self):
user = self.factory.create_user()
query = self.factory.create_query(user=user)
orig_api_key = query.api_key
rv = self.make_request('post', "/api/queries/{}/regenerate_api_key".format(query.id), user=user)
self.assertEqual(rv.status_code, 200)
updated_query = models.Query.query.get(query.id)
self.assertNotEqual(orig_api_key, updated_query.api_key)
class TestQueryForkResourcePost(BaseTestCase):
def test_forks_a_query(self):
ds = self.factory.create_data_source(group=self.factory.org.default_group, view_only=False)
query = self.factory.create_query(data_source=ds)
rv = self.make_request('post', '/api/queries/{}/fork'.format(query.id))
self.assertEqual(rv.status_code, 200)
def test_must_have_full_access_to_data_source(self):
ds = self.factory.create_data_source(group=self.factory.org.default_group, view_only=True)
query = self.factory.create_query(data_source=ds)
rv = self.make_request('post', '/api/queries/{}/fork'.format(query.id))
self.assertEqual(rv.status_code, 403)
class TestFormatSQLQueryAPI(BaseTestCase):
def test_format_sql_query(self):
admin = self.factory.create_admin()
query = 'select a,b,c FROM foobar Where x=1 and y=2;'
expected = """SELECT a,
b,
c
FROM foobar
WHERE x=1
AND y=2;"""
rv = self.make_request('post', '/api/queries/format', user=admin, data={'query': query})
self.assertEqual(rv.json['query'], expected)