redash/tests/test_cli.py

578 lines
20 KiB
Python
Raw Normal View History

2016-12-07 13:54:08 +00:00
import mock
import textwrap
from click.testing import CliRunner
from tests import BaseTestCase
from redash.utils.configuration import ConfigurationContainer
from redash.query_runner import query_runners
from redash.cli import manager
2016-12-06 05:01:11 +00:00
from redash.models import DataSource, Group, Organization, User, db
class DataSourceCommandTests(BaseTestCase):
def test_interactive_new(self):
runner = CliRunner()
pg_i = list(query_runners.keys()).index("pg") + 1
result = runner.invoke(
manager,
["ds", "new"],
input="test\n%s\n\n\nexample.com\n\n\ntestdb\n" % (pg_i,),
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 1)
ds = DataSource.query.first()
self.assertEqual(ds.name, "test")
self.assertEqual(ds.type, "pg")
self.assertEqual(ds.options["dbname"], "testdb")
def test_options_new(self):
runner = CliRunner()
result = runner.invoke(
manager,
[
"ds",
"new",
"test",
"--options",
'{"host": "example.com", "dbname": "testdb"}',
"--type",
"pg",
],
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 1)
ds = DataSource.query.first()
self.assertEqual(ds.name, "test")
self.assertEqual(ds.type, "pg")
self.assertEqual(ds.options["host"], "example.com")
self.assertEqual(ds.options["dbname"], "testdb")
def test_bad_type_new(self):
runner = CliRunner()
result = runner.invoke(manager, ["ds", "new", "test", "--type", "wrong"])
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("not supported", result.output)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 0)
def test_bad_options_new(self):
runner = CliRunner()
result = runner.invoke(
manager,
[
"ds",
"new",
"test",
"--options",
'{"host": 12345, "dbname": "testdb"}',
"--type",
"pg",
],
)
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("invalid configuration", result.output)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 0)
def test_list(self):
self.factory.create_data_source(
name="test1",
type="pg",
options=ConfigurationContainer(
{"host": "example.com", "dbname": "testdb1"}
),
)
self.factory.create_data_source(
name="test2",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
2018-11-06 15:45:39 +00:00
self.factory.create_data_source(
name="Atest",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(manager, ["ds", "list"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
expected_output = """
2018-11-06 15:45:39 +00:00
Id: 3
Name: Atest
Type: sqlite
Options: {"dbpath": "/tmp/test.db"}
--------------------
Id: 1
Name: test1
Type: pg
Options: {"dbname": "testdb1", "host": "example.com"}
--------------------
Id: 2
Name: test2
Type: sqlite
Options: {"dbpath": "/tmp/test.db"}
"""
self.assertMultiLineEqual(
result.output, textwrap.dedent(expected_output).lstrip()
)
def test_connection_test(self):
self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(manager, ["ds", "test", "test1"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertIn("Success", result.output)
def test_connection_bad_test(self):
self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": __file__}),
)
runner = CliRunner()
result = runner.invoke(manager, ["ds", "test", "test1"])
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("Failure", result.output)
def test_connection_delete(self):
self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(manager, ["ds", "delete", "test1"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertIn("Deleting", result.output)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 0)
def test_connection_bad_delete(self):
self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(manager, ["ds", "delete", "wrong"])
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("Couldn't find", result.output)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 1)
def test_options_edit(self):
self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(
manager,
[
"ds",
"edit",
"test1",
"--options",
'{"host": "example.com", "dbname": "testdb"}',
"--name",
"test2",
"--type",
"pg",
],
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-06 05:01:11 +00:00
self.assertEqual(DataSource.query.count(), 1)
ds = DataSource.query.first()
self.assertEqual(ds.name, "test2")
self.assertEqual(ds.type, "pg")
self.assertEqual(ds.options["host"], "example.com")
self.assertEqual(ds.options["dbname"], "testdb")
def test_bad_type_edit(self):
self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(manager, ["ds", "edit", "test", "--type", "wrong"])
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("not supported", result.output)
2016-12-06 05:01:11 +00:00
ds = DataSource.query.first()
self.assertEqual(ds.type, "sqlite")
def test_bad_options_edit(self):
ds = self.factory.create_data_source(
name="test1",
type="sqlite",
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}),
)
runner = CliRunner()
result = runner.invoke(
manager,
[
"ds",
"new",
"test",
"--options",
'{"host": 12345, "dbname": "testdb"}',
"--type",
"pg",
],
)
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("invalid configuration", result.output)
2016-12-06 05:01:11 +00:00
ds = DataSource.query.first()
self.assertEqual(ds.type, "sqlite")
self.assertEqual(ds.options._config, {"dbpath": "/tmp/test.db"})
class GroupCommandTests(BaseTestCase):
def test_create(self):
2016-12-06 05:01:11 +00:00
gcount = Group.query.count()
perms = ["create_query", "edit_query", "view_query"]
runner = CliRunner()
result = runner.invoke(
manager, ["groups", "create", "test", "--permissions", ",".join(perms)]
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-06 05:01:11 +00:00
self.assertEqual(Group.query.count(), gcount + 1)
g = Group.query.order_by(Group.id.desc()).first()
2016-12-07 19:18:07 +00:00
db.session.add(self.factory.org)
self.assertEqual(g.org_id, self.factory.org.id)
self.assertEqual(g.permissions, perms)
def test_change_permissions(self):
g = self.factory.create_group(permissions=["list_dashboards"])
2016-12-06 05:01:11 +00:00
db.session.flush()
g_id = g.id
perms = ["create_query", "edit_query", "view_query"]
runner = CliRunner()
result = runner.invoke(
manager,
[
"groups",
"change_permissions",
str(g_id),
"--permissions",
",".join(perms),
],
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-06 05:01:11 +00:00
g = Group.query.filter(Group.id == g_id).first()
self.assertEqual(g.permissions, perms)
def test_list(self):
self.factory.create_group(name="test", permissions=["list_dashboards"])
self.factory.create_group(name="agroup", permissions=["list_dashboards"])
self.factory.create_group(name="bgroup", permissions=["list_dashboards"])
2018-11-06 15:45:39 +00:00
self.factory.create_user(
name="Fred Foobar",
email="foobar@example.com",
org=self.factory.org,
group_ids=[self.factory.default_group.id],
)
runner = CliRunner()
result = runner.invoke(manager, ["groups", "list"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
output = """
Id: 1
Name: admin
Type: builtin
Organization: default
Permissions: [admin,super_admin]
Users:
--------------------
2018-11-06 15:45:39 +00:00
Id: 4
Name: agroup
2018-11-06 15:45:39 +00:00
Type: regular
Organization: default
Permissions: [list_dashboards]
2018-11-06 15:45:39 +00:00
Users:
--------------------
Id: 5
Name: bgroup
2018-11-06 15:45:39 +00:00
Type: regular
Organization: default
Permissions: [list_dashboards]
2018-11-06 15:45:39 +00:00
Users:
--------------------
Id: 2
Name: default
Type: builtin
Organization: default
Permissions: [create_dashboard,create_query,edit_dashboard,edit_query,view_query,view_source,execute_query,list_users,schedule_query,list_dashboards,list_alerts,list_data_sources]
Users: Fred Foobar
--------------------
Id: 3
Name: test
Type: regular
Organization: default
Permissions: [list_dashboards]
Users:
"""
self.assertMultiLineEqual(result.output, textwrap.dedent(output).lstrip())
class OrganizationCommandTests(BaseTestCase):
def test_set_google_apps_domains(self):
domains = ["example.org", "example.com"]
runner = CliRunner()
result = runner.invoke(
manager, ["org", "set_google_apps_domains", ",".join(domains)]
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-07 19:18:07 +00:00
db.session.add(self.factory.org)
2016-12-06 05:01:11 +00:00
self.assertEqual(self.factory.org.google_apps_domains, domains)
def test_show_google_apps_domains(self):
self.factory.org.settings[Organization.SETTING_GOOGLE_APPS_DOMAINS] = [
"example.org",
"example.com",
]
2016-12-06 05:01:11 +00:00
db.session.add(self.factory.org)
db.session.commit()
runner = CliRunner()
result = runner.invoke(manager, ["org", "show_google_apps_domains"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
output = """
Current list of Google Apps domains: example.org, example.com
"""
self.assertMultiLineEqual(result.output, textwrap.dedent(output).lstrip())
def test_list(self):
self.factory.create_org(name="test", slug="test_org")
self.factory.create_org(name="Borg", slug="B_org")
self.factory.create_org(name="Aorg", slug="A_org")
runner = CliRunner()
result = runner.invoke(manager, ["org", "list"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
output = """
2018-11-06 15:45:39 +00:00
Id: 4
Name: Aorg
Slug: A_org
--------------------
Id: 3
Name: Borg
Slug: B_org
--------------------
Id: 1
Name: Default
Slug: default
--------------------
Id: 2
Name: test
Slug: test_org
"""
self.assertMultiLineEqual(result.output, textwrap.dedent(output).lstrip())
class UserCommandTests(BaseTestCase):
def test_create_basic(self):
runner = CliRunner()
result = runner.invoke(
manager,
["users", "create", "foobar@example.com", "Fred Foobar"],
input="password1\npassword1\n",
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
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 09:42:13 +00:00
u = User.query.filter(User.email == "foobar@example.com").first()
self.assertEqual(u.name, "Fred Foobar")
self.assertTrue(u.verify_password("password1"))
2016-12-07 13:54:08 +00:00
self.assertEqual(u.group_ids, [u.org.default_group.id])
def test_create_admin(self):
runner = CliRunner()
result = runner.invoke(
manager,
[
"users",
"create",
"foobar@example.com",
"Fred Foobar",
"--password",
"password1",
"--admin",
],
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
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 09:42:13 +00:00
u = User.query.filter(User.email == "foobar@example.com").first()
self.assertEqual(u.name, "Fred Foobar")
self.assertTrue(u.verify_password("password1"))
self.assertEqual(u.group_ids, [u.org.default_group.id, u.org.admin_group.id])
def test_create_googleauth(self):
runner = CliRunner()
result = runner.invoke(
manager,
["users", "create", "foobar@example.com", "Fred Foobar", "--google"],
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
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 09:42:13 +00:00
u = User.query.filter(User.email == "foobar@example.com").first()
self.assertEqual(u.name, "Fred Foobar")
self.assertIsNone(u.password_hash)
2016-12-07 13:54:08 +00:00
self.assertEqual(u.group_ids, [u.org.default_group.id])
def test_create_bad(self):
self.factory.create_user(email="foobar@example.com")
runner = CliRunner()
result = runner.invoke(
manager,
["users", "create", "foobar@example.com", "Fred Foobar"],
input="password1\npassword1\n",
)
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("Failed", result.output)
def test_delete(self):
self.factory.create_user(email="foobar@example.com")
2016-12-06 05:01:11 +00:00
ucount = User.query.count()
runner = CliRunner()
result = runner.invoke(manager, ["users", "delete", "foobar@example.com"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertEqual(
User.query.filter(User.email == "foobar@example.com").count(), 0
)
2016-12-06 05:01:11 +00:00
self.assertEqual(User.query.count(), ucount - 1)
def test_delete_bad(self):
2016-12-06 05:01:11 +00:00
ucount = User.query.count()
runner = CliRunner()
result = runner.invoke(manager, ["users", "delete", "foobar@example.com"])
self.assertIn("Deleted 0 users", result.output)
2016-12-06 05:01:11 +00:00
self.assertEqual(User.query.count(), ucount)
def test_password(self):
self.factory.create_user(email="foobar@example.com")
runner = CliRunner()
result = runner.invoke(
manager, ["users", "password", "foobar@example.com", "xyzzy"]
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
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 09:42:13 +00:00
u = User.query.filter(User.email == "foobar@example.com").first()
self.assertTrue(u.verify_password("xyzzy"))
def test_password_bad(self):
runner = CliRunner()
result = runner.invoke(
manager, ["users", "password", "foobar@example.com", "xyzzy"]
)
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("not found", result.output)
def test_password_bad_org(self):
runner = CliRunner()
result = runner.invoke(
manager,
["users", "password", "foobar@example.com", "xyzzy", "--org", "default"],
)
self.assertTrue(result.exception)
self.assertEqual(result.exit_code, 1)
self.assertIn("not found", result.output)
def test_invite(self):
admin = self.factory.create_user(email="redash-admin@example.com")
runner = CliRunner()
with mock.patch("redash.cli.users.invite_user") as iu:
result = runner.invoke(
manager,
[
"users",
"invite",
"foobar@example.com",
"Fred Foobar",
"redash-admin@example.com",
],
)
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertTrue(iu.called)
c = iu.call_args[0]
2016-12-07 19:18:07 +00:00
db.session.add_all(c)
self.assertEqual(c[0].id, self.factory.org.id)
self.assertEqual(c[1].id, admin.id)
self.assertEqual(c[2].email, "foobar@example.com")
def test_list(self):
self.factory.create_user(
name="Fred Foobar", email="foobar@example.com", org=self.factory.org
)
2018-11-06 15:45:39 +00:00
self.factory.create_user(
name="William Foobar", email="william@example.com", org=self.factory.org
)
2018-11-06 15:45:39 +00:00
self.factory.create_user(
name="Andrew Foobar", email="andrew@example.com", org=self.factory.org
)
2018-11-06 15:45:39 +00:00
runner = CliRunner()
result = runner.invoke(manager, ["users", "list"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
output = """
2018-11-06 15:45:39 +00:00
Id: 3
Name: Andrew Foobar
Email: andrew@example.com
Organization: Default
Active: True
Groups: default
--------------------
Id: 1
Name: Fred Foobar
Email: foobar@example.com
Organization: Default
Active: True
Groups: default
2018-11-06 15:45:39 +00:00
--------------------
Id: 2
Name: William Foobar
Email: william@example.com
Organization: Default
Active: True
Groups: default
"""
self.assertMultiLineEqual(result.output, textwrap.dedent(output).lstrip())
def test_grant_admin(self):
u = self.factory.create_user(
name="Fred Foobar",
email="foobar@example.com",
org=self.factory.org,
group_ids=[self.factory.default_group.id],
)
runner = CliRunner()
result = runner.invoke(manager, ["users", "grant_admin", "foobar@example.com"])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
2016-12-07 19:18:07 +00:00
db.session.add(u)
self.assertEqual(u.group_ids, [u.org.default_group.id, u.org.admin_group.id])