mirror of
https://github.com/valitydev/redash.git
synced 2024-11-06 17:15:17 +00:00
Fixed tests;
This commit is contained in:
parent
dc7050d4ef
commit
c0329cc0ef
@ -13,6 +13,7 @@ if __name__ == '__main__':
|
||||
|
||||
with db.database.transaction():
|
||||
models.Group.insert(name='admin', permissions=['admin'], tables=['*']).execute()
|
||||
models.Group.insert(name='api', permissions=['view_query'], tables=['*']).execute()
|
||||
models.Group.insert(name='default', permissions=models.Group.DEFAULT_PERMISSIONS, tables=['*']).execute()
|
||||
|
||||
migrator.drop_column(models.User, 'permissions')
|
||||
|
@ -18,17 +18,16 @@ class BaseModel(db.Model):
|
||||
|
||||
class AnonymousUser(AnonymousUserMixin):
|
||||
@property
|
||||
def permissions(self):
|
||||
def groups(self):
|
||||
return []
|
||||
|
||||
|
||||
class ApiUser(UserMixin):
|
||||
def __init__(self, api_key):
|
||||
self.id = api_key
|
||||
|
||||
@property
|
||||
def permissions(self):
|
||||
return ['view_query']
|
||||
def groups(self):
|
||||
return ['api']
|
||||
|
||||
|
||||
class Group(BaseModel):
|
||||
|
@ -12,7 +12,10 @@ class require_permissions(object):
|
||||
def __call__(self, fn):
|
||||
@functools.wraps(fn)
|
||||
def decorated(*args, **kwargs):
|
||||
permissions = list(itertools.chain(*[g.permissions for g in models.Group.select().where(models.Group.name << current_user.groups)]))
|
||||
if len(current_user.groups) > 0:
|
||||
permissions = list(itertools.chain(*[g.permissions for g in models.Group.select().where(models.Group.name << current_user.groups)]))
|
||||
else:
|
||||
permissions = []
|
||||
|
||||
has_permissions = reduce(lambda a, b: a and b,
|
||||
map(lambda permission: permission in permissions,
|
||||
|
@ -38,10 +38,14 @@ class Sequence(object):
|
||||
|
||||
return self.string.format(self.sequence)
|
||||
|
||||
group_factory = ModelFactory(redash.models.Group,
|
||||
name='default', permissions=redash.models.Group.DEFAULT_PERMISSIONS,
|
||||
tables='{*}')
|
||||
|
||||
|
||||
user_factory = ModelFactory(redash.models.User,
|
||||
name='John Doe', email=Sequence('test{}@example.com'),
|
||||
is_admin=False)
|
||||
is_admin=False, groups=['default'])
|
||||
|
||||
|
||||
data_source_factory = ModelFactory(redash.models.DataSource,
|
||||
|
@ -36,7 +36,7 @@ class TestCreateAndLoginUser(BaseTestCase):
|
||||
def test_creates_vaild_new_user(self):
|
||||
openid_user = ObjectDict({'email': 'test@example.com', 'name': 'Test User'})
|
||||
|
||||
with patch.multiple(settings, GOOGLE_APPS_DOMAIN='example.com', ADMINS=['admin@example.com']), \
|
||||
with patch.multiple(settings, GOOGLE_APPS_DOMAIN='example.com'), \
|
||||
patch('redash.authentication.login_user') as login_user_mock:
|
||||
|
||||
create_and_login_user(None, openid_user)
|
||||
@ -46,18 +46,6 @@ class TestCreateAndLoginUser(BaseTestCase):
|
||||
|
||||
self.assertFalse(user.is_admin)
|
||||
|
||||
def test_creates_vaild_new_user_and_sets_is_admin(self):
|
||||
openid_user = ObjectDict({'email': 'admin@example.com', 'name': 'Test User'})
|
||||
|
||||
with patch.multiple(settings, GOOGLE_APPS_DOMAIN='example.com', ADMINS=['admin@example.com']), \
|
||||
patch('redash.authentication.login_user') as login_user_mock:
|
||||
|
||||
create_and_login_user(None, openid_user)
|
||||
|
||||
self.assertTrue(login_user_mock.called)
|
||||
user = models.User.get(models.User.email == openid_user.email)
|
||||
self.assertTrue(user.is_admin)
|
||||
|
||||
def test_ignores_invliad_user(self):
|
||||
user = ObjectDict({'email': 'test@whatever.com'})
|
||||
|
||||
|
@ -7,7 +7,7 @@ from flask.ext.login import current_user
|
||||
from mock import patch
|
||||
from tests import BaseTestCase
|
||||
from tests.factories import dashboard_factory, widget_factory, visualization_factory, query_factory, \
|
||||
query_result_factory, user_factory, data_source_factory
|
||||
query_result_factory, user_factory, data_source_factory, group_factory
|
||||
from redash import app, models, settings
|
||||
from redash.utils import json_dumps
|
||||
from redash.authentication import sign
|
||||
@ -18,6 +18,7 @@ settings.GOOGLE_APPS_DOMAIN = "example.com"
|
||||
@contextmanager
|
||||
def authenticated_user(c, user=None):
|
||||
if not user:
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
|
||||
with c.session_transaction() as sess:
|
||||
@ -77,7 +78,8 @@ class IndexTest(BaseTestCase, AuthenticationTestMixin):
|
||||
|
||||
class StatusTest(BaseTestCase):
|
||||
def test_returns_data_for_admin(self):
|
||||
admin = user_factory.create(permissions=['admin'])
|
||||
group = group_factory.create()
|
||||
admin = user_factory.create(groups=['admin','default'], is_admin=True)
|
||||
with app.test_client() as c, authenticated_user(c, user=admin):
|
||||
rv = c.get('/status.json')
|
||||
self.assertEqual(rv.status_code, 200)
|
||||
@ -111,6 +113,7 @@ class DashboardAPITest(BaseTestCase, AuthenticationTestMixin):
|
||||
self.assertEquals(rv.status_code, 404)
|
||||
|
||||
def test_create_new_dashboard(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
with app.test_client() as c, authenticated_user(c, user=user):
|
||||
dashboard_name = 'Test Dashboard'
|
||||
@ -227,6 +230,7 @@ class QueryAPITest(BaseTestCase, AuthenticationTestMixin):
|
||||
self.assertEquals(rv.json['name'], 'Testing')
|
||||
|
||||
def test_create_query(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
data_source = data_source_factory.create()
|
||||
query_data = {
|
||||
@ -392,6 +396,7 @@ class TestLogin(BaseTestCase):
|
||||
self.assertFalse(login_user_mock.called)
|
||||
|
||||
def test_submit_correct_user_and_password(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
user.hash_password('password')
|
||||
user.save()
|
||||
@ -402,6 +407,7 @@ class TestLogin(BaseTestCase):
|
||||
login_user_mock.assert_called_with(user, remember=False)
|
||||
|
||||
def test_submit_correct_user_and_password_and_remember_me(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
user.hash_password('password')
|
||||
user.save()
|
||||
@ -412,6 +418,7 @@ class TestLogin(BaseTestCase):
|
||||
login_user_mock.assert_called_with(user, remember=True)
|
||||
|
||||
def test_submit_correct_user_and_password_with_next(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
user.hash_password('password')
|
||||
user.save()
|
||||
@ -430,6 +437,7 @@ class TestLogin(BaseTestCase):
|
||||
self.assertFalse(login_user_mock.called)
|
||||
|
||||
def test_submit_incorrect_password(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
user.hash_password('password')
|
||||
user.save()
|
||||
@ -440,6 +448,7 @@ class TestLogin(BaseTestCase):
|
||||
self.assertFalse(login_user_mock.called)
|
||||
|
||||
def test_submit_incorrect_password(self):
|
||||
group = group_factory.create()
|
||||
user = user_factory.create()
|
||||
|
||||
with app.test_client() as c, patch('redash.controllers.login_user') as login_user_mock:
|
||||
|
Loading…
Reference in New Issue
Block a user