2019-07-15 12:09:30 +00:00
|
|
|
from tests import BaseTestCase
|
2015-12-01 13:44:08 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
from unittest import TestCase
|
|
|
|
from redash.permissions import has_access
|
2019-07-15 12:09:30 +00:00
|
|
|
from redash import models
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
MockUser = namedtuple("MockUser", ["permissions", "group_ids"])
|
2015-12-01 13:44:08 +00:00
|
|
|
view_only = True
|
|
|
|
|
|
|
|
|
2019-07-15 12:09:30 +00:00
|
|
|
class TestHasAccess(BaseTestCase):
|
2015-12-01 13:44:08 +00:00
|
|
|
def test_allows_admin_regardless_of_groups(self):
|
2019-12-11 11:54:29 +00:00
|
|
|
user = MockUser(["admin"], [])
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
self.assertTrue(has_access({}, user, view_only))
|
|
|
|
self.assertTrue(has_access({}, user, not view_only))
|
|
|
|
|
|
|
|
def test_allows_if_user_member_in_group_with_view_access(self):
|
|
|
|
user = MockUser([], [1])
|
|
|
|
|
|
|
|
self.assertTrue(has_access({1: view_only}, user, view_only))
|
|
|
|
|
|
|
|
def test_allows_if_user_member_in_group_with_full_access(self):
|
|
|
|
user = MockUser([], [1])
|
|
|
|
|
|
|
|
self.assertTrue(has_access({1: not view_only}, user, not view_only))
|
|
|
|
|
2016-06-09 16:59:26 +00:00
|
|
|
def test_allows_if_user_member_in_multiple_groups(self):
|
|
|
|
user = MockUser([], [1, 2, 3])
|
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
self.assertTrue(
|
|
|
|
has_access({1: not view_only, 2: view_only}, user, not view_only)
|
|
|
|
)
|
2016-06-09 16:59:26 +00:00
|
|
|
self.assertFalse(has_access({1: view_only, 2: view_only}, user, not view_only))
|
|
|
|
self.assertTrue(has_access({1: view_only, 2: view_only}, user, view_only))
|
2019-12-11 11:54:29 +00:00
|
|
|
self.assertTrue(
|
|
|
|
has_access({1: not view_only, 2: not view_only}, user, view_only)
|
|
|
|
)
|
2016-06-09 16:59:26 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
def test_not_allows_if_not_enough_permission(self):
|
|
|
|
user = MockUser([], [1])
|
|
|
|
|
|
|
|
self.assertFalse(has_access({1: view_only}, user, not view_only))
|
|
|
|
self.assertFalse(has_access({2: view_only}, user, not view_only))
|
|
|
|
self.assertFalse(has_access({2: view_only}, user, view_only))
|
2019-12-11 11:54:29 +00:00
|
|
|
self.assertFalse(
|
|
|
|
has_access({2: not view_only, 1: view_only}, user, not view_only)
|
|
|
|
)
|
2019-07-15 12:09:30 +00:00
|
|
|
|
|
|
|
def test_allows_access_to_query_by_query_api_key(self):
|
|
|
|
query = self.factory.create_query()
|
|
|
|
user = models.ApiUser(query.api_key, None, [])
|
|
|
|
|
|
|
|
self.assertTrue(has_access(query, user, view_only))
|
|
|
|
|
|
|
|
def test_doesnt_allow_access_to_query_by_different_api_key(self):
|
|
|
|
query = self.factory.create_query()
|
|
|
|
other_query = self.factory.create_query()
|
|
|
|
user = models.ApiUser(other_query.api_key, None, [])
|
|
|
|
|
|
|
|
self.assertFalse(has_access(query, user, view_only))
|
|
|
|
|
|
|
|
def test_allows_access_to_query_by_dashboard_api_key(self):
|
|
|
|
dashboard = self.factory.create_dashboard()
|
|
|
|
visualization = self.factory.create_visualization()
|
|
|
|
self.factory.create_widget(dashboard=dashboard, visualization=visualization)
|
|
|
|
query = self.factory.create_query(visualizations=[visualization])
|
|
|
|
|
|
|
|
api_key = self.factory.create_api_key(object=dashboard).api_key
|
|
|
|
user = models.ApiUser(api_key, None, [])
|
|
|
|
|
2019-12-11 11:54:29 +00:00
|
|
|
self.assertTrue(has_access(query, user, view_only))
|