2016-12-07 13:54:08 +00:00
|
|
|
import mock
|
2016-12-06 03:09:44 +00:00
|
|
|
import textwrap
|
|
|
|
from click.testing import CliRunner
|
|
|
|
|
|
|
|
from tests import BaseTestCase
|
|
|
|
from redash.utils.configuration import ConfigurationContainer
|
|
|
|
from redash.query_runner import query_runners
|
2016-12-07 12:58:33 +00:00
|
|
|
from redash.cli import manager
|
2016-12-06 05:01:11 +00:00
|
|
|
from redash.models import DataSource, Group, Organization, User, db
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DataSourceCommandTests(BaseTestCase):
|
|
|
|
def test_interactive_new(self):
|
|
|
|
runner = CliRunner()
|
|
|
|
pg_i = query_runners.keys().index('pg') + 1
|
|
|
|
result = runner.invoke(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager,
|
|
|
|
['ds', 'new'],
|
2017-10-01 11:26:04 +00:00
|
|
|
input="test\n%s\n\n\nexample.com\n\n\ntestdb\n" % (pg_i,))
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
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(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager,
|
|
|
|
['ds', 'new',
|
|
|
|
'test',
|
|
|
|
'--options', '{"host": "example.com", "dbname": "testdb"}',
|
|
|
|
'--type', 'pg'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
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(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['ds', 'new', 'test', '--type', 'wrong'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_bad_options_new(self):
|
|
|
|
runner = CliRunner()
|
|
|
|
result = runner.invoke(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['ds', 'new', 'test', '--options',
|
|
|
|
'{"host": 12345, "dbname": "testdb"}',
|
|
|
|
'--type', 'pg'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
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"}))
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['ds', 'list'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
expected_output = """
|
|
|
|
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()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['ds', 'test', 'test1'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['ds', 'test', 'test1'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['ds', 'delete', 'test1'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_connection_bad_delete(self):
|
|
|
|
self.factory.create_data_source(
|
|
|
|
name='test1', type='sqlite',
|
|
|
|
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}))
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['ds', 'delete', 'wrong'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
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(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['ds', 'edit', 'test1', '--options',
|
|
|
|
'{"host": "example.com", "dbname": "testdb"}',
|
|
|
|
'--name', 'test2',
|
|
|
|
'--type', 'pg'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
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(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['ds', 'edit', 'test', '--type', 'wrong'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
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(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['ds', 'new', 'test', '--options',
|
|
|
|
'{"host": 12345, "dbname": "testdb"}',
|
|
|
|
'--type', 'pg'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
perms = ['create_query', 'edit_query', 'view_query']
|
|
|
|
runner = CliRunner()
|
2016-12-07 13:11:42 +00:00
|
|
|
result = runner.invoke(manager, ['groups', 'create', 'test', '--permissions', ','.join(perms)])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-07 13:11:42 +00:00
|
|
|
self.assertEqual(g.org_id, self.factory.org.id)
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
g_id = g.id
|
|
|
|
perms = ['create_query', 'edit_query', 'view_query']
|
|
|
|
runner = CliRunner()
|
|
|
|
result = runner.invoke(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['groups', 'change_permissions', str(g_id), '--permissions', ','.join(perms)])
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertEqual(g.permissions, perms)
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
self.factory.create_group(name='test', permissions=['list_dashboards'])
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['groups', 'list'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
output = """
|
|
|
|
Id: 1
|
|
|
|
Name: admin
|
|
|
|
Type: builtin
|
|
|
|
Organization: default
|
|
|
|
--------------------
|
|
|
|
Id: 2
|
|
|
|
Name: default
|
|
|
|
Type: builtin
|
|
|
|
Organization: default
|
|
|
|
--------------------
|
|
|
|
Id: 3
|
|
|
|
Name: test
|
|
|
|
Type: regular
|
|
|
|
Organization: default
|
|
|
|
"""
|
|
|
|
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()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['org', 'set_google_apps_domains', ','.join(domains)])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['org', 'show_google_apps_domains'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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')
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['org', 'list'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
output = """
|
|
|
|
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(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['users', 'create', 'foobar@example.com', 'Fred Foobar'],
|
2016-12-06 03:09:44 +00:00
|
|
|
input="password1\npassword1\n")
|
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
2016-12-06 05:01:11 +00:00
|
|
|
u = User.query.filter(User.email == "foobar@example.com").first()
|
2016-12-06 03:09:44 +00:00
|
|
|
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])
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_create_admin(self):
|
|
|
|
runner = CliRunner()
|
|
|
|
result = runner.invoke(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['users', 'create', 'foobar@example.com', 'Fred Foobar',
|
|
|
|
'--password', 'password1', '--admin'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
2016-12-06 05:01:11 +00:00
|
|
|
u = User.query.filter(User.email == "foobar@example.com").first()
|
2016-12-06 03:09:44 +00:00
|
|
|
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,
|
|
|
|
u.org.admin_group.id])
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_create_googleauth(self):
|
|
|
|
runner = CliRunner()
|
|
|
|
result = runner.invoke(
|
2016-12-07 12:58:33 +00:00
|
|
|
manager, ['users', 'create', 'foobar@example.com', 'Fred Foobar', '--google'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
2016-12-06 05:01:11 +00:00
|
|
|
u = User.query.filter(User.email == "foobar@example.com").first()
|
2016-12-06 03:09:44 +00:00
|
|
|
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])
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_create_bad(self):
|
|
|
|
self.factory.create_user(email='foobar@example.com')
|
|
|
|
runner = CliRunner()
|
|
|
|
result = runner.invoke(
|
2016-12-07 13:54:08 +00:00
|
|
|
manager, ['users', 'create', 'foobar@example.com', 'Fred Foobar'],
|
2016-12-06 03:09:44 +00:00
|
|
|
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()
|
2016-12-06 03:09:44 +00:00
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'delete', 'foobar@example.com'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
2016-12-06 05:01:11 +00:00
|
|
|
self.assertEqual(User.query.filter(User.email ==
|
2016-12-07 12:58:33 +00:00
|
|
|
"foobar@example.com").count(), 0)
|
2016-12-06 05:01:11 +00:00
|
|
|
self.assertEqual(User.query.count(), ucount - 1)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_delete_bad(self):
|
2016-12-06 05:01:11 +00:00
|
|
|
ucount = User.query.count()
|
2016-12-06 03:09:44 +00:00
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'delete', 'foobar@example.com'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertIn('Deleted 0 users', result.output)
|
2016-12-06 05:01:11 +00:00
|
|
|
self.assertEqual(User.query.count(), ucount)
|
2016-12-06 03:09:44 +00:00
|
|
|
|
|
|
|
def test_password(self):
|
|
|
|
self.factory.create_user(email='foobar@example.com')
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'password', 'foobar@example.com', 'xyzzy'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
2016-12-06 05:01:11 +00:00
|
|
|
u = User.query.filter(User.email == "foobar@example.com").first()
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertTrue(u.verify_password('xyzzy'))
|
|
|
|
|
|
|
|
def test_password_bad(self):
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'password', 'foobar@example.com', 'xyzzy'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertTrue(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 1)
|
|
|
|
self.assertIn('not found', result.output)
|
|
|
|
|
|
|
|
def test_password_bad_org(self):
|
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'password', 'foobar@example.com', 'xyzzy', '--org', 'default'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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:
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'invite', 'foobar@example.com', 'Fred Foobar', 'redash-admin@example.com'])
|
2016-12-06 03:09:44 +00:00
|
|
|
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)
|
2016-12-06 03:09:44 +00:00
|
|
|
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',
|
2016-12-06 05:01:11 +00:00
|
|
|
org=self.factory.org)
|
2016-12-06 03:09:44 +00:00
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'list'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
output = """
|
|
|
|
Id: 1
|
|
|
|
Name: Fred Foobar
|
|
|
|
Email: foobar@example.com
|
|
|
|
Organization: Default
|
|
|
|
"""
|
|
|
|
self.assertMultiLineEqual(result.output,
|
|
|
|
textwrap.dedent(output).lstrip())
|
|
|
|
|
|
|
|
def test_grant_admin(self):
|
2016-12-06 05:01:11 +00:00
|
|
|
u = self.factory.create_user(name='Fred Foobar',
|
2016-12-06 03:09:44 +00:00
|
|
|
email='foobar@example.com',
|
|
|
|
org=self.factory.org,
|
2016-12-06 05:01:11 +00:00
|
|
|
group_ids=[self.factory.default_group.id])
|
2016-12-06 03:09:44 +00:00
|
|
|
runner = CliRunner()
|
2016-12-07 12:58:33 +00:00
|
|
|
result = runner.invoke(manager, ['users', 'grant_admin', 'foobar@example.com'])
|
2016-12-06 03:09:44 +00:00
|
|
|
self.assertFalse(result.exception)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
2016-12-07 19:18:07 +00:00
|
|
|
db.session.add(u)
|
2016-12-07 13:54:08 +00:00
|
|
|
self.assertEqual(u.group_ids, [u.org.default_group.id,
|
|
|
|
u.org.admin_group.id])
|